font_hz_file.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #include <dfs_posix.h>
  10. #define HZ_CACHE_MAX 64
  11. static int _font_cache_compare(struct hz_cache* node1, struct hz_cache* node2);
  12. static void rtgui_hz_file_font_load(struct rtgui_font* font);
  13. 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);
  14. static void rtgui_hz_file_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect);
  15. struct rtgui_font_engine rtgui_hz_file_font_engine =
  16. {
  17. RT_NULL,
  18. rtgui_hz_file_font_load,
  19. rtgui_hz_file_font_draw_text,
  20. rtgui_hz_file_font_get_metrics
  21. };
  22. SPLAY_PROTOTYPE(cache_tree, hz_cache, hz_node, _font_cache_compare);
  23. SPLAY_GENERATE (cache_tree, hz_cache, hz_node, _font_cache_compare);
  24. static int _font_cache_compare(struct hz_cache* cache_1, struct hz_cache* cache_2)
  25. {
  26. if (cache_1->hz_id > cache_2->hz_id) return 1;
  27. if (cache_1->hz_id < cache_2->hz_id) return -1;
  28. return 0;
  29. }
  30. static rt_uint8_t* _font_cache_get(struct rtgui_hz_file_font* font, rt_uint16_t hz_id)
  31. {
  32. rt_uint32_t seek;
  33. struct hz_cache *cache, search;
  34. search.hz_id = hz_id;
  35. cache = SPLAY_FIND(cache_tree, &(font->cache_root), &search);
  36. if (cache != RT_NULL)
  37. {
  38. /* find it */
  39. return (rt_uint8_t*)(cache + 1);
  40. }
  41. /* can not find it, load to cache */
  42. cache = (struct hz_cache*) rtgui_malloc(sizeof(struct hz_cache) + font->font_data_size);
  43. if (cache == RT_NULL) return RT_NULL; /* no memory yet */
  44. cache->hz_id = hz_id;
  45. seek = 94 * (((hz_id & 0xff) - 0xA0) - 1) + ((hz_id >> 8) - 0xA0) - 1;
  46. seek *= font->font_data_size;
  47. /* read hz font data */
  48. if ((lseek(font->fd, seek, SEEK_SET) < 0) ||
  49. read(font->fd, (char*)(cache + 1), font->font_data_size) !=
  50. font->font_data_size)
  51. {
  52. rtgui_free(cache);
  53. return RT_NULL;
  54. }
  55. /* insert to cache */
  56. SPLAY_INSERT(cache_tree, &(font->cache_root), cache);
  57. font->cache_size ++;
  58. if (font->cache_size > HZ_CACHE_MAX)
  59. {
  60. /* remove a cache */
  61. struct hz_cache* left;
  62. left = font->cache_root.sph_root;
  63. while (SPLAY_LEFT(left, hz_node) != RT_NULL) left = SPLAY_LEFT(left, hz_node);
  64. /* remove the left node */
  65. SPLAY_REMOVE(cache_tree, &(font->cache_root), left);
  66. font->cache_size --;
  67. }
  68. return (rt_uint8_t*)(cache + 1);
  69. }
  70. static void rtgui_hz_file_font_load(struct rtgui_font* font)
  71. {
  72. struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data;
  73. RT_ASSERT(hz_file_font != RT_NULL);
  74. hz_file_font->fd = open(hz_file_font->font_fn, O_RDONLY, 0);
  75. }
  76. 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)
  77. {
  78. rt_base_t h;
  79. rt_uint8_t* str;
  80. struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data;
  81. RT_ASSERT(hz_file_font != RT_NULL);
  82. /* drawing height */
  83. h = (hz_file_font->font_size + rect->y1 > rect->y2)?
  84. rect->y2 - rect->y1 : hz_file_font->font_size;
  85. str = (rt_uint8_t*)text;
  86. while (len > 0 && rect->x1 < rect->x2)
  87. {
  88. const rt_uint8_t* font_ptr;
  89. register rt_base_t i, j, k;
  90. /* get font pixel data */
  91. font_ptr = _font_cache_get(hz_file_font, *str | (*(str+1) << 8));
  92. /* draw word */
  93. for (i=0; i < h; i ++)
  94. {
  95. for (j=0; j < 2; j++)
  96. for (k=0; k < 8; k++)
  97. {
  98. if ( ((font_ptr[i*2 + j] >> (7-k)) & 0x01) != 0 &&
  99. (rect->x1 + 8 * j + k < rect->x2))
  100. {
  101. rtgui_dc_draw_point(dc, rect->x1 + 8*j + k, rect->y1 + i);
  102. }
  103. }
  104. }
  105. /* move x to next character */
  106. rect->x1 += hz_file_font->font_size;
  107. str += 2;
  108. len -= 2;
  109. }
  110. }
  111. static void rtgui_hz_file_font_get_metrics(struct rtgui_font* font, const char* text, rtgui_rect_t* rect)
  112. {
  113. struct rtgui_hz_file_font* hz_file_font = (struct rtgui_hz_file_font*)font->data;
  114. RT_ASSERT(hz_file_font != RT_NULL);
  115. /* set metrics rect */
  116. rect->x1 = rect->y1 = 0;
  117. rect->x2 = (rt_int16_t)(hz_file_font->font_size/2 * rt_strlen((const char*)text));
  118. rect->y2 = hz_file_font->font_size;
  119. }
  120. #endif