font_fnt.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * File : font_fnt.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. /*
  25. * rockbox fnt font engine
  26. */
  27. #include <rtgui/font_fnt.h>
  28. #include <rtgui/rtgui_system.h>
  29. static void rtgui_fnt_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect);
  30. static void rtgui_fnt_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect);
  31. const struct rtgui_font_engine fnt_font_engine =
  32. {
  33. RT_NULL,
  34. RT_NULL,
  35. rtgui_fnt_font_draw_text,
  36. rtgui_fnt_font_get_metrics
  37. };
  38. void rtgui_fnt_font_draw_text(struct rtgui_font *font, struct rtgui_dc *dc, const char *text, rt_ubase_t len, struct rtgui_rect *rect)
  39. {
  40. int ch, i, j, c, width;
  41. rt_uint32_t position;
  42. struct fnt_font *fnt;
  43. rt_uint8_t *data_ptr;
  44. struct rtgui_rect text_rect;
  45. fnt = (struct fnt_font*)font->data;
  46. RT_ASSERT(fnt != RT_NULL);
  47. rtgui_font_get_metrics(rtgui_dc_get_gc(dc)->font, text, &text_rect);
  48. rtgui_rect_move_to_align(rect, &text_rect, RTGUI_DC_TEXTALIGN(dc));
  49. while (len)
  50. {
  51. /* get character */
  52. ch = *text;
  53. /* NOTE: we only support asc character right now */
  54. if (ch > 0x80)
  55. {
  56. text += 1;
  57. len -= 1;
  58. continue;
  59. }
  60. /* get position and width */
  61. if (fnt->offset == RT_NULL)
  62. {
  63. width = fnt->header.max_width;
  64. position = (ch - fnt->header.first_char) * width * ((fnt->header.height + 7)/8);
  65. }
  66. else
  67. {
  68. width = fnt->width[ch - fnt->header.first_char];
  69. position = fnt->offset[ch - fnt->header.first_char];
  70. }
  71. /* draw a character */
  72. data_ptr = (rt_uint8_t*)&fnt->bits[position];
  73. for (i = 0; i < width; i ++) /* x */
  74. {
  75. for (j = 0; j < 8; j ++) /* y */
  76. {
  77. for (c = 0; c < (fnt->header.height + 7)/8; c ++)
  78. {
  79. /* check drawable region */
  80. if ((text_rect.x1 + i > text_rect.x2) || (text_rect.y1 + c * 8 + j > text_rect.y2))
  81. continue;
  82. if (data_ptr[i + c * width] & (1 << j))
  83. rtgui_dc_draw_point(dc, text_rect.x1 + i, text_rect.y1 + c * 8 + j);
  84. }
  85. }
  86. }
  87. text_rect.x1 += width;
  88. text += 1;
  89. len -= 1;
  90. }
  91. }
  92. void rtgui_fnt_font_get_metrics(struct rtgui_font *font, const char *text, rtgui_rect_t *rect)
  93. {
  94. int ch;
  95. struct fnt_font *fnt;
  96. fnt = (struct fnt_font*)font->data;
  97. RT_ASSERT(fnt != RT_NULL);
  98. rt_memset(rect, 0x00, sizeof(rtgui_rect_t));
  99. rect->y2 = fnt->header.height;
  100. while (*text)
  101. {
  102. if (fnt->width == RT_NULL)
  103. {
  104. /* fixed width font */
  105. rect->x2 += fnt->header.max_width;
  106. }
  107. else
  108. {
  109. ch = *text;
  110. /* NOTE: we only support asc character right now */
  111. if (ch > 0x80)
  112. {
  113. text += 1;
  114. continue;
  115. }
  116. rect->x2 += fnt->width[ch - fnt->header.first_char];
  117. }
  118. text += 1;
  119. }
  120. }
  121. #ifdef RTGUI_USING_FNT_FILE
  122. #include <dfs_posix.h>
  123. rt_inline int readbyte(int fd, unsigned char *cp)
  124. {
  125. unsigned char buf[1];
  126. if (read(fd, buf, 1) != 1)
  127. return 0;
  128. *cp = buf[0];
  129. return 1;
  130. }
  131. rt_inline int readshort(int fd, unsigned short *sp)
  132. {
  133. unsigned char buf[2];
  134. if (read(fd, buf, 2) != 2)
  135. return 0;
  136. *sp = buf[0] | (buf[1] << 8);
  137. return 1;
  138. }
  139. rt_inline int readlong(int fd, rt_uint32_t *lp)
  140. {
  141. unsigned char buf[4];
  142. if (read(fd, buf, 4) != 4)
  143. return 0;
  144. *lp = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
  145. return 1;
  146. }
  147. rt_inline int readstr(int fd, char *buf, int count)
  148. {
  149. return read(fd, buf, count);
  150. }
  151. struct rtgui_font *fnt_font_create(const char* filename, const char* font_family)
  152. {
  153. int fd = -1;
  154. rt_uint32_t index;
  155. struct rtgui_font *font = RT_NULL;
  156. struct fnt_font *fnt = RT_NULL;
  157. struct fnt_header *fnt_header;
  158. fd = open(filename, O_RDONLY, 0);
  159. if (fd < 0)
  160. {
  161. goto __exit;
  162. }
  163. font = (struct rtgui_font*) rtgui_malloc (sizeof(struct rtgui_font));
  164. if (font == RT_NULL) goto __exit;
  165. fnt = (struct fnt_font*) rtgui_malloc (sizeof(struct fnt_font));
  166. if (fnt == RT_NULL) goto __exit;
  167. rt_memset(fnt, 0x00, sizeof(struct fnt_font));
  168. font->data = (void*)fnt;
  169. fnt_header = &(fnt->header);
  170. if (readstr(fd, fnt_header->version, 4) != 4) goto __exit;
  171. if (!readshort(fd, &fnt_header->max_width)) goto __exit;
  172. if (!readshort(fd, &fnt_header->height)) goto __exit;
  173. if (!readshort(fd, &fnt_header->ascent)) goto __exit;
  174. if (!readshort(fd, &fnt_header->depth)) goto __exit;
  175. if (!readlong(fd, &fnt_header->first_char)) goto __exit;
  176. if (!readlong(fd, &fnt_header->default_char)) goto __exit;
  177. if (!readlong(fd, &fnt_header->size)) goto __exit;
  178. if (!readlong(fd, &fnt_header->nbits)) goto __exit;
  179. if (!readlong(fd, &fnt_header->noffset)) goto __exit;
  180. if (!readlong(fd, &fnt_header->nwidth)) goto __exit;
  181. fnt->bits = (MWIMAGEBITS*) rtgui_malloc (fnt_header->nbits * sizeof(MWIMAGEBITS));
  182. if (fnt->bits == RT_NULL) goto __exit;
  183. /* read data */
  184. if (readstr(fd, &(fnt->bits[0]), fnt_header->nbits) != fnt_header->nbits) goto __exit;
  185. /* check boundary */
  186. if (fnt_header->nbits & 0x01)
  187. {
  188. rt_uint16_t pad;
  189. readshort(fd, &pad);
  190. pad = pad; /* skip warning */
  191. }
  192. if (fnt_header->noffset != 0)
  193. {
  194. fnt->offset = rtgui_malloc (fnt_header->noffset * sizeof(rt_uint32_t));
  195. if (fnt->offset == RT_NULL) goto __exit;
  196. for (index = 0; index < fnt_header->noffset; index ++)
  197. {
  198. if (!readshort(fd, &(fnt->offset[index]))) goto __exit;
  199. }
  200. }
  201. if (fnt_header->nwidth != 0)
  202. {
  203. fnt->width = rtgui_malloc (fnt_header->nwidth * sizeof(rt_uint8_t));
  204. if (fnt->width == RT_NULL) goto __exit;
  205. for (index = 0; index < fnt_header->nwidth; index ++)
  206. {
  207. if (!readbyte(fd, &(fnt->width[index]))) goto __exit;
  208. }
  209. }
  210. close(fd);
  211. font->family = rt_strdup(font_family);
  212. font->height = fnt->header.height;
  213. font->refer_count = 0;
  214. font->engine = &fnt_font_engine;
  215. /* add to system */
  216. rtgui_font_system_add_font(font);
  217. return font;
  218. __exit:
  219. if (fd >= 0) close(fd);
  220. if (fnt != RT_NULL)
  221. {
  222. if (fnt->bits != RT_NULL) rtgui_free(fnt->bits);
  223. if (fnt->offset != RT_NULL) rtgui_free(fnt->offset);
  224. if (fnt->width != RT_NULL) rtgui_free(fnt->width);
  225. rtgui_free(fnt);
  226. }
  227. if (font != RT_NULL)
  228. {
  229. rtgui_free(font);
  230. }
  231. return RT_NULL;
  232. }
  233. #endif