No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

357 líneas
10 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. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file font.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Colour handling
  15. *
  16. * This file contains font handling functions.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # if defined(HAVE_ENDIAN_H)
  21. # include <endian.h>
  22. # endif
  23. # include <stdio.h>
  24. # include <stdlib.h>
  25. # include <string.h>
  26. # include <arpa/inet.h>
  27. #endif
  28. #include "cucul.h"
  29. #include "cucul_internals.h"
  30. /* Internal fonts */
  31. uint8_t const font_monospace9[] =
  32. #include "font_monospace9.h"
  33. ;
  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. #define DECLARE_UNPACKGLYPH(bpp) \
  59. static inline void \
  60. unpack_glyph ## bpp(uint8_t *glyph, uint8_t *packed_data, \
  61. unsigned int n) \
  62. { \
  63. unsigned int i; \
  64. \
  65. for(i = 0; i < n; i++) \
  66. { \
  67. uint8_t pixel = packed_data[i / (8 / bpp)]; \
  68. pixel >>= bpp * ((8 / bpp) - 1 - (i % (8 / bpp))); \
  69. pixel %= (1 << bpp); \
  70. pixel *= 0xff / ((1 << bpp) - 1); \
  71. *glyph++ = pixel; \
  72. } \
  73. }
  74. DECLARE_UNPACKGLYPH(4)
  75. DECLARE_UNPACKGLYPH(2)
  76. DECLARE_UNPACKGLYPH(1)
  77. struct cucul_font *cucul_load_font(void *data, unsigned int size)
  78. {
  79. struct cucul_font *f;
  80. unsigned int i;
  81. f = malloc(sizeof(struct cucul_font));
  82. f->private = data;
  83. memcpy(&f->header, f->private + 8, sizeof(struct font_header));
  84. f->header.control_size = htonl(f->header.control_size);
  85. f->header.data_size = htonl(f->header.data_size);
  86. f->header.version = htons(f->header.version);
  87. f->header.blocks = htons(f->header.blocks);
  88. f->header.glyphs = htonl(f->header.glyphs);
  89. f->header.bpp = htons(f->header.bpp);
  90. f->header.width = htons(f->header.width);
  91. f->header.height = htons(f->header.height);
  92. f->header.flags = htons(f->header.flags);
  93. f->block_list = malloc(f->header.blocks * sizeof(struct block_info));
  94. memcpy(f->block_list,
  95. f->private + 8 + sizeof(struct font_header),
  96. f->header.blocks * sizeof(struct block_info));
  97. for(i = 0; i < f->header.blocks; i++)
  98. {
  99. f->block_list[i].start = htonl(f->block_list[i].start);
  100. f->block_list[i].stop = htonl(f->block_list[i].stop);
  101. f->block_list[i].index = htonl(f->block_list[i].index);
  102. }
  103. f->glyph_list = malloc(f->header.glyphs * sizeof(struct glyph_info));
  104. memcpy(f->glyph_list,
  105. f->private + 8 + sizeof(struct font_header)
  106. + f->header.blocks * sizeof(struct block_info),
  107. f->header.glyphs * sizeof(struct glyph_info));
  108. for(i = 0; i < f->header.glyphs; i++)
  109. {
  110. f->glyph_list[i].width = htons(f->glyph_list[i].width);
  111. f->glyph_list[i].height = htons(f->glyph_list[i].height);
  112. f->glyph_list[i].data_offset = htonl(f->glyph_list[i].data_offset);
  113. }
  114. f->font_data = f->private + 8 + f->header.control_size;
  115. return f;
  116. }
  117. /** \brief Get a font's maximum glyph width.
  118. *
  119. * This function returns the maximum value for the current font's glyphs
  120. *
  121. * \param f The font, as returned by \e cucul_load_font()
  122. * \return The maximum glyph width.
  123. */
  124. unsigned int cucul_get_font_width(struct cucul_font *f)
  125. {
  126. return f->header.width;
  127. }
  128. /** \brief Get a font's maximum glyph height.
  129. *
  130. * This function returns the maximum value for the current font's glyphs
  131. *
  132. * \param f The font, as returned by \e cucul_load_font()
  133. * \return The maximum glyph height.
  134. */
  135. unsigned int cucul_get_font_height(struct cucul_font *f)
  136. {
  137. return f->header.height;
  138. }
  139. /** \brief Free a font structure.
  140. *
  141. * This function frees all data allocated by \e cucul_load_font(). The
  142. * font structure is no longer usable by other libcucul functions. Once
  143. * this function has returned, the memory area that was given to
  144. * \e cucul_load_font() can be freed.
  145. *
  146. * \param f The font, as returned by \e cucul_load_font()
  147. */
  148. void cucul_free_font(struct cucul_font *f)
  149. {
  150. free(f->glyph_list);
  151. free(f->block_list);
  152. free(f);
  153. }
  154. /** \brief Render the canvas onto an image buffer.
  155. *
  156. * This function renders the given canvas on an image buffer using a specific
  157. * font. The pixel format is fixed (32-bit ARGB, 8 bits for each component).
  158. *
  159. * The required image width can be computed using \e cucul_get_width(qq) and
  160. * \e cucul_get_font_width(f). The required height can be computed using
  161. * \e cucul_get_height(qq) and \e cucul_get_font_height(f).
  162. *
  163. * Glyphs that do not fit in the image buffer are currently not rendered at
  164. * all. They may be cropped instead in future versions.
  165. *
  166. * \param qq The canvas to render
  167. * \param f The font, as returned by \e cucul_load_font()
  168. * \param buf The image buffer
  169. * \param width The width (in pixels) of the image buffer
  170. * \param height The height (in pixels) of the image buffer
  171. * \param pitch The pitch (in bytes) of an image buffer line.
  172. */
  173. void cucul_render_canvas(cucul_t *qq, struct cucul_font *f,
  174. unsigned char *buf, unsigned int width,
  175. unsigned int height, unsigned int pitch)
  176. {
  177. uint8_t *glyph = NULL;
  178. unsigned int x, y, xmax, ymax;
  179. if(f->header.bpp != 8)
  180. glyph = malloc(f->header.width * f->header.height);
  181. if(width < qq->width * f->header.width)
  182. xmax = width / f->header.width;
  183. else
  184. xmax = qq->width;
  185. if(height < qq->height * f->header.height)
  186. ymax = height / f->header.height;
  187. else
  188. ymax = qq->height;
  189. for(y = 0; y < ymax; y++)
  190. {
  191. for(x = 0; x < xmax; x++)
  192. {
  193. uint8_t argb[8];
  194. unsigned int starty = y * f->header.height;
  195. unsigned int startx = x * f->header.width;
  196. uint32_t ch = qq->chars[y * qq->width + x];
  197. uint32_t attr = qq->attr[y * qq->width + x];
  198. unsigned int b, i, j;
  199. struct glyph_info *g;
  200. /* Find the Unicode block where our glyph lies */
  201. for(b = 0; b < f->header.blocks; b++)
  202. {
  203. if(ch < f->block_list[b].start)
  204. {
  205. b = f->header.blocks;
  206. break;
  207. }
  208. if(ch < f->block_list[b].stop)
  209. break;
  210. }
  211. /* Glyph not in font? Skip it. */
  212. if(b == f->header.blocks)
  213. continue;
  214. g = &f->glyph_list[f->block_list[b].index
  215. + ch - f->block_list[b].start];
  216. _cucul_argb32_to_argb4(attr, argb);
  217. /* Step 1: unpack glyph */
  218. switch(f->header.bpp)
  219. {
  220. case 8:
  221. glyph = f->font_data + g->data_offset;
  222. break;
  223. case 4:
  224. unpack_glyph4(glyph, f->font_data + g->data_offset,
  225. g->width * g->height);
  226. break;
  227. case 2:
  228. unpack_glyph2(glyph, f->font_data + g->data_offset,
  229. g->width * g->height);
  230. break;
  231. case 1:
  232. unpack_glyph1(glyph, f->font_data + g->data_offset,
  233. g->width * g->height);
  234. break;
  235. }
  236. /* Step 2: render glyph using colour attribute */
  237. for(j = 0; j < g->height; j++)
  238. {
  239. uint8_t *line = buf + (starty + j) * pitch + 4 * startx;
  240. for(i = 0; i < g->width; i++)
  241. {
  242. uint8_t *pixel = line + 4 * i;
  243. uint32_t p, q, t;
  244. p = glyph[j * g->width + i];
  245. q = 0xff - p;
  246. for(t = 0; t < 4; t++)
  247. pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf);
  248. }
  249. }
  250. }
  251. }
  252. if(f->header.bpp != 8)
  253. free(glyph);
  254. }
  255. /*
  256. * The libcaca font format, version 1
  257. * ----------------------------------
  258. *
  259. * All types are big endian.
  260. *
  261. * struct
  262. * {
  263. * uint8_t caca_header[4]; // "CACA"
  264. * uint8_t caca_file_type[4]; // "FONT"
  265. *
  266. * font_header:
  267. * uint32_t control_size; // Control size (font_data - font_header)
  268. * uint32_t data_size; // Data size (EOF - font_data)
  269. *
  270. * uint16_t version; // Font format version
  271. * // bit 0: set to 1 if font is compatible
  272. * // with version 1 of the format
  273. * // bits 1-15: unused yet, must be 0
  274. *
  275. * uint16_t blocks; // Number of blocks in the font
  276. * uint32_t glyphs; // Total number of glyphs in the font
  277. *
  278. * uint16_t bpp; // Bits per pixel for glyph data (valid
  279. * // Values are 1, 2, 4 and 8)
  280. * uint16_t width; // Maximum glyph width
  281. * uint16_t height; // Maximum glyph height
  282. *
  283. * uint16_t flags; // Feature flags
  284. * // bit 0: set to 1 if font is fixed width
  285. * // bits 1-15: unused yet, must be 0
  286. *
  287. * block_info:
  288. * struct
  289. * {
  290. * uint32_t start; // Unicode index of the first glyph
  291. * uint32_t stop; // Unicode index of the last glyph + 1
  292. * uint32_t index; // Glyph info index of the first glyph
  293. * }
  294. * block_list[blocks];
  295. *
  296. * glyph_info:
  297. * struct
  298. * {
  299. * uint16_t width; // Glyph width in pixels
  300. * uint16_t height; // Glyph height in pixels
  301. * uint32_t data_offset; // Offset (starting from data) to the data
  302. * // for the first character
  303. * }
  304. * glyph_list[glyphs];
  305. *
  306. * extension_1:
  307. * extension_2:
  308. * ...
  309. * extension_N:
  310. * ... // reserved for future use
  311. *
  312. * font_data:
  313. * uint8_t data[data_size]; // glyph data
  314. * };
  315. */