Remove solved issue
[maemo-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob364fb3c527a814d1f836245db83ffa03069d2081
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 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
257 int oldfgcolor = current_vp->fg_pattern;
258 int oldbgcolor = current_vp->bg_pattern;
259 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
260 DRMODE_INVERSEVID : 0);
261 if (style & STYLE_COLORED) {
262 if (current_vp->drawmode == DRMODE_SOLID)
263 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
264 else
265 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
267 current_vp->drawmode ^= DRMODE_INVERSEVID;
268 if (style & STYLE_GRADIENT) {
269 current_vp->drawmode = DRMODE_FG;
270 lcd_gradient_rect(xpos, current_vp->width, ypos, h,
271 NUMLN_UNPACK(style), CURLN_UNPACK(style));
272 current_vp->fg_pattern = current_vp->lst_pattern;
274 else if (style & STYLE_COLORBAR) {
275 current_vp->drawmode = DRMODE_FG;
276 current_vp->fg_pattern = current_vp->lss_pattern;
277 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
278 current_vp->fg_pattern = current_vp->lst_pattern;
280 else {
281 lcd_fillrect(x, ypos, current_vp->width - xrect, h);
282 current_vp->drawmode = (style & STYLE_INVERT) ?
283 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
285 if (str[0])
286 lcd_putsxyofs(xpos, ypos, offset, str);
287 current_vp->fg_pattern = oldfgcolor;
288 current_vp->bg_pattern = oldbgcolor;
289 #else
290 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
291 0 : DRMODE_INVERSEVID);
292 LCDFN(fillrect)(x, ypos, current_vp->width - xrect, h);
293 current_vp->drawmode ^= DRMODE_INVERSEVID;
294 if (str[0])
295 LCDFN(putsxyofs)(xpos, ypos, offset, str);
296 #endif
297 current_vp->drawmode = lastmode;
300 /*** Line oriented text output ***/
302 /* put a string at a given char position */
303 void LCDFN(puts_style_xyoffset)(int x, int y, const unsigned char *str,
304 int style, int x_offset, int y_offset)
306 int xpos, ypos, w, h;
307 LCDFN(scroll_stop_line)(current_vp, y);
308 if(!str)
309 return;
311 LCDFN(getstringsize)(str, &w, &h);
312 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
313 ypos = y * h + y_offset;
314 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, x_offset);
317 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
318 int style, int x_offset)
320 LCDFN(puts_style_xyoffset)(x, y, str, style, x_offset, 0);
323 void LCDFN(puts)(int x, int y, const unsigned char *str)
325 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
328 /* Formatting version of LCDFN(puts) */
329 void LCDFN(putsf)(int x, int y, const unsigned char *fmt, ...)
331 va_list ap;
332 char buf[256];
333 va_start(ap, fmt);
334 vsnprintf(buf, sizeof (buf), fmt, ap);
335 va_end(ap);
336 LCDFN(puts)(x, y, buf);
339 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
341 LCDFN(puts_style_offset)(x, y, str, style, 0);
344 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
346 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
349 /*** scrolling ***/
351 void LCDFN(puts_scroll_style_xyoffset)(int x, int y, const unsigned char *string,
352 int style, int x_offset, int y_offset)
354 struct scrollinfo* s;
355 char *end;
356 int w, h;
357 int len;
359 if ((unsigned)y >= (unsigned)current_vp->height)
360 return;
362 /* remove any previously scrolling line at the same location */
363 LCDFN(scroll_stop_line)(current_vp, y);
365 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
366 if (!string)
367 return;
368 LCDFN(puts_style_xyoffset)(x, y, string, style, x_offset, y_offset);
370 LCDFN(getstringsize)(string, &w, &h);
372 if (current_vp->width - x * 8 >= w)
373 return;
375 /* prepare scroll line */
376 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
377 s->start_tick = current_tick + LCDFN(scroll_info).delay;
378 s->style = style;
380 strlcpy(s->line, string, sizeof s->line);
382 /* get width */
383 s->width = LCDFN(getstringsize)(s->line, &w, &h);
385 /* scroll bidirectional or forward only depending on the string
386 width */
387 if ( LCDFN(scroll_info).bidir_limit ) {
388 s->bidir = s->width < (current_vp->width) *
389 (100 + LCDFN(scroll_info).bidir_limit) / 100;
391 else
392 s->bidir = false;
394 if (!s->bidir) { /* add spaces if scrolling in the round */
395 strlcat(s->line, " ", sizeof s->line);
396 /* get new width incl. spaces */
397 s->width = LCDFN(getstringsize)(s->line, &w, &h);
400 end = strchr(s->line, '\0');
401 len = sizeof s->line - (end - s->line);
402 strlcpy(end, string, MIN(current_vp->width/2, len));
404 s->vp = current_vp;
405 s->y = y;
406 s->offset = x_offset;
407 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
408 s->y_offset = y_offset;
409 s->backward = false;
411 LCDFN(scroll_info).lines++;
414 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
416 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
419 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
420 int style)
422 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
425 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
426 int offset)
428 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
431 void LCDFN(scroll_fn)(void)
433 struct font* pf;
434 struct scrollinfo* s;
435 int index;
436 int xpos, ypos;
437 struct viewport* old_vp = current_vp;
438 bool makedelay;
440 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
441 s = &LCDFN(scroll_info).scroll[index];
443 /* check pause */
444 if (TIME_BEFORE(current_tick, s->start_tick))
445 continue;
447 LCDFN(set_viewport)(s->vp);
449 if (s->backward)
450 s->offset -= LCDFN(scroll_info).step;
451 else
452 s->offset += LCDFN(scroll_info).step;
454 pf = font_get(current_vp->font);
455 xpos = s->startx;
456 ypos = s->y * pf->height + s->y_offset;
458 makedelay = false;
459 if (s->bidir) { /* scroll bidirectional */
460 if (s->offset <= 0) {
461 /* at beginning of line */
462 s->offset = 0;
463 s->backward = false;
464 makedelay = true;
466 else if (s->offset >= s->width - (current_vp->width - xpos)) {
467 /* at end of line */
468 s->offset = s->width - (current_vp->width - xpos);
469 s->backward = true;
470 makedelay = true;
473 else {
474 /* scroll forward the whole time */
475 if (s->offset >= s->width) {
476 s->offset = 0;
477 makedelay = true;
481 if (makedelay)
482 s->start_tick = current_tick + LCDFN(scroll_info).delay +
483 LCDFN(scroll_info).ticks;
485 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
486 pf->height, s->offset);
487 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
488 pf->height);
490 LCDFN(set_viewport)(old_vp);
493 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
494 int style, int x_offset)
496 LCDFN(puts_scroll_style_xyoffset)(x, y, string, style, x_offset, 0);