font_hz_bmp.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * File : font_hz_bmp.c
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2010-09-15 Bernard first version
  23. */
  24. #include <rtgui/dc.h>
  25. #include <rtgui/font.h>
  26. #ifdef RTGUI_USING_HZ_BMP
  27. static void rtgui_hz_bitmap_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  28. static void rtgui_hz_bitmap_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect);
  29. const struct rtgui_font_engine hz_bmp_font_engine =
  30. {
  31. RT_NULL,
  32. RT_NULL,
  33. rtgui_hz_bitmap_font_draw_text,
  34. rtgui_hz_bitmap_font_get_metrics
  35. };
  36. #ifdef RTGUI_USING_FONT_COMPACT
  37. extern rt_uint32_t rtgui_font_mph12(const rt_uint16_t key);
  38. extern rt_uint32_t rtgui_font_mph16(const rt_uint16_t key);
  39. rt_inline const rt_uint8_t *_rtgui_hz_bitmap_get_font_ptr(struct rtgui_font_bitmap *bmp_font,
  40. rt_uint8_t *str,
  41. rt_base_t font_bytes)
  42. {
  43. rt_uint16_t cha = *(rt_uint16_t *)str;
  44. int idx;
  45. if (bmp_font->height == 16)
  46. idx = rtgui_font_mph16(cha);
  47. else // asume the height is 12
  48. idx = rtgui_font_mph12(cha);
  49. /* don't access beyond the data */
  50. if (idx < 0)
  51. idx = 0;
  52. /* get font pixel data */
  53. return bmp_font->bmp + idx * font_bytes;
  54. }
  55. #else
  56. rt_inline const rt_uint8_t *_rtgui_hz_bitmap_get_font_ptr(struct rtgui_font_bitmap *bmp_font,
  57. rt_uint8_t *str,
  58. rt_base_t font_bytes)
  59. {
  60. rt_ubase_t sect, index;
  61. /* calculate section and index */
  62. sect = *str - 0xA0;
  63. index = *(str + 1) - 0xA0;
  64. /* get font pixel data */
  65. return bmp_font->bmp + (94 * (sect - 1) + (index - 1)) * font_bytes;
  66. }
  67. #endif
  68. static void _rtgui_hz_bitmap_font_draw_text(struct rtgui_font_bitmap *bmp_font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect)
  69. {
  70. rtgui_color_t bc;
  71. rt_uint16_t style;
  72. rt_uint8_t *str;
  73. register rt_base_t h, word_bytes, font_bytes;
  74. RT_ASSERT(bmp_font != RT_NULL);
  75. /* get text style */
  76. style = rtgui_dc_get_gc(dc)->textstyle;
  77. bc = rtgui_dc_get_gc(dc)->background;
  78. /* drawing height */
  79. h = (bmp_font->height + rect->y1 > rect->y2) ? rect->y2 - rect->y1 : bmp_font->height;
  80. word_bytes = (bmp_font->width + 7) / 8;
  81. font_bytes = word_bytes * bmp_font->height;
  82. str = (rt_uint8_t *)text;
  83. while (len > 0 && rect->x1 < rect->x2)
  84. {
  85. const rt_uint8_t *font_ptr;
  86. register rt_base_t i, j, k;
  87. /* get font pixel data */
  88. font_ptr = _rtgui_hz_bitmap_get_font_ptr(bmp_font, str, font_bytes);
  89. /* draw word */
  90. for (i = 0; i < h; i ++)
  91. {
  92. for (j = 0; j < word_bytes; j++)
  93. for (k = 0; k < 8; k++)
  94. {
  95. if (((font_ptr[i * word_bytes + j] >> (7 - k)) & 0x01) != 0 &&
  96. (rect->x1 + 8 * j + k < rect->x2))
  97. {
  98. rtgui_dc_draw_point(dc, rect->x1 + 8 * j + k, rect->y1 + i);
  99. }
  100. else if (style & RTGUI_TEXTSTYLE_DRAW_BACKGROUND)
  101. {
  102. rtgui_dc_draw_color_point(dc, rect->x1 + 8 * j + k, rect->y1 + i, bc);
  103. }
  104. }
  105. }
  106. /* move x to next character */
  107. rect->x1 += bmp_font->width;
  108. str += 2;
  109. len -= 2;
  110. }
  111. }
  112. static void rtgui_hz_bitmap_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t length, struct rtgui_rect *rect)
  113. {
  114. rt_uint32_t len;
  115. struct rtgui_font *efont;
  116. struct rtgui_font_bitmap *bmp_font = (struct rtgui_font_bitmap *)(font->data);
  117. RT_ASSERT(dc != RT_NULL);
  118. /* get English font */
  119. efont = rtgui_font_refer("asc", bmp_font->height);
  120. if (efont == RT_NULL) efont = rtgui_font_default(); /* use system default font */
  121. while (length > 0)
  122. {
  123. len = 0;
  124. while (((rt_uint8_t) * (text + len)) < 0x80 && *(text + len) && len < length) len ++;
  125. /* draw text with English font */
  126. if (len > 0)
  127. {
  128. rtgui_font_draw(efont, dc, text, len, rect);
  129. text += len;
  130. length -= len;
  131. }
  132. len = 0;
  133. while (((rt_uint8_t) * (text + len)) >= 0x80 && len < length) len ++;
  134. if (len > 0)
  135. {
  136. _rtgui_hz_bitmap_font_draw_text(bmp_font, dc, text, len, rect);
  137. text += len;
  138. length -= len;
  139. }
  140. }
  141. rtgui_font_derefer(efont);
  142. }
  143. static void rtgui_hz_bitmap_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect)
  144. {
  145. struct rtgui_font_bitmap *bmp_font = (struct rtgui_font_bitmap *)(font->data);
  146. RT_ASSERT(bmp_font != RT_NULL);
  147. /* set metrics rect */
  148. rect->x1 = rect->y1 = 0;
  149. /* Chinese font is always fixed font */
  150. rect->x2 = (rt_int16_t)(bmp_font->width * rt_strlen((const char *)text));
  151. rect->y2 = bmp_font->height;
  152. }
  153. #endif