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.
 
 
 
 
 
 

456 lines
14 KiB

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