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