You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

460 lines
14 KiB

  1. /*
  2. * makefont create libcaca font data
  3. * Copyright (c) 2006 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. *
  14. * Usage:
  15. * makefont <prefix> <font> <dpi> <bpp>
  16. */
  17. #include "config.h"
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <stdint.h>
  21. #if defined HAVE_ARPA_INET_H
  22. # include <arpa/inet.h>
  23. #elif defined HAVE_NETINET_IN_H
  24. # include <netinet/in.h>
  25. #endif
  26. #include <pango/pango.h>
  27. #include <pango/pangoft2.h>
  28. #include "caca_stubs.h"
  29. #include "caca.h"
  30. /* Split our big strings into chunks of 480 characters, because it is
  31. * the multiple of 32 directly below 509, which is the maximum allowed
  32. * string size in C89. */
  33. #define STRING_CHUNKS 480
  34. /* This list is built so that it includes all of ASCII, Latin-1, CP-437,
  35. * and the UTF-8 glyphs necessary for canvas rotation and mirroring. */
  36. static unsigned int const blocklist[] =
  37. {
  38. 0x0000, 0x0080, /* Basic latin: A, B, C, a, b, c */
  39. 0x0080, 0x0100, /* Latin-1 Supplement: Ä, Ç, å, ß */
  40. 0x0100, 0x0180, /* Latin Extended-A: Ā č Ō œ */
  41. 0x0180, 0x0250, /* Latin Extended-B: Ǝ Ƹ */
  42. 0x0250, 0x02b0, /* IPA Extensions: ɐ ɔ ɘ ʌ ʍ */
  43. 0x0370, 0x0400, /* Greek and Coptic: Λ α β */
  44. 0x0400, 0x0500, /* Cyrillic: И Я */
  45. 0x0530, 0x0590, /* Armenian: Ո */
  46. 0x1401, 0x1677, /* Unified Canadian Aboriginal Syllabics: ᒐ ᗡ */
  47. 0x1d00, 0x1d80, /* Phonetic Extensions: ᴉ ᵷ */
  48. 0x2000, 0x2070, /* General Punctuation: ‘’ “” */
  49. 0x2100, 0x2150, /* Letterlike Symbols: Ⅎ */
  50. 0x2200, 0x2300, /* Mathematical Operators: ∀ √ ∞ ∙ */
  51. 0x2300, 0x2400, /* Miscellaneous Technical: ⌐ ⌂ ⌠ ⌡ */
  52. 0x2500, 0x2580, /* Box Drawing: ═ ║ ╗ ╔ ╩ */
  53. 0x2580, 0x25a0, /* Block Elements: ▛ ▞ ░ ▒ ▓ */
  54. 0x25a0, 0x2600, /* Geometric Shapes: ◆ ○ ● */
  55. 0x2600, 0x2700, /* Miscellaneous Symbols: ♥ ★ ☭ */
  56. 0x3000, 0x3040, /* CJK Symbols and Punctuation: 。「」 */
  57. 0x3040, 0x30a0, /* Hiragana: で す */
  58. 0x30a0, 0x3100, /* Katakana: ロ ル */
  59. 0xff00, 0xfff0, /* Halfwidth and Fullwidth Forms: A, B, C, a, b, c */
  60. 0x10400, 0x10450, /* Deseret: 𐐒 𐐋 */
  61. 0, 0
  62. };
  63. struct glyph
  64. {
  65. uint32_t unicode;
  66. char buf[10];
  67. unsigned int same_as;
  68. unsigned int data_offset;
  69. unsigned int data_width;
  70. unsigned int data_size;
  71. };
  72. static void fix_glyph(FT_Bitmap *, uint32_t, unsigned int, unsigned int);
  73. static int printf_unicode(struct glyph *);
  74. static int printf_hex(char const *, uint8_t *, int);
  75. static int printf_u32(char const *, uint32_t);
  76. static int printf_u16(char const *, uint16_t);
  77. /* Counter for written bytes */
  78. static int written = 0;
  79. int main(int argc, char *argv[])
  80. {
  81. PangoContext *cx;
  82. PangoFontDescription *fd;
  83. PangoFontMap *fm;
  84. PangoLayout *l;
  85. PangoRectangle r;
  86. FT_Bitmap img;
  87. int stdwidth, fullwidth, height, blocks, glyphs, fullglyphs;
  88. unsigned int n, b, i;
  89. unsigned int stdsize, fullsize, control_size, data_size, current_offset;
  90. uint8_t *glyph_data;
  91. struct glyph *gtab;
  92. unsigned int bpp, dpi;
  93. char const *prefix, *font;
  94. if(argc != 5)
  95. {
  96. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  97. fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]);
  98. fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]);
  99. return -1;
  100. }
  101. prefix = argv[1];
  102. font = argv[2];
  103. dpi = atoi(argv[3]);
  104. bpp = atoi(argv[4]);
  105. if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8))
  106. {
  107. fprintf(stderr, "%s: invalid argument\n", argv[0]);
  108. return -1;
  109. }
  110. fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp);
  111. /* Initialise Pango */
  112. fm = pango_ft2_font_map_new();
  113. pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fm), dpi, dpi);
  114. cx = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fm));
  115. l = pango_layout_new(cx);
  116. if(!l)
  117. {
  118. fprintf(stderr, "%s: unable to initialise pango\n", argv[0]);
  119. g_object_unref(cx);
  120. return -1;
  121. }
  122. fd = pango_font_description_from_string(font);
  123. pango_layout_set_font_description(l, fd);
  124. pango_font_description_free(fd);
  125. /* Initialise our FreeType2 bitmap */
  126. img.width = 256;
  127. img.pitch = 256;
  128. img.rows = 256;
  129. img.buffer = malloc(256 * 256);
  130. img.num_grays = 256;
  131. img.pixel_mode = ft_pixel_mode_grays;
  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. stdwidth = PANGO_PIXELS(r.width);
  136. fullwidth = stdwidth * 2;
  137. height = PANGO_PIXELS(r.height);
  138. stdsize = ((stdwidth * height) + (8 / bpp) - 1) / (8 / bpp);
  139. fullsize = ((fullwidth * height) + (8 / bpp) - 1) / (8 / bpp);
  140. /* Compute blocks and glyphs count */
  141. blocks = 0;
  142. glyphs = 0;
  143. fullglyphs = 0;
  144. for(b = 0; blocklist[b + 1]; b += 2)
  145. {
  146. blocks++;
  147. glyphs += blocklist[b + 1] - blocklist[b];
  148. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  149. if(caca_utf32_is_fullwidth(i))
  150. fullglyphs++;
  151. }
  152. control_size = 28 + 12 * blocks + 8 * glyphs;
  153. data_size = stdsize * (glyphs - fullglyphs) + fullsize * fullglyphs;
  154. gtab = malloc(glyphs * sizeof(struct glyph));
  155. glyph_data = malloc(data_size);
  156. /* Let's go! */
  157. printf("/* libcaca font file\n");
  158. printf(" * \"%s\": %i dpi, %i bpp, %ix%i/%ix%i glyphs\n",
  159. font, dpi, bpp, stdwidth, height, fullwidth, height);
  160. printf(" * Automatically generated by tools/makefont.c:\n");
  161. printf(" * tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp);
  162. printf(" */\n");
  163. printf("\n");
  164. printf("static size_t const %s_size = %i;\n",
  165. prefix, 4 + control_size + data_size);
  166. printf("static uint8_t %s_data[%i] =\n",
  167. prefix, 4 + control_size + data_size);
  168. printf("{\n");
  169. printf("/* file: */\n");
  170. printf("0xCA,0xCA, /* caca_header */\n");
  171. written += 2;
  172. printf("'F','T', /* caca_file_type */\n");
  173. written += 2;
  174. printf("\n");
  175. printf("/* font_header: */\n");
  176. printf_u32("%s /* control_size */\n", control_size);
  177. printf_u32("%s /* data_size */\n", data_size);
  178. printf_u16("%s /* version */\n", 1);
  179. printf_u16("%s /* blocks */\n", blocks);
  180. printf_u32("%s /* glyphs */\n", glyphs);
  181. printf_u16("%s /* bpp */\n", bpp);
  182. printf_u16("%s /* std width */\n", stdwidth);
  183. printf_u16("%s /* std height */\n", height);
  184. printf_u16("%s /* max width */\n", fullwidth);
  185. printf_u16("%s /* max height */\n", height);
  186. printf_u16("%s /* flags */\n", 1);
  187. printf("\n");
  188. printf("/* block_info: */\n");
  189. n = 0;
  190. for(b = 0; blocklist[b + 1]; b += 2)
  191. {
  192. printf_u32("%s", blocklist[b]);
  193. printf_u32("%s", blocklist[b + 1]);
  194. printf_u32("%s\n", n);
  195. n += blocklist[b + 1] - blocklist[b];
  196. }
  197. printf("\n");
  198. /* Render all glyphs, so that we can know their offset */
  199. current_offset = n = 0;
  200. for(b = 0; blocklist[b + 1]; b += 2)
  201. {
  202. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  203. {
  204. int x, y, bytes, current_width = stdwidth;
  205. unsigned int k, current_size = stdsize;
  206. if(caca_utf32_is_fullwidth(i))
  207. {
  208. current_width = fullwidth;
  209. current_size = fullsize;
  210. }
  211. gtab[n].unicode = i;
  212. bytes = caca_utf32_to_utf8(gtab[n].buf, gtab[n].unicode);
  213. gtab[n].buf[bytes] = '\0';
  214. /* Render glyph on a bitmap */
  215. pango_layout_set_text(l, gtab[n].buf, -1);
  216. memset(img.buffer, 0, img.pitch * height);
  217. pango_ft2_render_layout(&img, l, 0, 0);
  218. /* Fix glyphs that we know how to handle better */
  219. fix_glyph(&img, gtab[n].unicode, current_width, height);
  220. /* Write bitmap as an escaped C string */
  221. memset(glyph_data + current_offset, 0, current_size);
  222. k = 0;
  223. for(y = 0; y < height; y++)
  224. {
  225. for(x = 0; x < current_width; x++)
  226. {
  227. uint8_t pixel = img.buffer[y * img.pitch + x];
  228. pixel >>= (8 - bpp);
  229. glyph_data[current_offset + k / 8]
  230. |= pixel << (8 - bpp - (k % 8));
  231. k += bpp;
  232. }
  233. }
  234. /* Check whether this is the same glyph as another one. Please
  235. * don't bullshit me about sorting, hashing and stuff like that,
  236. * our data is small enough for this to work. */
  237. for(k = 0; k < n; k++)
  238. {
  239. if(gtab[k].data_size != current_size)
  240. continue;
  241. #if 0
  242. if(!memcmp(glyph_data + gtab[k].data_offset,
  243. glyph_data + current_offset, current_size))
  244. break;
  245. #endif
  246. }
  247. gtab[n].data_offset = current_offset;
  248. gtab[n].data_width = current_width;
  249. gtab[n].data_size = current_size;
  250. gtab[n].same_as = k;
  251. if(k == n)
  252. current_offset += current_size;
  253. n++;
  254. }
  255. }
  256. printf("/* glyph_info: */\n");
  257. n = 0;
  258. for(b = 0; blocklist[b + 1]; b += 2)
  259. {
  260. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  261. {
  262. printf_u16("%s", gtab[n].data_width);
  263. printf_u16("%s", height);
  264. printf_u32("%s\n", gtab[gtab[n].same_as].data_offset);
  265. n++;
  266. }
  267. }
  268. printf("\n");
  269. printf("/* font_data: */\n");
  270. n = 0;
  271. for(b = 0; blocklist[b + 1]; b += 2)
  272. {
  273. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  274. {
  275. /* Print glyph value in comment */
  276. printf("/* ");
  277. printf_unicode(&gtab[n]);
  278. if(gtab[n].same_as == n)
  279. printf_hex(" */ %s\n",
  280. glyph_data + gtab[n].data_offset, gtab[n].data_size);
  281. else
  282. {
  283. printf(" is ");
  284. printf_unicode(&gtab[gtab[n].same_as]);
  285. printf(" */\n");
  286. }
  287. n++;
  288. }
  289. }
  290. printf("};\n");
  291. free(img.buffer);
  292. free(gtab);
  293. free(glyph_data);
  294. g_object_unref(l);
  295. g_object_unref(cx);
  296. return 0;
  297. }
  298. /*
  299. * XXX: the following functions are local
  300. */
  301. static void fix_glyph(FT_Bitmap *i, uint32_t ch,
  302. unsigned int width, unsigned int height)
  303. {
  304. unsigned int x, y;
  305. switch(ch)
  306. {
  307. case 0x00002580: /* ▀ */
  308. for(y = 0; y < height; y++)
  309. for(x = 0; x < width; x++)
  310. i->buffer[x + y * i->pitch] = y < height / 2 ? 0xff : 0x00;
  311. if(height & 1)
  312. for(x = 0; x < width; x++)
  313. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  314. break;
  315. case 0x00002584: /* ▄ */
  316. for(y = 0; y < height; y++)
  317. for(x = 0; x < width; x++)
  318. i->buffer[x + y * i->pitch] = y < height / 2 ? 0x00 : 0xff;
  319. if(height & 1)
  320. for(x = 0; x < width; x++)
  321. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  322. break;
  323. case 0x0000258c: /* ▌ */
  324. for(y = 0; y < height; y++)
  325. for(x = 0; x < width; x++)
  326. i->buffer[x + y * i->pitch] = x < width / 2 ? 0xff : 0x00;
  327. if(width & 1)
  328. for(y = 0; y < height; y++)
  329. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  330. break;
  331. case 0x00002590: /* ▐ */
  332. for(y = 0; y < height; y++)
  333. for(x = 0; x < width; x++)
  334. i->buffer[x + y * i->pitch] = x < width / 2 ? 0x00 : 0xff;
  335. if(width & 1)
  336. for(y = 0; y < height; y++)
  337. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  338. break;
  339. case 0x000025a0: /* ■ */
  340. for(y = 0; y < height; y++)
  341. for(x = 0; x < width; x++)
  342. i->buffer[x + y * i->pitch] =
  343. (y >= height / 4) && (y < 3 * height / 4) ? 0xff : 0x00;
  344. if(height & 3)
  345. for(x = 0; x < width; x++) /* FIXME: could be more precise */
  346. i->buffer[x + (height / 4) * i->pitch] =
  347. i->buffer[x + (3 * height / 4) * i->pitch] = 0x7f;
  348. break;
  349. case 0x00002588: /* █ */
  350. memset(i->buffer, 0xff, height * i->pitch);
  351. break;
  352. case 0x00002593: /* ▓ */
  353. for(y = 0; y < height; y++)
  354. for(x = 0; x < width; x++)
  355. i->buffer[x + y * i->pitch] =
  356. ((x + 2 * (y & 1)) & 3) ? 0xff : 0x00;
  357. break;
  358. case 0x00002592: /* ▒ */
  359. for(y = 0; y < height; y++)
  360. for(x = 0; x < width; x++)
  361. i->buffer[x + y * i->pitch] = ((x + y) & 1) ? 0xff : 0x00;
  362. break;
  363. case 0x00002591: /* ░ */
  364. for(y = 0; y < height; y++)
  365. for(x = 0; x < width; x++)
  366. i->buffer[x + y * i->pitch] =
  367. ((x + 2 * (y & 1)) & 3) ? 0x00 : 0xff;
  368. break;
  369. }
  370. }
  371. static int printf_unicode(struct glyph *g)
  372. {
  373. int wr = 0;
  374. wr += printf("U+%.04X: \"", g->unicode);
  375. if(g->unicode < 0x20 || (g->unicode >= 0x7f && g->unicode <= 0xa0))
  376. wr += printf("\\x%.02x\"", g->unicode);
  377. else
  378. wr += printf("%s\"", g->buf);
  379. return wr;
  380. }
  381. static int printf_u32(char const *fmt, uint32_t i)
  382. {
  383. uint32_t ni = hton32(i);
  384. return printf_hex(fmt, (uint8_t *)&ni, 4);
  385. }
  386. static int printf_u16(char const *fmt, uint16_t i)
  387. {
  388. uint16_t ni = hton16(i);
  389. return printf_hex(fmt, (uint8_t *)&ni, 2);
  390. }
  391. static int printf_hex(char const *fmt, uint8_t *data, int bytes)
  392. {
  393. char buf[BUFSIZ];
  394. char *parser = buf;
  395. while(bytes--)
  396. parser += sprintf(parser, "%i,", (unsigned int)*data++);
  397. parser[0] = '\0';
  398. return printf(fmt, buf);
  399. }