Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

491 řádky
15 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; 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. #include "config.h"
  17. #include "common.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 "cucul.h"
  27. #include <pango/pango.h>
  28. #include <pango/pangoft2.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. 0x3000, 0x3040, /* CJK Symbols and Punctuation: 。「」 */
  53. 0x3040, 0x30a0, /* Hiragana: で す */
  54. 0x30a0, 0x3100, /* Katakana: ロ ル */
  55. 0, 0
  56. };
  57. struct glyph
  58. {
  59. uint32_t unicode;
  60. char buf[10];
  61. unsigned int same_as;
  62. unsigned int data_offset;
  63. unsigned int data_width;
  64. unsigned int data_size;
  65. };
  66. static void fix_glyph(FT_Bitmap *, uint32_t, unsigned int, unsigned int);
  67. static int printf_unicode(struct glyph *);
  68. static int printf_hex(char const *, uint8_t *, int);
  69. static int printf_u32(char const *, uint32_t);
  70. static int printf_u16(char const *, uint16_t);
  71. /* Counter for written bytes */
  72. static int written = 0;
  73. int main(int argc, char *argv[])
  74. {
  75. PangoContext *cx;
  76. PangoFontDescription *fd;
  77. PangoFontMap *fm;
  78. PangoLayout *l;
  79. PangoRectangle r;
  80. FT_Bitmap img;
  81. int stdwidth, fullwidth, height, blocks, glyphs, fullglyphs;
  82. unsigned int n, b, i, index;
  83. unsigned int stdsize, fullsize, control_size, data_size, current_offset;
  84. uint8_t *glyph_data;
  85. struct glyph *gtab;
  86. unsigned int bpp, dpi;
  87. char const *prefix, *font;
  88. if(argc != 5)
  89. {
  90. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  91. fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]);
  92. fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]);
  93. return -1;
  94. }
  95. prefix = argv[1];
  96. font = argv[2];
  97. dpi = atoi(argv[3]);
  98. bpp = atoi(argv[4]);
  99. if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8))
  100. {
  101. fprintf(stderr, "%s: invalid argument\n", argv[0]);
  102. return -1;
  103. }
  104. fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp);
  105. /* Initialise Pango */
  106. fm = pango_ft2_font_map_new();
  107. pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fm), dpi, dpi);
  108. cx = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fm));
  109. l = pango_layout_new(cx);
  110. if(!l)
  111. {
  112. fprintf(stderr, "%s: unable to initialise pango\n", argv[0]);
  113. g_object_unref(cx);
  114. return -1;
  115. }
  116. fd = pango_font_description_from_string(font);
  117. pango_layout_set_font_description(l, fd);
  118. pango_font_description_free(fd);
  119. /* Initialise our FreeType2 bitmap */
  120. img.width = 256;
  121. img.pitch = 256;
  122. img.rows = 256;
  123. img.buffer = malloc(256 * 256);
  124. img.num_grays = 256;
  125. img.pixel_mode = ft_pixel_mode_grays;
  126. /* Test rendering so that we know the glyph width */
  127. pango_layout_set_markup(l, "@", -1);
  128. pango_layout_get_extents(l, NULL, &r);
  129. stdwidth = PANGO_PIXELS(r.width);
  130. fullwidth = stdwidth * 2;
  131. height = PANGO_PIXELS(r.height);
  132. stdsize = ((stdwidth * height) + (8 / bpp) - 1) / (8 / bpp);
  133. fullsize = ((fullwidth * height) + (8 / bpp) - 1) / (8 / bpp);
  134. /* Compute blocks and glyphs count */
  135. blocks = 0;
  136. glyphs = 0;
  137. fullglyphs = 0;
  138. for(b = 0; blocklist[b + 1]; b += 2)
  139. {
  140. blocks++;
  141. glyphs += blocklist[b + 1] - blocklist[b];
  142. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  143. if(cucul_utf32_is_fullwidth(i))
  144. fullglyphs++;
  145. }
  146. control_size = 28 + 12 * blocks + 8 * glyphs;
  147. data_size = stdsize * (glyphs - fullglyphs) + fullsize * fullglyphs;
  148. gtab = malloc(glyphs * sizeof(struct glyph));
  149. glyph_data = malloc(data_size);
  150. /* Let's go! */
  151. printf("/* libcucul font file\n");
  152. printf(" * \"%s\": %i dpi, %i bpp, %ix%i/%ix%i glyphs\n",
  153. font, dpi, bpp, stdwidth, height, fullwidth, height);
  154. printf(" * Automatically generated by tools/makefont.c:\n");
  155. printf(" * tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp);
  156. printf(" */\n");
  157. printf("\n");
  158. printf("static unsigned int const %s_size = %i;\n",
  159. prefix, 4 + control_size + data_size);
  160. printf("static struct {\n");
  161. printf("char ");
  162. for(i = 0; (i + 1) * STRING_CHUNKS < 8 + control_size + data_size; i++)
  163. printf("d%x[%i],%c", i, STRING_CHUNKS, (i % 6) == 5 ? '\n' : ' ');
  164. printf("d%x[%i];\n", i, 4 + control_size + data_size - i * STRING_CHUNKS);
  165. printf("} %s_data = {\n", prefix);
  166. printf("\n");
  167. printf("/* file: */\n");
  168. printf("\"\\xCA\\xCA\" /* caca_header */\n");
  169. written += 2;
  170. printf("\"FT\" /* caca_file_type */\n");
  171. written += 2;
  172. printf("\n");
  173. printf("/* font_header: */\n");
  174. printf_u32("\"%s\" /* control_size */\n", control_size);
  175. printf_u32("\"%s\" /* data_size */\n", data_size);
  176. printf_u16("\"%s\" /* version */\n", 1);
  177. printf_u16("\"%s\" /* blocks */\n", blocks);
  178. printf_u32("\"%s\" /* glyphs */\n", glyphs);
  179. printf_u16("\"%s\" /* bpp */\n", bpp);
  180. printf_u16("\"%s\" /* std width */\n", stdwidth);
  181. printf_u16("\"%s\" /* std height */\n", height);
  182. printf_u16("\"%s\" /* max width */\n", fullwidth);
  183. printf_u16("\"%s\" /* max height */\n", height);
  184. printf_u16("\"%s\" /* flags */\n", 1);
  185. printf("\n");
  186. printf("/* block_info: */\n");
  187. n = 0;
  188. for(b = 0; blocklist[b + 1]; b += 2)
  189. {
  190. printf_u32("\"%s", blocklist[b]);
  191. printf_u32("%s", blocklist[b + 1]);
  192. printf_u32("%s\"\n", n);
  193. n += blocklist[b + 1] - blocklist[b];
  194. }
  195. printf("\n");
  196. /* Render all glyphs, so that we can know their offset */
  197. current_offset = n = index = 0;
  198. for(b = 0; blocklist[b + 1]; b += 2)
  199. {
  200. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  201. {
  202. int x, y, bytes, current_width = stdwidth;
  203. unsigned int k, current_size = stdsize;
  204. if(cucul_utf32_is_fullwidth(i))
  205. {
  206. current_width = fullwidth;
  207. current_size = fullsize;
  208. }
  209. gtab[n].unicode = i;
  210. bytes = cucul_utf32_to_utf8(gtab[n].buf, gtab[n].unicode);
  211. gtab[n].buf[bytes] = '\0';
  212. /* Render glyph on a bitmap */
  213. pango_layout_set_text(l, gtab[n].buf, -1);
  214. memset(img.buffer, 0, img.pitch * height);
  215. pango_ft2_render_layout(&img, l, 0, 0);
  216. /* Fix glyphs that we know how to handle better */
  217. fix_glyph(&img, gtab[n].unicode, current_width, height);
  218. /* Write bitmap as an escaped C string */
  219. memset(glyph_data + current_offset, 0, current_size);
  220. k = 0;
  221. for(y = 0; y < height; y++)
  222. {
  223. for(x = 0; x < current_width; x++)
  224. {
  225. uint8_t pixel = img.buffer[y * img.pitch + x];
  226. pixel >>= (8 - bpp);
  227. glyph_data[current_offset + k / 8]
  228. |= pixel << (8 - bpp - (k % 8));
  229. k += bpp;
  230. }
  231. }
  232. /* Check whether this is the same glyph as another one. Please
  233. * don't bullshit me about sorting, hashing and stuff like that,
  234. * our data is small enough for this to work. */
  235. for(k = 0; k < n; k++)
  236. {
  237. if(gtab[k].data_size != current_size)
  238. continue;
  239. #if 0
  240. if(!memcmp(glyph_data + gtab[k].data_offset,
  241. glyph_data + current_offset, current_size))
  242. break;
  243. #endif
  244. }
  245. gtab[n].data_offset = current_offset;
  246. gtab[n].data_width = current_width;
  247. gtab[n].data_size = current_size;
  248. gtab[n].same_as = k;
  249. if(k == n)
  250. current_offset += current_size;
  251. n++;
  252. }
  253. }
  254. printf("/* glyph_info: */\n");
  255. n = 0;
  256. for(b = 0; blocklist[b + 1]; b += 2)
  257. {
  258. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  259. {
  260. printf_u16("\"%s", gtab[n].data_width);
  261. printf_u16("%s", height);
  262. printf_u32("%s\"\n", gtab[gtab[n].same_as].data_offset);
  263. n++;
  264. }
  265. }
  266. printf("\n");
  267. printf("/* font_data: */\n");
  268. n = 0;
  269. for(b = 0; blocklist[b + 1]; b += 2)
  270. {
  271. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  272. {
  273. /* Print glyph value in comment */
  274. printf("/* ");
  275. printf_unicode(&gtab[n]);
  276. if(gtab[n].same_as == n)
  277. printf_hex(" */ \"%s\"\n",
  278. glyph_data + gtab[n].data_offset, gtab[n].data_size);
  279. else
  280. {
  281. printf(" is ");
  282. printf_unicode(&gtab[gtab[n].same_as]);
  283. printf(" */\n");
  284. }
  285. n++;
  286. }
  287. }
  288. printf("};\n");
  289. free(img.buffer);
  290. free(gtab);
  291. free(glyph_data);
  292. g_object_unref(l);
  293. g_object_unref(cx);
  294. return 0;
  295. }
  296. /*
  297. * XXX: the following functions are local
  298. */
  299. static void fix_glyph(FT_Bitmap *i, uint32_t ch,
  300. unsigned int width, unsigned int height)
  301. {
  302. unsigned int x, y;
  303. switch(ch)
  304. {
  305. case 0x00002580: /* ▀ */
  306. for(y = 0; y < height; y++)
  307. for(x = 0; x < width; x++)
  308. i->buffer[x + y * i->pitch] = y < height / 2 ? 0xff : 0x00;
  309. if(height & 1)
  310. for(x = 0; x < width; x++)
  311. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  312. break;
  313. case 0x00002584: /* ▄ */
  314. for(y = 0; y < height; y++)
  315. for(x = 0; x < width; x++)
  316. i->buffer[x + y * i->pitch] = y < height / 2 ? 0x00 : 0xff;
  317. if(height & 1)
  318. for(x = 0; x < width; x++)
  319. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  320. break;
  321. case 0x0000258c: /* ▌ */
  322. for(y = 0; y < height; y++)
  323. for(x = 0; x < width; x++)
  324. i->buffer[x + y * i->pitch] = x < width / 2 ? 0xff : 0x00;
  325. if(width & 1)
  326. for(y = 0; y < height; y++)
  327. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  328. break;
  329. case 0x00002590: /* ▐ */
  330. for(y = 0; y < height; y++)
  331. for(x = 0; x < width; x++)
  332. i->buffer[x + y * i->pitch] = x < width / 2 ? 0x00 : 0xff;
  333. if(width & 1)
  334. for(y = 0; y < height; y++)
  335. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  336. break;
  337. case 0x000025a0: /* ■ */
  338. for(y = 0; y < height; y++)
  339. for(x = 0; x < width; x++)
  340. i->buffer[x + y * i->pitch] =
  341. (y >= height / 4) && (y < 3 * height / 4) ? 0xff : 0x00;
  342. if(height & 3)
  343. for(x = 0; x < width; x++) /* FIXME: could be more precise */
  344. i->buffer[x + (height / 4) * i->pitch] =
  345. i->buffer[x + (3 * height / 4) * i->pitch] = 0x7f;
  346. break;
  347. case 0x00002588: /* █ */
  348. memset(i->buffer, 0xff, height * i->pitch);
  349. break;
  350. case 0x00002593: /* ▓ */
  351. for(y = 0; y < height; y++)
  352. for(x = 0; x < width; x++)
  353. i->buffer[x + y * i->pitch] =
  354. ((x + 2 * (y & 1)) & 3) ? 0xff : 0x00;
  355. break;
  356. case 0x00002592: /* ▒ */
  357. for(y = 0; y < height; y++)
  358. for(x = 0; x < width; x++)
  359. i->buffer[x + y * i->pitch] = ((x + y) & 1) ? 0xff : 0x00;
  360. break;
  361. case 0x00002591: /* ░ */
  362. for(y = 0; y < height; y++)
  363. for(x = 0; x < width; x++)
  364. i->buffer[x + y * i->pitch] =
  365. ((x + 2 * (y & 1)) & 3) ? 0x00 : 0xff;
  366. break;
  367. }
  368. }
  369. static int printf_unicode(struct glyph *g)
  370. {
  371. int written = 0;
  372. written += printf("U+%.04X: \"", g->unicode);
  373. if(g->unicode < 0x20 || (g->unicode >= 0x7f && g->unicode <= 0xa0))
  374. written += printf("\\x%.02x\"", g->unicode);
  375. else
  376. written += printf("%s\"", g->buf);
  377. return written;
  378. }
  379. static int printf_u32(char const *fmt, uint32_t i)
  380. {
  381. uint32_t ni = hton32(i);
  382. return printf_hex(fmt, (uint8_t *)&ni, 4);
  383. }
  384. static int printf_u16(char const *fmt, uint16_t i)
  385. {
  386. uint16_t ni = hton16(i);
  387. return printf_hex(fmt, (uint8_t *)&ni, 2);
  388. }
  389. static int printf_hex(char const *fmt, uint8_t *data, int bytes)
  390. {
  391. char buf[BUFSIZ];
  392. char *parser = buf;
  393. int rewind = 0; /* we use this variable to rewind 2 bytes after \000
  394. * was printed when the next char starts with "\", too. */
  395. while(bytes--)
  396. {
  397. uint8_t ch = *data++;
  398. if(written == STRING_CHUNKS)
  399. {
  400. parser += sprintf(parser, "\", \"");
  401. written = 0;
  402. rewind = 0;
  403. }
  404. if(ch == '\\' || ch == '"')
  405. {
  406. parser -= rewind;
  407. parser += sprintf(parser, "\\%c", ch);
  408. rewind = 0;
  409. }
  410. else if(ch >= 0x20 && ch < 0x7f)
  411. {
  412. parser += sprintf(parser, "%c", ch);
  413. rewind = 0;
  414. }
  415. else
  416. {
  417. parser -= rewind;
  418. parser += sprintf(parser, "\\%.03o", ch);
  419. rewind = ch ? 0 : 2;
  420. }
  421. written++;
  422. }
  423. parser -= rewind;
  424. parser[0] = '\0';
  425. return printf(fmt, buf);
  426. }