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 861 2006-04-24 20:45:55Z 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 <pango/pango.h> |
---|
31 | #include <pango/pangoft2.h> |
---|
32 | |
---|
33 | static int const blocklist[] = |
---|
34 | { |
---|
35 | 0x0000, 0x0080, /* Basic latin: A, B, C, a, img, c */ |
---|
36 | 0x0080, 0x0100, /* Latin-1 Supplement: Ä, Ç, å, ß */ |
---|
37 | 0x0100, 0x0180, /* Latin Extended-A: Ā č Ō œ */ |
---|
38 | 0x0180, 0x0250, /* Latin Extended-B: Ǝ Ƹ */ |
---|
39 | 0x0250, 0x02b0, /* IPA Extensions: ɐ ɔ ɘ ʌ ʍ */ |
---|
40 | 0x0370, 0x0400, /* Greek and Coptic: Λ α β */ |
---|
41 | 0x0400, 0x0500, /* Cyrillic: И Я */ |
---|
42 | 0x2000, 0x2070, /* General Punctuation: ‘’ “” */ |
---|
43 | #if 0 |
---|
44 | 0x2100, 0x2150, /* Letterlike Symbols: Ⅎ */ |
---|
45 | #endif |
---|
46 | 0x2300, 0x2400, /* Miscellaneous Technical: ⌂ */ |
---|
47 | 0x2500, 0x2580, /* Box Drawing: ═ ║ ╗ ╔ ╩ */ |
---|
48 | 0x2580, 0x25a0, /* Block Elements: ▛ ▞ ░ ▒ ▓ */ |
---|
49 | 0, 0 |
---|
50 | }; |
---|
51 | |
---|
52 | static int printf_hex(char const *, uint8_t *, int); |
---|
53 | static int printf_u32(char const *, uint32_t); |
---|
54 | static int printf_u16(char const *, uint16_t); |
---|
55 | |
---|
56 | int main(int argc, char *argv[]) |
---|
57 | { |
---|
58 | PangoContext *cx; |
---|
59 | PangoFontDescription *fd; |
---|
60 | PangoFontMap *fm; |
---|
61 | PangoLayout *l; |
---|
62 | PangoRectangle r; |
---|
63 | |
---|
64 | FT_Bitmap img; |
---|
65 | int width, height, b, i, n, blocks, glyphs; |
---|
66 | unsigned int glyph_size, control_size, data_size; |
---|
67 | uint8_t *glyph_data; |
---|
68 | |
---|
69 | unsigned int bpp, dpi; |
---|
70 | char const *prefix, *font; |
---|
71 | |
---|
72 | if(argc != 5) |
---|
73 | { |
---|
74 | fprintf(stderr, "%s: wrong argument count\n", argv[0]); |
---|
75 | fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]); |
---|
76 | fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]); |
---|
77 | return -1; |
---|
78 | } |
---|
79 | |
---|
80 | prefix = argv[1]; |
---|
81 | font = argv[2]; |
---|
82 | dpi = atoi(argv[3]); |
---|
83 | bpp = atoi(argv[4]); |
---|
84 | |
---|
85 | if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8)) |
---|
86 | { |
---|
87 | fprintf(stderr, "%s: invalid argument\n", argv[0]); |
---|
88 | return -1; |
---|
89 | } |
---|
90 | |
---|
91 | fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp); |
---|
92 | |
---|
93 | /* Initialise Pango */ |
---|
94 | fm = pango_ft2_font_map_new(); |
---|
95 | pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fm), dpi, dpi); |
---|
96 | cx = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fm)); |
---|
97 | |
---|
98 | l = pango_layout_new(cx); |
---|
99 | if(!l) |
---|
100 | { |
---|
101 | fprintf(stderr, "%s: unable to initialise pango\n", argv[0]); |
---|
102 | g_object_unref(cx); |
---|
103 | return -1; |
---|
104 | } |
---|
105 | |
---|
106 | fd = pango_font_description_from_string(font); |
---|
107 | pango_layout_set_font_description(l, fd); |
---|
108 | pango_font_description_free(fd); |
---|
109 | |
---|
110 | /* Initialise our FreeType2 bitmap */ |
---|
111 | img.width = 256; |
---|
112 | img.pitch = 256; |
---|
113 | img.rows = 256; |
---|
114 | img.buffer = malloc(256 * 256); |
---|
115 | img.num_grays = 256; |
---|
116 | img.pixel_mode = ft_pixel_mode_grays; |
---|
117 | |
---|
118 | /* Test rendering so that we know the glyph width */ |
---|
119 | pango_layout_set_markup(l, "@", -1); |
---|
120 | pango_layout_get_extents(l, NULL, &r); |
---|
121 | width = PANGO_PIXELS(r.width); |
---|
122 | height = PANGO_PIXELS(r.height); |
---|
123 | glyph_size = ((width * height) + (8 / bpp) - 1) / (8 / bpp); |
---|
124 | glyph_data = malloc(glyph_size); |
---|
125 | |
---|
126 | /* Compute blocks and glyphs count */ |
---|
127 | blocks = 0; |
---|
128 | glyphs = 0; |
---|
129 | for(b = 0; blocklist[b + 1]; b += 2) |
---|
130 | { |
---|
131 | blocks++; |
---|
132 | glyphs += blocklist[b + 1] - blocklist[b]; |
---|
133 | } |
---|
134 | |
---|
135 | control_size = 24 + 12 * blocks + 8 * glyphs; |
---|
136 | data_size = glyph_size * glyphs; |
---|
137 | |
---|
138 | /* Let's go! */ |
---|
139 | printf("/* libcucul font file\n"); |
---|
140 | printf(" * \"%s\": %i dpi, %i bpp, %ix%i glyphs\n", |
---|
141 | font, dpi, bpp, width, height); |
---|
142 | printf(" * Automatically generated by tools/makefont.c:\n"); |
---|
143 | printf(" * tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp); |
---|
144 | printf(" */\n"); |
---|
145 | printf("\n"); |
---|
146 | |
---|
147 | printf("static unsigned int const %s_size = %i;\n", |
---|
148 | prefix, 8 + control_size + data_size); |
---|
149 | printf("static unsigned char const %s_data[] =\n", prefix); |
---|
150 | |
---|
151 | printf("/* file: */\n"); |
---|
152 | printf("\"CACA\" /* caca_header */\n"); |
---|
153 | printf("\"FONT\" /* caca_file_type */\n"); |
---|
154 | printf("\n"); |
---|
155 | |
---|
156 | printf("/* font_header: */\n"); |
---|
157 | printf_u32("\"%s\" /* control_size */\n", control_size); |
---|
158 | printf_u32("\"%s\" /* data_size */\n", data_size); |
---|
159 | printf_u16("\"%s\" /* version */\n", 1); |
---|
160 | printf_u16("\"%s\" /* blocks */\n", blocks); |
---|
161 | printf_u32("\"%s\" /* glyphs */\n", glyphs); |
---|
162 | printf_u16("\"%s\" /* bpp */\n", bpp); |
---|
163 | printf_u16("\"%s\" /* width */\n", width); |
---|
164 | printf_u16("\"%s\" /* height */\n", height); |
---|
165 | printf_u16("\"%s\" /* flags */\n", 1); |
---|
166 | printf("\n"); |
---|
167 | |
---|
168 | printf("/* block_info: */\n"); |
---|
169 | n = 0; |
---|
170 | for(b = 0; blocklist[b + 1]; b += 2) |
---|
171 | { |
---|
172 | printf_u32("\"%s", blocklist[b]); |
---|
173 | printf_u32("%s", blocklist[b + 1]); |
---|
174 | printf_u32("%s\"\n", n); |
---|
175 | n += blocklist[b + 1] - blocklist[b]; |
---|
176 | } |
---|
177 | printf("\n"); |
---|
178 | |
---|
179 | printf("/* glyph_info: */\n"); |
---|
180 | n = 0; |
---|
181 | for(b = 0; blocklist[b + 1]; b += 2) |
---|
182 | { |
---|
183 | for(i = blocklist[b]; i < blocklist[b + 1]; i++) |
---|
184 | { |
---|
185 | printf_u16("\"%s", width); |
---|
186 | printf_u16("%s", height); |
---|
187 | printf_u32("%s\"\n", n * glyph_size); |
---|
188 | n++; |
---|
189 | } |
---|
190 | } |
---|
191 | printf("\n"); |
---|
192 | |
---|
193 | printf("/* font_data: */\n"); |
---|
194 | for(b = 0; blocklist[b + 1]; b += 2) |
---|
195 | { |
---|
196 | for(i = blocklist[b]; i < blocklist[b + 1]; i++) |
---|
197 | { |
---|
198 | unsigned int ch = i; |
---|
199 | char buf[10], *parser; |
---|
200 | int x, y, bytes; |
---|
201 | |
---|
202 | if(ch < 0x80) |
---|
203 | { |
---|
204 | bytes = 1; |
---|
205 | buf[0] = ch; |
---|
206 | buf[1] = '\0'; |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | static const unsigned char mark[7] = |
---|
211 | { |
---|
212 | 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC |
---|
213 | }; |
---|
214 | |
---|
215 | /* FIXME: use libcucul instead of this shit */ |
---|
216 | bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4; |
---|
217 | buf[bytes] = '\0'; |
---|
218 | parser = buf + bytes; |
---|
219 | |
---|
220 | switch(bytes) |
---|
221 | { |
---|
222 | case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; |
---|
223 | case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; |
---|
224 | case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6; |
---|
225 | } |
---|
226 | *--parser = ch | mark[bytes]; |
---|
227 | } |
---|
228 | |
---|
229 | /* Print glyph value in comment */ |
---|
230 | printf("/* U+%.04X: \"", i); |
---|
231 | |
---|
232 | if(i < 0x20 || (i >= 0x80 && i <= 0xa0)) |
---|
233 | printf("\\x%.02x\" */", i); |
---|
234 | else |
---|
235 | printf("%s\" */ ", buf); |
---|
236 | |
---|
237 | /* Render glyph on a bitmap */ |
---|
238 | pango_layout_set_text(l, buf, -1); |
---|
239 | memset(glyph_data, 0, glyph_size); |
---|
240 | memset(img.buffer, 0, img.pitch * height); |
---|
241 | pango_ft2_render_layout(&img, l, 0, 0); |
---|
242 | |
---|
243 | /* Write bitmap as an escaped C string */ |
---|
244 | n = 0; |
---|
245 | for(y = 0; y < height; y++) |
---|
246 | { |
---|
247 | for(x = 0; x < width; x++) |
---|
248 | { |
---|
249 | uint8_t pixel = img.buffer[y * img.pitch + x]; |
---|
250 | |
---|
251 | pixel >>= (8 - bpp); |
---|
252 | glyph_data[n / 8] |= pixel << (8 - bpp - (n % 8)); |
---|
253 | n += bpp; |
---|
254 | } |
---|
255 | } |
---|
256 | printf_hex("\"%s\"\n", glyph_data, glyph_size); |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | printf(";\n"); |
---|
261 | |
---|
262 | free(img.buffer); |
---|
263 | g_object_unref(l); |
---|
264 | g_object_unref(cx); |
---|
265 | |
---|
266 | return 0; |
---|
267 | } |
---|
268 | |
---|
269 | /* |
---|
270 | * XXX: the following functions are local |
---|
271 | */ |
---|
272 | |
---|
273 | static int printf_u32(char const *fmt, uint32_t i) |
---|
274 | { |
---|
275 | uint32_t ni = htonl(i); |
---|
276 | return printf_hex(fmt, (uint8_t *)&ni, 4); |
---|
277 | } |
---|
278 | |
---|
279 | static int printf_u16(char const *fmt, uint16_t i) |
---|
280 | { |
---|
281 | uint16_t ni = htons(i); |
---|
282 | return printf_hex(fmt, (uint8_t *)&ni, 2); |
---|
283 | } |
---|
284 | |
---|
285 | static int printf_hex(char const *fmt, uint8_t *data, int bytes) |
---|
286 | { |
---|
287 | char buf[BUFSIZ]; |
---|
288 | char *parser = buf; |
---|
289 | int rewind = 0; /* we use this variable to rewind 2 bytes after \000 |
---|
290 | * was printed when the next char starts with "\", too. */ |
---|
291 | |
---|
292 | while(bytes--) |
---|
293 | { |
---|
294 | uint8_t ch = *data++; |
---|
295 | if(ch == '\\' || ch == '"') |
---|
296 | { |
---|
297 | parser -= rewind; |
---|
298 | parser += sprintf(parser, "\\%c", ch); |
---|
299 | rewind = 0; |
---|
300 | } |
---|
301 | else if(ch >= 0x20 && ch < 0x7f) |
---|
302 | { |
---|
303 | parser += sprintf(parser, "%c", ch); |
---|
304 | rewind = 0; |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | parser -= rewind; |
---|
309 | parser += sprintf(parser, "\\%.03o", ch); |
---|
310 | rewind = ch ? 0 : 2; |
---|
311 | } |
---|
312 | } |
---|
313 | |
---|
314 | parser -= rewind; |
---|
315 | parser[0] = '\0'; |
---|
316 | |
---|
317 | return printf(fmt, buf); |
---|
318 | } |
---|
319 | |
---|