You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

523 regels
16 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. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains font handling functions.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # if defined(HAVE_ENDIAN_H)
  20. # include <endian.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. /* Internal fonts */
  29. #include "mono9.data"
  30. #include "monobold12.data"
  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, maxwidth, maxheight, 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. uint32_t *user_block_list;
  54. struct glyph_info *glyph_list;
  55. uint8_t *font_data;
  56. uint8_t *private;
  57. };
  58. #endif
  59. #define DECLARE_UNPACKGLYPH(bpp) \
  60. static inline void \
  61. unpack_glyph ## bpp(uint8_t *glyph, uint8_t *packed_data, int n) \
  62. { \
  63. 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 cucul_render_canvas()
  81. * for bitmap output.
  82. *
  83. * Internal fonts can also be loaded: if \c size is set to 0, \c data must
  84. * be a string containing the internal font name.
  85. *
  86. * If \c size is non-zero, the \c size bytes of memory at address \c 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 cucul_free_font().
  89. *
  90. * If an error occurs, NULL is returned and \b errno is set accordingly:
  91. * - \c ENOENT Requested built-in font does not exist.
  92. * - \c EINVAL Invalid font data in memory area.
  93. * - \c ENOMEM Not enough memory to allocate font structure.
  94. *
  95. * \param data The memory area containing the font or its name.
  96. * \param size The size of the memory area, or 0 if the font name is given.
  97. * \return A font handle or NULL in case of error.
  98. */
  99. cucul_font_t *cucul_load_font(void const *data, size_t size)
  100. {
  101. cucul_font_t *f;
  102. int i;
  103. if(size == 0)
  104. {
  105. if(!strcasecmp(data, "Monospace 9"))
  106. return cucul_load_font((char *)&mono9_data, mono9_size);
  107. if(!strcasecmp(data, "Monospace Bold 12"))
  108. return cucul_load_font((char *)&monobold12_data, monobold12_size);
  109. seterrno(ENOENT);
  110. return NULL;
  111. }
  112. if(size < sizeof(struct font_header))
  113. {
  114. debug("font error: data size %i < header size %i",
  115. size, (int)sizeof(struct font_header));
  116. seterrno(EINVAL);
  117. return NULL;
  118. }
  119. f = malloc(sizeof(cucul_font_t));
  120. if(!f)
  121. {
  122. seterrno(ENOMEM);
  123. return NULL;
  124. }
  125. f->private = (void *)(uintptr_t)data;
  126. memcpy(&f->header, f->private + 4, sizeof(struct font_header));
  127. f->header.control_size = hton32(f->header.control_size);
  128. f->header.data_size = hton32(f->header.data_size);
  129. f->header.version = hton16(f->header.version);
  130. f->header.blocks = hton16(f->header.blocks);
  131. f->header.glyphs = hton32(f->header.glyphs);
  132. f->header.bpp = hton16(f->header.bpp);
  133. f->header.width = hton16(f->header.width);
  134. f->header.height = hton16(f->header.height);
  135. f->header.maxwidth = hton16(f->header.maxwidth);
  136. f->header.maxheight = hton16(f->header.maxheight);
  137. f->header.flags = hton16(f->header.flags);
  138. if(size != 4 + f->header.control_size + f->header.data_size
  139. || (f->header.bpp != 8 && f->header.bpp != 4 &&
  140. f->header.bpp != 2 && f->header.bpp != 1)
  141. || (f->header.flags & 1) == 0)
  142. {
  143. #if defined DEBUG
  144. if(size != 4 + f->header.control_size + f->header.data_size)
  145. debug("font error: data size %i < expected size %i",
  146. size, 4 + f->header.control_size + f->header.data_size);
  147. else if(f->header.bpp != 8 && f->header.bpp != 4 &&
  148. f->header.bpp != 2 && f->header.bpp != 1)
  149. debug("font error: invalid bpp %i", f->header.bpp);
  150. else if((f->header.flags & 1) == 0)
  151. debug("font error: invalid flags %.04x", f->header.flags);
  152. #endif
  153. free(f);
  154. seterrno(EINVAL);
  155. return NULL;
  156. }
  157. f->block_list = malloc(f->header.blocks * sizeof(struct block_info));
  158. if(!f->block_list)
  159. {
  160. free(f);
  161. seterrno(ENOMEM);
  162. return NULL;
  163. }
  164. f->user_block_list = malloc((f->header.blocks + 1)
  165. * 2 * sizeof(uint32_t));
  166. if(!f->user_block_list)
  167. {
  168. free(f->block_list);
  169. free(f);
  170. seterrno(ENOMEM);
  171. return NULL;
  172. }
  173. memcpy(f->block_list,
  174. f->private + 4 + sizeof(struct font_header),
  175. f->header.blocks * sizeof(struct block_info));
  176. for(i = 0; i < f->header.blocks; i++)
  177. {
  178. f->block_list[i].start = hton32(f->block_list[i].start);
  179. f->block_list[i].stop = hton32(f->block_list[i].stop);
  180. f->block_list[i].index = hton32(f->block_list[i].index);
  181. if(f->block_list[i].start > f->block_list[i].stop
  182. || (i > 0 && f->block_list[i].start < f->block_list[i - 1].stop)
  183. || f->block_list[i].index >= f->header.glyphs)
  184. {
  185. #if defined DEBUG
  186. if(f->block_list[i].start > f->block_list[i].stop)
  187. debug("font error: block %i has start %i > stop %i",
  188. i, f->block_list[i].start, f->block_list[i].stop);
  189. else if(i > 0 && f->block_list[i].start < f->block_list[i - 1].stop)
  190. debug("font error: block %i has start %i < previous stop %i",
  191. f->block_list[i].start, f->block_list[i - 1].stop);
  192. else if(f->block_list[i].index >= f->header.glyphs)
  193. debug("font error: block %i has index >= glyph count %i",
  194. f->block_list[i].index, f->header.glyphs);
  195. #endif
  196. free(f->user_block_list);
  197. free(f->block_list);
  198. free(f);
  199. seterrno(EINVAL);
  200. return NULL;
  201. }
  202. f->user_block_list[i * 2] = f->block_list[i].start;
  203. f->user_block_list[i * 2 + 1] = f->block_list[i].stop;
  204. }
  205. f->user_block_list[i * 2] = 0;
  206. f->user_block_list[i * 2 + 1] = 0;
  207. f->glyph_list = malloc(f->header.glyphs * sizeof(struct glyph_info));
  208. if(!f->glyph_list)
  209. {
  210. free(f->user_block_list);
  211. free(f->block_list);
  212. free(f);
  213. seterrno(ENOMEM);
  214. return NULL;
  215. }
  216. memcpy(f->glyph_list,
  217. f->private + 4 + sizeof(struct font_header)
  218. + f->header.blocks * sizeof(struct block_info),
  219. f->header.glyphs * sizeof(struct glyph_info));
  220. for(i = 0; i < (int)f->header.glyphs; i++)
  221. {
  222. f->glyph_list[i].width = hton16(f->glyph_list[i].width);
  223. f->glyph_list[i].height = hton16(f->glyph_list[i].height);
  224. f->glyph_list[i].data_offset = hton32(f->glyph_list[i].data_offset);
  225. if(f->glyph_list[i].data_offset >= f->header.data_size
  226. || f->glyph_list[i].data_offset
  227. + (f->glyph_list[i].width * f->glyph_list[i].height *
  228. f->header.bpp + 7) / 8 > f->header.data_size
  229. || f->glyph_list[i].width > f->header.maxwidth
  230. || f->glyph_list[i].height > f->header.maxheight)
  231. {
  232. #if defined DEBUG
  233. if(f->glyph_list[i].data_offset >= f->header.data_size)
  234. debug("font error: glyph %i has data start %i > "
  235. "data end %i",
  236. f->glyph_list[i].data_offset, f->header.data_size);
  237. else if(f->glyph_list[i].data_offset
  238. + (f->glyph_list[i].width * f->glyph_list[i].height *
  239. f->header.bpp + 7) / 8 > f->header.data_size)
  240. debug("font error: glyph %i has data end %i > "
  241. "data end %i", f->glyph_list[i].data_offset
  242. + (f->glyph_list[i].width * f->glyph_list[i].height *
  243. f->header.bpp + 7) / 8, f->header.data_size);
  244. else if(f->glyph_list[i].width > f->header.maxwidth)
  245. debug("font error: glyph %i has width %i > max width %i",
  246. f->glyph_list[i].width, f->header.maxwidth);
  247. else if(f->glyph_list[i].height > f->header.maxheight)
  248. debug("font error: glyph %i has height %i > max height %i",
  249. f->glyph_list[i].height, f->header.maxheight);
  250. #endif
  251. free(f->glyph_list);
  252. free(f->user_block_list);
  253. free(f->block_list);
  254. free(f);
  255. seterrno(EINVAL);
  256. return NULL;
  257. }
  258. }
  259. f->font_data = f->private + 4 + f->header.control_size;
  260. return f;
  261. }
  262. /** \brief Get available builtin fonts
  263. *
  264. * Return a list of available builtin fonts. The list is a NULL-terminated
  265. * array of strings.
  266. *
  267. * This function never fails.
  268. *
  269. * \return An array of strings.
  270. */
  271. char const * const * cucul_get_font_list(void)
  272. {
  273. static char const * const list[] =
  274. {
  275. "Monospace 9",
  276. "Monospace Bold 12",
  277. NULL
  278. };
  279. return list;
  280. }
  281. /** \brief Get a font's standard glyph width.
  282. *
  283. * Return the standard value for the current font's glyphs. Most glyphs in
  284. * the font will have this width, except fullwidth characters.
  285. *
  286. * This function never fails.
  287. *
  288. * \param f The font, as returned by cucul_load_font()
  289. * \return The standard glyph width.
  290. */
  291. int cucul_get_font_width(cucul_font_t const *f)
  292. {
  293. return f->header.width;
  294. }
  295. /** \brief Get a font's standard glyph height.
  296. *
  297. * Returns the standard value for the current font's glyphs. Most glyphs in
  298. * the font will have this height.
  299. *
  300. * This function never fails.
  301. *
  302. * \param f The font, as returned by cucul_load_font()
  303. * \return The standard glyph height.
  304. */
  305. int cucul_get_font_height(cucul_font_t const *f)
  306. {
  307. return f->header.height;
  308. }
  309. /** \brief Get a font's list of supported glyphs.
  310. *
  311. * This function returns the list of Unicode blocks supported by the
  312. * given font. The list is a zero-terminated list of indices. Here is
  313. * an example:
  314. *
  315. * \code
  316. * {
  317. * 0x0000, 0x0080, // Basic latin: A, B, C, a, b, c
  318. * 0x0080, 0x0100, // Latin-1 supplement: "A, 'e, ^u
  319. * 0x0530, 0x0590, // Armenian
  320. * 0x0000, 0x0000, // END
  321. * };
  322. * \endcode
  323. *
  324. * This function never fails.
  325. *
  326. * \param f The font, as returned by cucul_load_font()
  327. * \return The list of Unicode blocks supported by the font.
  328. */
  329. uint32_t const *cucul_get_font_blocks(cucul_font_t const *f)
  330. {
  331. return (uint32_t const *)f->user_block_list;
  332. }
  333. /** \brief Free a font structure.
  334. *
  335. * This function frees all data allocated by cucul_load_font(). The
  336. * font structure is no longer usable by other libcucul functions. Once
  337. * this function has returned, the memory area that was given to
  338. * cucul_load_font() can be freed.
  339. *
  340. * This function never fails.
  341. *
  342. * \param f The font, as returned by cucul_load_font()
  343. * \return This function always returns 0.
  344. */
  345. int cucul_free_font(cucul_font_t *f)
  346. {
  347. free(f->glyph_list);
  348. free(f->user_block_list);
  349. free(f->block_list);
  350. free(f);
  351. return 0;
  352. }
  353. /** \brief Render the canvas onto an image buffer.
  354. *
  355. * This function renders the given canvas on an image buffer using a specific
  356. * font. The pixel format is fixed (32-bit ARGB, 8 bits for each component).
  357. *
  358. * The required image width can be computed using
  359. * cucul_get_canvas_width() and cucul_get_font_width(). The required
  360. * height can be computed using cucul_get_canvas_height() and
  361. * cucul_get_font_height().
  362. *
  363. * Glyphs that do not fit in the image buffer are currently not rendered at
  364. * all. They may be cropped instead in future versions.
  365. *
  366. * If an error occurs, -1 is returned and \b errno is set accordingly:
  367. * - \c EINVAL Specified width, height or pitch is invalid.
  368. *
  369. * \param cv The canvas to render
  370. * \param f The font, as returned by cucul_load_font()
  371. * \param buf The image buffer
  372. * \param width The width (in pixels) of the image buffer
  373. * \param height The height (in pixels) of the image buffer
  374. * \param pitch The pitch (in bytes) of an image buffer line.
  375. * \return 0 in case of success, -1 if an error occurred.
  376. */
  377. int cucul_render_canvas(cucul_canvas_t const *cv, cucul_font_t const *f,
  378. void *buf, int width, int height, int pitch)
  379. {
  380. uint8_t *glyph = NULL;
  381. int x, y, xmax, ymax;
  382. if(width < 0 || height < 0 || pitch < 0)
  383. {
  384. seterrno(EINVAL);
  385. return -1;
  386. }
  387. if(f->header.bpp != 8)
  388. glyph = malloc(f->header.width * 2 * f->header.height);
  389. if(width < cv->width * f->header.width)
  390. xmax = width / f->header.width;
  391. else
  392. xmax = cv->width;
  393. if(height < cv->height * f->header.height)
  394. ymax = height / f->header.height;
  395. else
  396. ymax = cv->height;
  397. for(y = 0; y < ymax; y++)
  398. {
  399. for(x = 0; x < xmax; x++)
  400. {
  401. uint8_t argb[8];
  402. int starty = y * f->header.height;
  403. int startx = x * f->header.width;
  404. uint32_t ch = cv->chars[y * cv->width + x];
  405. uint32_t attr = cv->attrs[y * cv->width + x];
  406. int b, i, j;
  407. struct glyph_info *g;
  408. /* Find the Unicode block where our glyph lies */
  409. for(b = 0; b < f->header.blocks; b++)
  410. {
  411. if(ch < f->block_list[b].start)
  412. {
  413. b = f->header.blocks;
  414. break;
  415. }
  416. if(ch < f->block_list[b].stop)
  417. break;
  418. }
  419. /* Glyph not in font? Skip it. */
  420. if(b == f->header.blocks)
  421. continue;
  422. g = &f->glyph_list[f->block_list[b].index
  423. + ch - f->block_list[b].start];
  424. cucul_attr_to_argb64(attr, argb);
  425. /* Step 1: unpack glyph */
  426. switch(f->header.bpp)
  427. {
  428. case 8:
  429. glyph = f->font_data + g->data_offset;
  430. break;
  431. case 4:
  432. unpack_glyph4(glyph, f->font_data + g->data_offset,
  433. g->width * g->height);
  434. break;
  435. case 2:
  436. unpack_glyph2(glyph, f->font_data + g->data_offset,
  437. g->width * g->height);
  438. break;
  439. case 1:
  440. unpack_glyph1(glyph, f->font_data + g->data_offset,
  441. g->width * g->height);
  442. break;
  443. }
  444. /* Step 2: render glyph using colour attribute */
  445. for(j = 0; j < g->height; j++)
  446. {
  447. uint8_t *line = buf;
  448. line += (starty + j) * pitch + 4 * startx;
  449. for(i = 0; i < g->width; i++)
  450. {
  451. uint8_t *pixel = line + 4 * i;
  452. uint32_t p, q, t;
  453. p = glyph[j * g->width + i];
  454. q = 0xff - p;
  455. for(t = 0; t < 4; t++)
  456. pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
  457. }
  458. }
  459. }
  460. }
  461. if(f->header.bpp != 8)
  462. free(glyph);
  463. return 0;
  464. }