1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2005 Dave Chapman
12 * Copyright (C) 2006 Shachar Liberman
13 * Offset text, scrolling
14 * Copyright (C) 2007 Nicolas Pennequin, Tom Ross, Ken Fazzone, Akio Idehara
15 * Color gradient background
16 * Copyright (C) 2009 Andrew Mahone
17 * Merged common LCD bitmap code
19 * Rockbox common bitmap LCD functions
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version 2
24 * of the License, or (at your option) any later version.
26 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
27 * KIND, either express or implied.
29 ****************************************************************************/
32 #include "diacritic.h"
34 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
35 #define LCDFN(fn) lcd_ ## fn
36 #define FBFN(fn) fb_ ## fn
37 #define LCDM(ma) LCD_ ## ma
38 #define LCDNAME "lcd_"
42 #define HAS_BACKDROP ((defined(MAIN_LCD) && LCD_DEPTH > 1) \
43 || (!defined(MAIN_LCD) && LCD_REMOTE_DEPTH > 1))
45 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
46 /* Fill a rectangle with a gradient */
47 static void lcd_gradient_rect(int x1
, int x2
, int y
, unsigned h
,
48 int num_lines
, int cur_line
)
50 int old_pattern
= current_vp
->fg_pattern
;
56 step_mul
= (1 << 16) / (num_lines
);
57 int h_r
= RGB_UNPACK_RED(current_vp
->lss_pattern
);
58 int h_g
= RGB_UNPACK_GREEN(current_vp
->lss_pattern
);
59 int h_b
= RGB_UNPACK_BLUE(current_vp
->lss_pattern
);
60 int rstep
= (h_r
- RGB_UNPACK_RED(current_vp
->lse_pattern
)) * step_mul
;
61 int gstep
= (h_g
- RGB_UNPACK_GREEN(current_vp
->lse_pattern
)) * step_mul
;
62 int bstep
= (h_b
- RGB_UNPACK_BLUE(current_vp
->lse_pattern
)) * step_mul
;
63 h_r
= (h_r
<< 16) + (1 << 15);
64 h_g
= (h_g
<< 16) + (1 << 15);
65 h_b
= (h_b
<< 16) + (1 << 15);
68 h_r
-= cur_line
* rstep
;
69 h_g
-= cur_line
* gstep
;
70 h_b
-= cur_line
* bstep
;
74 for(count
= 0; count
< h
; count
++) {
75 current_vp
->fg_pattern
= LCD_RGBPACK(h_r
>> 16, h_g
>> 16, h_b
>> 16);
76 lcd_hline(x1
, x2
, y
+ count
);
82 current_vp
->fg_pattern
= old_pattern
;
86 /* put a string at a given pixel position, skipping first ofs pixel columns */
87 static void LCDFN(putsxyofs
)(int x
, int y
, int ofs
, const unsigned char *str
)
90 struct font
* pf
= font_get(current_vp
->font
);
91 int vp_flags
= current_vp
->flags
;
92 int rtl_next_non_diac_width
, last_non_diacritic_width
;
94 if ((vp_flags
& VP_FLAG_ALIGNMENT_MASK
) != 0)
98 LCDFN(getstringsize
)(str
, &w
, NULL
);
99 /* center takes precedence */
100 if (vp_flags
& VP_FLAG_ALIGN_CENTER
)
102 x
= ((current_vp
->width
- w
)/ 2) + x
;
108 x
= current_vp
->width
- w
- x
;
114 rtl_next_non_diac_width
= 0;
115 last_non_diacritic_width
= 0;
116 /* Mark diacritic and rtl flags for each character */
117 for (ucs
= bidi_l2v(str
, 1); *ucs
; ucs
++)
119 bool is_rtl
, is_diac
;
120 const unsigned char *bits
;
121 int width
, base_width
, drawmode
= 0, base_ofs
= 0;
122 const unsigned short next_ch
= ucs
[1];
124 if (x
>= current_vp
->width
)
127 is_diac
= is_diacritic(*ucs
, &is_rtl
);
129 /* Get proportional width and glyph bits */
130 width
= font_get_width(pf
, *ucs
);
132 /* Calculate base width */
135 /* Forward-seek the next non-diacritic character for base width */
138 if (!rtl_next_non_diac_width
)
140 const unsigned short *u
;
142 /* Jump to next non-diacritic char, and calc its width */
143 for (u
= &ucs
[1]; *u
&& is_diacritic(*u
, NULL
); u
++);
145 rtl_next_non_diac_width
= *u
? font_get_width(pf
, *u
) : 0;
147 base_width
= rtl_next_non_diac_width
;
151 rtl_next_non_diac_width
= 0; /* Mark */
158 last_non_diacritic_width
= width
;
160 base_width
= last_non_diacritic_width
;
171 /* XXX: Suggested by amiconn:
172 * This will produce completely wrong results if the original
173 * drawmode is DRMODE_COMPLEMENT. We need to pre-render the current
174 * character with all its diacritics at least (in mono) and then
175 * finally draw that. And we'll need an extra buffer that can hold
176 * one char's bitmap. Basically we can't just change the draw mode
177 * to something else irrespective of the original mode and expect
178 * the result to look as intended and with DRMODE_COMPLEMENT (which
179 * means XORing pixels), overdrawing this way will cause odd results
180 * if the diacritics and the base char both have common pixels set.
181 * So we need to combine the char and its diacritics in a temp
182 * buffer using OR, and then draw the final bitmap instead of the
183 * chars, without touching the drawmode
185 drawmode
= current_vp
->drawmode
;
186 current_vp
->drawmode
= DRMODE_FG
;
188 base_ofs
= (base_width
- width
) / 2;
191 bits
= font_get_bits(pf
, *ucs
);
192 LCDFN(mono_bitmap_part
)(bits
, ofs
, 0, width
, x
+ base_ofs
, y
,
193 width
- ofs
, pf
->height
);
197 current_vp
->drawmode
= drawmode
;
203 bool next_is_diacritic
= is_diacritic(next_ch
, &next_is_rtl
);
206 * LTR: Next char is not diacritic,
207 * RTL: Current char is non-diacritic and next char is diacritic */
208 if ((is_rtl
&& !is_diac
) ||
209 (!is_rtl
&& (!next_is_diacritic
|| next_is_rtl
)))
211 x
+= base_width
- ofs
;
218 /* put a string at a given pixel position */
219 void LCDFN(putsxy
)(int x
, int y
, const unsigned char *str
)
221 LCDFN(putsxyofs
)(x
, y
, 0, str
);
224 static void LCDFN(putsxyofs_style
)(int xpos
, int ypos
,
225 const unsigned char *str
, int style
,
226 int w
, int h
, int offset
)
228 int lastmode
= current_vp
->drawmode
;
229 int xrect
= xpos
+ MAX(w
- offset
, 0);
230 int x
= VP_IS_RTL(current_vp
) ? xpos
: xrect
;
231 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
232 int oldfgcolor
= current_vp
->fg_pattern
;
233 int oldbgcolor
= current_vp
->bg_pattern
;
234 current_vp
->drawmode
= DRMODE_SOLID
| ((style
& STYLE_INVERT
) ?
235 DRMODE_INVERSEVID
: 0);
236 if (style
& STYLE_COLORED
) {
237 if (current_vp
->drawmode
== DRMODE_SOLID
)
238 current_vp
->fg_pattern
= style
& STYLE_COLOR_MASK
;
240 current_vp
->bg_pattern
= style
& STYLE_COLOR_MASK
;
242 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
243 if (style
& STYLE_GRADIENT
) {
244 current_vp
->drawmode
= DRMODE_FG
;
245 lcd_gradient_rect(xpos
, current_vp
->width
, ypos
, h
,
246 NUMLN_UNPACK(style
), CURLN_UNPACK(style
));
247 current_vp
->fg_pattern
= current_vp
->lst_pattern
;
249 else if (style
& STYLE_COLORBAR
) {
250 current_vp
->drawmode
= DRMODE_FG
;
251 current_vp
->fg_pattern
= current_vp
->lss_pattern
;
252 lcd_fillrect(xpos
, ypos
, current_vp
->width
- xpos
, h
);
253 current_vp
->fg_pattern
= current_vp
->lst_pattern
;
256 lcd_fillrect(x
, ypos
, current_vp
->width
- xrect
, h
);
257 current_vp
->drawmode
= (style
& STYLE_INVERT
) ?
258 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
261 lcd_putsxyofs(xpos
, ypos
, offset
, str
);
262 current_vp
->fg_pattern
= oldfgcolor
;
263 current_vp
->bg_pattern
= oldbgcolor
;
265 current_vp
->drawmode
= DRMODE_SOLID
| ((style
& STYLE_INVERT
) ?
266 0 : DRMODE_INVERSEVID
);
267 LCDFN(fillrect
)(x
, ypos
, current_vp
->width
- xrect
, h
);
268 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
270 LCDFN(putsxyofs
)(xpos
, ypos
, offset
, str
);
272 current_vp
->drawmode
= lastmode
;
275 /*** Line oriented text output ***/
277 /* put a string at a given char position */
278 void LCDFN(puts_style_offset
)(int x
, int y
, const unsigned char *str
,
279 int style
, int offset
)
281 int xpos
, ypos
, w
, h
;
282 LCDFN(scroll_stop_line
)(current_vp
, y
);
286 LCDFN(getstringsize
)(str
, &w
, &h
);
287 xpos
= x
* LCDFN(getstringsize
)(" ", NULL
, NULL
);
289 LCDFN(putsxyofs_style
)(xpos
, ypos
, str
, style
, w
, h
, offset
);
292 void LCDFN(puts
)(int x
, int y
, const unsigned char *str
)
294 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, 0);
297 /* Formatting version of LCDFN(puts) */
298 void LCDFN(putsf
)(int x
, int y
, const unsigned char *fmt
, ...)
303 vsnprintf(buf
, sizeof (buf
), fmt
, ap
);
305 LCDFN(puts
)(x
, y
, buf
);
308 void LCDFN(puts_style
)(int x
, int y
, const unsigned char *str
, int style
)
310 LCDFN(puts_style_offset
)(x
, y
, str
, style
, 0);
313 void LCDFN(puts_offset
)(int x
, int y
, const unsigned char *str
, int offset
)
315 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, offset
);
320 void LCDFN(puts_scroll_style_offset
)(int x
, int y
, const unsigned char *string
,
321 int style
, int offset
)
323 struct scrollinfo
* s
;
327 if ((unsigned)y
>= (unsigned)current_vp
->height
)
330 /* remove any previously scrolling line at the same location */
331 LCDFN(scroll_stop_line
)(current_vp
, y
);
333 if (LCDFN(scroll_info
).lines
>= LCDM(SCROLLABLE_LINES
)) return;
336 LCDFN(puts_style_offset
)(x
, y
, string
, style
, offset
);
338 LCDFN(getstringsize
)(string
, &w
, &h
);
340 if (current_vp
->width
- x
* 8 >= w
)
343 /* prepare scroll line */
344 s
= &LCDFN(scroll_info
).scroll
[LCDFN(scroll_info
).lines
];
345 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
;
348 strlcpy(s
->line
, string
, sizeof s
->line
);
351 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
353 /* scroll bidirectional or forward only depending on the string
355 if ( LCDFN(scroll_info
).bidir_limit
) {
356 s
->bidir
= s
->width
< (current_vp
->width
) *
357 (100 + LCDFN(scroll_info
).bidir_limit
) / 100;
362 if (!s
->bidir
) { /* add spaces if scrolling in the round */
363 strcat(s
->line
, " ");
364 /* get new width incl. spaces */
365 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
368 end
= strchr(s
->line
, '\0');
369 strlcpy(end
, string
, current_vp
->width
/2);
373 s
->len
= utf8length(string
);
375 s
->startx
= x
* LCDFN(getstringsize
)(" ", NULL
, NULL
);
378 s
->backdrop
= (char*)LCDFN(get_backdrop());
381 LCDFN(scroll_info
).lines
++;
384 void LCDFN(puts_scroll
)(int x
, int y
, const unsigned char *string
)
386 LCDFN(puts_scroll_style
)(x
, y
, string
, STYLE_DEFAULT
);
389 void LCDFN(puts_scroll_style
)(int x
, int y
, const unsigned char *string
,
392 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, style
, 0);
395 void LCDFN(puts_scroll_offset
)(int x
, int y
, const unsigned char *string
,
398 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, STYLE_DEFAULT
, offset
);
401 void LCDFN(scroll_fn
)(void)
404 struct scrollinfo
* s
;
407 struct viewport
* old_vp
= current_vp
;
409 FBFN(data
*) old_backdrop
= LCDFN(get_backdrop
)();
412 for ( index
= 0; index
< LCDFN(scroll_info
).lines
; index
++ ) {
413 s
= &LCDFN(scroll_info
).scroll
[index
];
416 if (TIME_BEFORE(current_tick
, s
->start_tick
))
419 LCDFN(set_viewport
)(s
->vp
);
421 LCDFN(set_backdrop
)((FBFN(data
*))s
->backdrop
);
424 s
->offset
-= LCDFN(scroll_info
).step
;
426 s
->offset
+= LCDFN(scroll_info
).step
;
428 pf
= font_get(current_vp
->font
);
430 ypos
= s
->y
* pf
->height
;
432 if (s
->bidir
) { /* scroll bidirectional */
433 if (s
->offset
<= 0) {
434 /* at beginning of line */
437 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
439 if (s
->offset
>= s
->width
- (current_vp
->width
- xpos
)) {
441 s
->offset
= s
->width
- (current_vp
->width
- xpos
);
443 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
447 /* scroll forward the whole time */
448 if (s
->offset
>= s
->width
)
449 s
->offset
%= s
->width
;
451 LCDFN(putsxyofs_style
)(xpos
, ypos
, s
->line
, s
->style
, s
->width
,
452 pf
->height
, s
->offset
);
453 LCDFN(update_viewport_rect
)(xpos
, ypos
, current_vp
->width
- xpos
,
457 LCDFN(set_backdrop
)(old_backdrop
);
459 LCDFN(set_viewport
)(old_vp
);