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.
 
 
 
 
 
 

439 lines
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(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 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. * \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 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 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 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. * cucul_load_font() can be freed.
  216. *
  217. * \param f The font, as returned by 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
  231. * cucul_get_canvas_width() and cucul_get_font_width(). The required
  232. * height can be computed using cucul_get_canvas_height() and
  233. * cucul_get_font_height().
  234. *
  235. * Glyphs that do not fit in the image buffer are currently not rendered at
  236. * all. They may be cropped instead in future versions.
  237. *
  238. * \param cv The canvas to render
  239. * \param f The font, as returned by cucul_load_font()
  240. * \param buf The image buffer
  241. * \param width The width (in pixels) of the image buffer
  242. * \param height The height (in pixels) of the image buffer
  243. * \param pitch The pitch (in bytes) of an image buffer line.
  244. */
  245. void cucul_render_canvas(cucul_canvas_t *cv, cucul_font_t *f,
  246. void *buf, unsigned int width,
  247. unsigned int height, unsigned int pitch)
  248. {
  249. uint8_t *glyph = NULL;
  250. unsigned int x, y, xmax, ymax;
  251. if(f->header.bpp != 8)
  252. glyph = malloc(f->header.width * f->header.height);
  253. if(width < cv->width * f->header.width)
  254. xmax = width / f->header.width;
  255. else
  256. xmax = cv->width;
  257. if(height < cv->height * f->header.height)
  258. ymax = height / f->header.height;
  259. else
  260. ymax = cv->height;
  261. for(y = 0; y < ymax; y++)
  262. {
  263. for(x = 0; x < xmax; x++)
  264. {
  265. uint8_t argb[8];
  266. unsigned int starty = y * f->header.height;
  267. unsigned int startx = x * f->header.width;
  268. uint32_t ch = cv->chars[y * cv->width + x];
  269. uint32_t attr = cv->attr[y * cv->width + x];
  270. unsigned int b, i, j;
  271. struct glyph_info *g;
  272. /* Find the Unicode block where our glyph lies */
  273. for(b = 0; b < f->header.blocks; b++)
  274. {
  275. if(ch < f->block_list[b].start)
  276. {
  277. b = f->header.blocks;
  278. break;
  279. }
  280. if(ch < f->block_list[b].stop)
  281. break;
  282. }
  283. /* Glyph not in font? Skip it. */
  284. if(b == f->header.blocks)
  285. continue;
  286. g = &f->glyph_list[f->block_list[b].index
  287. + ch - f->block_list[b].start];
  288. _cucul_argb32_to_argb4(attr, argb);
  289. /* Step 1: unpack glyph */
  290. switch(f->header.bpp)
  291. {
  292. case 8:
  293. glyph = f->font_data + g->data_offset;
  294. break;
  295. case 4:
  296. unpack_glyph4(glyph, f->font_data + g->data_offset,
  297. g->width * g->height);
  298. break;
  299. case 2:
  300. unpack_glyph2(glyph, f->font_data + g->data_offset,
  301. g->width * g->height);
  302. break;
  303. case 1:
  304. unpack_glyph1(glyph, f->font_data + g->data_offset,
  305. g->width * g->height);
  306. break;
  307. }
  308. /* Step 2: render glyph using colour attribute */
  309. for(j = 0; j < g->height; j++)
  310. {
  311. uint8_t *line = buf;
  312. line += (starty + j) * pitch + 4 * startx;
  313. for(i = 0; i < g->width; i++)
  314. {
  315. uint8_t *pixel = line + 4 * i;
  316. uint32_t p, q, t;
  317. p = glyph[j * g->width + i];
  318. q = 0xff - p;
  319. for(t = 0; t < 4; t++)
  320. pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
  321. }
  322. }
  323. }
  324. }
  325. if(f->header.bpp != 8)
  326. free(glyph);
  327. }
  328. /*
  329. * The libcaca font format, version 1
  330. * ----------------------------------
  331. *
  332. * All types are big endian.
  333. *
  334. * struct
  335. * {
  336. * uint8_t caca_header[4]; // "CACA"
  337. * uint8_t caca_file_type[4]; // "FONT"
  338. *
  339. * font_header:
  340. * uint32_t control_size; // Control size (font_data - font_header)
  341. * uint32_t data_size; // Data size (EOF - font_data)
  342. *
  343. * uint16_t version; // Font format version
  344. * // bit 0: set to 1 if font is compatible
  345. * // with version 1 of the format
  346. * // bits 1-15: unused yet, must be 0
  347. *
  348. * uint16_t blocks; // Number of blocks in the font
  349. * uint32_t glyphs; // Total number of glyphs in the font
  350. *
  351. * uint16_t bpp; // Bits per pixel for glyph data (valid
  352. * // Values are 1, 2, 4 and 8)
  353. * uint16_t width; // Maximum glyph width
  354. * uint16_t height; // Maximum glyph height
  355. *
  356. * uint16_t flags; // Feature flags
  357. * // bit 0: set to 1 if font is fixed width
  358. * // bits 1-15: unused yet, must be 0
  359. *
  360. * block_info:
  361. * struct
  362. * {
  363. * uint32_t start; // Unicode index of the first glyph
  364. * uint32_t stop; // Unicode index of the last glyph + 1
  365. * uint32_t index; // Glyph info index of the first glyph
  366. * }
  367. * block_list[blocks];
  368. *
  369. * glyph_info:
  370. * struct
  371. * {
  372. * uint16_t width; // Glyph width in pixels
  373. * uint16_t height; // Glyph height in pixels
  374. * uint32_t data_offset; // Offset (starting from data) to the data
  375. * // for the first character
  376. * }
  377. * glyph_list[glyphs];
  378. *
  379. * extension_1:
  380. * extension_2:
  381. * ...
  382. * extension_N:
  383. * ... // reserved for future use
  384. *
  385. * font_data:
  386. * uint8_t data[data_size]; // glyph data
  387. * };
  388. */
  389. #endif