font.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. rtgui_font_system_add_font(&rtgui_font_asc12);
  36. #ifdef RTGUI_USING_FONTHZ
  37. rtgui_font_system_add_font(&rtgui_font_hz12);
  38. #endif
  39. rtgui_font_set_defaut(&rtgui_font_asc12);
  40. }
  41. void rtgui_font_system_add_font(struct rtgui_font* font)
  42. {
  43. rtgui_list_init(&(font->list));
  44. rtgui_list_append(&_rtgui_font_list, &(font->list));
  45. }
  46. void rtgui_font_system_remove_font(struct rtgui_font* font)
  47. {
  48. rtgui_list_remove(&_rtgui_font_list, &(font->list));
  49. }
  50. struct rtgui_font* rtgui_font_default()
  51. {
  52. return rtgui_default_font;
  53. }
  54. void rtgui_font_set_defaut(struct rtgui_font* font)
  55. {
  56. rtgui_default_font = font;
  57. }
  58. struct rtgui_font* rtgui_font_refer(const rt_uint8_t* family, rt_uint16_t height)
  59. {
  60. /* search font */
  61. struct rtgui_list_node* node;
  62. struct rtgui_font* font;
  63. rtgui_list_foreach(node, &_rtgui_font_list)
  64. {
  65. font = rtgui_list_entry(node, struct rtgui_font, list);
  66. if ((rt_strncmp((const char*)font->family, (const char*)family, RTGUI_NAME_MAX) == 0) &&
  67. font->height == height)
  68. {
  69. font->refer_count ++;
  70. return font;
  71. }
  72. }
  73. return RT_NULL;
  74. }
  75. void rtgui_font_derefer(struct rtgui_font* font)
  76. {
  77. RT_ASSERT(font != RT_NULL);
  78. font->refer_count --;
  79. /* no refer, remove font */
  80. if (font->refer_count == 0)
  81. {
  82. rtgui_font_system_remove_font(font);
  83. }
  84. }
  85. /* draw a text */
  86. 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)
  87. {
  88. RT_ASSERT(font != RT_NULL);
  89. if (font->engine != RT_NULL &&
  90. font->engine->font_draw_text != RT_NULL)
  91. {
  92. font->engine->font_draw_text(font, dc, text, len, rect);
  93. }
  94. }
  95. int rtgui_font_get_string_width(struct rtgui_font* font, const rt_uint8_t* text)
  96. {
  97. rtgui_rect_t rect;
  98. /* get metrics */
  99. rtgui_font_get_metrics(font, text, &rect);
  100. return rect.x2 - rect.x1;
  101. }
  102. void rtgui_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect)
  103. {
  104. RT_ASSERT(font != RT_NULL);
  105. if (font->engine != RT_NULL &&
  106. font->engine->font_get_metrics != RT_NULL)
  107. {
  108. font->engine->font_get_metrics(font, text, rect);
  109. }
  110. else
  111. {
  112. /* no font engine found, set rect to zero */
  113. rt_memset(rect, 0, sizeof(rtgui_rect_t));
  114. }
  115. }
  116. 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);
  117. static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect);
  118. struct rtgui_font_engine bmp_font_engine =
  119. {
  120. RT_NULL,
  121. RT_NULL,
  122. rtgui_bitmap_font_draw_text,
  123. rtgui_bitmap_font_get_metrics
  124. };
  125. void rtgui_bitmap_font_draw_char(struct rtgui_font_bitmap* font, struct rtgui_dc* dc, const char ch,
  126. rtgui_rect_t* rect)
  127. {
  128. const rt_uint8_t* font_ptr = font->bmp + ch * font->height;
  129. rt_uint16_t x, y, w, h;
  130. register rt_base_t i, j;
  131. x = rect->x1;
  132. y = rect->y1;
  133. /* check first and last char */
  134. if (ch < font->first_char || ch > font->last_char) return;
  135. w = (font->width + x > rect->x2)? rect->x2 - rect->x1 : font->width;
  136. h = (font->height + y > rect->y2)? rect->y2 - rect->y1 : font->height;
  137. for (i = 0; i < h; i ++ )
  138. {
  139. for (j = 0; j < w; j ++)
  140. {
  141. if ( ((font_ptr[i] >> (7-j)) & 0x01) != 0)
  142. {
  143. /* draw a pixel */
  144. rtgui_dc_draw_point(dc, j + x, i + y);
  145. }
  146. }
  147. }
  148. }
  149. 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)
  150. {
  151. struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
  152. RT_ASSERT(bmp_font != RT_NULL);
  153. while (len-- && rect->x1 < rect->x2)
  154. {
  155. rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect);
  156. /* move x to next character */
  157. rect->x1 += bmp_font->width;
  158. text ++;
  159. }
  160. }
  161. static void rtgui_bitmap_font_get_metrics(struct rtgui_font* font, const rt_uint8_t* text, rtgui_rect_t* rect)
  162. {
  163. struct rtgui_font_bitmap* bmp_font = (struct rtgui_font_bitmap*)(font->data);
  164. RT_ASSERT(bmp_font != RT_NULL);
  165. /* set metrics rect */
  166. rect->x1 = rect->y1 = 0;
  167. rect->x2 = bmp_font->width * (rt_int16_t)rt_strlen((const char*)text);
  168. rect->y2 = bmp_font->height;
  169. }