25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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