Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

238 рядки
6.8 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. struct font_header
  31. {
  32. uint32_t control_size, data_size;
  33. uint16_t version, blocks;
  34. uint32_t glyphs;
  35. uint16_t bpp, width, height, flags;
  36. };
  37. struct block_info
  38. {
  39. uint32_t start, stop, index;
  40. };
  41. struct glyph_info
  42. {
  43. uint16_t width, height;
  44. uint32_t data_offset;
  45. };
  46. struct cucul_font
  47. {
  48. struct font_header header;
  49. struct block_info *block_list;
  50. struct glyph_info *glyph_list;
  51. uint8_t *font_data;
  52. uint8_t *private;
  53. };
  54. struct cucul_font *cucul_load_font(void *data, unsigned int size)
  55. {
  56. struct cucul_font *f;
  57. unsigned int i;
  58. f = malloc(sizeof(struct cucul_font));
  59. f->private = data;
  60. memcpy(&f->header, f->private + 8, sizeof(struct font_header));
  61. f->header.control_size = htonl(f->header.control_size);
  62. f->header.data_size = htonl(f->header.data_size);
  63. f->header.version = htons(f->header.version);
  64. f->header.blocks = htons(f->header.blocks);
  65. f->header.glyphs = htonl(f->header.glyphs);
  66. f->header.bpp = htons(f->header.bpp);
  67. f->header.width = htons(f->header.width);
  68. f->header.height = htons(f->header.height);
  69. f->header.flags = htons(f->header.flags);
  70. f->block_list = malloc(f->header.blocks * sizeof(struct block_info));
  71. memcpy(f->block_list,
  72. f->private + 8 + sizeof(struct font_header),
  73. f->header.blocks * sizeof(struct block_info));
  74. for(i = 0; i < f->header.blocks; i++)
  75. {
  76. f->block_list[i].start = htonl(f->block_list[i].start);
  77. f->block_list[i].stop = htonl(f->block_list[i].stop);
  78. f->block_list[i].index = htonl(f->block_list[i].index);
  79. }
  80. f->glyph_list = malloc(f->header.glyphs * sizeof(struct glyph_info));
  81. memcpy(f->glyph_list,
  82. f->private + 8 + sizeof(struct font_header)
  83. + f->header.blocks * sizeof(struct block_info),
  84. f->header.glyphs * sizeof(struct glyph_info));
  85. for(i = 0; i < f->header.glyphs; i++)
  86. {
  87. f->glyph_list[i].width = htons(f->glyph_list[i].width);
  88. f->glyph_list[i].height = htons(f->glyph_list[i].height);
  89. f->glyph_list[i].data_offset = htonl(f->glyph_list[i].data_offset);
  90. }
  91. f->font_data = f->private + 8 + f->header.control_size;
  92. return f;
  93. }
  94. void cucul_free_font(struct cucul_font *f)
  95. {
  96. free(f->glyph_list);
  97. free(f->block_list);
  98. free(f);
  99. }
  100. void cucul_render_canvas(cucul_t *qq, struct cucul_font *f)
  101. {
  102. uint8_t *buf = malloc(qq->width * f->header.width * qq->height * f->header.height);
  103. unsigned int x, y, i, j;
  104. for(y = 0; y < qq->height; y++)
  105. {
  106. for(x = 0; x < qq->width; x++)
  107. {
  108. unsigned int starty = y * f->header.height;
  109. unsigned int startx = x * f->header.width;
  110. uint32_t ch = qq->chars[y * qq->width + x];
  111. unsigned int b;
  112. struct glyph_info *g;
  113. /* Find the Unicode block where our glyph lies */
  114. for(b = 0; b < f->header.blocks; b++)
  115. {
  116. if(ch < f->block_list[b].start)
  117. {
  118. b = f->header.blocks;
  119. break;
  120. }
  121. if(ch < f->block_list[b].stop)
  122. break;
  123. }
  124. /* Glyph not in font? Skip it. */
  125. if(b == f->header.blocks)
  126. continue;
  127. g = &f->glyph_list[f->block_list[b].index
  128. + ch - f->block_list[b].start];
  129. for(j = 0; j < g->height; j++)
  130. {
  131. for(i = 0; i < g->width; i++)
  132. {
  133. /* FIXME: this is 8 bit only */
  134. buf[(starty + j) * qq->width * f->header.width
  135. + startx + i] = f->font_data[g->data_offset + j * g->width + i];
  136. }
  137. }
  138. }
  139. }
  140. for(y = 0; y < qq->height * f->header.height; y++)
  141. {
  142. for(x = 0; x < qq->width * f->header.width; x++)
  143. {
  144. printf("%.02x", buf[y * qq->width * f->header.width + x]);
  145. }
  146. printf("\n");
  147. }
  148. }
  149. /*
  150. * The libcaca font format, version 1
  151. * ----------------------------------
  152. *
  153. * All types are big endian.
  154. *
  155. * struct
  156. * {
  157. * uint8_t caca_header[4]; // "CACA"
  158. * uint8_t caca_file_type[4]; // "FONT"
  159. *
  160. * font_header:
  161. * uint32_t control_size; // Control size (font_data - font_header)
  162. * uint32_t data_size; // Data size (EOF - font_data)
  163. *
  164. * uint16_t version; // Font format version
  165. * // bit 0: set to 1 if font is compatible
  166. * // with version 1 of the format
  167. * // bits 1-15: unused yet, must be 0
  168. *
  169. * uint16_t blocks; // Number of blocks in the font
  170. * uint32_t glyphs; // Total number of glyphs in the font
  171. *
  172. * uint16_t bpp; // Bits per pixel for glyph data (valid
  173. * // Values are 1, 2, 4 and 8)
  174. * uint16_t width; // Maximum glyph width
  175. * uint16_t height; // Maximum glyph height
  176. *
  177. * uint16_t flags; // Feature flags
  178. * // bit 0: set to 1 if font is fixed width
  179. * // bits 1-15: unused yet, must be 0
  180. *
  181. * block_info:
  182. * struct
  183. * {
  184. * uint32_t start; // Unicode index of the first glyph
  185. * uint32_t stop; // Unicode index of the last glyph + 1
  186. * uint32_t index; // Glyph info index of the first glyph
  187. * }
  188. * block_list[blocks];
  189. *
  190. * glyph_info:
  191. * struct
  192. * {
  193. * uint16_t width; // Glyph width in pixels
  194. * uint16_t height; // Glyph height in pixels
  195. * uint32_t data_offset; // Offset (starting from data) to the data
  196. * // for the first character
  197. * }
  198. * glyph_list[glyphs];
  199. *
  200. * extension_1:
  201. * extension_2:
  202. * ...
  203. * extension_N:
  204. * ... // reserved for future use
  205. *
  206. * font_data:
  207. * uint8_t data[data_size]; // glyph data
  208. * };
  209. */