Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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