font_hz_file.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Cached HZ font engine
  3. */
  4. #include <rtgui/dc.h>
  5. #include <rtgui/font.h>
  6. #include <rtgui/tree.h>
  7. #include <rtgui/rtgui_system.h>
  8. #ifdef RTGUI_USING_HZ_FILE
  9. #ifdef _WIN32_NATIVE
  10. #include <fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <io.h>
  14. #define open _open
  15. #define close _close
  16. #define read _read
  17. #define write _write
  18. #define unlink _unlink
  19. #else
  20. #include <dfs_posix.h>
  21. #endif
  22. #define HZ_CACHE_MAX 64
  23. static int _font_cache_compare(struct hz_cache *node1, struct hz_cache *node2);
  24. static void rtgui_hz_file_font_load(struct rtgui_font *font);
  25. static void rtgui_hz_file_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  26. static void rtgui_hz_file_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect);
  27. const struct rtgui_font_engine rtgui_hz_file_font_engine =
  28. {
  29. RT_NULL,
  30. rtgui_hz_file_font_load,
  31. rtgui_hz_file_font_draw_text,
  32. rtgui_hz_file_font_get_metrics
  33. };
  34. SPLAY_PROTOTYPE(cache_tree, hz_cache, hz_node, _font_cache_compare);
  35. SPLAY_GENERATE(cache_tree, hz_cache, hz_node, _font_cache_compare);
  36. static int _font_cache_compare(struct hz_cache *cache_1, struct hz_cache *cache_2)
  37. {
  38. if (cache_1->hz_id > cache_2->hz_id) return 1;
  39. if (cache_1->hz_id < cache_2->hz_id) return -1;
  40. return 0;
  41. }
  42. static rt_uint8_t *_font_cache_get(struct rtgui_hz_file_font *font, rt_uint16_t hz_id)
  43. {
  44. rt_uint32_t seek;
  45. struct hz_cache *cache, search;
  46. search.hz_id = hz_id;
  47. /* enter critical */
  48. rtgui_enter_critical();
  49. cache = SPLAY_FIND(cache_tree, &(font->cache_root), &search);
  50. if (cache != RT_NULL)
  51. {
  52. /* exit critical */
  53. rtgui_exit_critical();
  54. /* found it */
  55. return (rt_uint8_t *)(cache + 1);
  56. }
  57. /* exit critical */
  58. rtgui_exit_critical();
  59. /* can not find it, load to cache */
  60. cache = (struct hz_cache *) rtgui_malloc(sizeof(struct hz_cache) + font->font_data_size);
  61. if (cache == RT_NULL)
  62. return RT_NULL; /* no memory yet */
  63. cache->hz_id = hz_id;
  64. seek = 94 * (((hz_id & 0xff) - 0xA0) - 1) + ((hz_id >> 8) - 0xA0) - 1;
  65. seek *= font->font_data_size;
  66. /* read hz font data */
  67. if ((lseek(font->fd, seek, SEEK_SET) < 0) ||
  68. read(font->fd, (char *)(cache + 1), font->font_data_size) !=
  69. font->font_data_size)
  70. {
  71. rtgui_free(cache);
  72. return RT_NULL;
  73. }
  74. /* enter critical */
  75. rtgui_enter_critical();
  76. if (font->cache_size >= HZ_CACHE_MAX)
  77. {
  78. /* remove a cache */
  79. struct hz_cache *left;
  80. left = font->cache_root.sph_root;
  81. while (SPLAY_LEFT(left, hz_node) != RT_NULL) left = SPLAY_LEFT(left, hz_node);
  82. /* remove the left node */
  83. SPLAY_REMOVE(cache_tree, &(font->cache_root), left);
  84. rtgui_free(left);
  85. font->cache_size --;
  86. }
  87. /* insert to cache */
  88. SPLAY_INSERT(cache_tree, &(font->cache_root), cache);
  89. font->cache_size ++;
  90. /* exit critical */
  91. rtgui_exit_critical();
  92. return (rt_uint8_t *)(cache + 1);
  93. }
  94. static void rtgui_hz_file_font_load(struct rtgui_font *font)
  95. {
  96. struct rtgui_hz_file_font *hz_file_font = (struct rtgui_hz_file_font *)font->data;
  97. RT_ASSERT(hz_file_font != RT_NULL);
  98. hz_file_font->fd = open(hz_file_font->font_fn, O_RDONLY, 0);
  99. if (hz_file_font->fd < 0)
  100. {
  101. rt_kprintf("RTGUI: could not open the font file:%s\n", hz_file_font->font_fn);
  102. rt_kprintf("RTGUI: please mount the fs first and make sure the file is there\n");
  103. }
  104. }
  105. static void _rtgui_hz_file_font_draw_text(struct rtgui_hz_file_font *hz_file_font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect)
  106. {
  107. rt_uint8_t *str;
  108. rtgui_color_t bc;
  109. rt_uint16_t style;
  110. register rt_base_t h, word_bytes;
  111. /* get text style */
  112. style = rtgui_dc_get_gc(dc)->textstyle;
  113. bc = rtgui_dc_get_gc(dc)->background;
  114. /* drawing height */
  115. h = (hz_file_font->font_size + rect->y1 > rect->y2) ?
  116. rect->y2 - rect->y1 : hz_file_font->font_size;
  117. word_bytes = (hz_file_font->font_size + 7) / 8;
  118. str = (rt_uint8_t *)text;
  119. while (len > 0 && rect->x1 < rect->x2)
  120. {
  121. const rt_uint8_t *font_ptr;
  122. register rt_base_t i, j, k;
  123. /* get font pixel data */
  124. font_ptr = _font_cache_get(hz_file_font, *str | (*(str + 1) << 8));
  125. /* draw word */
  126. for (i = 0; i < h; i ++)
  127. {
  128. for (j = 0; j < word_bytes; j++)
  129. for (k = 0; k < 8; k++)
  130. {
  131. if (((font_ptr[i * word_bytes + j] >> (7 - k)) & 0x01) != 0 &&
  132. (rect->x1 + 8 * j + k < rect->x2))
  133. {
  134. rtgui_dc_draw_point(dc, rect->x1 + 8 * j + k, rect->y1 + i);
  135. }
  136. else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND)
  137. {
  138. rtgui_dc_draw_color_point(dc, rect->x1 + 8 * j + k, rect->y1 + i, bc);
  139. }
  140. }
  141. }
  142. /* move x to next character */
  143. rect->x1 += hz_file_font->font_size;
  144. str += 2;
  145. len -= 2;
  146. }
  147. }
  148. static void rtgui_hz_file_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t length, struct rtgui_rect *rect)
  149. {
  150. rt_uint32_t len;
  151. struct rtgui_font *efont;
  152. struct rtgui_hz_file_font *hz_file_font = (struct rtgui_hz_file_font *)font->data;
  153. RT_ASSERT(dc != RT_NULL);
  154. RT_ASSERT(hz_file_font != RT_NULL);
  155. /* get English font */
  156. efont = rtgui_font_refer("asc", hz_file_font->font_size);
  157. if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */
  158. while (length > 0)
  159. {
  160. len = 0;
  161. while (((rt_uint8_t) * (text + len)) < 0x80 && *(text + len) && len < length) len ++;
  162. /* draw text with English font */
  163. if (len > 0)
  164. {
  165. rtgui_font_draw(efont, dc, text, len, rect);
  166. text += len;
  167. length -= len;
  168. }
  169. len = 0;
  170. while (((rt_uint8_t) * (text + len)) >= 0x80 && len < length) len ++;
  171. if (len > 0)
  172. {
  173. _rtgui_hz_file_font_draw_text(hz_file_font, dc, text, len, rect);
  174. text += len;
  175. length -= len;
  176. }
  177. }
  178. rtgui_font_derefer(efont);
  179. }
  180. static void rtgui_hz_file_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect)
  181. {
  182. struct rtgui_hz_file_font *hz_file_font = (struct rtgui_hz_file_font *)font->data;
  183. RT_ASSERT(hz_file_font != RT_NULL);
  184. /* set metrics rect */
  185. rect->x1 = rect->y1 = 0;
  186. rect->x2 = (rt_int16_t)(hz_file_font->font_size / 2 * rt_strlen((const char *)text));
  187. rect->y2 = hz_file_font->font_size;
  188. }
  189. #endif