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.

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