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.
 
 
 
 
 
 

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