Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

435 linhas
13 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library 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. /*
  14. * This file contains font handling functions.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # if defined(HAVE_ENDIAN_H)
  19. # include <endian.h>
  20. # endif
  21. # include <stdio.h>
  22. # include <stdlib.h>
  23. # include <string.h>
  24. # include <arpa/inet.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. /* Internal fonts */
  29. #include "font_mono9.h"
  30. #include "font_monobold12.h"
  31. /* Helper structures for font loading */
  32. #if !defined(_DOXYGEN_SKIP_ME)
  33. struct font_header
  34. {
  35. uint32_t control_size, data_size;
  36. uint16_t version, blocks;
  37. uint32_t glyphs;
  38. uint16_t bpp, width, height, flags;
  39. };
  40. struct block_info
  41. {
  42. uint32_t start, stop, index;
  43. };
  44. struct glyph_info
  45. {
  46. uint16_t width, height;
  47. uint32_t data_offset;
  48. };
  49. struct cucul_font
  50. {
  51. struct font_header header;
  52. struct block_info *block_list;
  53. struct glyph_info *glyph_list;
  54. uint8_t *font_data;
  55. uint8_t *private;
  56. };
  57. #endif
  58. #define DECLARE_UNPACKGLYPH(bpp) \
  59. static inline void \
  60. unpack_glyph ## bpp(uint8_t *glyph, uint8_t *packed_data, \
  61. unsigned int n) \
  62. { \
  63. unsigned int i; \
  64. \
  65. for(i = 0; i < n; i++) \
  66. { \
  67. uint8_t pixel = packed_data[i / (8 / bpp)]; \
  68. pixel >>= bpp * ((8 / bpp) - 1 - (i % (8 / bpp))); \
  69. pixel %= (1 << bpp); \
  70. pixel *= 0xff / ((1 << bpp) - 1); \
  71. *glyph++ = pixel; \
  72. } \
  73. }
  74. DECLARE_UNPACKGLYPH(4)
  75. DECLARE_UNPACKGLYPH(2)
  76. DECLARE_UNPACKGLYPH(1)
  77. /** \brief Load a font from memory for future use.
  78. *
  79. * This function loads a font and returns a handle to its internal
  80. * structure. The handle can then be used with \e cucul_render_canvas()
  81. * for bitmap output.
  82. *
  83. * Internal fonts can also be loaded: if \e size is set to 0, \e data must
  84. * be a string containing the internal font name.
  85. *
  86. * If \e size is non-zero, the \e size bytes of memory at address \e data
  87. * are loaded as a font. This memory are must not be freed by the calling
  88. * program until the font handle has been freed with \e cucul_free_font().
  89. *
  90. * \param data The memory area containing the font or its name.
  91. * \param size The size of the memory area, or 0 if the font name is given.
  92. * \return A font handle or NULL in case of error.
  93. */
  94. struct cucul_font *cucul_load_font(void const *data, unsigned int size)
  95. {
  96. struct cucul_font *f;
  97. unsigned int i;
  98. if(size == 0)
  99. {
  100. if(!strcasecmp(data, "Monospace 9"))
  101. return cucul_load_font(mono9_data, mono9_size);
  102. if(!strcasecmp(data, "Monospace Bold 12"))
  103. return cucul_load_font(monobold12_data, monobold12_size);
  104. return NULL;
  105. }
  106. if(size < sizeof(struct font_header))
  107. return NULL;
  108. f = malloc(sizeof(struct cucul_font));
  109. f->private = (void *)(uintptr_t)data;
  110. memcpy(&f->header, f->private + 8, sizeof(struct font_header));
  111. f->header.control_size = htonl(f->header.control_size);
  112. f->header.data_size = htonl(f->header.data_size);
  113. f->header.version = htons(f->header.version);
  114. f->header.blocks = htons(f->header.blocks);
  115. f->header.glyphs = htonl(f->header.glyphs);
  116. f->header.bpp = htons(f->header.bpp);
  117. f->header.width = htons(f->header.width);
  118. f->header.height = htons(f->header.height);
  119. f->header.flags = htons(f->header.flags);
  120. if(size != 8 + f->header.control_size + f->header.data_size
  121. || (f->header.bpp != 8 && f->header.bpp != 4 &&
  122. f->header.bpp != 2 && f->header.bpp != 1)
  123. || (f->header.flags & 1) == 0)
  124. {
  125. free(f);
  126. return NULL;
  127. }
  128. f->block_list = malloc(f->header.blocks * sizeof(struct block_info));
  129. memcpy(f->block_list,
  130. f->private + 8 + sizeof(struct font_header),
  131. f->header.blocks * sizeof(struct block_info));
  132. for(i = 0; i < f->header.blocks; i++)
  133. {
  134. f->block_list[i].start = htonl(f->block_list[i].start);
  135. f->block_list[i].stop = htonl(f->block_list[i].stop);
  136. f->block_list[i].index = htonl(f->block_list[i].index);
  137. if(f->block_list[i].start > f->block_list[i].stop
  138. || (i > 0 && f->block_list[i].start < f->block_list[i - 1].stop)
  139. || f->block_list[i].index >= f->header.glyphs)
  140. {
  141. free(f->block_list);
  142. free(f);
  143. return NULL;
  144. }
  145. }
  146. f->glyph_list = malloc(f->header.glyphs * sizeof(struct glyph_info));
  147. memcpy(f->glyph_list,
  148. f->private + 8 + sizeof(struct font_header)
  149. + f->header.blocks * sizeof(struct block_info),
  150. f->header.glyphs * sizeof(struct glyph_info));
  151. for(i = 0; i < f->header.glyphs; i++)
  152. {
  153. f->glyph_list[i].width = htons(f->glyph_list[i].width);
  154. f->glyph_list[i].height = htons(f->glyph_list[i].height);
  155. f->glyph_list[i].data_offset = htonl(f->glyph_list[i].data_offset);
  156. if(f->glyph_list[i].data_offset >= f->header.data_size
  157. || f->glyph_list[i].data_offset
  158. + f->glyph_list[i].width * f->glyph_list[i].height *
  159. f->header.bpp / 8 >= f->header.data_size)
  160. {
  161. free(f->glyph_list);
  162. free(f->block_list);
  163. free(f);
  164. return NULL;
  165. }
  166. }
  167. f->font_data = f->private + 8 + f->header.control_size;
  168. return f;
  169. }
  170. /** \brief Get available builtin fonts
  171. *
  172. * Return a list of available builtin fonts. The list is a NULL-terminated
  173. * array of strings.
  174. *
  175. * \return An array of strings.
  176. */
  177. char const * const * cucul_get_font_list(void)
  178. {
  179. static char const * const list[] =
  180. {
  181. "Monospace 9",
  182. "Monospace Bold 12",
  183. NULL
  184. };
  185. return list;
  186. }
  187. /** \brief Get a font's maximum glyph width.
  188. *
  189. * This function returns the maximum value for the current font's glyphs
  190. *
  191. * \param f The font, as returned by \e cucul_load_font()
  192. * \return The maximum glyph width.
  193. */
  194. unsigned int cucul_get_font_width(struct cucul_font *f)
  195. {
  196. return f->header.width;
  197. }
  198. /** \brief Get a font's maximum glyph height.
  199. *
  200. * This function returns the maximum value for the current font's glyphs
  201. *
  202. * \param f The font, as returned by \e cucul_load_font()
  203. * \return The maximum glyph height.
  204. */
  205. unsigned int cucul_get_font_height(struct cucul_font *f)
  206. {
  207. return f->header.height;
  208. }
  209. /** \brief Free a font structure.
  210. *
  211. * This function frees all data allocated by \e cucul_load_font(). The
  212. * font structure is no longer usable by other libcucul functions. Once
  213. * this function has returned, the memory area that was given to
  214. * \e cucul_load_font() can be freed.
  215. *
  216. * \param f The font, as returned by \e cucul_load_font()
  217. */
  218. void cucul_free_font(struct cucul_font *f)
  219. {
  220. free(f->glyph_list);
  221. free(f->block_list);
  222. free(f);
  223. }
  224. /** \brief Render the canvas onto an image buffer.
  225. *
  226. * This function renders the given canvas on an image buffer using a specific
  227. * font. The pixel format is fixed (32-bit ARGB, 8 bits for each component).
  228. *
  229. * The required image width can be computed using \e cucul_get_width(qq) and
  230. * \e cucul_get_font_width(f). The required height can be computed using
  231. * \e cucul_get_height(qq) and \e cucul_get_font_height(f).
  232. *
  233. * Glyphs that do not fit in the image buffer are currently not rendered at
  234. * all. They may be cropped instead in future versions.
  235. *
  236. * \param qq The canvas to render
  237. * \param f The font, as returned by \e cucul_load_font()
  238. * \param buf The image buffer
  239. * \param width The width (in pixels) of the image buffer
  240. * \param height The height (in pixels) of the image buffer
  241. * \param pitch The pitch (in bytes) of an image buffer line.
  242. */
  243. void cucul_render_canvas(cucul_t *qq, struct cucul_font *f,
  244. unsigned char *buf, unsigned int width,
  245. unsigned int height, unsigned int pitch)
  246. {
  247. uint8_t *glyph = NULL;
  248. unsigned int x, y, xmax, ymax;
  249. if(f->header.bpp != 8)
  250. glyph = malloc(f->header.width * f->header.height);
  251. if(width < qq->width * f->header.width)
  252. xmax = width / f->header.width;
  253. else
  254. xmax = qq->width;
  255. if(height < qq->height * f->header.height)
  256. ymax = height / f->header.height;
  257. else
  258. ymax = qq->height;
  259. for(y = 0; y < ymax; y++)
  260. {
  261. for(x = 0; x < xmax; x++)
  262. {
  263. uint8_t argb[8];
  264. unsigned int starty = y * f->header.height;
  265. unsigned int startx = x * f->header.width;
  266. uint32_t ch = qq->chars[y * qq->width + x];
  267. uint32_t attr = qq->attr[y * qq->width + x];
  268. unsigned int b, i, j;
  269. struct glyph_info *g;
  270. /* Find the Unicode block where our glyph lies */
  271. for(b = 0; b < f->header.blocks; b++)
  272. {
  273. if(ch < f->block_list[b].start)
  274. {
  275. b = f->header.blocks;
  276. break;
  277. }
  278. if(ch < f->block_list[b].stop)
  279. break;
  280. }
  281. /* Glyph not in font? Skip it. */
  282. if(b == f->header.blocks)
  283. continue;
  284. g = &f->glyph_list[f->block_list[b].index
  285. + ch - f->block_list[b].start];
  286. _cucul_argb32_to_argb4(attr, argb);
  287. /* Step 1: unpack glyph */
  288. switch(f->header.bpp)
  289. {
  290. case 8:
  291. glyph = f->font_data + g->data_offset;
  292. break;
  293. case 4:
  294. unpack_glyph4(glyph, f->font_data + g->data_offset,
  295. g->width * g->height);
  296. break;
  297. case 2:
  298. unpack_glyph2(glyph, f->font_data + g->data_offset,
  299. g->width * g->height);
  300. break;
  301. case 1:
  302. unpack_glyph1(glyph, f->font_data + g->data_offset,
  303. g->width * g->height);
  304. break;
  305. }
  306. /* Step 2: render glyph using colour attribute */
  307. for(j = 0; j < g->height; j++)
  308. {
  309. uint8_t *line = buf + (starty + j) * pitch + 4 * startx;
  310. for(i = 0; i < g->width; i++)
  311. {
  312. uint8_t *pixel = line + 4 * i;
  313. uint32_t p, q, t;
  314. p = glyph[j * g->width + i];
  315. q = 0xff - p;
  316. for(t = 0; t < 4; t++)
  317. pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
  318. }
  319. }
  320. }
  321. }
  322. if(f->header.bpp != 8)
  323. free(glyph);
  324. }
  325. /*
  326. * The libcaca font format, version 1
  327. * ----------------------------------
  328. *
  329. * All types are big endian.
  330. *
  331. * struct
  332. * {
  333. * uint8_t caca_header[4]; // "CACA"
  334. * uint8_t caca_file_type[4]; // "FONT"
  335. *
  336. * font_header:
  337. * uint32_t control_size; // Control size (font_data - font_header)
  338. * uint32_t data_size; // Data size (EOF - font_data)
  339. *
  340. * uint16_t version; // Font format version
  341. * // bit 0: set to 1 if font is compatible
  342. * // with version 1 of the format
  343. * // bits 1-15: unused yet, must be 0
  344. *
  345. * uint16_t blocks; // Number of blocks in the font
  346. * uint32_t glyphs; // Total number of glyphs in the font
  347. *
  348. * uint16_t bpp; // Bits per pixel for glyph data (valid
  349. * // Values are 1, 2, 4 and 8)
  350. * uint16_t width; // Maximum glyph width
  351. * uint16_t height; // Maximum glyph height
  352. *
  353. * uint16_t flags; // Feature flags
  354. * // bit 0: set to 1 if font is fixed width
  355. * // bits 1-15: unused yet, must be 0
  356. *
  357. * block_info:
  358. * struct
  359. * {
  360. * uint32_t start; // Unicode index of the first glyph
  361. * uint32_t stop; // Unicode index of the last glyph + 1
  362. * uint32_t index; // Glyph info index of the first glyph
  363. * }
  364. * block_list[blocks];
  365. *
  366. * glyph_info:
  367. * struct
  368. * {
  369. * uint16_t width; // Glyph width in pixels
  370. * uint16_t height; // Glyph height in pixels
  371. * uint32_t data_offset; // Offset (starting from data) to the data
  372. * // for the first character
  373. * }
  374. * glyph_list[glyphs];
  375. *
  376. * extension_1:
  377. * extension_2:
  378. * ...
  379. * extension_N:
  380. * ... // reserved for future use
  381. *
  382. * font_data:
  383. * uint8_t data[data_size]; // glyph data
  384. * };
  385. */