source: libcaca/trunk/tools/makefont.c @ 929

Last change on this file since 929 was 929, checked in by Sam Hocevar, 14 years ago
  • Factored more UTF32 to UTF8 conversions using _cucul_utf32_to_utf8().
  • Property svn:keywords set to Id
File size: 8.3 KB
Line 
1/*
2 *  makefont       create libcaca font data
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: makefont.c 929 2006-05-05 14:03:40Z sam $
7 *
8 *  This program is free software; you can redistribute it and/or
9 *  modify it under the terms of the Do What The Fuck You Want To
10 *  Public License, Version 2, as published by Sam Hocevar. See
11 *  http://sam.zoy.org/wtfpl/COPYING for more details.
12 *
13 * Usage:
14 *   makefont <prefix> <font> <dpi> <bpp>
15 */
16
17#include "config.h"
18#include "common.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <stdint.h>
23
24#if defined(HAVE_ARPA_INET_H)
25#   include <arpa/inet.h>
26#elif defined(HAVE_NETINET_IN_H)
27#   include <netinet/in.h>
28#endif
29
30#include "cucul.h"
31#include "cucul_internals.h"
32
33#include <pango/pango.h>
34#include <pango/pangoft2.h>
35
36/* This list is built so that it includes all of ASCII, Latin-1, CP-437,
37 * and the UTF-8 glyphs necessary for canvas rotation and mirroring. */
38static int const blocklist[] =
39{
40    0x0000, 0x0080, /* Basic latin: A, B, C, a, b, c */
41    0x0080, 0x0100, /* Latin-1 Supplement: Ä, Ç, å, ß */
42    0x0100, 0x0180, /* Latin Extended-A: Ā č Ō œ */
43    0x0180, 0x0250, /* Latin Extended-B: Ǝ Ƹ */
44    0x0250, 0x02b0, /* IPA Extensions: ɐ ɔ ɘ ʌ ʍ */
45    0x0370, 0x0400, /* Greek and Coptic: Λ α β */
46    0x0400, 0x0500, /* Cyrillic: И Я */
47    0x2000, 0x2070, /* General Punctuation: ‘’ “” */
48#if 0
49    0x2100, 0x2150, /* Letterlike Symbols: Ⅎ */
50#endif
51    0x2200, 0x2300, /* Mathematical Operators: √ ∞ ∙ */
52    0x2300, 0x2400, /* Miscellaneous Technical: ⌐ ⌂ ⌠ ⌡ */
53    0x2500, 0x2580, /* Box Drawing: ═ ║ ╗ ╔ ╩ */
54    0x2580, 0x25a0, /* Block Elements: ▛ ▞ ░ ▒ ▓ */
55    0, 0
56};
57
58static int printf_hex(char const *, uint8_t *, int);
59static int printf_u32(char const *, uint32_t);
60static int printf_u16(char const *, uint16_t);
61
62int main(int argc, char *argv[])
63{
64    PangoContext *cx;
65    PangoFontDescription *fd;
66    PangoFontMap *fm;
67    PangoLayout *l;
68    PangoRectangle r;
69
70    FT_Bitmap img;
71    int width, height, b, i, n, blocks, glyphs;
72    unsigned int glyph_size, control_size, data_size;
73    uint8_t *glyph_data;
74
75    unsigned int bpp, dpi;
76    char const *prefix, *font;
77
78    if(argc != 5)
79    {
80        fprintf(stderr, "%s: wrong argument count\n", argv[0]);
81        fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]);
82        fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]);
83        return -1;
84    }
85
86    prefix = argv[1];
87    font = argv[2];
88    dpi = atoi(argv[3]);
89    bpp = atoi(argv[4]);
90
91    if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8))
92    {
93        fprintf(stderr, "%s: invalid argument\n", argv[0]);
94        return -1;
95    }
96
97    fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp);
98
99    /* Initialise Pango */
100    fm = pango_ft2_font_map_new();
101    pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fm), dpi, dpi);
102    cx = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fm));
103
104    l = pango_layout_new(cx);
105    if(!l)
106    {
107        fprintf(stderr, "%s: unable to initialise pango\n", argv[0]);
108        g_object_unref(cx);
109        return -1;
110    }
111
112    fd = pango_font_description_from_string(font);
113    pango_layout_set_font_description(l, fd);
114    pango_font_description_free(fd);
115
116    /* Initialise our FreeType2 bitmap */
117    img.width = 256;
118    img.pitch = 256;
119    img.rows = 256;
120    img.buffer = malloc(256 * 256);
121    img.num_grays = 256;
122    img.pixel_mode = ft_pixel_mode_grays;
123
124    /* Test rendering so that we know the glyph width */
125    pango_layout_set_markup(l, "@", -1);
126    pango_layout_get_extents(l, NULL, &r);
127    width = PANGO_PIXELS(r.width);
128    height = PANGO_PIXELS(r.height);
129    glyph_size = ((width * height) + (8 / bpp) - 1) / (8 / bpp);
130    glyph_data = malloc(glyph_size);
131
132    /* Compute blocks and glyphs count */
133    blocks = 0;
134    glyphs = 0;
135    for(b = 0; blocklist[b + 1]; b += 2)
136    {
137        blocks++;
138        glyphs += blocklist[b + 1] - blocklist[b];
139    }
140
141    control_size = 24 + 12 * blocks + 8 * glyphs;
142    data_size = glyph_size * glyphs;
143
144    /* Let's go! */
145    printf("/* libcucul font file\n");
146    printf(" * \"%s\": %i dpi, %i bpp, %ix%i glyphs\n",
147           font, dpi, bpp, width, height);
148    printf(" * Automatically generated by tools/makefont.c:\n");
149    printf(" *   tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp);
150    printf(" */\n");
151    printf("\n");
152
153    printf("static unsigned int const %s_size = %i;\n",
154           prefix, 8 + control_size + data_size);
155    printf("static unsigned char const %s_data[] =\n", prefix);
156
157    printf("/* file: */\n");
158    printf("\"CACA\" /* caca_header */\n");
159    printf("\"FONT\" /* caca_file_type */\n");
160    printf("\n");
161
162    printf("/* font_header: */\n");
163    printf_u32("\"%s\" /* control_size */\n", control_size);
164    printf_u32("\"%s\" /* data_size */\n", data_size);
165    printf_u16("\"%s\" /* version */\n", 1);
166    printf_u16("\"%s\" /* blocks */\n", blocks);
167    printf_u32("\"%s\" /* glyphs */\n", glyphs);
168    printf_u16("\"%s\" /* bpp */\n", bpp);
169    printf_u16("\"%s\" /* width */\n", width);
170    printf_u16("\"%s\" /* height */\n", height);
171    printf_u16("\"%s\" /* flags */\n", 1);
172    printf("\n");
173
174    printf("/* block_info: */\n");
175    n = 0;
176    for(b = 0; blocklist[b + 1]; b += 2)
177    {
178        printf_u32("\"%s", blocklist[b]);
179        printf_u32("%s", blocklist[b + 1]);
180        printf_u32("%s\"\n", n);
181        n += blocklist[b + 1] - blocklist[b];
182    }
183    printf("\n");
184
185    printf("/* glyph_info: */\n");
186    n = 0;
187    for(b = 0; blocklist[b + 1]; b += 2)
188    {
189        for(i = blocklist[b]; i < blocklist[b + 1]; i++)
190        {
191            printf_u16("\"%s", width);
192            printf_u16("%s", height);
193            printf_u32("%s\"\n", n * glyph_size);
194            n++;
195        }
196    }
197    printf("\n");
198
199    printf("/* font_data: */\n");
200    for(b = 0; blocklist[b + 1]; b += 2)
201    {
202        for(i = blocklist[b]; i < blocklist[b + 1]; i++)
203        {
204            unsigned int ch = i;
205            char buf[10];
206            int x, y, bytes;
207
208            bytes = _cucul_utf32_to_utf8(buf, ch);
209            buf[bytes] = '\0';
210
211            /* Print glyph value in comment */
212            printf("/* U+%.04X: \"", i);
213
214            if(i < 0x20 || (i >= 0x80 && i <= 0xa0))
215                printf("\\x%.02x\" */", i);
216            else
217                printf("%s\" */ ", buf);
218
219            /* Render glyph on a bitmap */
220            pango_layout_set_text(l, buf, -1);
221            memset(glyph_data, 0, glyph_size);
222            memset(img.buffer, 0, img.pitch * height);
223            pango_ft2_render_layout(&img, l, 0, 0);
224
225            /* Write bitmap as an escaped C string */
226            n = 0;
227            for(y = 0; y < height; y++)
228            {
229                for(x = 0; x < width; x++)
230                {
231                    uint8_t pixel = img.buffer[y * img.pitch + x];
232
233                    pixel >>= (8 - bpp);
234                    glyph_data[n / 8] |= pixel << (8 - bpp - (n % 8));
235                    n += bpp;
236                }
237            }
238            printf_hex("\"%s\"\n", glyph_data, glyph_size);
239        }
240    }
241
242    printf(";\n");
243
244    free(img.buffer);
245    g_object_unref(l);
246    g_object_unref(cx);
247
248    return 0;
249}
250
251/*
252 * XXX: the following functions are local
253 */
254
255static int printf_u32(char const *fmt, uint32_t i)
256{
257    uint32_t ni = hton32(i);
258    return printf_hex(fmt, (uint8_t *)&ni, 4);
259}
260
261static int printf_u16(char const *fmt, uint16_t i)
262{
263    uint16_t ni = hton16(i);
264    return printf_hex(fmt, (uint8_t *)&ni, 2);
265}
266
267static int printf_hex(char const *fmt, uint8_t *data, int bytes)
268{
269    char buf[BUFSIZ];
270    char *parser = buf;
271    int rewind = 0; /* we use this variable to rewind 2 bytes after \000
272                     * was printed when the next char starts with "\", too. */
273
274    while(bytes--)
275    {
276        uint8_t ch = *data++;
277        if(ch == '\\' || ch == '"')
278        {
279            parser -= rewind;
280            parser += sprintf(parser, "\\%c", ch);
281            rewind = 0;
282        }
283        else if(ch >= 0x20 && ch < 0x7f)
284        {
285            parser += sprintf(parser, "%c", ch);
286            rewind = 0;
287        }
288        else
289        {
290            parser -= rewind;
291            parser += sprintf(parser, "\\%.03o", ch);
292            rewind = ch ? 0 : 2;
293        }
294    }
295
296    parser -= rewind;
297    parser[0] = '\0';
298
299    return printf(fmt, buf);
300}
301
Note: See TracBrowser for help on using the repository browser.