fix a bug where the backdrop from the sbs is displayed on scrolling lines in the...
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob24c5aa15cb44dd81e924243c8317773f692f05f1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2005 Dave Chapman
11 * Text rendering
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 ****************************************************************************/
30 #include "stdarg.h"
31 #include "sprintf.h"
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_"
39 #define MAIN_LCD
40 #endif
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;
51 int step_mul;
52 if (h == 0) return;
54 num_lines *= h;
55 cur_line *= h;
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);
66 if (cur_line)
68 h_r -= cur_line * rstep;
69 h_g -= cur_line * gstep;
70 h_b -= cur_line * bstep;
72 unsigned count;
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);
77 h_r -= rstep;
78 h_g -= gstep;
79 h_b -= bstep;
82 current_vp->fg_pattern = old_pattern;
84 #endif
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)
89 unsigned short *ucs;
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)
96 int w;
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;
103 if (x < 0)
104 x = 0;
106 else
108 x = current_vp->width - w - x;
109 x += ofs;
110 ofs = 0;
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)
125 break;
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 */
133 if (is_rtl)
135 /* Forward-seek the next non-diacritic character for base width */
136 if (is_diac)
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;
149 else
151 rtl_next_non_diac_width = 0; /* Mark */
152 base_width = width;
155 else
157 if (!is_diac)
158 last_non_diacritic_width = width;
160 base_width = last_non_diacritic_width;
163 if (ofs > width)
165 ofs -= width;
166 continue;
169 if (is_diac)
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);
195 if (is_diac)
197 current_vp->drawmode = drawmode;
200 if (next_ch)
202 bool next_is_rtl;
203 bool next_is_diacritic = is_diacritic(next_ch, &next_is_rtl);
205 /* Increment if:
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;
212 ofs = 0;
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;
239 else
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;
255 else {
256 lcd_fillrect(x, ypos, current_vp->width - xrect, h);
257 current_vp->drawmode = (style & STYLE_INVERT) ?
258 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
260 if (str[0])
261 lcd_putsxyofs(xpos, ypos, offset, str);
262 current_vp->fg_pattern = oldfgcolor;
263 current_vp->bg_pattern = oldbgcolor;
264 #else
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;
269 if (str[0])
270 LCDFN(putsxyofs)(xpos, ypos, offset, str);
271 #endif
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);
283 if(!str)
284 return;
286 LCDFN(getstringsize)(str, &w, &h);
287 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
288 ypos = y * h;
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, ...)
300 va_list ap;
301 char buf[256];
302 va_start(ap, fmt);
303 vsnprintf(buf, sizeof (buf), fmt, ap);
304 va_end(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);
318 /*** scrolling ***/
320 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
321 int style, int offset)
323 struct scrollinfo* s;
324 char *end;
325 int w, h;
327 if ((unsigned)y >= (unsigned)current_vp->height)
328 return;
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;
334 if (!string)
335 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)
341 return;
343 /* prepare scroll line */
344 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
345 s->start_tick = current_tick + LCDFN(scroll_info).delay;
346 s->style = style;
348 strlcpy(s->line, string, sizeof s->line);
350 /* get width */
351 s->width = LCDFN(getstringsize)(s->line, &w, &h);
353 /* scroll bidirectional or forward only depending on the string
354 width */
355 if ( LCDFN(scroll_info).bidir_limit ) {
356 s->bidir = s->width < (current_vp->width) *
357 (100 + LCDFN(scroll_info).bidir_limit) / 100;
359 else
360 s->bidir = false;
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);
371 s->vp = current_vp;
372 s->y = y;
373 s->len = utf8length(string);
374 s->offset = offset;
375 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
376 s->backward = false;
377 #if HAS_BACKDROP
378 s->backdrop = (char*)LCDFN(get_backdrop());
379 #endif
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,
390 int style)
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,
396 int offset)
398 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
401 void LCDFN(scroll_fn)(void)
403 struct font* pf;
404 struct scrollinfo* s;
405 int index;
406 int xpos, ypos;
407 struct viewport* old_vp = current_vp;
408 #if HAS_BACKDROP
409 FBFN(data*) old_backdrop = LCDFN(get_backdrop)();
410 #endif
412 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
413 s = &LCDFN(scroll_info).scroll[index];
415 /* check pause */
416 if (TIME_BEFORE(current_tick, s->start_tick))
417 continue;
419 LCDFN(set_viewport)(s->vp);
420 #if HAS_BACKDROP
421 LCDFN(set_backdrop)((FBFN(data*))s->backdrop);
422 #endif
423 if (s->backward)
424 s->offset -= LCDFN(scroll_info).step;
425 else
426 s->offset += LCDFN(scroll_info).step;
428 pf = font_get(current_vp->font);
429 xpos = s->startx;
430 ypos = s->y * pf->height;
432 if (s->bidir) { /* scroll bidirectional */
433 if (s->offset <= 0) {
434 /* at beginning of line */
435 s->offset = 0;
436 s->backward = false;
437 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
439 if (s->offset >= s->width - (current_vp->width - xpos)) {
440 /* at end of line */
441 s->offset = s->width - (current_vp->width - xpos);
442 s->backward = true;
443 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
446 else {
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,
454 pf->height);
456 #if HAS_BACKDROP
457 LCDFN(set_backdrop)(old_backdrop);
458 #endif
459 LCDFN(set_viewport)(old_vp);