Ignore:
Timestamp:
Apr 13, 2006, 5:36:09 PM (14 years ago)
Author:
Sam Hocevar
Message:
  • Made makefont more configurable. Also, store the commandline in a comment so that the font can be easily rebuilt.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/tools/makefont.c

    r755 r762  
    2323#include <pango/pango.h>
    2424#include <pango/pangoft2.h>
    25 
    26 #define FONT "Monospace 9"
    27 #define DPI 96
    28 #define BPP 4
    2925
    3026static int const blocklist[] =
     
    5147static int printf_u16(char const *, uint16_t);
    5248
    53 int main(void)
     49int main(int argc, char *argv[])
    5450{
    5551    PangoContext *cx;
     
    6056
    6157    FT_Bitmap img;
    62     int width, height, b, i, n, blocks, glyphs, data_bytes;
     58    int width, height, b, i, n, blocks, glyphs;
     59    unsigned int glyph_size, control_size, data_size;
    6360    uint8_t *glyph_data;
    6461
    65     int bpp = BPP;
    66     int dpi = DPI;
    67     char const *font = FONT;
     62    unsigned int bpp, dpi;
     63    char const *prefix, *font;
     64
     65    if(argc != 5)
     66    {
     67        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
     68        fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]);
     69        fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]);
     70        return -1;
     71    }
     72
     73    prefix = argv[1];
     74    font = argv[2];
     75    dpi = atoi(argv[3]);
     76    bpp = atoi(argv[4]);
     77
     78    if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8))
     79    {
     80        fprintf(stderr, "%s: invalid argument\n", argv[0]);
     81        return -1;
     82    }
    6883
    6984    fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp);
     
    7792    if(!l)
    7893    {
     94        fprintf(stderr, "%s: unable to initialise pango\n", argv[0]);
    7995        g_object_unref(cx);
    8096        return -1;
     
    98114    width = PANGO_PIXELS(r.width);
    99115    height = PANGO_PIXELS(r.height);
    100     data_bytes = ((width * height) + (8 / bpp) - 1) / (8 / bpp);
    101     glyph_data = malloc(data_bytes);
     116    glyph_size = ((width * height) + (8 / bpp) - 1) / (8 / bpp);
     117    glyph_data = malloc(glyph_size);
    102118
    103119    /* Compute blocks and glyphs count */
     
    110126    }
    111127
     128    control_size = 24 + 12 * blocks + 8 * glyphs;
     129    data_size = glyph_size * glyphs;
     130
    112131    /* Let's go! */
    113132    printf("/* libcucul font file\n");
    114133    printf(" * \"%s\": %i dpi, %i bpp, %ix%i glyphs\n",
    115134           font, dpi, bpp, width, height);
    116     printf(" * Automatically generated by tools/makefont.c */\n");
    117     printf("\n");
     135    printf(" * Automatically generated by tools/makefont.c:\n");
     136    printf(" *   tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp);
     137    printf(" */\n");
     138    printf("\n");
     139
     140    printf("static unsigned int const %s_size = %i;\n",
     141           prefix, 8 + control_size + data_size);
     142    printf("static unsigned char const %s_data[] =\n", prefix);
    118143
    119144    printf("/* file: */\n");
     
    123148
    124149    printf("/* font_header: */\n");
    125     printf_u32("\"%s\" /* header_size */\n", 24 + 12 * blocks + 8 * glyphs);
    126     printf_u32("\"%s\" /* data_size */\n", data_bytes * glyphs);
     150    printf_u32("\"%s\" /* control_size */\n", control_size);
     151    printf_u32("\"%s\" /* data_size */\n", data_size);
    127152    printf_u16("\"%s\" /* version */\n", 1);
    128153    printf_u16("\"%s\" /* blocks */\n", blocks);
     
    153178            printf_u16("\"%s", width);
    154179            printf_u16("%s", height);
    155             printf_u32("%s\"\n", n * data_bytes);
     180            printf_u32("%s\"\n", n * glyph_size);
    156181            n++;
    157182        }
     
    205230            /* Render glyph on a bitmap */
    206231            pango_layout_set_text(l, buf, -1);
    207             memset(glyph_data, 0, data_bytes);
     232            memset(glyph_data, 0, glyph_size);
    208233            memset(img.buffer, 0, img.pitch * height);
    209234            pango_ft2_render_layout(&img, l, 0, 0);
     
    222247                }
    223248            }
    224             printf_hex("\"%s\"\n", glyph_data, data_bytes);
    225         }
    226     }
     249            printf_hex("\"%s\"\n", glyph_data, glyph_size);
     250        }
     251    }
     252
     253    printf(";\n");
    227254
    228255    free(img.buffer);
Note: See TracChangeset for help on using the changeset viewer.