font.c 5.3 KB

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