font_bmp.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * File : font.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2010, 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. * 2010-09-15 Bernard first version
  13. */
  14. #include <rtgui/font.h>
  15. #include <rtgui/dc.h>
  16. /* bitmap font private data */
  17. static void rtgui_bitmap_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  18. static void rtgui_bitmap_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect);
  19. const struct rtgui_font_engine bmp_font_engine =
  20. {
  21. RT_NULL,
  22. RT_NULL,
  23. rtgui_bitmap_font_draw_text,
  24. rtgui_bitmap_font_get_metrics
  25. };
  26. void rtgui_bitmap_font_draw_char(struct rtgui_font_bitmap *font, struct rtgui_dc *dc, const char ch,
  27. rtgui_rect_t *rect)
  28. {
  29. rtgui_color_t bc;
  30. const rt_uint8_t *font_ptr;
  31. rt_uint16_t x, y, w, h, style;
  32. register rt_base_t i, j, /*k,*/ word_bytes;
  33. /* check first and last char */
  34. if (ch < font->first_char || ch > font->last_char) return;
  35. /* get text style */
  36. style = rtgui_dc_get_gc(dc)->textstyle;
  37. bc = rtgui_dc_get_gc(dc)->background;
  38. x = rect->x1;
  39. y = rect->y1;
  40. /* get width */
  41. if (font->char_width == RT_NULL)
  42. {
  43. word_bytes = (((font->width - 1) / 8) + 1);
  44. font_ptr = font->bmp + (ch - font->first_char) * word_bytes * font->height;
  45. }
  46. else
  47. {
  48. word_bytes = ((font->char_width[ch - font->first_char] - 1) / 8) + 1;
  49. font_ptr = font->bmp + font->offset[ch - font->first_char];
  50. }
  51. w = (font->width + x > rect->x2) ? rect->x2 - rect->x1 : font->width;
  52. h = (font->height + y > rect->y2) ? rect->y2 - rect->y1 : font->height;
  53. for (i = 0; i < h; i++)
  54. {
  55. rt_uint8_t chr;
  56. const rt_uint8_t *ptr = font_ptr + i * word_bytes;
  57. for (j = 0; j < w; j++)
  58. {
  59. if (j % 8 == 0)chr = *ptr++;
  60. if (chr & 0x80)
  61. rtgui_dc_draw_point(dc, j + x, i + y);
  62. else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND)
  63. rtgui_dc_draw_color_point(dc, j + x, i + y, bc);
  64. chr <<= 1;
  65. }
  66. }
  67. }
  68. static void rtgui_bitmap_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc,
  69. const char *text, rt_ubase_t len, struct rtgui_rect *rect)
  70. {
  71. rt_uint32_t length;
  72. struct rtgui_font_bitmap *bmp_font = (struct rtgui_font_bitmap *)(font->data);
  73. #ifdef RTGUI_USING_FONTHZ
  74. struct rtgui_font *hz_font;
  75. RT_ASSERT(bmp_font != RT_NULL);
  76. if (rect->y1 > rect->y2) return;
  77. hz_font = rtgui_font_refer("hz", font->height);
  78. while ((rect->x1 < rect->x2) && len)
  79. {
  80. length = 0;
  81. while ((rt_uint8_t) * (text + length) >= 0x80) length ++; /* it's not a ascii character */
  82. if (length > 0)
  83. {
  84. if (hz_font != RT_NULL) rtgui_font_draw(hz_font, dc, text, length, rect);
  85. text += length;
  86. len -= length;
  87. }
  88. length = 0;
  89. while (((rt_uint8_t) * (text + length) < 0x80) && *(text + length)) length ++;
  90. if (length > 0)
  91. {
  92. len -= length;
  93. while (length-- && rect->x1 < rect->x2)
  94. {
  95. rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect);
  96. /* move x to next character */
  97. if (bmp_font->char_width == RT_NULL)
  98. rect->x1 += bmp_font->width;
  99. else
  100. rect->x1 += bmp_font->char_width[*text - bmp_font->first_char];
  101. text ++;
  102. }
  103. }
  104. }
  105. if (hz_font != RT_NULL) rtgui_font_derefer(hz_font);
  106. #else
  107. if (rect->y1 > rect->y2) return;
  108. while ((rect->x1 < rect->x2) && len)
  109. {
  110. length = 0;
  111. while (((rt_uint8_t) * (text + length) < 0x80) && *(text + length)) length ++;
  112. if (length > 0)
  113. {
  114. len -= length;
  115. while (length-- && rect->x1 < rect->x2)
  116. {
  117. rtgui_bitmap_font_draw_char(bmp_font, dc, *text, rect);
  118. /* move x to next character */
  119. if (bmp_font->char_width == RT_NULL)
  120. rect->x1 += bmp_font->width;
  121. else
  122. rect->x1 += bmp_font->char_width[*text - bmp_font->first_char];
  123. text ++;
  124. }
  125. }
  126. }
  127. #endif
  128. }
  129. static void rtgui_bitmap_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect)
  130. {
  131. rt_uint32_t length;
  132. struct rtgui_font_bitmap *bmp_font = (struct rtgui_font_bitmap *)(font->data);
  133. RT_ASSERT(bmp_font != RT_NULL);
  134. /* set init metrics rect */
  135. rect->x1 = rect->y1 = 0;
  136. rect->x2 = 0;
  137. rect->y2 = bmp_font->height;
  138. while (*text)
  139. {
  140. length = 0;
  141. while ((rt_uint8_t) * (text + length) >= 0x80) length ++; /* it's not a ascii character */
  142. rect->x2 += (font->height / 2) * length;
  143. text += length;
  144. length = 0;
  145. while (((rt_uint8_t) * (text + length) < 0x80) && *(text + length)) length ++;
  146. if (bmp_font->char_width != NULL)
  147. {
  148. /* get width for each character */
  149. while (*text && ((rt_uint8_t)*text < 0x80))
  150. {
  151. rect->x2 += bmp_font->char_width[*text - bmp_font->first_char];
  152. text ++;
  153. }
  154. }
  155. else
  156. {
  157. /* set metrics rect */
  158. rect->x2 += bmp_font->width * length;
  159. text += length;
  160. }
  161. }
  162. }