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.
 
 
 
 
 
 

318 righe
8.8 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. #endif
  24. #include <pango/pango.h>
  25. #include <pango/pangoft2.h>
  26. static int const blocklist[] =
  27. {
  28. 0x0000, 0x0080, /* Basic latin: A, B, C, a, img, c */
  29. 0x0080, 0x0100, /* Latin-1 Supplement: Ä, Ç, å, ß */
  30. 0x0100, 0x0180, /* Latin Extended-A: Ā č Ō œ */
  31. 0x0180, 0x0250, /* Latin Extended-B: Ǝ Ƹ */
  32. 0x0250, 0x02b0, /* IPA Extensions: ɐ ɔ ɘ ʌ ʍ */
  33. 0x0370, 0x0400, /* Greek and Coptic: Λ α β */
  34. 0x0400, 0x0500, /* Cyrillic: И Я */
  35. 0x2000, 0x2070, /* General Punctuation: ‘’ “” */
  36. #if 0
  37. 0x2100, 0x2150, /* Letterlike Symbols: Ⅎ */
  38. #endif
  39. 0x2300, 0x2400, /* Miscellaneous Technical: ⌂ */
  40. 0x2500, 0x2580, /* Box Drawing: ═ ║ ╗ ╔ ╩ */
  41. 0x2580, 0x25a0, /* Block Elements: ▛ ▞ ░ ▒ ▓ */
  42. 0, 0
  43. };
  44. static int printf_hex(char const *, uint8_t *, int);
  45. static int printf_u32(char const *, uint32_t);
  46. static int printf_u16(char const *, uint16_t);
  47. int main(int argc, char *argv[])
  48. {
  49. PangoContext *cx;
  50. PangoFontDescription *fd;
  51. PangoFontMap *fm;
  52. PangoLayout *l;
  53. PangoRectangle r;
  54. FT_Bitmap img;
  55. int width, height, b, i, n, blocks, glyphs;
  56. unsigned int glyph_size, control_size, data_size;
  57. uint8_t *glyph_data;
  58. unsigned int bpp, dpi;
  59. char const *prefix, *font;
  60. if(argc != 5)
  61. {
  62. fprintf(stderr, "%s: wrong argument count\n", argv[0]);
  63. fprintf(stderr, "usage: %s <prefix> <font> <dpi> <bpp>\n", argv[0]);
  64. fprintf(stderr, "eg: %s monospace9 \"Monospace 9\" 96 4\n", argv[0]);
  65. return -1;
  66. }
  67. prefix = argv[1];
  68. font = argv[2];
  69. dpi = atoi(argv[3]);
  70. bpp = atoi(argv[4]);
  71. if(dpi == 0 || (bpp != 1 && bpp != 2 && bpp != 4 && bpp != 8))
  72. {
  73. fprintf(stderr, "%s: invalid argument\n", argv[0]);
  74. return -1;
  75. }
  76. fprintf(stderr, "Font \"%s\", %i dpi, %i bpp\n", font, dpi, bpp);
  77. /* Initialise Pango */
  78. fm = pango_ft2_font_map_new();
  79. pango_ft2_font_map_set_resolution(PANGO_FT2_FONT_MAP(fm), dpi, dpi);
  80. cx = pango_ft2_font_map_create_context(PANGO_FT2_FONT_MAP(fm));
  81. l = pango_layout_new(cx);
  82. if(!l)
  83. {
  84. fprintf(stderr, "%s: unable to initialise pango\n", argv[0]);
  85. g_object_unref(cx);
  86. return -1;
  87. }
  88. fd = pango_font_description_from_string(font);
  89. pango_layout_set_font_description(l, fd);
  90. pango_font_description_free(fd);
  91. /* Initialise our FreeType2 bitmap */
  92. img.width = 256;
  93. img.pitch = 256;
  94. img.rows = 256;
  95. img.buffer = malloc(256 * 256);
  96. img.num_grays = 256;
  97. img.pixel_mode = ft_pixel_mode_grays;
  98. /* Test rendering so that we know the glyph width */
  99. pango_layout_set_markup(l, "@", -1);
  100. pango_layout_get_extents(l, NULL, &r);
  101. width = PANGO_PIXELS(r.width);
  102. height = PANGO_PIXELS(r.height);
  103. glyph_size = ((width * height) + (8 / bpp) - 1) / (8 / bpp);
  104. glyph_data = malloc(glyph_size);
  105. /* Compute blocks and glyphs count */
  106. blocks = 0;
  107. glyphs = 0;
  108. for(b = 0; blocklist[b + 1]; b += 2)
  109. {
  110. blocks++;
  111. glyphs += blocklist[b + 1] - blocklist[b];
  112. }
  113. control_size = 24 + 12 * blocks + 8 * glyphs;
  114. data_size = glyph_size * glyphs;
  115. /* Let's go! */
  116. printf("/* libcucul font file\n");
  117. printf(" * \"%s\": %i dpi, %i bpp, %ix%i glyphs\n",
  118. font, dpi, bpp, width, height);
  119. printf(" * Automatically generated by tools/makefont.c:\n");
  120. printf(" * tools/makefont %s \"%s\" %i %i\n", prefix, font, dpi, bpp);
  121. printf(" */\n");
  122. printf("\n");
  123. printf("static unsigned int const %s_size = %i;\n",
  124. prefix, 8 + control_size + data_size);
  125. printf("static unsigned char const %s_data[] =\n", prefix);
  126. printf("/* file: */\n");
  127. printf("\"CACA\" /* caca_header */\n");
  128. printf("\"FONT\" /* caca_file_type */\n");
  129. printf("\n");
  130. printf("/* font_header: */\n");
  131. printf_u32("\"%s\" /* control_size */\n", control_size);
  132. printf_u32("\"%s\" /* data_size */\n", data_size);
  133. printf_u16("\"%s\" /* version */\n", 1);
  134. printf_u16("\"%s\" /* blocks */\n", blocks);
  135. printf_u32("\"%s\" /* glyphs */\n", glyphs);
  136. printf_u16("\"%s\" /* bpp */\n", bpp);
  137. printf_u16("\"%s\" /* width */\n", width);
  138. printf_u16("\"%s\" /* height */\n", height);
  139. printf_u16("\"%s\" /* flags */\n", 1);
  140. printf("\n");
  141. printf("/* block_info: */\n");
  142. n = 0;
  143. for(b = 0; blocklist[b + 1]; b += 2)
  144. {
  145. printf_u32("\"%s", blocklist[b]);
  146. printf_u32("%s", blocklist[b + 1]);
  147. printf_u32("%s\"\n", n);
  148. n += blocklist[b + 1] - blocklist[b];
  149. }
  150. printf("\n");
  151. printf("/* glyph_info: */\n");
  152. n = 0;
  153. for(b = 0; blocklist[b + 1]; b += 2)
  154. {
  155. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  156. {
  157. printf_u16("\"%s", width);
  158. printf_u16("%s", height);
  159. printf_u32("%s\"\n", n * glyph_size);
  160. n++;
  161. }
  162. }
  163. printf("\n");
  164. printf("/* font_data: */\n");
  165. for(b = 0; blocklist[b + 1]; b += 2)
  166. {
  167. for(i = blocklist[b]; i < blocklist[b + 1]; i++)
  168. {
  169. unsigned int ch = i;
  170. char buf[10], *parser;
  171. int x, y, bytes;
  172. if(ch < 0x80)
  173. {
  174. bytes = 1;
  175. buf[0] = ch;
  176. buf[1] = '\0';
  177. }
  178. else
  179. {
  180. static const unsigned char mark[7] =
  181. {
  182. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  183. };
  184. /* FIXME: use libcucul instead of this shit */
  185. bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
  186. buf[bytes] = '\0';
  187. parser = buf + bytes;
  188. switch(bytes)
  189. {
  190. case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  191. case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  192. case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  193. }
  194. *--parser = ch | mark[bytes];
  195. }
  196. /* Print glyph value in comment */
  197. printf("/* U+%.04X: \"", i);
  198. if(i < 0x20 || (i >= 0x80 && i <= 0xa0))
  199. printf("\\x%.02x\" */", i);
  200. else
  201. printf("%s\" */ ", buf);
  202. /* Render glyph on a bitmap */
  203. pango_layout_set_text(l, buf, -1);
  204. memset(glyph_data, 0, glyph_size);
  205. memset(img.buffer, 0, img.pitch * height);
  206. pango_ft2_render_layout(&img, l, 0, 0);
  207. /* Write bitmap as an escaped C string */
  208. n = 0;
  209. for(y = 0; y < height; y++)
  210. {
  211. for(x = 0; x < width; x++)
  212. {
  213. uint8_t pixel = img.buffer[y * img.pitch + x];
  214. pixel >>= (8 - bpp);
  215. glyph_data[n / 8] |= pixel << (8 - bpp - (n % 8));
  216. n += bpp;
  217. }
  218. }
  219. printf_hex("\"%s\"\n", glyph_data, glyph_size);
  220. }
  221. }
  222. printf(";\n");
  223. free(img.buffer);
  224. g_object_unref(l);
  225. g_object_unref(cx);
  226. return 0;
  227. }
  228. /*
  229. * XXX: the following functions are local
  230. */
  231. static int printf_u32(char const *fmt, uint32_t i)
  232. {
  233. uint32_t ni = htonl(i);
  234. return printf_hex(fmt, (uint8_t *)&ni, 4);
  235. }
  236. static int printf_u16(char const *fmt, uint16_t i)
  237. {
  238. uint16_t ni = htons(i);
  239. return printf_hex(fmt, (uint8_t *)&ni, 2);
  240. }
  241. static int printf_hex(char const *fmt, uint8_t *data, int bytes)
  242. {
  243. char buf[BUFSIZ];
  244. char *parser = buf;
  245. int rewind = 0; /* we use this variable to rewind 2 bytes after \000
  246. * was printed when the next char starts with "\", too. */
  247. while(bytes--)
  248. {
  249. uint8_t ch = *data++;
  250. if(ch == '\\' || ch == '"')
  251. {
  252. parser -= rewind;
  253. parser += sprintf(parser, "\\%c", ch);
  254. rewind = 0;
  255. }
  256. else if(ch >= 0x20 && ch < 0x7f)
  257. {
  258. parser += sprintf(parser, "%c", ch);
  259. rewind = 0;
  260. }
  261. else
  262. {
  263. parser -= rewind;
  264. parser += sprintf(parser, "\\%.03o", ch);
  265. rewind = ch ? 0 : 2;
  266. }
  267. }
  268. parser -= rewind;
  269. parser[0] = '\0';
  270. return printf(fmt, buf);
  271. }