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: font.c 2299 2008-04-19 12:42:50Z sam $ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * This file contains font handling functions. |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | |
---|
21 | #if !defined(__KERNEL__) |
---|
22 | # if defined(HAVE_ENDIAN_H) |
---|
23 | # include <endian.h> |
---|
24 | # endif |
---|
25 | # include <stdio.h> |
---|
26 | # include <stdlib.h> |
---|
27 | # include <string.h> |
---|
28 | #endif |
---|
29 | |
---|
30 | #include "cucul.h" |
---|
31 | #include "cucul_internals.h" |
---|
32 | |
---|
33 | /* Internal fonts */ |
---|
34 | #include "mono9.data" |
---|
35 | #include "monobold12.data" |
---|
36 | |
---|
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, maxwidth, maxheight, flags; |
---|
45 | }; |
---|
46 | |
---|
47 | struct block_info |
---|
48 | { |
---|
49 | uint32_t start, stop, index; |
---|
50 | }; |
---|
51 | |
---|
52 | struct glyph_info |
---|
53 | { |
---|
54 | uint16_t width, height; |
---|
55 | uint32_t data_offset; |
---|
56 | }; |
---|
57 | |
---|
58 | struct cucul_font |
---|
59 | { |
---|
60 | struct font_header header; |
---|
61 | |
---|
62 | struct block_info *block_list; |
---|
63 | unsigned long int *user_block_list; |
---|
64 | struct glyph_info *glyph_list; |
---|
65 | uint8_t *font_data; |
---|
66 | |
---|
67 | uint8_t *private; |
---|
68 | }; |
---|
69 | #endif |
---|
70 | |
---|
71 | #define DECLARE_UNPACKGLYPH(bpp) \ |
---|
72 | static inline void \ |
---|
73 | unpack_glyph ## bpp(uint8_t *glyph, uint8_t *packed_data, \ |
---|
74 | unsigned int n) \ |
---|
75 | { \ |
---|
76 | unsigned int i; \ |
---|
77 | \ |
---|
78 | for(i = 0; i < n; i++) \ |
---|
79 | { \ |
---|
80 | uint8_t pixel = packed_data[i / (8 / bpp)]; \ |
---|
81 | pixel >>= bpp * ((8 / bpp) - 1 - (i % (8 / bpp))); \ |
---|
82 | pixel %= (1 << bpp); \ |
---|
83 | pixel *= 0xff / ((1 << bpp) - 1); \ |
---|
84 | *glyph++ = pixel; \ |
---|
85 | } \ |
---|
86 | } |
---|
87 | |
---|
88 | DECLARE_UNPACKGLYPH(4) |
---|
89 | DECLARE_UNPACKGLYPH(2) |
---|
90 | DECLARE_UNPACKGLYPH(1) |
---|
91 | |
---|
92 | /** \brief Load a font from memory for future use. |
---|
93 | * |
---|
94 | * This function loads a font and returns a handle to its internal |
---|
95 | * structure. The handle can then be used with cucul_render_canvas() |
---|
96 | * for bitmap output. |
---|
97 | * |
---|
98 | * Internal fonts can also be loaded: if \c size is set to 0, \c data must |
---|
99 | * be a string containing the internal font name. |
---|
100 | * |
---|
101 | * If \c size is non-zero, the \c size bytes of memory at address \c data |
---|
102 | * are loaded as a font. This memory are must not be freed by the calling |
---|
103 | * program until the font handle has been freed with cucul_free_font(). |
---|
104 | * |
---|
105 | * If an error occurs, NULL is returned and \b errno is set accordingly: |
---|
106 | * - \c ENOENT Requested built-in font does not exist. |
---|
107 | * - \c EINVAL Invalid font data in memory area. |
---|
108 | * - \c ENOMEM Not enough memory to allocate font structure. |
---|
109 | * |
---|
110 | * \param data The memory area containing the font or its name. |
---|
111 | * \param size The size of the memory area, or 0 if the font name is given. |
---|
112 | * \return A font handle or NULL in case of error. |
---|
113 | */ |
---|
114 | cucul_font_t *cucul_load_font(void const *data, unsigned int size) |
---|
115 | { |
---|
116 | cucul_font_t *f; |
---|
117 | unsigned int i; |
---|
118 | |
---|
119 | if(size == 0) |
---|
120 | { |
---|
121 | if(!strcasecmp(data, "Monospace 9")) |
---|
122 | return cucul_load_font((char *)&mono9_data, mono9_size); |
---|
123 | if(!strcasecmp(data, "Monospace Bold 12")) |
---|
124 | return cucul_load_font((char *)&monobold12_data, monobold12_size); |
---|
125 | |
---|
126 | seterrno(ENOENT); |
---|
127 | return NULL; |
---|
128 | } |
---|
129 | |
---|
130 | if(size < sizeof(struct font_header)) |
---|
131 | { |
---|
132 | debug("font error: data size %i < header size %i", |
---|
133 | size, (int)sizeof(struct font_header)); |
---|
134 | seterrno(EINVAL); |
---|
135 | return NULL; |
---|
136 | } |
---|
137 | |
---|
138 | f = malloc(sizeof(cucul_font_t)); |
---|
139 | if(!f) |
---|
140 | { |
---|
141 | seterrno(ENOMEM); |
---|
142 | return NULL; |
---|
143 | } |
---|
144 | |
---|
145 | f->private = (void *)(uintptr_t)data; |
---|
146 | |
---|
147 | memcpy(&f->header, f->private + 4, sizeof(struct font_header)); |
---|
148 | f->header.control_size = hton32(f->header.control_size); |
---|
149 | f->header.data_size = hton32(f->header.data_size); |
---|
150 | f->header.version = hton16(f->header.version); |
---|
151 | f->header.blocks = hton16(f->header.blocks); |
---|
152 | f->header.glyphs = hton32(f->header.glyphs); |
---|
153 | f->header.bpp = hton16(f->header.bpp); |
---|
154 | f->header.width = hton16(f->header.width); |
---|
155 | f->header.height = hton16(f->header.height); |
---|
156 | f->header.maxwidth = hton16(f->header.maxwidth); |
---|
157 | f->header.maxheight = hton16(f->header.maxheight); |
---|
158 | f->header.flags = hton16(f->header.flags); |
---|
159 | |
---|
160 | if(size != 4 + f->header.control_size + f->header.data_size |
---|
161 | || (f->header.bpp != 8 && f->header.bpp != 4 && |
---|
162 | f->header.bpp != 2 && f->header.bpp != 1) |
---|
163 | || (f->header.flags & 1) == 0) |
---|
164 | { |
---|
165 | #if defined DEBUG |
---|
166 | if(size != 4 + f->header.control_size + f->header.data_size) |
---|
167 | debug("font error: data size %i < expected size %i", |
---|
168 | size, 4 + f->header.control_size + f->header.data_size); |
---|
169 | else if(f->header.bpp != 8 && f->header.bpp != 4 && |
---|
170 | f->header.bpp != 2 && f->header.bpp != 1) |
---|
171 | debug("font error: invalid bpp %i", f->header.bpp); |
---|
172 | else if((f->header.flags & 1) == 0) |
---|
173 | debug("font error: invalid flags %.04x", f->header.flags); |
---|
174 | #endif |
---|
175 | free(f); |
---|
176 | seterrno(EINVAL); |
---|
177 | return NULL; |
---|
178 | } |
---|
179 | |
---|
180 | f->block_list = malloc(f->header.blocks * sizeof(struct block_info)); |
---|
181 | if(!f->block_list) |
---|
182 | { |
---|
183 | free(f); |
---|
184 | seterrno(ENOMEM); |
---|
185 | return NULL; |
---|
186 | } |
---|
187 | |
---|
188 | f->user_block_list = malloc((f->header.blocks + 1) |
---|
189 | * 2 * sizeof(unsigned long int)); |
---|
190 | if(!f->user_block_list) |
---|
191 | { |
---|
192 | free(f->block_list); |
---|
193 | free(f); |
---|
194 | seterrno(ENOMEM); |
---|
195 | return NULL; |
---|
196 | } |
---|
197 | |
---|
198 | memcpy(f->block_list, |
---|
199 | f->private + 4 + sizeof(struct font_header), |
---|
200 | f->header.blocks * sizeof(struct block_info)); |
---|
201 | for(i = 0; i < f->header.blocks; i++) |
---|
202 | { |
---|
203 | f->block_list[i].start = hton32(f->block_list[i].start); |
---|
204 | f->block_list[i].stop = hton32(f->block_list[i].stop); |
---|
205 | f->block_list[i].index = hton32(f->block_list[i].index); |
---|
206 | |
---|
207 | if(f->block_list[i].start > f->block_list[i].stop |
---|
208 | || (i > 0 && f->block_list[i].start < f->block_list[i - 1].stop) |
---|
209 | || f->block_list[i].index >= f->header.glyphs) |
---|
210 | { |
---|
211 | #if defined DEBUG |
---|
212 | if(f->block_list[i].start > f->block_list[i].stop) |
---|
213 | debug("font error: block %i has start %i > stop %i", |
---|
214 | i, f->block_list[i].start, f->block_list[i].stop); |
---|
215 | else if(i > 0 && f->block_list[i].start < f->block_list[i - 1].stop) |
---|
216 | debug("font error: block %i has start %i < previous stop %i", |
---|
217 | f->block_list[i].start, f->block_list[i - 1].stop); |
---|
218 | else if(f->block_list[i].index >= f->header.glyphs) |
---|
219 | debug("font error: block %i has index >= glyph count %i", |
---|
220 | f->block_list[i].index, f->header.glyphs); |
---|
221 | #endif |
---|
222 | free(f->user_block_list); |
---|
223 | free(f->block_list); |
---|
224 | free(f); |
---|
225 | seterrno(EINVAL); |
---|
226 | return NULL; |
---|
227 | } |
---|
228 | |
---|
229 | f->user_block_list[i * 2] = f->block_list[i].start; |
---|
230 | f->user_block_list[i * 2 + 1] = f->block_list[i].stop; |
---|
231 | } |
---|
232 | |
---|
233 | f->user_block_list[i * 2] = 0; |
---|
234 | f->user_block_list[i * 2 + 1] = 0; |
---|
235 | |
---|
236 | f->glyph_list = malloc(f->header.glyphs * sizeof(struct glyph_info)); |
---|
237 | if(!f->glyph_list) |
---|
238 | { |
---|
239 | free(f->user_block_list); |
---|
240 | free(f->block_list); |
---|
241 | free(f); |
---|
242 | seterrno(ENOMEM); |
---|
243 | return NULL; |
---|
244 | } |
---|
245 | |
---|
246 | memcpy(f->glyph_list, |
---|
247 | f->private + 4 + sizeof(struct font_header) |
---|
248 | + f->header.blocks * sizeof(struct block_info), |
---|
249 | f->header.glyphs * sizeof(struct glyph_info)); |
---|
250 | for(i = 0; i < f->header.glyphs; i++) |
---|
251 | { |
---|
252 | f->glyph_list[i].width = hton16(f->glyph_list[i].width); |
---|
253 | f->glyph_list[i].height = hton16(f->glyph_list[i].height); |
---|
254 | f->glyph_list[i].data_offset = hton32(f->glyph_list[i].data_offset); |
---|
255 | |
---|
256 | if(f->glyph_list[i].data_offset >= f->header.data_size |
---|
257 | || f->glyph_list[i].data_offset |
---|
258 | + (f->glyph_list[i].width * f->glyph_list[i].height * |
---|
259 | f->header.bpp + 7) / 8 > f->header.data_size |
---|
260 | || f->glyph_list[i].width > f->header.maxwidth |
---|
261 | || f->glyph_list[i].height > f->header.maxheight) |
---|
262 | { |
---|
263 | #if defined DEBUG |
---|
264 | if(f->glyph_list[i].data_offset >= f->header.data_size) |
---|
265 | debug("font error: glyph %i has data start %i > " |
---|
266 | "data end %i", |
---|
267 | f->glyph_list[i].data_offset, f->header.data_size); |
---|
268 | else if(f->glyph_list[i].data_offset |
---|
269 | + (f->glyph_list[i].width * f->glyph_list[i].height * |
---|
270 | f->header.bpp + 7) / 8 > f->header.data_size) |
---|
271 | debug("font error: glyph %i has data end %i > " |
---|
272 | "data end %i", f->glyph_list[i].data_offset |
---|
273 | + (f->glyph_list[i].width * f->glyph_list[i].height * |
---|
274 | f->header.bpp + 7) / 8, f->header.data_size); |
---|
275 | else if(f->glyph_list[i].width > f->header.maxwidth) |
---|
276 | debug("font error: glyph %i has width %i > max width %i", |
---|
277 | f->glyph_list[i].width, f->header.maxwidth); |
---|
278 | else if(f->glyph_list[i].height > f->header.maxheight) |
---|
279 | debug("font error: glyph %i has height %i > max height %i", |
---|
280 | f->glyph_list[i].height, f->header.maxheight); |
---|
281 | #endif |
---|
282 | free(f->glyph_list); |
---|
283 | free(f->user_block_list); |
---|
284 | free(f->block_list); |
---|
285 | free(f); |
---|
286 | seterrno(EINVAL); |
---|
287 | return NULL; |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | f->font_data = f->private + 4 + f->header.control_size; |
---|
292 | |
---|
293 | return f; |
---|
294 | } |
---|
295 | |
---|
296 | /** \brief Get available builtin fonts |
---|
297 | * |
---|
298 | * Return a list of available builtin fonts. The list is a NULL-terminated |
---|
299 | * array of strings. |
---|
300 | * |
---|
301 | * This function never fails. |
---|
302 | * |
---|
303 | * \return An array of strings. |
---|
304 | */ |
---|
305 | char const * const * cucul_get_font_list(void) |
---|
306 | { |
---|
307 | static char const * const list[] = |
---|
308 | { |
---|
309 | "Monospace 9", |
---|
310 | "Monospace Bold 12", |
---|
311 | NULL |
---|
312 | }; |
---|
313 | |
---|
314 | return list; |
---|
315 | } |
---|
316 | |
---|
317 | /** \brief Get a font's standard glyph width. |
---|
318 | * |
---|
319 | * Return the standard value for the current font's glyphs. Most glyphs in |
---|
320 | * the font will have this width, except fullwidth characters. |
---|
321 | * |
---|
322 | * This function never fails. |
---|
323 | * |
---|
324 | * \param f The font, as returned by cucul_load_font() |
---|
325 | * \return The standard glyph width. |
---|
326 | */ |
---|
327 | unsigned int cucul_get_font_width(cucul_font_t const *f) |
---|
328 | { |
---|
329 | return f->header.width; |
---|
330 | } |
---|
331 | |
---|
332 | /** \brief Get a font's standard glyph height. |
---|
333 | * |
---|
334 | * Returns the standard value for the current font's glyphs. Most glyphs in |
---|
335 | * the font will have this height. |
---|
336 | * |
---|
337 | * This function never fails. |
---|
338 | * |
---|
339 | * \param f The font, as returned by cucul_load_font() |
---|
340 | * \return The standard glyph height. |
---|
341 | */ |
---|
342 | unsigned int cucul_get_font_height(cucul_font_t const *f) |
---|
343 | { |
---|
344 | return f->header.height; |
---|
345 | } |
---|
346 | |
---|
347 | /** \brief Get a font's list of supported glyphs. |
---|
348 | * |
---|
349 | * This function returns the list of Unicode blocks supported by the |
---|
350 | * given font. The list is a zero-terminated list of indices. Here is |
---|
351 | * an example: |
---|
352 | * |
---|
353 | * \code |
---|
354 | * { |
---|
355 | * 0x0000, 0x0080, // Basic latin: A, B, C, a, b, c |
---|
356 | * 0x0080, 0x0100, // Latin-1 supplement: "A, 'e, ^u |
---|
357 | * 0x0530, 0x0590, // Armenian |
---|
358 | * 0x0000, 0x0000, // END |
---|
359 | * }; |
---|
360 | * \endcode |
---|
361 | * |
---|
362 | * This function never fails. |
---|
363 | * |
---|
364 | * \param f The font, as returned by cucul_load_font() |
---|
365 | * \return The list of Unicode blocks supported by the font. |
---|
366 | */ |
---|
367 | unsigned long int const *cucul_get_font_blocks(cucul_font_t const *f) |
---|
368 | { |
---|
369 | return (unsigned long int const *)f->user_block_list; |
---|
370 | } |
---|
371 | |
---|
372 | /** \brief Free a font structure. |
---|
373 | * |
---|
374 | * This function frees all data allocated by cucul_load_font(). The |
---|
375 | * font structure is no longer usable by other libcucul functions. Once |
---|
376 | * this function has returned, the memory area that was given to |
---|
377 | * cucul_load_font() can be freed. |
---|
378 | * |
---|
379 | * This function never fails. |
---|
380 | * |
---|
381 | * \param f The font, as returned by cucul_load_font() |
---|
382 | * \return This function always returns 0. |
---|
383 | */ |
---|
384 | int cucul_free_font(cucul_font_t *f) |
---|
385 | { |
---|
386 | free(f->glyph_list); |
---|
387 | free(f->user_block_list); |
---|
388 | free(f->block_list); |
---|
389 | free(f); |
---|
390 | |
---|
391 | return 0; |
---|
392 | } |
---|
393 | |
---|
394 | /** \brief Render the canvas onto an image buffer. |
---|
395 | * |
---|
396 | * This function renders the given canvas on an image buffer using a specific |
---|
397 | * font. The pixel format is fixed (32-bit ARGB, 8 bits for each component). |
---|
398 | * |
---|
399 | * The required image width can be computed using |
---|
400 | * cucul_get_canvas_width() and cucul_get_font_width(). The required |
---|
401 | * height can be computed using cucul_get_canvas_height() and |
---|
402 | * cucul_get_font_height(). |
---|
403 | * |
---|
404 | * Glyphs that do not fit in the image buffer are currently not rendered at |
---|
405 | * all. They may be cropped instead in future versions. |
---|
406 | * |
---|
407 | * This function never fails. |
---|
408 | * |
---|
409 | * \param cv The canvas to render |
---|
410 | * \param f The font, as returned by cucul_load_font() |
---|
411 | * \param buf The image buffer |
---|
412 | * \param width The width (in pixels) of the image buffer |
---|
413 | * \param height The height (in pixels) of the image buffer |
---|
414 | * \param pitch The pitch (in bytes) of an image buffer line. |
---|
415 | * \return This function always returns 0. |
---|
416 | */ |
---|
417 | int cucul_render_canvas(cucul_canvas_t const *cv, cucul_font_t const *f, |
---|
418 | void *buf, unsigned int width, |
---|
419 | unsigned int height, unsigned int pitch) |
---|
420 | { |
---|
421 | uint8_t *glyph = NULL; |
---|
422 | unsigned int x, y, xmax, ymax; |
---|
423 | |
---|
424 | if(f->header.bpp != 8) |
---|
425 | glyph = malloc(f->header.width * 2 * f->header.height); |
---|
426 | |
---|
427 | if(width < cv->width * f->header.width) |
---|
428 | xmax = width / f->header.width; |
---|
429 | else |
---|
430 | xmax = cv->width; |
---|
431 | |
---|
432 | if(height < cv->height * f->header.height) |
---|
433 | ymax = height / f->header.height; |
---|
434 | else |
---|
435 | ymax = cv->height; |
---|
436 | |
---|
437 | for(y = 0; y < ymax; y++) |
---|
438 | { |
---|
439 | for(x = 0; x < xmax; x++) |
---|
440 | { |
---|
441 | uint8_t argb[8]; |
---|
442 | unsigned int starty = y * f->header.height; |
---|
443 | unsigned int startx = x * f->header.width; |
---|
444 | uint32_t ch = cv->chars[y * cv->width + x]; |
---|
445 | uint32_t attr = cv->attrs[y * cv->width + x]; |
---|
446 | unsigned int b, i, j; |
---|
447 | struct glyph_info *g; |
---|
448 | |
---|
449 | /* Find the Unicode block where our glyph lies */ |
---|
450 | for(b = 0; b < f->header.blocks; b++) |
---|
451 | { |
---|
452 | if(ch < f->block_list[b].start) |
---|
453 | { |
---|
454 | b = f->header.blocks; |
---|
455 | break; |
---|
456 | } |
---|
457 | |
---|
458 | if(ch < f->block_list[b].stop) |
---|
459 | break; |
---|
460 | } |
---|
461 | |
---|
462 | /* Glyph not in font? Skip it. */ |
---|
463 | if(b == f->header.blocks) |
---|
464 | continue; |
---|
465 | |
---|
466 | g = &f->glyph_list[f->block_list[b].index |
---|
467 | + ch - f->block_list[b].start]; |
---|
468 | |
---|
469 | cucul_attr_to_argb64(attr, argb); |
---|
470 | |
---|
471 | /* Step 1: unpack glyph */ |
---|
472 | switch(f->header.bpp) |
---|
473 | { |
---|
474 | case 8: |
---|
475 | glyph = f->font_data + g->data_offset; |
---|
476 | break; |
---|
477 | case 4: |
---|
478 | unpack_glyph4(glyph, f->font_data + g->data_offset, |
---|
479 | g->width * g->height); |
---|
480 | break; |
---|
481 | case 2: |
---|
482 | unpack_glyph2(glyph, f->font_data + g->data_offset, |
---|
483 | g->width * g->height); |
---|
484 | break; |
---|
485 | case 1: |
---|
486 | unpack_glyph1(glyph, f->font_data + g->data_offset, |
---|
487 | g->width * g->height); |
---|
488 | break; |
---|
489 | } |
---|
490 | |
---|
491 | /* Step 2: render glyph using colour attribute */ |
---|
492 | for(j = 0; j < g->height; j++) |
---|
493 | { |
---|
494 | uint8_t *line = buf; |
---|
495 | line += (starty + j) * pitch + 4 * startx; |
---|
496 | |
---|
497 | for(i = 0; i < g->width; i++) |
---|
498 | { |
---|
499 | uint8_t *pixel = line + 4 * i; |
---|
500 | uint32_t p, q, t; |
---|
501 | |
---|
502 | p = glyph[j * g->width + i]; |
---|
503 | q = 0xff - p; |
---|
504 | |
---|
505 | for(t = 0; t < 4; t++) |
---|
506 | pixel[t] = (((q * argb[t]) + (p * argb[4 + t])) / 0xf); |
---|
507 | } |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | if(f->header.bpp != 8) |
---|
513 | free(glyph); |
---|
514 | |
---|
515 | return 0; |
---|
516 | } |
---|
517 | |
---|