Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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