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

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