lcd_buf_draw.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 100
  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. #define LANGUAGE_LINK_WIDTH 23
  153. #define LANGUAGE_LINK_HEIGHT 21
  154. #define LANGUAGE_LINK_WIDTH_GAP 6
  155. #define LANGUAGE_LINK_HEIGHT_GAP 6
  156. // The italic fonts may not define the bitmaps for all characters.
  157. // The characters that are minssing in the italic fonts will try to return the bitmaps of it's supplement_font file.
  158. // The supplement_font file is assigned in init_lcd_draw_buf().
  159. #define ITALIC_FONT_IDX 1
  160. #define DEFAULT_FONT_IDX 2
  161. #define TITLE_FONT_IDX 3
  162. #define SUBTITLE_FONT_IDX 4
  163. // The above are the primary fonts for the article body. The index of any one of them needs to be under 7.
  164. #define DEFAULT_ALL_FONT_IDX 5
  165. #define TITLE_ALL_FONT_IDX 6
  166. #define SUBTITLE_ALL_FONT_IDX 7
  167. #define SEARCH_HEADING_FONT_IDX TITLE_FONT_IDX
  168. #define SEARCH_LIST_FONT_IDX DEFAULT_FONT_IDX
  169. #define MESSAGE_FONT_IDX BOLD_FONT_IDX
  170. #define H2_FONT_IDX SUBTITLE_FONT_IDX
  171. #define H3_FONT_IDX SUBTITLE_FONT_IDX
  172. #define H4_FONT_IDX SUBTITLE_FONT_IDX
  173. #define H5_FONT_IDX SUBTITLE_FONT_IDX
  174. #define LICENSE_TEXT_FONT ITALIC_FONT_IDX
  175. #define FILE_BUFFER_SIZE (512 * 1024)
  176. typedef struct _LCD_DRAW_BUF
  177. {
  178. unsigned char *screen_buf;
  179. long current_x;
  180. long current_y;
  181. pcffont_bmf_t *pPcfFont;
  182. int drawing;
  183. int line_height;
  184. int actual_height;
  185. int align_adjustment;
  186. int vertical_adjustment;
  187. } LCD_DRAW_BUF;
  188. #define MAX_RESULT_LIST 512
  189. #define MAX_ARTICLE_LINKS 2048
  190. #define MAX_EXTERNAL_LINKS 128
  191. #define SPACE_BEFORE_LICENSE_TEXT 40
  192. #define SPACE_AFTER_LICENSE_TEXT 5
  193. #define MAX_ARTICLES_PER_COMPRESSION 64
  194. /* Structure of a single article in a file with mutiple articles */
  195. /* byte 0~3: (long) offset from the beginning of the article header to the start of article text */
  196. /* byte 4~5: (short) number of ARTICLE_LINK blocks */
  197. /* byte 6~7: (short) number of EXTERNAL_LINK blocks */
  198. /* section for a number of ARTICLE_LINK blocks */
  199. /* section for a number of EXTERNAL_LINK blocks */
  200. /* section for a number of external link strings */
  201. /* section for the article string */
  202. typedef struct __attribute__ ((packed)) _ARTICLE_HEADER
  203. {
  204. uint32_t offset_article;
  205. uint16_t article_link_count;
  206. uint16_t external_link_count;
  207. } ARTICLE_HEADER;
  208. typedef struct __attribute__ ((packed)) _ARTICLE_LINK
  209. {
  210. uint32_t start_xy; /* byte 0: x; byte 1~3: y */
  211. uint32_t end_xy;
  212. uint32_t article_id;
  213. } ARTICLE_LINK;
  214. typedef struct __attribute__ ((packed)) _EXTERNAL_LINK
  215. {
  216. char *link_str;
  217. } EXTERNAL_LINK;
  218. typedef struct __attribute__ ((packed)) _CONCAT_ARTICLE_INFO
  219. {
  220. uint32_t article_id;
  221. uint32_t offset_article;
  222. uint32_t article_len;
  223. } CONCAT_ARTICLE_INFO;
  224. void init_lcd_draw_buf();
  225. char* FontFile(int id);
  226. ucs4_t UTF8_to_UCS4(unsigned char **);
  227. void buf_draw_UTF8_str(unsigned char **sUTF8);
  228. void buf_draw_horizontal_line(unsigned long start_x, unsigned long end_x);
  229. void buf_draw_vertical_line(unsigned long start_y, unsigned long end_y);
  230. void buf_draw_char(ucs4_t u);
  231. int get_UTF8_char_width(int idxFont, char **pContent, long *lenContent, int *nCharBytes);
  232. int render_article_with_pcf();
  233. int render_history_with_pcf();
  234. int render_wiki_selection_with_pcf();
  235. void restore_search_list_page(void);
  236. int render_search_result_with_pcf();
  237. void display_article_with_pcf(int y_move);
  238. void init_file_buffer();
  239. int div_wiki(int a,int b);
  240. int GetFontLinespace(int font);
  241. #ifndef WIKIPCF
  242. void init_render_article();
  243. void display_link_article(long idx_article);
  244. void display_retrieved_article(long idx_article);
  245. void display_str(unsigned char *str);
  246. void open_article_link(int x,int y);
  247. void open_article_link_with_link_number(int article_link_number);
  248. void scroll_article(void);
  249. int draw_bmf_char(ucs4_t u,int font,int x,int y, int inverted, int b_clear);
  250. int buf_draw_bmf_char(char *buf, ucs4_t u,int font,int x,int y, int inverted);
  251. int isArticleLinkSelected(int x,int y);
  252. int check_invert_link(void);
  253. void set_article_link_number(int num, unsigned long);
  254. void reset_article_link_number(void);
  255. int get_activated_article_link_number(void);
  256. void init_invert_link(void);
  257. void invert_link(int article_link_number);
  258. int load_init_article(long);
  259. void show_scroll_bar(int);
  260. #endif
  261. void msg_on_lcd(int x, int y, char *fmt, ...);
  262. void msg_on_lcd_clear(int x, int y);
  263. 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);
  264. int get_external_str_pixel_width(unsigned char *pUTF8, int font_idx);
  265. int extract_str_fitting_width(unsigned char **pIn, char *pOut, int width, int font_idx);
  266. void lcd_set_pixel(unsigned char *membuffer,int x, int y);
  267. void lcd_clear_pixel(unsigned char *membuffer,int x, int y);
  268. void lcd_set_framebuffer_pixel(int x, int y);
  269. void lcd_clear_framebuffer_pixel(int x, int y);
  270. void extract_title_from_article(unsigned char *article_buf, unsigned char *title);
  271. extern LCD_DRAW_BUF lcd_draw_buf;
  272. extern pcffont_bmf_t pcfFonts[FONT_COUNT];
  273. extern unsigned char *article_buf_pointer;
  274. #endif /* _LCD_BUF_DRAW_H */