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.

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