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.
 
 
 
 
 
 

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