Merge branch 'master' into app-list-itemsize
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob95dda5d7e51e9f5849e3d368de7652f8393d6c91
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 <stdio.h>
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_"
40 #define MAIN_LCD
41 #endif
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;
49 int step_mul;
50 if (h == 0) return;
52 num_lines *= h;
53 cur_line *= h;
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);
64 if (cur_line)
66 h_r -= cur_line * rstep;
67 h_g -= cur_line * gstep;
68 h_b -= cur_line * bstep;
70 unsigned count;
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);
75 h_r -= rstep;
76 h_g -= gstep;
77 h_b -= bstep;
80 current_vp->fg_pattern = old_pattern;
82 #endif
85 * draws the borders of the current viewport
86 **/
87 void LCDFN(draw_border_viewport)(void)
89 LCDFN(drawrect)(0, 0, current_vp->width, current_vp->height);
93 * fills the rectangle formed by current_vp
94 **/
95 void LCDFN(fill_viewport)(void)
97 LCDFN(fillrect)(0, 0, current_vp->width, current_vp->height);
100 /* put a string at a given pixel position, skipping first ofs pixel columns */
101 static void LCDFN(putsxyofs)(int x, int y, int ofs, const unsigned char *str)
103 unsigned short *ucs;
104 struct font* pf = font_get(current_vp->font);
105 int vp_flags = current_vp->flags;
106 int rtl_next_non_diac_width, last_non_diacritic_width;
108 if ((vp_flags & VP_FLAG_ALIGNMENT_MASK) != 0)
110 int w;
112 LCDFN(getstringsize)(str, &w, NULL);
113 /* center takes precedence */
114 if (vp_flags & VP_FLAG_ALIGN_CENTER)
116 x = ((current_vp->width - w)/ 2) + x;
117 if (x < 0)
118 x = 0;
120 else
122 x = current_vp->width - w - x;
123 x += ofs;
124 ofs = 0;
128 rtl_next_non_diac_width = 0;
129 last_non_diacritic_width = 0;
130 /* Mark diacritic and rtl flags for each character */
131 for (ucs = bidi_l2v(str, 1); *ucs; ucs++)
133 bool is_rtl, is_diac;
134 const unsigned char *bits;
135 int width, base_width, drawmode = 0, base_ofs = 0;
136 const unsigned short next_ch = ucs[1];
138 if (x >= current_vp->width)
139 break;
141 is_diac = is_diacritic(*ucs, &is_rtl);
143 /* Get proportional width and glyph bits */
144 width = font_get_width(pf, *ucs);
146 /* Calculate base width */
147 if (is_rtl)
149 /* Forward-seek the next non-diacritic character for base width */
150 if (is_diac)
152 if (!rtl_next_non_diac_width)
154 const unsigned short *u;
156 /* Jump to next non-diacritic char, and calc its width */
157 for (u = &ucs[1]; *u && is_diacritic(*u, NULL); u++);
159 rtl_next_non_diac_width = *u ? font_get_width(pf, *u) : 0;
161 base_width = rtl_next_non_diac_width;
163 else
165 rtl_next_non_diac_width = 0; /* Mark */
166 base_width = width;
169 else
171 if (!is_diac)
172 last_non_diacritic_width = width;
174 base_width = last_non_diacritic_width;
177 if (ofs > width)
179 ofs -= width;
180 continue;
183 if (is_diac)
185 /* XXX: Suggested by amiconn:
186 * This will produce completely wrong results if the original
187 * drawmode is DRMODE_COMPLEMENT. We need to pre-render the current
188 * character with all its diacritics at least (in mono) and then
189 * finally draw that. And we'll need an extra buffer that can hold
190 * one char's bitmap. Basically we can't just change the draw mode
191 * to something else irrespective of the original mode and expect
192 * the result to look as intended and with DRMODE_COMPLEMENT (which
193 * means XORing pixels), overdrawing this way will cause odd results
194 * if the diacritics and the base char both have common pixels set.
195 * So we need to combine the char and its diacritics in a temp
196 * buffer using OR, and then draw the final bitmap instead of the
197 * chars, without touching the drawmode
199 drawmode = current_vp->drawmode;
200 current_vp->drawmode = DRMODE_FG;
202 base_ofs = (base_width - width) / 2;
205 bits = font_get_bits(pf, *ucs);
206 LCDFN(mono_bitmap_part)(bits, ofs, 0, width, x + base_ofs, y,
207 width - ofs, pf->height);
209 if (is_diac)
211 current_vp->drawmode = drawmode;
214 if (next_ch)
216 bool next_is_rtl;
217 bool next_is_diacritic = is_diacritic(next_ch, &next_is_rtl);
219 /* Increment if:
220 * LTR: Next char is not diacritic,
221 * RTL: Current char is non-diacritic and next char is diacritic */
222 if ((is_rtl && !is_diac) ||
223 (!is_rtl && (!next_is_diacritic || next_is_rtl)))
225 x += base_width - ofs;
226 ofs = 0;
232 /* put a string at a given pixel position */
233 void LCDFN(putsxy)(int x, int y, const unsigned char *str)
235 LCDFN(putsxyofs)(x, y, 0, str);
238 /* Formatting version of LCDFN(putsxy) */
239 void LCDFN(putsxyf)(int x, int y, const unsigned char *fmt, ...)
241 va_list ap;
242 char buf[256];
243 va_start(ap, fmt);
244 vsnprintf(buf, sizeof (buf), fmt, ap);
245 va_end(ap);
246 LCDFN(putsxy)(x, y, buf);
249 static void LCDFN(putsxyofs_style)(int xpos, int ypos,
250 const unsigned char *str, int style,
251 int w, int h, int offset)
253 int lastmode = current_vp->drawmode;
254 int xrect = xpos + MAX(w - offset, 0);
255 int x = VP_IS_RTL(current_vp) ? xpos : xrect;
256 int text_ypos = ypos;
257 text_ypos += h/2 - font_get(current_vp->font)->height/2;
258 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
259 int oldfgcolor = current_vp->fg_pattern;
260 int oldbgcolor = current_vp->bg_pattern;
261 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
262 DRMODE_INVERSEVID : 0);
263 if (style & STYLE_COLORED) {
264 if (current_vp->drawmode == DRMODE_SOLID)
265 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
266 else
267 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
269 current_vp->drawmode ^= DRMODE_INVERSEVID;
270 if (style & STYLE_GRADIENT) {
271 current_vp->drawmode = DRMODE_FG;
272 lcd_gradient_rect(xpos, current_vp->width, ypos, h,
273 NUMLN_UNPACK(style), CURLN_UNPACK(style));
274 current_vp->fg_pattern = current_vp->lst_pattern;
276 else if (style & STYLE_COLORBAR) {
277 current_vp->drawmode = DRMODE_FG;
278 current_vp->fg_pattern = current_vp->lss_pattern;
279 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
280 current_vp->fg_pattern = current_vp->lst_pattern;
282 else {
283 lcd_fillrect(x, ypos, current_vp->width - xrect, h);
284 current_vp->drawmode = (style & STYLE_INVERT) ?
285 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
287 if (str[0])
288 lcd_putsxyofs(xpos, text_ypos, offset, str);
289 current_vp->fg_pattern = oldfgcolor;
290 current_vp->bg_pattern = oldbgcolor;
291 #else
292 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
293 0 : DRMODE_INVERSEVID);
294 LCDFN(fillrect)(x, ypos, current_vp->width - xrect, h);
295 current_vp->drawmode ^= DRMODE_INVERSEVID;
296 if (str[0])
297 LCDFN(putsxyofs)(xpos, text_ypos, offset, str);
298 #endif
299 current_vp->drawmode = lastmode;
302 /*** Line oriented text output ***/
304 /* put a string at a given char position */
305 void LCDFN(puts_style_xyoffset)(int x, int y, const unsigned char *str,
306 int style, int x_offset, int y_offset)
308 int xpos, ypos, w, h;
309 LCDFN(scroll_stop_line)(current_vp, y);
310 if(!str)
311 return;
313 LCDFN(getstringsize)(str, &w, &h);
314 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
315 ypos = y * h + y_offset;
316 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, x_offset);
319 void LCDFN(puts_style_xyoffset_h)(int x, int y, const unsigned char *str,
320 int style, int x_offset, int y_offset, int height)
322 int xpos, ypos, w;
323 LCDFN(scroll_stop_line)(current_vp, y);
324 if(!str)
325 return;
327 LCDFN(getstringsize)(str, &w, NULL);
328 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
329 ypos = y * height + y_offset;
330 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, height, x_offset);
333 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
334 int style, int x_offset)
336 LCDFN(puts_style_xyoffset)(x, y, str, style, x_offset, 0);
339 void LCDFN(puts)(int x, int y, const unsigned char *str)
341 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
344 /* Formatting version of LCDFN(puts) */
345 void LCDFN(putsf)(int x, int y, const unsigned char *fmt, ...)
347 va_list ap;
348 char buf[256];
349 va_start(ap, fmt);
350 vsnprintf(buf, sizeof (buf), fmt, ap);
351 va_end(ap);
352 LCDFN(puts)(x, y, buf);
355 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
357 LCDFN(puts_style_offset)(x, y, str, style, 0);
360 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
362 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
365 /*** scrolling ***/
367 void LCDFN(puts_scroll_style_xyoffset_h)(int x, int y,
368 const unsigned char *string,
369 int style, int x_offset, int y_offset,
370 int height)
372 struct scrollinfo* s;
373 char *end;
374 int w, h;
375 int len;
377 if ((unsigned)y >= (unsigned)current_vp->height)
378 return;
380 /* remove any previously scrolling line at the same location */
381 LCDFN(scroll_stop_line)(current_vp, y);
383 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
384 if (!string)
385 return;
387 LCDFN(getstringsize)(string, &w, &h);
388 if (height > 0)
389 h = height;
390 LCDFN(puts_style_xyoffset_h)(x, y, string, style, x_offset, y_offset, h);
392 if (current_vp->width - x * 8 >= w)
393 return;
395 /* prepare scroll line */
396 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
397 s->start_tick = current_tick + LCDFN(scroll_info).delay;
398 s->style = style;
400 strlcpy(s->line, string, sizeof s->line);
402 /* get width */
403 s->width = LCDFN(getstringsize)(s->line, &w, NULL);
405 /* scroll bidirectional or forward only depending on the string
406 width */
407 if ( LCDFN(scroll_info).bidir_limit ) {
408 s->bidir = s->width < (current_vp->width) *
409 (100 + LCDFN(scroll_info).bidir_limit) / 100;
411 else
412 s->bidir = false;
414 if (!s->bidir) { /* add spaces if scrolling in the round */
415 strlcat(s->line, " ", sizeof s->line);
416 /* get new width incl. spaces */
417 s->width = LCDFN(getstringsize)(s->line, &w, NULL);
420 end = strchr(s->line, '\0');
421 len = sizeof s->line - (end - s->line);
422 strlcpy(end, string, MIN(current_vp->width/2, len));
424 s->vp = current_vp;
425 s->y = y;
426 s->offset = x_offset;
427 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
428 s->y_offset = y_offset;
429 s->backward = false;
430 s->height = h; /* string height if height is 0 */
432 LCDFN(scroll_info).lines++;
435 void LCDFN(puts_scroll_style_xyoffset)(int x, int y, const unsigned char *string,
436 int style, int x_offset, int y_offset)
438 LCDFN(puts_scroll_style_xyoffset_h)(x,y,string,style,x_offset,y_offset,0);
441 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
443 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
446 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
447 int style)
449 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
452 void LCDFN(puts_scroll_style_h)(int x, int y, const unsigned char *string,
453 int style, int height)
455 LCDFN(puts_scroll_style_offset_h)(x, y, string, style, 0, height);
458 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
459 int offset)
461 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
464 void LCDFN(scroll_fn)(void)
466 struct scrollinfo* s;
467 int index;
468 int xpos, ypos;
469 struct viewport* old_vp = current_vp;
470 bool makedelay;
472 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
473 s = &LCDFN(scroll_info).scroll[index];
475 /* check pause */
476 if (TIME_BEFORE(current_tick, s->start_tick))
477 continue;
479 LCDFN(set_viewport)(s->vp);
481 if (s->backward)
482 s->offset -= LCDFN(scroll_info).step;
483 else
484 s->offset += LCDFN(scroll_info).step;
486 xpos = s->startx;
487 ypos = s->y * s->height + s->y_offset;
489 makedelay = false;
490 if (s->bidir) { /* scroll bidirectional */
491 if (s->offset <= 0) {
492 /* at beginning of line */
493 s->offset = 0;
494 s->backward = false;
495 makedelay = true;
497 else if (s->offset >= s->width - (current_vp->width - xpos)) {
498 /* at end of line */
499 s->offset = s->width - (current_vp->width - xpos);
500 s->backward = true;
501 makedelay = true;
504 else {
505 /* scroll forward the whole time */
506 if (s->offset >= s->width) {
507 s->offset = 0;
508 makedelay = true;
512 if (makedelay)
513 s->start_tick = current_tick + LCDFN(scroll_info).delay +
514 LCDFN(scroll_info).ticks;
516 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
517 s->height, s->offset);
518 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
519 s->height);
521 LCDFN(set_viewport)(old_vp);
524 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
525 int style, int x_offset)
527 LCDFN(puts_scroll_style_xyoffset)(x, y, string, style, x_offset, 0);
530 void LCDFN(puts_scroll_style_offset_h)(int x, int y, const unsigned char *string,
531 int style, int x_offset, int height)
533 LCDFN(puts_scroll_style_xyoffset_h)(x, y, string, style, x_offset, 0, height);