glyph.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * guilib - a minimal pixel framework
  3. *
  4. * Copyright (c) 2009 Openmoko Inc.
  5. *
  6. * Authors Daniel Mack <daniel@caiaq.de>
  7. * Holger Hans Peter Freyther <zecke@openmoko.org>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <string.h>
  23. /* wiki-lib includes */
  24. #include <wikilib.h>
  25. #include <file-io.h>
  26. /* gui-lib includes */
  27. #include "guilib.h"
  28. #include "glyph.h"
  29. #include <regs.h>
  30. #include <lcd.h>
  31. #include <samo.h>
  32. #include "lcd_buf_draw.h"
  33. #include "search.h"
  34. #include "msg.h"
  35. #define DBG_GLYPH 0
  36. void render_glyph(int start_x, int start_y, const struct glyph *glyph, char *buf)
  37. {
  38. int x, y, w, bit = 0;
  39. const char *d = glyph->data;
  40. for (y = start_y; y < start_y + glyph->height; y++) {
  41. for (x = start_x, w = glyph->width; w > 0;) {
  42. int use;
  43. unsigned byte = (x + LCD_VRAM_WIDTH_PIXELS * y) / 8;
  44. if (byte >= LCD_BUF_HEIGHT_PIXELS * LCD_BUF_WIDTH_BYTES)
  45. return;
  46. use = MIN(8 - (x % 8) , w);
  47. use = MIN(8 - bit, use);
  48. #ifdef DISPLAY_INVERTED
  49. buf[byte] &= ~((*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8));
  50. #else
  51. buf[byte] |= (*d << bit & (unsigned char)(0xff << (8 - use))) >> (x % 8);
  52. #endif
  53. bit += use;
  54. x += use;
  55. w -= use;
  56. if (bit == 8) {
  57. bit = 0;
  58. d++;
  59. }
  60. }
  61. }
  62. }
  63. int buf_render_string(char *buf, const int font, int start_x,
  64. int start_y, char *string, int text_length, int inverted)
  65. {
  66. int x;
  67. int width;
  68. long len = text_length;
  69. long lenLast = 0;
  70. long widthLast = 0;
  71. char *p = (char *)string;
  72. unsigned char *q;
  73. int nCharBytes;
  74. ucs4_t c;
  75. if (start_x < 0)
  76. width = 0;
  77. else
  78. width = start_x;
  79. while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
  80. {
  81. lenLast = len;
  82. widthLast = width;
  83. width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
  84. }
  85. if (width > LCD_BUF_WIDTH_PIXELS)
  86. {
  87. text_length -= lenLast;
  88. width = widthLast;
  89. }
  90. if (start_x < 0) // to be centered
  91. {
  92. start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
  93. if (start_x < 0)
  94. start_x = 0;
  95. }
  96. x = start_x;
  97. q = (unsigned char *)string;
  98. while (*q) {
  99. c = UTF8_to_UCS4(&q);
  100. x = buf_draw_bmf_char(buf, c,font-1,x,start_y, inverted);
  101. if(x<0)
  102. return 0;
  103. }
  104. return x;
  105. }
  106. int render_string(const int font, int start_x,
  107. int start_y, char *string, int text_length, int inverted)
  108. {
  109. int x;
  110. int width;
  111. long len = text_length;
  112. long lenLast = 0;
  113. long widthLast = 0;
  114. char *p = (char *)string;
  115. unsigned char *q;
  116. int nCharBytes;
  117. ucs4_t c;
  118. if (start_x < 0)
  119. width = 0;
  120. else
  121. width = start_x;
  122. while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
  123. {
  124. lenLast = len;
  125. widthLast = width;
  126. width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
  127. }
  128. if (width > LCD_BUF_WIDTH_PIXELS)
  129. {
  130. text_length -= lenLast;
  131. width = widthLast;
  132. }
  133. if (start_x < 0) // to be centered
  134. {
  135. start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
  136. if (start_x < 0)
  137. start_x = 0;
  138. }
  139. x = start_x;
  140. q = (unsigned char *)string;
  141. while (*q) {
  142. c = UTF8_to_UCS4(&q);
  143. x = draw_bmf_char(c,font-1,x,start_y, inverted, 0);
  144. if(x<0)
  145. return 0;
  146. }
  147. return x;
  148. }
  149. int render_string_and_clear(const int font, int start_x,
  150. int start_y, char *string, int text_length, int inverted,
  151. int clear_start_x, int clear_start_y, int clear_end_x, int clear_end_y)
  152. {
  153. int x;
  154. int width;
  155. int height;
  156. long len = text_length;
  157. long lenLast = 0;
  158. long widthLast = 0;
  159. char *p = (char *)string;
  160. unsigned char *q;
  161. int nCharBytes;
  162. ucs4_t c;
  163. if (clear_start_x >= 0 && clear_start_y < start_y)
  164. {
  165. if (clear_end_y < start_y)
  166. {
  167. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
  168. clear_start_x = -1; // no more area to clear
  169. }
  170. else
  171. {
  172. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, start_y - 1);
  173. clear_start_y = start_y;
  174. }
  175. }
  176. if (start_x < 0)
  177. width = 0;
  178. else
  179. width = start_x;
  180. height = GetFontLinespace(font);
  181. while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
  182. {
  183. lenLast = len;
  184. widthLast = width;
  185. width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
  186. }
  187. if (width > LCD_BUF_WIDTH_PIXELS)
  188. {
  189. text_length -= lenLast;
  190. width = widthLast;
  191. }
  192. if (start_x < 0) // to be centered
  193. {
  194. start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
  195. if (start_x < 0)
  196. start_x = 0;
  197. }
  198. if (clear_start_y < start_y + height)
  199. {
  200. if (clear_start_x < start_x)
  201. guilib_clear_area(clear_start_x, clear_start_y, start_x - 1,
  202. clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
  203. if (clear_end_x >= width)
  204. guilib_clear_area(width, clear_start_y, clear_end_x,
  205. clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
  206. if (clear_end_y >= start_y + height)
  207. clear_start_y = start_x + height;
  208. else
  209. clear_start_x = -1;
  210. }
  211. if (clear_start_x >= 0)
  212. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
  213. x = start_x;
  214. q = (unsigned char *)string;
  215. while (*q) {
  216. c = UTF8_to_UCS4(&q);
  217. x = draw_bmf_char(c,font-1,x,start_y, inverted, 1);
  218. if(x<0)
  219. return 0;
  220. }
  221. return x;
  222. }
  223. // if search string is longer than the LCD width, keep the right of it to fit
  224. int render_string_right(const int font, int start_x,
  225. int start_y, char *string, int text_length, int inverted)
  226. {
  227. int i;
  228. int x;
  229. int utf8_chars = 0;
  230. int widths[MAX_TITLE_SEARCH];
  231. int lens[MAX_TITLE_SEARCH];
  232. int width = start_x;
  233. long len = text_length;
  234. char *p = (char *)string;
  235. unsigned char *q;
  236. int nCharBytes;
  237. int rc;
  238. ucs4_t c;
  239. while (len > 0 && utf8_chars < MAX_TITLE_SEARCH)
  240. {
  241. lens[utf8_chars] = len;
  242. widths[utf8_chars] = get_UTF8_char_width(font, &p, &len, &nCharBytes);
  243. width += widths[utf8_chars];
  244. utf8_chars++;
  245. }
  246. rc = width;
  247. if (width > LCD_BUF_WIDTH_PIXELS)
  248. {
  249. int width_to_descrease = width - LCD_BUF_WIDTH_PIXELS;
  250. width = 0;
  251. for (i = 0; i < utf8_chars && width < width_to_descrease; i++)
  252. width += widths[i];
  253. if (i < utf8_chars)
  254. {
  255. string = &string[text_length - lens[i]];
  256. text_length = lens[i];
  257. }
  258. }
  259. x = start_x;
  260. q = (unsigned char *)string;
  261. while (*q) {
  262. c = UTF8_to_UCS4(&q);
  263. x = draw_bmf_char(c,font-1,x,start_y, inverted, 1);
  264. if(x<0)
  265. return 0;
  266. }
  267. guilib_clear_area(x, start_y, LCD_BUF_WIDTH_PIXELS - 1, start_y + GetFontLinespace(font));
  268. return rc;
  269. }