font.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * File : font.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. */
  14. #include <rtgui/font.h>
  15. #include <rtgui/dc.h>
  16. static rtgui_list_t _rtgui_font_list;
  17. static struct rtgui_font* rtgui_default_font;
  18. extern struct rtgui_font rtgui_font_asc16;
  19. extern struct rtgui_font rtgui_font_asc12;
  20. #ifdef RTGUI_USING_FONTHZ
  21. extern struct rtgui_font rtgui_font_hz16;
  22. extern struct rtgui_font rtgui_font_hz12;
  23. #endif
  24. void rtgui_font_system_init()
  25. {
  26. rtgui_list_init(&(_rtgui_font_list));
  27. /* set default font to NULL */
  28. rtgui_default_font = RT_NULL;
  29. #ifdef RTGUI_USING_FONT16
  30. rtgui_font_system_add_font(&rtgui_font_asc16);
  31. #ifdef RTGUI_USING_FONTHZ
  32. rtgui_font_system_add_font(&rtgui_font_hz16);
  33. #endif
  34. #endif
  35. #ifdef RTGUI_USING_FONT12
  36. rtgui_font_system_add_font(&rtgui_font_asc12);
  37. #ifdef RTGUI_USING_FONTHZ
  38. rtgui_font_system_add_font(&rtgui_font_hz12);
  39. #endif
  40. #endif
  41. #ifdef RTGUI_USING_FONT12
  42. rtgui_font_set_defaut(&rtgui_font_asc12);
  43. #endif
  44. #ifdef RTGUI_USING_FONT16
  45. rtgui_font_set_defaut(&rtgui_font_asc16);
  46. #endif
  47. }
  48. void rtgui_font_system_add_font(struct rtgui_font* font)
  49. {
  50. rtgui_list_init(&(font->list));
  51. rtgui_list_append(&_rtgui_font_list, &(font->list));
  52. /* init font */
  53. if (font->engine->font_init != RT_NULL)
  54. font->engine->font_init(font);
  55. /* first refer, load it */
  56. if (font->engine->font_load != RT_NULL)
  57. font->engine->font_load(font);
  58. }
  59. void rtgui_font_system_remove_font(struct rtgui_font* font)
  60. {
  61. rtgui_list_remove(&_rtgui_font_list, &(font->list));
  62. }
  63. struct rtgui_font* rtgui_font_default()
  64. {
  65. return rtgui_default_font;
  66. }
  67. void rtgui_font_set_defaut(struct rtgui_font* font)
  68. {
  69. rtgui_default_font = font;
  70. }
  71. struct rtgui_font* rtgui_font_refer(const rt_uint8_t* family, rt_uint16_t height)
  72. {
  73. /* search font */
  74. struct rtgui_list_node* node;
  75. struct rtgui_font* font;
  76. rtgui_list_foreach(node, &_rtgui_font_list)
  77. {
  78. font = rtgui_list_entry(node, struct rtgui_font, list);
  79. if ((rt_strncmp((const char*)font->family, (const char*)family, RTGUI_NAME_MAX) == 0) &&
  80. font->height == height)
  81. {
  82. font->refer_count ++;
  83. return font;
  84. }
  85. }
  86. return RT_NULL;
  87. }
  88. void rtgui_font_derefer(struct rtgui_font* font)
  89. {
  90. RT_ASSERT(font != RT_NULL);
  91. font->refer_count --;
  92. /* no refer, remove font */
  93. if (font->refer_count == 0)
  94. {
  95. rtgui_font_system_remove_font(font);
  96. }
  97. }
  98. /* draw a text */
  99. void rtgui_font_draw(struct rtgui_font* font, struct rtgui_dc* dc, const rt_uint8_t* text, rt_ubase_t len, struct rtgui_rect* rect)
  100. {
  101. RT_ASSERT(font != RT_NULL);
  102. if (font->engine != RT_NULL &&
  103. font->engine->font_draw_text != RT_NULL)
  104. {
  105. font->engine->font_draw_text(font, dc, text, len, rect);
  106. }
  107. }
  108. int rtgui_font_get_string_width(struct rtgui_font* font, const rt_uint8_t* text)
  109. {
  110. rtgui_rect_t rect;
  111. /* get metrics */
  112. rtgui_font_get_metrics(font, text, &rect);
  113. return rect.x2 - rect.x1;
  114. }
  115. void rtgui_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect)
  116. {
  117. RT_ASSERT(font != RT_NULL);
  118. if (font->engine != RT_NULL &&
  119. font->engine->font_get_metrics != RT_NULL)
  120. {
  121. font->engine->font_get_metrics(font, text, rect);
  122. }
  123. else
  124. {
  125. /* no font engine found, set rect to zero */
  126. rt_memset(rect, 0, sizeof(rtgui_rect_t));
  127. }
  128. }
  129. static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const rt_uint8_t* text, rt_ubase_t len, struct rtgui_rect* rect);
  130. static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect);
  131. struct rtgui_font_engine bmp_font_engine =
  132. {
  133. RT_NULL,
  134. RT_NULL,
  135. rtgui_bitmap_font_draw_text,
  136. rtgui_bitmap_font_get_metrics
  137. };
  138. void rtgui_bitmap_font_draw_char(struct rtgui_font_bitmap* font, struct rtgui_dc* dc, const char ch,
  139. rtgui_rect_t* rect)
  140. {
  141. const rt_uint8_t* font_ptr = font->bmp + ch * font->height;
  142. rt_uint16_t x, y, w, h;
  143. register rt_base_t i, j;
  144. x = rect->x1;
  145. y = rect->y1;
  146. /* check first and last char */
  147. if (ch < font->first_char || ch > font->last_char) return;
  148. w = (font->width + x > rect->x2)? rect->x2 - rect->x1 : font->width;
  149. h = (font->height + y > rect->y2)? rect->y2 - rect->y1 : font->height;
  150. for (i = 0; i < h; i ++ )
  151. {
  152. for (j = 0; j < w; j ++)
  153. {
  154. if ( ((font_ptr[i] >> (7-j)) & 0x01) != 0)
  155. {
  156. /* draw a pixel */
  157. rtgui_dc_draw_point(dc, j + x, i + y);
  158. }
  159. }
  160. }
  161. }
  162. static void rtgui_bitmap_font_draw_text(struct rtgui_font* font, struct rtgui_dc* dc, const rt_uint8_t* text, rt_ubase_t len, struct rtgui_rect* rect)
  163. {
  164. struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
  165. RT_ASSERT(bmp_font != RT_NULL);
  166. while (len-- && rect->x1 < rect->x2)
  167. {
  168. rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect);
  169. /* move x to next character */
  170. rect->x1 += bmp_font->width;
  171. text ++;
  172. }
  173. }
  174. static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect)
  175. {
  176. struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
  177. RT_ASSERT(bmp_font != RT_NULL);
  178. /* set metrics rect */
  179. rect->x1 = rect->y1 = 0;
  180. rect->x2 = bmp_font->width * (rt_int16_t)rt_strlen((const char*)text);
  181. rect->y2 = bmp_font->height;
  182. }