lcd_buf_draw.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. * Copyright (c) 2009 Openmoko Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. Assumptions:
  19. 1. Support up to 9 fonts - the default font and font 0 to font 7. For
  20. example, the normal 12-pixel font is used as the default font.
  21. 2. There is a default line space (e.g. 16 pixels).
  22. 3. The characters in the line are horizontally aligned to the bottom
  23. of the line by default. Most characters got 2 blank pixel space at
  24. the bottom with some exceptions. For example, "_" only got one
  25. blank pixel line and "gjpqy" got no blank pixel space. A 12-pixel
  26. character actually takes 14 pixels from top to the bottom on
  27. Windows and I think it's a good practice. To save the storage, we
  28. can still bitmap them in 12 pixels, but the font API should be
  29. smart enough to return 14 pixels by padding with blank pixels at
  30. the top or the bottom.
  31. 4. Each character in the line is adjacent to the previous character
  32. without space. The font API should pad one blank pixel space at
  33. the right of the character. (In some Chinese fonts, the blank
  34. pixel space is at the left of the character and some characters
  35. leave no blank space at all. Since we are dealing with English
  36. wiki, it is less important.)
  37. 5. If characters overlap, the glyphs of all characters retain. (For
  38. example, space does not overwrite the overlapped character since it
  39. got no glyphs.)
  40. 6. New line is adjacent to the previous line.
  41. 7. The article content is coded in UTF-8. Some UTF-8 control
  42. characters are used as escape characters.
  43. 1 - escape character 0 (space line), the byte after it contains the
  44. pixels of the space line
  45. 2 - escape character 1 (new line with the default font and the default
  46. line space)
  47. 3 - escape character 2 (new line with the same font and line space as
  48. the previous line, not considering the font changes and
  49. adjustments)
  50. 4 - escape character 3 (new line with non-default font and line space)
  51. Definition of the byte after it:
  52. bit0~2 - font id (1 ~ 7)
  53. bit3~7 - pixels of the line space
  54. 5 - escape character 4 (change font)
  55. The font change (including alignment adjustment) is reset after
  56. the escape character 1, 3, 4 or 5.
  57. Definition of the byte after it:
  58. bit0~2 - font id (1 ~ 7)
  59. bit3~7 - adjustment to the default horizontal alignment
  60. (0: 0; 0x01~0x0F: 1~15; 0x10~0x1F: -16~-1)
  61. 6 - escape character 5 (reset to the default font)
  62. 7 - escape character 6 (reset to the default vertical alignment)
  63. 8 - escape character 7 (forward), the byte after it stands for the
  64. pixels of move right (as the starting point of the next character).
  65. The next character or drawing line starts at the position after the movement.
  66. The rightmost position after the movement is the rightmost pixel of the screen.
  67. 9 - escape character 8 (backward), the byte after it stands for the
  68. pixels to move left (from the original starting point of the next
  69. character).
  70. The next character or line starts at the position after the movement.
  71. The leftmost position after the movement is the leftmost pixel of the screen.
  72. 10 - escape character 9 (alignment vertical adjustment), the byte after
  73. it stand for the pixels to adjust to the default vertical
  74. alignment (0x01~0x7F: 1~127; 0x80~0x8F: -128~-1)
  75. It takes effect for all lines after that until another excape
  76. character 9 or escape character 6 is encountered.
  77. 11 - escape character 10 (drawing horizontal line from right to left,
  78. 1 pixel thick), the next byte after it stands for the length of
  79. the line in pixels.
  80. The line starts at 1 pixel left to the starting position of the
  81. next character.
  82. Move forward/backward or set the alignment adjustment before
  83. drawing lines as necessary.
  84. The horizontal line does not change the horizontal position of
  85. the next character.
  86. 12 - escape character 11 (drawing vertical line from bottom to top, 1
  87. pixel thick), the next byte after it stands for the length of the
  88. line in pixels.
  89. The vertical line starts at the current vertical position (the
  90. starting position of the next character) and the bottom of the
  91. current horizontal alignment.
  92. Move forward/backward or set the alignment adjustment before
  93. drawing lines as necessary.
  94. The horizontal line does not change the horizontal position of the next character.
  95. 13 - escape character 12 (single pixel horizontal line), the
  96. horizontal line pccupies from the left-most pixel to the
  97. right-most pixel.
  98. The line is 1 pixel high.
  99. 14 - escape character 13 (single pixel vertical line), the vertical
  100. line pccupies from the top to the bottom of the current line
  101. space.
  102. The vertical line pccupies 1 pixel width.
  103. 15 - escape character 14 (bitmap), the next byte after it stands for
  104. the width of the bitmap in pixels and the 3rd and 4th bytes sand for the height.
  105. */
  106. #ifndef _LCD_BUF_DRAW_H
  107. #define _LCD_BUF_DRAW_H
  108. #include "bmf.h"
  109. #ifndef WIKIPCF
  110. #include <lcd.h>
  111. #endif
  112. #define ESC_0_SPACE_LINE 1
  113. #define ESC_1_NEW_LINE_DEFAULT_FONT 2
  114. #define ESC_2_NEW_LINE_SAME_FONT 3
  115. #define ESC_3_NEW_LINE_WITH_FONT 4
  116. #define ESC_4_CHANGE_FONT 5
  117. #define ESC_5_RESET_TO_DEFAULT_FONT 6
  118. #define ESC_6_RESET_TO_DEFAULT_ALIGN 7
  119. #define ESC_7_FORWARD 8
  120. #define ESC_8_BACKWARD 9
  121. #define ESC_9_ALIGN_ADJUSTMENT 10
  122. #define ESC_10_HORIZONTAL_LINE 11
  123. #define ESC_11_VERTICAL_LINE 12
  124. #define ESC_12_FULL_HORIZONTAL_LINE 13
  125. #define ESC_13_FULL_VERTICAL_LINE 14
  126. #define ESC_14_BITMAP 15
  127. #define MAX_ESC_CHAR ESC_14_BITMAP
  128. #define LINE_SPACE_ADDON 1
  129. #define LCD_BUF_WIDTH_PIXELS 240
  130. #define LCD_LEFT_MARGIN 6
  131. #define LCD_EXTRA_LEFT_MARGIN_FOR_IMAGE 2
  132. #define LCD_TOP_MARGIN 6
  133. #define LCD_BUF_HEIGHT_PIXELS 128 * 1024
  134. #define LINK_X_DIFF_ALLOWANCE 10
  135. #define LINK_Y_DIFF_ALLOWANCE 5
  136. #define INITIAL_ARTICLE_SCROLL_THRESHOLD 8
  137. #define ARTICLE_MOVING_SCROLL_THRESHOLD 1
  138. #define LINK_DEACTIVATION_MOVE_THRESHOLD 1
  139. #define SMOOTH_SCROLL_ACTIVATION_OFFSET_LOW_THRESHOLD 10
  140. #define SMOOTH_SCROLL_ACTIVATION_OFFSET_HIGH_THRESHOLD 200
  141. #define SMOOTH_SCROLL_ACTIVATION_SPPED_THRESHOLD 50
  142. #define LIST_SMOOTH_SCROLL_SPEED_FACTOR 5
  143. #define ARTICLE_SMOOTH_SCROLL_SPEED_FACTOR 3
  144. #define LINK_ACTIVATION_TIME_THRESHOLD 0.1
  145. #ifdef WIKIPCF
  146. #define LCD_BUF_WIDTH_BYTES LCD_BUF_WIDTH_PIXELS/8
  147. #define LCD_HEIGHT_LINES 208
  148. #define LCD_VRAM_WIDTH_PIXELS 256
  149. #else
  150. #define LCD_BUF_WIDTH_BYTES LCD_VRAM_WIDTH_PIXELS/8
  151. #endif
  152. // The italic fonts may not define the bitmaps for all characters.
  153. // The characters that are minssing in the italic fonts will try to return the bitmaps in the next font file.
  154. // For example, return bitmaps in DEFAULT_FONT_IDX if not found in ITALIC_FONT_IDX.
  155. #define ITALIC_FONT_IDX 1
  156. #define DEFAULT_FONT_IDX 2
  157. #define TITLE_FONT_IDX 3
  158. #define SUBTITLE_FONT_IDX 4
  159. // The above are the primary fonts for the article body. The index of any one of them needs to be under 7.
  160. #define DEFAULT_ALL_FONT_IDX 5
  161. #define SEARCH_HEADING_FONT_IDX TITLE_FONT_IDX
  162. #define SEARCH_LIST_FONT_IDX DEFAULT_FONT_IDX
  163. #define MESSAGE_FONT_IDX BOLD_FONT_IDX
  164. #define H2_FONT_IDX SUBTITLE_FONT_IDX
  165. #define H3_FONT_IDX SUBTITLE_FONT_IDX
  166. #define H4_FONT_IDX SUBTITLE_FONT_IDX
  167. #define H5_FONT_IDX SUBTITLE_FONT_IDX
  168. #define LICENSE_TEXT_FONT ITALIC_FONT_IDX
  169. #define FILE_BUFFER_SIZE 386*1024
  170. typedef struct _LCD_DRAW_BUF
  171. {
  172. unsigned char *screen_buf;
  173. long current_x;
  174. long current_y;
  175. pcffont_bmf_t *pPcfFont;
  176. int drawing;
  177. int line_height;
  178. int actual_height;
  179. int align_adjustment;
  180. int vertical_adjustment;
  181. } LCD_DRAW_BUF;
  182. #define MAX_RESULT_LIST 512
  183. #define MAX_ARTICLE_LINKS 2048
  184. #define SPACE_BEFORE_LICENSE_TEXT 40
  185. #define SPACE_AFTER_LICENSE_TEXT 5
  186. /* Structure of a single article in a file with mutiple articles */
  187. /* byte 0~3: (long) offset from the beginning of the article header to the start of article text */
  188. /* byte 4~5: (short) number of ARTICLE_LINK blocks */
  189. /* byte 6~7: (short) number of EXTERNAL_LINK blocks */
  190. /* section for a number of ARTICLE_LINK blocks */
  191. /* section for a number of EXTERNAL_LINK blocks */
  192. /* section for a number of external link strings */
  193. /* section for the article string */
  194. typedef struct _ARTICLE_HEADER
  195. {
  196. uint32_t offset_article;
  197. uint16_t article_link_count;
  198. uint16_t external_link_count;
  199. } ARTICLE_HEADER;
  200. typedef struct _ARTICLE_LINK
  201. {
  202. uint32_t start_xy; /* byte 0: x; byte 1~3: y */
  203. uint32_t end_xy;
  204. uint32_t article_id;
  205. } ARTICLE_LINK;
  206. typedef struct _EXTERNAL_LINK
  207. {
  208. uint32_t start_xy;
  209. uint32_t end_xy;
  210. uint16_t offset_link_str;
  211. uint16_t link_str_len;
  212. } EXTERNAL_LINK;
  213. typedef struct _CONCAT_ARTICLE_INFO
  214. {
  215. unsigned long article_id;
  216. unsigned long offset_article;
  217. } CONCAT_ARTICLE_INFO;
  218. void init_lcd_draw_buf();
  219. char* FontFile(int id);
  220. ucs4_t UTF8_to_UCS4(unsigned char **);
  221. void buf_draw_UTF8_str(unsigned char **sUTF8);
  222. void buf_draw_horizontal_line(unsigned long start_x, unsigned long end_x);
  223. void buf_draw_vertical_line(unsigned long start_y, unsigned long end_y);
  224. void buf_draw_char(ucs4_t u);
  225. int get_UTF8_char_width(int idxFont, char **pContent, long *lenContent, int *nCharBytes);
  226. int render_article_with_pcf();
  227. int render_history_with_pcf();
  228. int render_wiki_selection_with_pcf();
  229. void restore_search_list_page(void);
  230. int render_search_result_with_pcf();
  231. void display_article_with_pcf(int y_move);
  232. void init_file_buffer();
  233. int div_wiki(int a,int b);
  234. int GetFontLinespace(int font);
  235. #ifndef WIKIPCF
  236. void init_render_article();
  237. void display_link_article(long idx_article);
  238. void display_retrieved_article(long idx_article);
  239. void display_str(unsigned char *str);
  240. void open_article_link(int x,int y);
  241. void open_article_link_with_link_number(int article_link_number);
  242. void scroll_article(void);
  243. int draw_bmf_char(ucs4_t u,int font,int x,int y, int inverted, int b_clear);
  244. int buf_draw_bmf_char(char *buf, ucs4_t u,int font,int x,int y, int inverted);
  245. int isArticleLinkSelected(int x,int y);
  246. int check_invert_link(void);
  247. void set_article_link_number(int num, unsigned long);
  248. void reset_article_link_number(void);
  249. int get_activated_article_link_number(void);
  250. void init_invert_link(void);
  251. void invert_link(int article_link_number);
  252. int load_init_article(long);
  253. void show_scroll_bar(int);
  254. #endif
  255. void msg_on_lcd(int x, int y, char *fmt, ...);
  256. void msg_on_lcd_clear(int x, int y);
  257. void buf_draw_UTF8_str_in_copy_buffer(char *framebuffer_copy,unsigned char **pUTF8,int start_x,int end_x,int start_y,int end_y,int offset_x,int font_idx);
  258. int get_external_str_pixel_width(unsigned char *pUTF8, int font_idx);
  259. int extract_str_fitting_width(unsigned char **pIn, char *pOut, int width, int font_idx);
  260. void lcd_set_pixel(unsigned char *membuffer,int x, int y);
  261. void lcd_set_framebuffer_pixel(int x, int y);
  262. void lcd_clear_framebuffer_pixel(int x, int y);
  263. void extract_title_from_article(unsigned char *title);
  264. extern LCD_DRAW_BUF lcd_draw_buf;
  265. extern pcffont_bmf_t pcfFonts[FONT_COUNT];
  266. extern unsigned char *article_buf_pointer;
  267. #endif /* _LCD_BUF_DRAW_H */