glyph.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 render_string(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 = draw_bmf_char(c,font-1,x,start_y, inverted, 0);
  101. if(x<0)
  102. return 0;
  103. }
  104. return x;
  105. }
  106. int render_string_and_clear(const int font, int start_x,
  107. int start_y, char *string, int text_length, int inverted,
  108. int clear_start_x, int clear_start_y, int clear_end_x, int clear_end_y)
  109. {
  110. int x;
  111. int width;
  112. int height;
  113. long len = text_length;
  114. long lenLast = 0;
  115. long widthLast = 0;
  116. char *p = (char *)string;
  117. unsigned char *q;
  118. int nCharBytes;
  119. ucs4_t c;
  120. if (clear_start_x >= 0 && clear_start_y < start_y)
  121. {
  122. if (clear_end_y < start_y)
  123. {
  124. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
  125. clear_start_x = -1; // no more area to clear
  126. }
  127. else
  128. {
  129. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, start_y - 1);
  130. clear_start_y = start_y;
  131. }
  132. }
  133. if (start_x < 0)
  134. width = 0;
  135. else
  136. width = start_x;
  137. height = GetFontLinespace(font);
  138. while (len > 0 && width < LCD_BUF_WIDTH_PIXELS)
  139. {
  140. lenLast = len;
  141. widthLast = width;
  142. width += get_UTF8_char_width(font, &p, &len, &nCharBytes);
  143. }
  144. if (width > LCD_BUF_WIDTH_PIXELS)
  145. {
  146. text_length -= lenLast;
  147. width = widthLast;
  148. }
  149. if (start_x < 0) // to be centered
  150. {
  151. start_x = (LCD_BUF_WIDTH_PIXELS - width) / 2;
  152. if (start_x < 0)
  153. start_x = 0;
  154. }
  155. if (clear_start_y < start_y + height)
  156. {
  157. if (clear_start_x < start_x)
  158. guilib_clear_area(clear_start_x, clear_start_y, start_x - 1,
  159. clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
  160. if (clear_end_x >= width)
  161. guilib_clear_area(width, clear_start_y, clear_end_x,
  162. clear_end_y < start_x + height ? clear_end_y : start_x + height - 1);
  163. if (clear_end_y >= start_y + height)
  164. clear_start_y = start_x + height;
  165. else
  166. clear_start_x = -1;
  167. }
  168. if (clear_start_x >= 0)
  169. guilib_clear_area(clear_start_x, clear_start_y, clear_end_x, clear_end_y);
  170. x = start_x;
  171. q = (unsigned char *)string;
  172. while (*q) {
  173. c = UTF8_to_UCS4(&q);
  174. x = draw_bmf_char(c,font-1,x,start_y, inverted, 1);
  175. if(x<0)
  176. return 0;
  177. }
  178. return x;
  179. }
  180. // if search string is longer than the LCD width, keep the right of it to fit
  181. int render_string_right(const int font, int start_x,
  182. int start_y, char *string, int text_length, int inverted)
  183. {
  184. int i;
  185. int x;
  186. int utf8_chars = 0;
  187. int widths[MAX_TITLE_SEARCH];
  188. int lens[MAX_TITLE_SEARCH];
  189. int width = start_x;
  190. long len = text_length;
  191. char *p = (char *)string;
  192. unsigned char *q;
  193. int nCharBytes;
  194. int rc;
  195. ucs4_t c;
  196. while (len > 0 && utf8_chars < MAX_TITLE_SEARCH)
  197. {
  198. lens[utf8_chars] = len;
  199. widths[utf8_chars] = get_UTF8_char_width(font, &p, &len, &nCharBytes);
  200. width += widths[utf8_chars];
  201. utf8_chars++;
  202. }
  203. rc = width;
  204. if (width > LCD_BUF_WIDTH_PIXELS)
  205. {
  206. int width_to_descrease = width - LCD_BUF_WIDTH_PIXELS;
  207. width = 0;
  208. for (i = 0; i < utf8_chars && width < width_to_descrease; i++)
  209. width += widths[i];
  210. if (i < utf8_chars)
  211. {
  212. string = &string[text_length - lens[i]];
  213. text_length = lens[i];
  214. }
  215. }
  216. x = start_x;
  217. q = (unsigned char *)string;
  218. while (*q) {
  219. c = UTF8_to_UCS4(&q);
  220. x = draw_bmf_char(c,font-1,x,start_y, inverted, 0);
  221. if(x<0)
  222. return 0;
  223. }
  224. return rc;
  225. }