Ignore:
Timestamp:
Nov 6, 2006, 6:24:02 PM (16 years ago)
Author:
Sam Hocevar
Message:
  • Add --half and --quarter options to caca2tlf.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • toilet/trunk/tools/caca2tlf.c

    r1291 r1296  
    2222#endif
    2323#include <stdio.h>
     24#include <string.h>
    2425#include <stdlib.h>
    2526#include <cucul.h>
     27
     28enum mode { GRAY, HALFBLOCKS, QUARTERBLOCKS } mode;
    2629
    2730static void list_fonts(void);
     
    3235unsigned long int const *blocks;
    3336uint8_t * image;
    34 unsigned int width, height;
     37unsigned int w, h, gw, gh, iw, ih;
    3538
    3639int main(int argc, char *argv[])
    3740{
     41    char *fontname, *extraflag;
    3842    unsigned int b, i;
    3943
    4044    if(argc < 2)
    4145    {
    42         fprintf(stderr, "Wrong argument count. Usage: caca2tlf <font>\n");
     46        fprintf(stderr, "Usage: %s [--half|--quarter] <font>\n", argv[0]);
    4347        list_fonts();
    4448        return -1;
    4549    }
    4650
    47     f = cucul_load_font(argv[1], 0);
     51    if(!strcmp(argv[1], "--half") && argc >= 3)
     52    {
     53        extraflag = "--half ";
     54        mode = HALFBLOCKS;
     55        fontname = argv[2];
     56    }
     57    else if(!strcmp(argv[1], "--quarter") && argc >= 3)
     58    {
     59        extraflag = "--quarter ";
     60        mode = QUARTERBLOCKS;
     61        fontname = argv[2];
     62    }
     63    else
     64    {
     65        extraflag = "";
     66        mode = GRAY;
     67        fontname = argv[1];
     68    }
     69
     70    f = cucul_load_font(fontname, 0);
    4871
    4972    if(!f)
     
    5477    }
    5578
    56     width = cucul_get_font_width(f);
    57     height = cucul_get_font_height(f);
     79    w = cucul_get_font_width(f);
     80    h = cucul_get_font_height(f);
     81    iw = w + 1;
     82    ih = h + 1;
     83    switch(mode)
     84    {
     85    case GRAY:
     86        gw = w;
     87        gh = h;
     88        break;
     89    case HALFBLOCKS:
     90        gw = w;
     91        gh = (h + 1) / 2;
     92        break;
     93    case QUARTERBLOCKS:
     94        gw = (w + 1) / 2;
     95        gh = (h + 1) / 2;
     96        break;
     97    }
     98
    5899    blocks = cucul_get_font_blocks(f);
    59100    onechar = cucul_create_canvas(1, 1); /* FIXME: support double width */
    60101    cucul_set_color_ansi(onechar, CUCUL_WHITE, CUCUL_BLACK);
    61     out = cucul_create_canvas(width + 2, height);
    62     image = malloc(4 * width * height);
    63 
    64     printf("tlf2a$ %u %u %u 0 3 0 0 0\n", height, height - 1, width + 2);
    65 
    66     printf("================================================================================\n");
    67     printf("  This font was automatically generated by caca2tlf\n");
    68     printf("================================================================================\n");
     102    image = malloc(4 * iw * ih);
     103
     104    out = cucul_create_canvas(gw + 2, gh);
     105    printf("tlf2a$ %u %u %u 0 4 0 0 0\n", gh, gh - 1, gw + 2);
     106
     107    printf("=============================================="
     108                                       "==================================\n");
     109    printf("  This font was automatically generated using:\n");
     110    printf("   %% caca2tlf %s\"%s\"\n", extraflag, fontname);
     111    printf("=============================================="
     112                                       "==================================\n");
    69113
    70114    for(i = 32; i < 127; i++)
     
    89133            unsigned long int ch = blocks[i] + j;
    90134
    91             if(ch < 0x80)
     135            if(ch <= 127 || ch == 196 || ch == 214 || ch == 220
     136               || ch == 228 || ch == 246 || ch == 252 || ch == 223)
    92137                continue;
    93138
     
    111156    char const * const * fonts;
    112157    unsigned int i;
    113        
     158
    114159    fprintf(stderr, "Available fonts:\n");
    115160
     
    125170
    126171    cucul_putchar(onechar, 0, 0, ch);
    127     cucul_render_canvas(onechar, f, image, width, height, 4 * width);
    128 
    129     for(y = 0; y < height; y++)
    130        for(x = 0; x < width; x++)
    131     {
    132         uint8_t c = image[4 * (x + y * width) + 2];
    133 
    134         if(c >= 0xa0)
    135             cucul_putstr(out, x, y, "█");
    136         else if(c >= 0x80)
    137             cucul_putstr(out, x, y, "▓");
    138         else if(c >= 0x40)
    139             cucul_putstr(out, x, y, "▒");
    140         else if(c >= 0x20)
    141             cucul_putstr(out, x, y, "░");
    142         else
    143             cucul_putchar(out, x, y, ' ');
    144     }
    145     cucul_draw_line(out, width, 0, width, height - 1, "@");
    146     cucul_putchar(out, width + 1, height - 1, '@');
     172    cucul_render_canvas(onechar, f, image, iw, ih, 4 * iw);
     173
     174    switch(mode)
     175    {
     176    case GRAY:
     177        for(y = 0; y < h; y++)
     178           for(x = 0; x < w; x++)
     179        {
     180            uint8_t c = image[4 * (x + y * iw) + 2];
     181
     182            if(c >= 0xa0)
     183                cucul_putstr(out, x, y, "█");
     184            else if(c >= 0x80)
     185                cucul_putstr(out, x, y, "▓");
     186            else if(c >= 0x40)
     187                cucul_putstr(out, x, y, "▒");
     188            else if(c >= 0x20)
     189                cucul_putstr(out, x, y, "░");
     190            else
     191                cucul_putchar(out, x, y, ' ');
     192        }
     193        break;
     194    case HALFBLOCKS:
     195        for(y = 0; y < gh; y++)
     196           for(x = 0; x < gw; x++)
     197        {
     198            static char const *str[] = { " ", "▀", "▄", "█" };
     199
     200            uint8_t p1 = image[4 * (x + y * 2 * iw) + 2];
     201            uint8_t p2 = image[4 * (x + (y * 2 + 1) * iw) + 2];
     202
     203            cucul_putstr(out, x, y,
     204                         str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2)]);
     205        }
     206        break;
     207    case QUARTERBLOCKS:
     208        for(y = 0; y < gh; y++)
     209           for(x = 0; x < gw; x++)
     210        {
     211            static char const *str[] =
     212            {
     213                " ", "▘", "▝", "▀", "▖", "▌", "▞", "▛",
     214                "▗", "▚", "▐", "▜", "▄", "▙", "▟", "█"
     215            };
     216
     217            uint8_t p1 = image[4 * (x * 2 + y * 2 * iw) + 2];
     218            uint8_t p2 = image[4 * (x * 2 + 1 + y * 2 * iw) + 2];
     219            uint8_t p3 = image[4 * (x * 2 + (y * 2 + 1) * iw) + 2];
     220            uint8_t p4 = image[4 * (x * 2 + 1 + (y * 2 + 1) * iw) + 2];
     221
     222            cucul_putstr(out, x, y,
     223                         str[(p1 < 0x80 ? 0 : 1) + (p2 < 0x80 ? 0 : 2) +
     224                             (p3 < 0x80 ? 0 : 4) + (p4 < 0x80 ? 0 : 8)]);
     225        }
     226        break;
     227    }
     228
     229    cucul_draw_line(out, gw, 0, gw, gh - 1, "@");
     230    cucul_putchar(out, gw + 1, gh - 1, '@');
    147231
    148232    buf = cucul_export_canvas(out, "utf8");
Note: See TracChangeset for help on using the changeset viewer.