font.c 5.6 KB

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