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 "string-extra.h"
33 #include "diacritic.h"
35 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
36 #define LCDFN(fn) lcd_ ## fn
37 #define FBFN(fn) fb_ ## fn
38 #define LCDM(ma) LCD_ ## ma
39 #define LCDNAME "lcd_"
43 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
44 /* Fill a rectangle with a gradient */
45 static void lcd_gradient_rect(int x1
, int x2
, int y
, unsigned h
,
46 int num_lines
, int cur_line
)
48 int old_pattern
= current_vp
->fg_pattern
;
54 step_mul
= (1 << 16) / (num_lines
);
55 int h_r
= RGB_UNPACK_RED(current_vp
->lss_pattern
);
56 int h_g
= RGB_UNPACK_GREEN(current_vp
->lss_pattern
);
57 int h_b
= RGB_UNPACK_BLUE(current_vp
->lss_pattern
);
58 int rstep
= (h_r
- RGB_UNPACK_RED(current_vp
->lse_pattern
)) * step_mul
;
59 int gstep
= (h_g
- RGB_UNPACK_GREEN(current_vp
->lse_pattern
)) * step_mul
;
60 int bstep
= (h_b
- RGB_UNPACK_BLUE(current_vp
->lse_pattern
)) * step_mul
;
61 h_r
= (h_r
<< 16) + (1 << 15);
62 h_g
= (h_g
<< 16) + (1 << 15);
63 h_b
= (h_b
<< 16) + (1 << 15);
66 h_r
-= cur_line
* rstep
;
67 h_g
-= cur_line
* gstep
;
68 h_b
-= cur_line
* bstep
;
72 for(count
= 0; count
< h
; count
++) {
73 current_vp
->fg_pattern
= LCD_RGBPACK(h_r
>> 16, h_g
>> 16, h_b
>> 16);
74 lcd_hline(x1
, x2
, y
+ count
);
80 current_vp
->fg_pattern
= old_pattern
;
84 /* put a string at a given pixel position, skipping first ofs pixel columns */
85 static void LCDFN(putsxyofs
)(int x
, int y
, int ofs
, const unsigned char *str
)
88 struct font
* pf
= font_get(current_vp
->font
);
89 int vp_flags
= current_vp
->flags
;
90 int rtl_next_non_diac_width
, last_non_diacritic_width
;
92 if ((vp_flags
& VP_FLAG_ALIGNMENT_MASK
) != 0)
96 LCDFN(getstringsize
)(str
, &w
, NULL
);
97 /* center takes precedence */
98 if (vp_flags
& VP_FLAG_ALIGN_CENTER
)
100 x
= ((current_vp
->width
- w
)/ 2) + x
;
106 x
= current_vp
->width
- w
- x
;
112 rtl_next_non_diac_width
= 0;
113 last_non_diacritic_width
= 0;
114 /* Mark diacritic and rtl flags for each character */
115 for (ucs
= bidi_l2v(str
, 1); *ucs
; ucs
++)
117 bool is_rtl
, is_diac
;
118 const unsigned char *bits
;
119 int width
, base_width
, drawmode
= 0, base_ofs
= 0;
120 const unsigned short next_ch
= ucs
[1];
122 if (x
>= current_vp
->width
)
125 is_diac
= is_diacritic(*ucs
, &is_rtl
);
127 /* Get proportional width and glyph bits */
128 width
= font_get_width(pf
, *ucs
);
130 /* Calculate base width */
133 /* Forward-seek the next non-diacritic character for base width */
136 if (!rtl_next_non_diac_width
)
138 const unsigned short *u
;
140 /* Jump to next non-diacritic char, and calc its width */
141 for (u
= &ucs
[1]; *u
&& is_diacritic(*u
, NULL
); u
++);
143 rtl_next_non_diac_width
= *u
? font_get_width(pf
, *u
) : 0;
145 base_width
= rtl_next_non_diac_width
;
149 rtl_next_non_diac_width
= 0; /* Mark */
156 last_non_diacritic_width
= width
;
158 base_width
= last_non_diacritic_width
;
169 /* XXX: Suggested by amiconn:
170 * This will produce completely wrong results if the original
171 * drawmode is DRMODE_COMPLEMENT. We need to pre-render the current
172 * character with all its diacritics at least (in mono) and then
173 * finally draw that. And we'll need an extra buffer that can hold
174 * one char's bitmap. Basically we can't just change the draw mode
175 * to something else irrespective of the original mode and expect
176 * the result to look as intended and with DRMODE_COMPLEMENT (which
177 * means XORing pixels), overdrawing this way will cause odd results
178 * if the diacritics and the base char both have common pixels set.
179 * So we need to combine the char and its diacritics in a temp
180 * buffer using OR, and then draw the final bitmap instead of the
181 * chars, without touching the drawmode
183 drawmode
= current_vp
->drawmode
;
184 current_vp
->drawmode
= DRMODE_FG
;
186 base_ofs
= (base_width
- width
) / 2;
189 bits
= font_get_bits(pf
, *ucs
);
190 LCDFN(mono_bitmap_part
)(bits
, ofs
, 0, width
, x
+ base_ofs
, y
,
191 width
- ofs
, pf
->height
);
195 current_vp
->drawmode
= drawmode
;
201 bool next_is_diacritic
= is_diacritic(next_ch
, &next_is_rtl
);
204 * LTR: Next char is not diacritic,
205 * RTL: Current char is non-diacritic and next char is diacritic */
206 if ((is_rtl
&& !is_diac
) ||
207 (!is_rtl
&& (!next_is_diacritic
|| next_is_rtl
)))
209 x
+= base_width
- ofs
;
216 /* put a string at a given pixel position */
217 void LCDFN(putsxy
)(int x
, int y
, const unsigned char *str
)
219 LCDFN(putsxyofs
)(x
, y
, 0, str
);
222 static void LCDFN(putsxyofs_style
)(int xpos
, int ypos
,
223 const unsigned char *str
, int style
,
224 int w
, int h
, int offset
)
226 int lastmode
= current_vp
->drawmode
;
227 int xrect
= xpos
+ MAX(w
- offset
, 0);
228 int x
= VP_IS_RTL(current_vp
) ? xpos
: xrect
;
229 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
230 int oldfgcolor
= current_vp
->fg_pattern
;
231 int oldbgcolor
= current_vp
->bg_pattern
;
232 current_vp
->drawmode
= DRMODE_SOLID
| ((style
& STYLE_INVERT
) ?
233 DRMODE_INVERSEVID
: 0);
234 if (style
& STYLE_COLORED
) {
235 if (current_vp
->drawmode
== DRMODE_SOLID
)
236 current_vp
->fg_pattern
= style
& STYLE_COLOR_MASK
;
238 current_vp
->bg_pattern
= style
& STYLE_COLOR_MASK
;
240 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
241 if (style
& STYLE_GRADIENT
) {
242 current_vp
->drawmode
= DRMODE_FG
;
243 lcd_gradient_rect(xpos
, current_vp
->width
, ypos
, h
,
244 NUMLN_UNPACK(style
), CURLN_UNPACK(style
));
245 current_vp
->fg_pattern
= current_vp
->lst_pattern
;
247 else if (style
& STYLE_COLORBAR
) {
248 current_vp
->drawmode
= DRMODE_FG
;
249 current_vp
->fg_pattern
= current_vp
->lss_pattern
;
250 lcd_fillrect(xpos
, ypos
, current_vp
->width
- xpos
, h
);
251 current_vp
->fg_pattern
= current_vp
->lst_pattern
;
254 lcd_fillrect(x
, ypos
, current_vp
->width
- xrect
, h
);
255 current_vp
->drawmode
= (style
& STYLE_INVERT
) ?
256 (DRMODE_SOLID
|DRMODE_INVERSEVID
) : DRMODE_SOLID
;
259 lcd_putsxyofs(xpos
, ypos
, offset
, str
);
260 current_vp
->fg_pattern
= oldfgcolor
;
261 current_vp
->bg_pattern
= oldbgcolor
;
263 current_vp
->drawmode
= DRMODE_SOLID
| ((style
& STYLE_INVERT
) ?
264 0 : DRMODE_INVERSEVID
);
265 LCDFN(fillrect
)(x
, ypos
, current_vp
->width
- xrect
, h
);
266 current_vp
->drawmode
^= DRMODE_INVERSEVID
;
268 LCDFN(putsxyofs
)(xpos
, ypos
, offset
, str
);
270 current_vp
->drawmode
= lastmode
;
273 /*** Line oriented text output ***/
275 /* put a string at a given char position */
276 void LCDFN(puts_style_offset
)(int x
, int y
, const unsigned char *str
,
277 int style
, int offset
)
279 int xpos
, ypos
, w
, h
;
280 LCDFN(scroll_stop_line
)(current_vp
, y
);
284 LCDFN(getstringsize
)(str
, &w
, &h
);
285 xpos
= x
* LCDFN(getstringsize
)(" ", NULL
, NULL
);
287 LCDFN(putsxyofs_style
)(xpos
, ypos
, str
, style
, w
, h
, offset
);
290 void LCDFN(puts
)(int x
, int y
, const unsigned char *str
)
292 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, 0);
295 /* Formatting version of LCDFN(puts) */
296 void LCDFN(putsf
)(int x
, int y
, const unsigned char *fmt
, ...)
301 vsnprintf(buf
, sizeof (buf
), fmt
, ap
);
303 LCDFN(puts
)(x
, y
, buf
);
306 void LCDFN(puts_style
)(int x
, int y
, const unsigned char *str
, int style
)
308 LCDFN(puts_style_offset
)(x
, y
, str
, style
, 0);
311 void LCDFN(puts_offset
)(int x
, int y
, const unsigned char *str
, int offset
)
313 LCDFN(puts_style_offset
)(x
, y
, str
, STYLE_DEFAULT
, offset
);
318 void LCDFN(puts_scroll_style_offset
)(int x
, int y
, const unsigned char *string
,
319 int style
, int offset
)
321 struct scrollinfo
* s
;
326 if ((unsigned)y
>= (unsigned)current_vp
->height
)
329 /* remove any previously scrolling line at the same location */
330 LCDFN(scroll_stop_line
)(current_vp
, y
);
332 if (LCDFN(scroll_info
).lines
>= LCDM(SCROLLABLE_LINES
)) return;
335 LCDFN(puts_style_offset
)(x
, y
, string
, style
, offset
);
337 LCDFN(getstringsize
)(string
, &w
, &h
);
339 if (current_vp
->width
- x
* 8 >= w
)
342 /* prepare scroll line */
343 s
= &LCDFN(scroll_info
).scroll
[LCDFN(scroll_info
).lines
];
344 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
;
347 strlcpy(s
->line
, string
, sizeof s
->line
);
350 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
352 /* scroll bidirectional or forward only depending on the string
354 if ( LCDFN(scroll_info
).bidir_limit
) {
355 s
->bidir
= s
->width
< (current_vp
->width
) *
356 (100 + LCDFN(scroll_info
).bidir_limit
) / 100;
361 if (!s
->bidir
) { /* add spaces if scrolling in the round */
362 strlcat(s
->line
, " ", sizeof s
->line
);
363 /* get new width incl. spaces */
364 s
->width
= LCDFN(getstringsize
)(s
->line
, &w
, &h
);
367 end
= strchr(s
->line
, '\0');
368 len
= sizeof s
->line
- (end
- s
->line
);
369 strlcpy(end
, string
, MIN(current_vp
->width
/2, len
));
374 s
->startx
= x
* LCDFN(getstringsize
)(" ", NULL
, NULL
);
377 LCDFN(scroll_info
).lines
++;
380 void LCDFN(puts_scroll
)(int x
, int y
, const unsigned char *string
)
382 LCDFN(puts_scroll_style
)(x
, y
, string
, STYLE_DEFAULT
);
385 void LCDFN(puts_scroll_style
)(int x
, int y
, const unsigned char *string
,
388 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, style
, 0);
391 void LCDFN(puts_scroll_offset
)(int x
, int y
, const unsigned char *string
,
394 LCDFN(puts_scroll_style_offset
)(x
, y
, string
, STYLE_DEFAULT
, offset
);
397 void LCDFN(scroll_fn
)(void)
400 struct scrollinfo
* s
;
403 struct viewport
* old_vp
= current_vp
;
405 for ( index
= 0; index
< LCDFN(scroll_info
).lines
; index
++ ) {
406 s
= &LCDFN(scroll_info
).scroll
[index
];
409 if (TIME_BEFORE(current_tick
, s
->start_tick
))
412 LCDFN(set_viewport
)(s
->vp
);
415 s
->offset
-= LCDFN(scroll_info
).step
;
417 s
->offset
+= LCDFN(scroll_info
).step
;
419 pf
= font_get(current_vp
->font
);
421 ypos
= s
->y
* pf
->height
;
423 if (s
->bidir
) { /* scroll bidirectional */
424 if (s
->offset
<= 0) {
425 /* at beginning of line */
428 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
430 if (s
->offset
>= s
->width
- (current_vp
->width
- xpos
)) {
432 s
->offset
= s
->width
- (current_vp
->width
- xpos
);
434 s
->start_tick
= current_tick
+ LCDFN(scroll_info
).delay
* 2;
438 /* scroll forward the whole time */
439 if (s
->offset
>= s
->width
)
440 s
->offset
%= s
->width
;
442 LCDFN(putsxyofs_style
)(xpos
, ypos
, s
->line
, s
->style
, s
->width
,
443 pf
->height
, s
->offset
);
444 LCDFN(update_viewport_rect
)(xpos
, ypos
, current_vp
->width
- xpos
,
447 LCDFN(set_viewport
)(old_vp
);