fixed printing spaces; added -W and -L options; simplified improved rand seeding, backing out latest commit

This commit is contained in:
Jedi Hacker 2024-08-27 14:23:26 -04:00
parent 0225b8881d
commit 5d8d132197

View File

@ -24,12 +24,12 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sysexits.h> #include <sysexits.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/queue.h> #include <sys/queue.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef DEBUG #ifdef DEBUG
@ -72,6 +72,8 @@
typedef struct opt_s { typedef struct opt_s {
uint8_t justify; uint8_t justify;
uint8_t width; uint8_t width;
uint8_t wspc;
uint8_t lspc;
uint8_t color; uint8_t color;
uint8_t encoding; uint8_t encoding;
bool random; bool random;
@ -99,6 +101,7 @@ typedef struct font_s {
uint8_t *data; uint8_t *data;
glyph_t *glyphs[NUM_CHARS]; glyph_t *glyphs[NUM_CHARS];
uint8_t height; uint8_t height;
glyph_t *space;
} font_t; } font_t;
struct dirname_s { struct dirname_s {
@ -107,7 +110,7 @@ struct dirname_s {
}; };
const char *charlist = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO" const char *charlist = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNO"
"PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; "PQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ ";
opt_t opt; opt_t opt;
@ -123,6 +126,7 @@ void printcell(char *utfchar, uint8_t color);
void printrow(const glyph_t *glyph, int row); void printrow(const glyph_t *glyph, int row);
void printstr(const char *str, font_t *font); void printstr(const char *str, font_t *font);
void void
usage(void) usage(void)
{ {
@ -131,6 +135,8 @@ usage(void)
fprintf(stderr, " -f [font] Specify font file used.\n"); fprintf(stderr, " -f [font] Specify font file used.\n");
fprintf(stderr, " -j l|r|c Justify left, right, or center. Default is left.\n"); fprintf(stderr, " -j l|r|c Justify left, right, or center. Default is left.\n");
fprintf(stderr, " -w n Set screen width. Default is 80.\n"); fprintf(stderr, " -w n Set screen width. Default is 80.\n");
fprintf(stderr, " -W n Width of space character. Default is 4.\n");
fprintf(stderr, " -L n Line spacing. Default is 1.\n");
fprintf(stderr, " -c a|m Color format ANSI or mirc. Default is ANSI.\n"); fprintf(stderr, " -c a|m Color format ANSI or mirc. Default is ANSI.\n");
fprintf(stderr, " -e u|a Encode as unicode or ASCII. Default is unicode.\n"); fprintf(stderr, " -e u|a Encode as unicode or ASCII. Default is unicode.\n");
fprintf(stderr, " -i Print font details.\n"); fprintf(stderr, " -i Print font details.\n");
@ -148,13 +154,13 @@ main(int argc, char *argv[])
opt.justify = LEFT_JUSTIFY; opt.justify = LEFT_JUSTIFY;
opt.width = 80; opt.width = 80;
opt.wspc = 4;
opt.lspc = 1;
opt.info = false; opt.info = false;
opt.encoding = ENC_UNICODE; opt.encoding = ENC_UNICODE;
opt.random = false; opt.random = false;
char *fontfile = NULL; char *fontfile = NULL;
struct timeval tv;
DIR *d; DIR *d;
struct dirent *dir; struct dirent *dir;
SLIST_HEAD(, dirname_s) head = SLIST_HEAD_INITIALIZER(dirname_s); SLIST_HEAD(, dirname_s) head = SLIST_HEAD_INITIALIZER(dirname_s);
@ -164,7 +170,7 @@ main(int argc, char *argv[])
int r = 0; int r = 0;
int dll = 0; int dll = 0;
while((o = getopt(argc, argv, "f:w:j:c:e:ir")) != -1) { while((o = getopt(argc, argv, "f:w:W:L:j:c:e:ir")) != -1) {
switch (o) { switch (o) {
case 'f': case 'f':
fontfile = optarg; fontfile = optarg;
@ -172,6 +178,12 @@ main(int argc, char *argv[])
case 'w': case 'w':
opt.width = atoi(optarg); opt.width = atoi(optarg);
break; break;
case 'W':
opt.wspc = atoi(optarg);
break;
case 'L':
opt.lspc = atoi(optarg);
break;
case 'j': case 'j':
switch (optarg[0]) { switch (optarg[0]) {
case 'l': case 'l':
@ -253,9 +265,7 @@ main(int argc, char *argv[])
} }
closedir(d); closedir(d);
gettimeofday(&tv, NULL); srand(time(NULL) + getpid());
srand(tv.tv_usec);
r = dll ? rand() % dll : 0; r = dll ? rand() % dll : 0;
dp = SLIST_FIRST(&head); dp = SLIST_FIRST(&head);
@ -273,11 +283,11 @@ main(int argc, char *argv[])
font = loadfont(fontfile); font = loadfont(fontfile);
printf("\n"); if (opt.lspc) printf("\n");
for (int i = 0; i < argc; i++) { for (int i = 0; i < argc; i++) {
printstr(argv[i], font); printstr(argv[i], font);
printf("\n"); for (int i = 0; i < opt.lspc; i++) printf("\n");
} }
return(0); return(0);
@ -358,6 +368,7 @@ font_t
font->charlist = (uint16_t *)&map[45]; font->charlist = (uint16_t *)&map[45];
font->data = &map[233]; font->data = &map[233];
font->height = 0; font->height = 0;
font->space = calloc(1, sizeof(glyph_t));
if (strncmp(magic, (const char *)map, strlen(magic)) || font->fonttype != COLOR_FNT) { if (strncmp(magic, (const char *)map, strlen(magic)) || font->fonttype != COLOR_FNT) {
fprintf(stderr, "Invalid font file: %s\n", fn); fprintf(stderr, "Invalid font file: %s\n", fn);
@ -411,6 +422,7 @@ font_t
font->glyphs[i] = NULL; font->glyphs[i] = NULL;
} }
} }
readchar(NUM_CHARS, font->space, font);
return font; return font;
} }
@ -418,20 +430,25 @@ font_t
void void
readchar(int i, glyph_t *glyph, font_t *font) readchar(int i, glyph_t *glyph, font_t *font)
{ {
if (font->charlist[i] == 0xffff) { uint8_t *p;
uint8_t color;
if (i != NUM_CHARS && font->charlist[i] == 0xffff) {
printf("char not found\n"); printf("char not found\n");
return; return;
} }
uint8_t *p = font->data + font->charlist[i]; if (i == NUM_CHARS) {
glyph->width = opt.wspc;
uint8_t ch; glyph->height = font->height;
uint8_t color; } else {
p = font->data + font->charlist[i];
glyph->width = *p; glyph->width = *p;
p++; p++;
glyph->height = *p; glyph->height = *p;
p++; p++;
}
int row = 0; int row = 0;
int col = 0; int col = 0;
@ -453,12 +470,12 @@ readchar(int i, glyph_t *glyph, font_t *font)
glyph->cell[i].color = 0; glyph->cell[i].color = 0;
} }
if (i != NUM_CHARS) {
while (*p) { while (*p) {
ch = *p; uint8_t ch = *p;
p++; p++;
if (ch == '\r') { if (ch == '\r') {
ch = ' '; ch = ' ';
row++; row++;
@ -488,10 +505,12 @@ readchar(int i, glyph_t *glyph, font_t *font)
} }
} }
} }
}
int int
lookupchar(char c, const font_t *font) lookupchar(char c, const font_t *font)
{ {
if (c == ' ') return NUM_CHARS;
for (int i = 0; i < NUM_CHARS; i++) { for (int i = 0; i < NUM_CHARS; i++) {
if (charlist[i] == c && font->charlist[i] != 0xffff) if (charlist[i] == c && font->charlist[i] != 0xffff)
return i; return i;
@ -587,9 +606,13 @@ printstr(const char *str, font_t *font)
continue; continue;
} }
if (n == NUM_CHARS) {
g = font->space;
} else {
g = font->glyphs[n]; g = font->glyphs[n];
}
if (g->height > maxheight) { if (n != NUM_CHARS && g->height > maxheight) {
maxheight = g->height; maxheight = g->height;
} }
@ -598,6 +621,7 @@ printstr(const char *str, font_t *font)
linewidth += font->spacing; linewidth += font->spacing;
} }
} }
font->space->height = maxheight;
if (opt.justify == CENTER_JUSTIFY) { if (opt.justify == CENTER_JUSTIFY) {
padding = (opt.width - linewidth) / 2; padding = (opt.width - linewidth) / 2;
@ -617,7 +641,12 @@ printstr(const char *str, font_t *font)
continue; continue;
} }
glyph_t *g = font->glyphs[n]; glyph_t *g;
if (n == NUM_CHARS) {
g = font->space;
} else {
g = font->glyphs[n];
}
printrow(g, i); printrow(g, i);
if (opt.color == COLOR_ANSI) { if (opt.color == COLOR_ANSI) {