* Add --half and --quarter options to caca2tlf.

This commit is contained in:
Sam Hocevar 2006-11-06 17:24:02 +00:00 committed by sam
parent a550751f89
commit 1cd33835f5

View file

@ -21,9 +21,12 @@
# include <inttypes.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <cucul.h>
enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
static void list_fonts(void);
static void add_char(unsigned int);
@ -31,20 +34,40 @@ cucul_font_t *f;
cucul_canvas_t *out, *onechar;
unsigned long int const *blocks;
uint8_t * image;
unsigned int width, height;
unsigned int w, h, gw, gh, iw, ih;
int main(int argc, char *argv[])
{
char *fontname, *extraflag;
unsigned int b, i;
if(argc < 2)
{
fprintf(stderr, "Wrong argument count. Usage: caca2tlf <font>\n");
fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
list_fonts();
return -1;
}
f = cucul_load_font(argv[1], 0);
if(!strcmp(argv[1], "--half") && argc >= 3)
{
extraflag = "--half ";
mode = HALFBLOCKS;
fontname = argv[2];
}
else if(!strcmp(argv[1], "--quarter") && argc >= 3)
{
extraflag = "--quarter ";
mode = QUARTERBLOCKS;
fontname = argv[2];
}
else
{
extraflag = "";
mode = GRAY;
fontname = argv[1];
}
f = cucul_load_font(fontname, 0);
if(!f)
{
@ -53,19 +76,40 @@ int main(int argc, char *argv[])
return -2;
}
width = cucul_get_font_width(f);
height = cucul_get_font_height(f);
w = cucul_get_font_width(f);
h = cucul_get_font_height(f);
iw = w + 1;
ih = h + 1;
switch(mode)
{
case GRAY:
gw = w;
gh = h;
break;
case HALFBLOCKS:
gw = w;
gh = (h + 1) / 2;
break;
case QUARTERBLOCKS:
gw = (w + 1) / 2;
gh = (h + 1) / 2;
break;
}
blocks = cucul_get_font_blocks(f);
onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
out = cucul_create_canvas(width + 2, height);
image = malloc(4 * width * height);
image = malloc(4 * iw * ih);
printf("tlf2a$ %u %u %u 0 3 0 0 0\n", height, height - 1, width + 2);
out = cucul_create_canvas(gw + 2, gh);
printf("tlf2a$ %u %u %u 0 4 0 0 0\n", gh, gh - 1, gw + 2);
printf("================================================================================\n");
printf(" This font was automatically generated by caca2tlf\n");
printf("================================================================================\n");
printf("=============================================="
"==================================\n");
printf(" This font was automatically generated using:\n");
printf(" %% caca2tlf %s\"%s\"\n", extraflag, fontname);
printf("=============================================="
"==================================\n");
for(i = 32; i < 127; i++)
add_char(i);
@ -88,7 +132,8 @@ int main(int argc, char *argv[])
unsigned int len;
unsigned long int ch = blocks[i] + j;
if(ch < 0x80)
if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
|| ch == 228 || ch == 246 || ch == 252 || ch == 223)
continue;
len = cucul_utf32_to_utf8(buf, ch);
@ -110,7 +155,7 @@ static void list_fonts(void)
{
char const * const * fonts;
unsigned int i;
fprintf(stderr, "Available fonts:\n");
fonts = cucul_get_font_list();
@ -124,26 +169,65 @@ static void add_char(unsigned int ch)
unsigned int x, y;
cucul_putchar(onechar, 0, 0, ch);
cucul_render_canvas(onechar, f, image, width, height, 4 * width);
cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
for(y = 0; y < height; y++)
for(x = 0; x < width; x++)
switch(mode)
{
uint8_t c = image[4 * (x + y * width) + 2];
case GRAY:
for(y = 0; y < h; y++)
for(x = 0; x < w; x++)
{
uint8_t c = image[4 * (x + y * iw) + 2];
if(c >= 0xa0)
cucul_putstr(out, x, y, "");
else if(c >= 0x80)
cucul_putstr(out, x, y, "");
else if(c >= 0x40)
cucul_putstr(out, x, y, "");
else if(c >= 0x20)
cucul_putstr(out, x, y, "");
else
cucul_putchar(out, x, y, ' ');
if(c >= 0xa0)
cucul_putstr(out, x, y, "");
else if(c >= 0x80)
cucul_putstr(out, x, y, "");
else if(c >= 0x40)
cucul_putstr(out, x, y, "");
else if(c >= 0x20)
cucul_putstr(out, x, y, "");
else
cucul_putchar(out, x, y, ' ');
}
break;
case HALFBLOCKS:
for(y = 0; y < gh; y++)
for(x = 0; x < gw; x++)
{
static char const *str[] = { " ", "", "", "" };
uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
cucul_putstr(out, x, y,
str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2)]);
}
break;
case QUARTERBLOCKS:
for(y = 0; y < gh; y++)
for(x = 0; x < gw; x++)
{
static char const *str[] =
{
" ", "", "", "", "", "", "", "",
"", "", "", "", "", "", "", ""
};
uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
cucul_putstr(out, x, y,
str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2) +
(p3 < 0x80 ? 0 : 4) + (p4 < 0x80 ? 0 : 8)]);
}
break;
}
cucul_draw_line(out, width, 0, width, height - 1, "@");
cucul_putchar(out, width + 1, height - 1, '@');
cucul_draw_line(out, gw, 0, gw, gh - 1, "@");
cucul_putchar(out, gw + 1, gh - 1, '@');
buf = cucul_export_canvas(out, "utf8");
fwrite(cucul_get_buffer_data(buf), cucul_get_buffer_size(buf), 1, stdout);