Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

494 рядки
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. 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 "cucul.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(cucul_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("/* libcucul 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 struct {\n");
  164. printf("char ");
  165. for(i = 0; (i + 1) * STRING_CHUNKS < 8 + control_size + data_size; i++)
  166. printf("d%x[%i],%c", i, STRING_CHUNKS, (i % 6) == 5 ? '\n' : ' ');
  167. printf("d%x[%i];\n", i, 4 + control_size + data_size - i * STRING_CHUNKS);
  168. printf("} %s_data = {\n", prefix);
  169. printf("\n");
  170. printf("/* file: */\n");
  171. printf("\"\\xCA\\xCA\" /* caca_header */\n");
  172. written += 2;
  173. printf("\"FT\" /* caca_file_type */\n");
  174. written += 2;
  175. printf("\n");
  176. printf("/* font_header: */\n");
  177. printf_u32("\"%s\" /* control_size */\n", control_size);
  178. printf_u32("\"%s\" /* data_size */\n", data_size);
  179. printf_u16("\"%s\" /* version */\n", 1);
  180. printf_u16("\"%s\" /* blocks */\n", blocks);
  181. printf_u32("\"%s\" /* glyphs */\n", glyphs);
  182. printf_u16("\"%s\" /* bpp */\n", bpp);
  183. printf_u16("\"%s\" /* std width */\n", stdwidth);
  184. printf_u16("\"%s\" /* std height */\n", height);
  185. printf_u16("\"%s\" /* max width */\n", fullwidth);
  186. printf_u16("\"%s\" /* max height */\n", height);
  187. printf_u16("\"%s\" /* flags */\n", 1);
  188. printf("\n");
  189. printf("/* block_info: */\n");
  190. n = 0;
  191. for(b = 0; blocklist[b + 1]; b += 2)
  192. {
  193. printf_u32("\"%s", blocklist[b]);
  194. printf_u32("%s", blocklist[b + 1]);
  195. printf_u32("%s\"\n", n);
  196. n += blocklist[b + 1] - blocklist[b];
  197. }
  198. printf("\n");
  199. /* Render all glyphs, so that we can know their offset */
  200. current_offset = n = 0;
  201. for(b = 0; blocklist[b + 1]; b += 2)
  202. {
  203. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  204. {
  205. int x, y, bytes, current_width = stdwidth;
  206. unsigned int k, current_size = stdsize;
  207. if(cucul_utf32_is_fullwidth(i))
  208. {
  209. current_width = fullwidth;
  210. current_size = fullsize;
  211. }
  212. gtab[n].unicode = i;
  213. bytes = cucul_utf32_to_utf8(gtab[n].buf, gtab[n].unicode);
  214. gtab[n].buf[bytes] = '\0';
  215. /* Render glyph on a bitmap */
  216. pango_layout_set_text(l, gtab[n].buf, -1);
  217. memset(img.buffer, 0, img.pitch * height);
  218. pango_ft2_render_layout(&img, l, 0, 0);
  219. /* Fix glyphs that we know how to handle better */
  220. fix_glyph(&img, gtab[n].unicode, current_width, height);
  221. /* Write bitmap as an escaped C string */
  222. memset(glyph_data + current_offset, 0, current_size);
  223. k = 0;
  224. for(y = 0; y < height; y++)
  225. {
  226. for(x = 0; x < current_width; x++)
  227. {
  228. uint8_t pixel = img.buffer[y * img.pitch + x];
  229. pixel >>= (8 - bpp);
  230. glyph_data[current_offset + k / 8]
  231. |= pixel << (8 - bpp - (k % 8));
  232. k += bpp;
  233. }
  234. }
  235. /* Check whether this is the same glyph as another one. Please
  236. * don't bullshit me about sorting, hashing and stuff like that,
  237. * our data is small enough for this to work. */
  238. for(k = 0; k < n; k++)
  239. {
  240. if(gtab[k].data_size != current_size)
  241. continue;
  242. #if 0
  243. if(!memcmp(glyph_data + gtab[k].data_offset,
  244. glyph_data + current_offset, current_size))
  245. break;
  246. #endif
  247. }
  248. gtab[n].data_offset = current_offset;
  249. gtab[n].data_width = current_width;
  250. gtab[n].data_size = current_size;
  251. gtab[n].same_as = k;
  252. if(k == n)
  253. current_offset += current_size;
  254. n++;
  255. }
  256. }
  257. printf("/* glyph_info: */\n");
  258. n = 0;
  259. for(b = 0; blocklist[b + 1]; b += 2)
  260. {
  261. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  262. {
  263. printf_u16("\"%s", gtab[n].data_width);
  264. printf_u16("%s", height);
  265. printf_u32("%s\"\n", gtab[gtab[n].same_as].data_offset);
  266. n++;
  267. }
  268. }
  269. printf("\n");
  270. printf("/* font_data: */\n");
  271. n = 0;
  272. for(b = 0; blocklist[b + 1]; b += 2)
  273. {
  274. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  275. {
  276. /* Print glyph value in comment */
  277. printf("/* ");
  278. printf_unicode(&gtab[n]);
  279. if(gtab[n].same_as == n)
  280. printf_hex(" */ \"%s\"\n",
  281. glyph_data + gtab[n].data_offset, gtab[n].data_size);
  282. else
  283. {
  284. printf(" is ");
  285. printf_unicode(&gtab[gtab[n].same_as]);
  286. printf(" */\n");
  287. }
  288. n++;
  289. }
  290. }
  291. printf("};\n");
  292. free(img.buffer);
  293. free(gtab);
  294. free(glyph_data);
  295. g_object_unref(l);
  296. g_object_unref(cx);
  297. return 0;
  298. }
  299. /*
  300. * XXX: the following functions are local
  301. */
  302. static void fix_glyph(FT_Bitmap *i, uint32_t ch,
  303. unsigned int width, unsigned int height)
  304. {
  305. unsigned int x, y;
  306. switch(ch)
  307. {
  308. case 0x00002580: /* ▀ */
  309. for(y = 0; y < height; y++)
  310. for(x = 0; x < width; x++)
  311. i->buffer[x + y * i->pitch] = y < height / 2 ? 0xff : 0x00;
  312. if(height & 1)
  313. for(x = 0; x < width; x++)
  314. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  315. break;
  316. case 0x00002584: /* ▄ */
  317. for(y = 0; y < height; y++)
  318. for(x = 0; x < width; x++)
  319. i->buffer[x + y * i->pitch] = y < height / 2 ? 0x00 : 0xff;
  320. if(height & 1)
  321. for(x = 0; x < width; x++)
  322. i->buffer[x + (height / 2) * i->pitch] = 0x7f;
  323. break;
  324. case 0x0000258c: /* ▌ */
  325. for(y = 0; y < height; y++)
  326. for(x = 0; x < width; x++)
  327. i->buffer[x + y * i->pitch] = x < width / 2 ? 0xff : 0x00;
  328. if(width & 1)
  329. for(y = 0; y < height; y++)
  330. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  331. break;
  332. case 0x00002590: /* ▐ */
  333. for(y = 0; y < height; y++)
  334. for(x = 0; x < width; x++)
  335. i->buffer[x + y * i->pitch] = x < width / 2 ? 0x00 : 0xff;
  336. if(width & 1)
  337. for(y = 0; y < height; y++)
  338. i->buffer[(width / 2) + y * i->pitch] = 0x7f;
  339. break;
  340. case 0x000025a0: /* ■ */
  341. for(y = 0; y < height; y++)
  342. for(x = 0; x < width; x++)
  343. i->buffer[x + y * i->pitch] =
  344. (y >= height / 4) && (y < 3 * height / 4) ? 0xff : 0x00;
  345. if(height & 3)
  346. for(x = 0; x < width; x++) /* FIXME: could be more precise */
  347. i->buffer[x + (height / 4) * i->pitch] =
  348. i->buffer[x + (3 * height / 4) * i->pitch] = 0x7f;
  349. break;
  350. case 0x00002588: /* █ */
  351. memset(i->buffer, 0xff, height * i->pitch);
  352. break;
  353. case 0x00002593: /* ▓ */
  354. for(y = 0; y < height; y++)
  355. for(x = 0; x < width; x++)
  356. i->buffer[x + y * i->pitch] =
  357. ((x + 2 * (y & 1)) & 3) ? 0xff : 0x00;
  358. break;
  359. case 0x00002592: /* ▒ */
  360. for(y = 0; y < height; y++)
  361. for(x = 0; x < width; x++)
  362. i->buffer[x + y * i->pitch] = ((x + y) & 1) ? 0xff : 0x00;
  363. break;
  364. case 0x00002591: /* ░ */
  365. for(y = 0; y < height; y++)
  366. for(x = 0; x < width; x++)
  367. i->buffer[x + y * i->pitch] =
  368. ((x + 2 * (y & 1)) & 3) ? 0x00 : 0xff;
  369. break;
  370. }
  371. }
  372. static int printf_unicode(struct glyph *g)
  373. {
  374. int wr = 0;
  375. wr += printf("U+%.04X: \"", g->unicode);
  376. if(g->unicode < 0x20 || (g->unicode >= 0x7f && g->unicode <= 0xa0))
  377. wr += printf("\\x%.02x\"", g->unicode);
  378. else
  379. wr += printf("%s\"", g->buf);
  380. return wr;
  381. }
  382. static int printf_u32(char const *fmt, uint32_t i)
  383. {
  384. uint32_t ni = htonl(i);
  385. return printf_hex(fmt, (uint8_t *)&ni, 4);
  386. }
  387. static int printf_u16(char const *fmt, uint16_t i)
  388. {
  389. uint16_t ni = htons(i);
  390. return printf_hex(fmt, (uint8_t *)&ni, 2);
  391. }
  392. static int printf_hex(char const *fmt, uint8_t *data, int bytes)
  393. {
  394. char buf[BUFSIZ];
  395. char *parser = buf;
  396. int rev = 0; /* we use this variable to rewind 2 bytes after \000
  397. * was printed when the next char starts with "\", too. */
  398. while(bytes--)
  399. {
  400. uint8_t ch = *data++;
  401. if(written == STRING_CHUNKS)
  402. {
  403. parser += sprintf(parser, "\", \"");
  404. written = 0;
  405. rev = 0;
  406. }
  407. if(ch == '\\' || ch == '"')
  408. {
  409. parser -= rev;
  410. parser += sprintf(parser, "\\%c", ch);
  411. rev = 0;
  412. }
  413. else if(ch >= 0x20 && ch < 0x7f)
  414. {
  415. parser += sprintf(parser, "%c", ch);
  416. rev = 0;
  417. }
  418. else
  419. {
  420. parser -= rev;
  421. parser += sprintf(parser, "\\%.03o", ch);
  422. rev = ch ? 0 : 2;
  423. }
  424. written++;
  425. }
  426. parser -= rev;
  427. parser[0] = '\0';
  428. return printf(fmt, buf);
  429. }