font_fnt.c 7.4 KB

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