get rid of the filename in the delete bookmark confirmation as its not really helpful
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob78ceb848d5b1606def7e413b2eaa660c763477e6
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 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
43 /* Fill a rectangle with a gradient */
44 static void lcd_gradient_rect(int x1, int x2, int y, unsigned h,
45 int num_lines, int cur_line)
47 int old_pattern = current_vp->fg_pattern;
48 int step_mul;
49 if (h == 0) return;
51 num_lines *= h;
52 cur_line *= h;
53 step_mul = (1 << 16) / (num_lines);
54 int h_r = RGB_UNPACK_RED(current_vp->lss_pattern);
55 int h_g = RGB_UNPACK_GREEN(current_vp->lss_pattern);
56 int h_b = RGB_UNPACK_BLUE(current_vp->lss_pattern);
57 int rstep = (h_r - RGB_UNPACK_RED(current_vp->lse_pattern)) * step_mul;
58 int gstep = (h_g - RGB_UNPACK_GREEN(current_vp->lse_pattern)) * step_mul;
59 int bstep = (h_b - RGB_UNPACK_BLUE(current_vp->lse_pattern)) * step_mul;
60 h_r = (h_r << 16) + (1 << 15);
61 h_g = (h_g << 16) + (1 << 15);
62 h_b = (h_b << 16) + (1 << 15);
63 if (cur_line)
65 h_r -= cur_line * rstep;
66 h_g -= cur_line * gstep;
67 h_b -= cur_line * bstep;
69 unsigned count;
71 for(count = 0; count < h; count++) {
72 current_vp->fg_pattern = LCD_RGBPACK(h_r >> 16, h_g >> 16, h_b >> 16);
73 lcd_hline(x1, x2, y + count);
74 h_r -= rstep;
75 h_g -= gstep;
76 h_b -= bstep;
79 current_vp->fg_pattern = old_pattern;
81 #endif
83 /* put a string at a given pixel position, skipping first ofs pixel columns */
84 static void LCDFN(putsxyofs)(int x, int y, int ofs, const unsigned char *str)
86 unsigned short *ucs;
87 struct font* pf = font_get(current_vp->font);
88 int vp_flags = current_vp->flags;
89 int rtl_next_non_diac_width, last_non_diacritic_width;
91 if ((vp_flags & VP_FLAG_ALIGNMENT_MASK) != 0)
93 int w;
95 LCDFN(getstringsize)(str, &w, NULL);
96 /* center takes precedence */
97 if (vp_flags & VP_FLAG_ALIGN_CENTER)
99 x = ((current_vp->width - w)/ 2) + x;
100 if (x < 0)
101 x = 0;
103 else
105 x = current_vp->width - w - x;
106 x += ofs;
107 ofs = 0;
111 rtl_next_non_diac_width = 0;
112 last_non_diacritic_width = 0;
113 /* Mark diacritic and rtl flags for each character */
114 for (ucs = bidi_l2v(str, 1); *ucs; ucs++)
116 bool is_rtl, is_diac;
117 const unsigned char *bits;
118 int width, base_width, drawmode = 0, base_ofs = 0;
119 const unsigned short next_ch = ucs[1];
121 if (x >= current_vp->width)
122 break;
124 is_diac = is_diacritic(*ucs, &is_rtl);
126 /* Get proportional width and glyph bits */
127 width = font_get_width(pf, *ucs);
129 /* Calculate base width */
130 if (is_rtl)
132 /* Forward-seek the next non-diacritic character for base width */
133 if (is_diac)
135 if (!rtl_next_non_diac_width)
137 const unsigned short *u;
139 /* Jump to next non-diacritic char, and calc its width */
140 for (u = &ucs[1]; *u && is_diacritic(*u, NULL); u++);
142 rtl_next_non_diac_width = *u ? font_get_width(pf, *u) : 0;
144 base_width = rtl_next_non_diac_width;
146 else
148 rtl_next_non_diac_width = 0; /* Mark */
149 base_width = width;
152 else
154 if (!is_diac)
155 last_non_diacritic_width = width;
157 base_width = last_non_diacritic_width;
160 if (ofs > width)
162 ofs -= width;
163 continue;
166 if (is_diac)
168 /* XXX: Suggested by amiconn:
169 * This will produce completely wrong results if the original
170 * drawmode is DRMODE_COMPLEMENT. We need to pre-render the current
171 * character with all its diacritics at least (in mono) and then
172 * finally draw that. And we'll need an extra buffer that can hold
173 * one char's bitmap. Basically we can't just change the draw mode
174 * to something else irrespective of the original mode and expect
175 * the result to look as intended and with DRMODE_COMPLEMENT (which
176 * means XORing pixels), overdrawing this way will cause odd results
177 * if the diacritics and the base char both have common pixels set.
178 * So we need to combine the char and its diacritics in a temp
179 * buffer using OR, and then draw the final bitmap instead of the
180 * chars, without touching the drawmode
182 drawmode = current_vp->drawmode;
183 current_vp->drawmode = DRMODE_FG;
185 base_ofs = (base_width - width) / 2;
188 bits = font_get_bits(pf, *ucs);
189 LCDFN(mono_bitmap_part)(bits, ofs, 0, width, MAX(x + base_ofs, 0), y,
190 width - ofs, pf->height);
192 if (is_diac)
194 current_vp->drawmode = drawmode;
197 if (next_ch)
199 bool next_is_rtl;
200 bool next_is_diacritic = is_diacritic(next_ch, &next_is_rtl);
202 /* Increment if:
203 * LTR: Next char is not diacritic,
204 * RTL: Current char is non-diacritic and next char is diacritic */
205 if ((is_rtl && !is_diac) ||
206 (!is_rtl && (!next_is_diacritic || next_is_rtl)))
208 x += base_width - ofs;
209 ofs = 0;
215 /* put a string at a given pixel position */
216 void LCDFN(putsxy)(int x, int y, const unsigned char *str)
218 LCDFN(putsxyofs)(x, y, 0, str);
221 static void LCDFN(putsxyofs_style)(int xpos, int ypos,
222 const unsigned char *str, int style,
223 int w, int h, int offset)
225 int lastmode = current_vp->drawmode;
226 int xrect = xpos + MAX(w - offset, 0);
227 int x = VP_IS_RTL(current_vp) ? xpos : xrect;
228 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
229 int oldfgcolor = current_vp->fg_pattern;
230 int oldbgcolor = current_vp->bg_pattern;
231 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
232 DRMODE_INVERSEVID : 0);
233 if (style & STYLE_COLORED) {
234 if (current_vp->drawmode == DRMODE_SOLID)
235 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
236 else
237 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
239 current_vp->drawmode ^= DRMODE_INVERSEVID;
240 if (style & STYLE_GRADIENT) {
241 current_vp->drawmode = DRMODE_FG;
242 lcd_gradient_rect(xpos, current_vp->width, ypos, h,
243 NUMLN_UNPACK(style), CURLN_UNPACK(style));
244 current_vp->fg_pattern = current_vp->lst_pattern;
246 else if (style & STYLE_COLORBAR) {
247 current_vp->drawmode = DRMODE_FG;
248 current_vp->fg_pattern = current_vp->lss_pattern;
249 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
250 current_vp->fg_pattern = current_vp->lst_pattern;
252 else {
253 lcd_fillrect(x, ypos, current_vp->width - xrect, h);
254 current_vp->drawmode = (style & STYLE_INVERT) ?
255 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
257 lcd_putsxyofs(xpos, ypos, offset, str);
258 current_vp->fg_pattern = oldfgcolor;
259 current_vp->bg_pattern = oldbgcolor;
260 #else
261 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
262 0 : DRMODE_INVERSEVID);
263 LCDFN(fillrect)(x, ypos, current_vp->width - xrect, h);
264 current_vp->drawmode ^= DRMODE_INVERSEVID;
265 LCDFN(putsxyofs)(xpos, ypos, offset, str);
266 #endif
267 current_vp->drawmode = lastmode;
270 /*** Line oriented text output ***/
272 /* put a string at a given char position */
273 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
274 int style, int offset)
276 int xpos, ypos, w, h;
277 LCDFN(scroll_stop_line)(current_vp, y);
278 if(!str || !str[0])
279 return;
281 LCDFN(getstringsize)(str, &w, &h);
282 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
283 ypos = y * h;
284 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, offset);
287 void LCDFN(puts)(int x, int y, const unsigned char *str)
289 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
292 /* Formatting version of LCDFN(puts) */
293 void LCDFN(putsf)(int x, int y, const unsigned char *fmt, ...)
295 va_list ap;
296 char buf[256];
297 va_start(ap, fmt);
298 vsnprintf(buf, sizeof (buf), fmt, ap);
299 va_end(ap);
300 LCDFN(puts)(x, y, buf);
303 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
305 LCDFN(puts_style_offset)(x, y, str, style, 0);
308 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
310 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
313 /*** scrolling ***/
315 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
316 int style, int offset)
318 struct scrollinfo* s;
319 char *end;
320 int w, h;
322 if ((unsigned)y >= (unsigned)current_vp->height)
323 return;
325 /* remove any previously scrolling line at the same location */
326 lcd_scroll_stop_line(current_vp, y);
328 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
329 if (!string)
330 return;
331 LCDFN(puts_style_offset)(x, y, string, style, offset);
333 LCDFN(getstringsize)(string, &w, &h);
335 if (current_vp->width - x * 8 >= w)
336 return;
338 /* prepare scroll line */
339 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
340 s->start_tick = current_tick + LCDFN(scroll_info).delay;
341 s->style = style;
343 strlcpy(s->line, string, sizeof s->line);
345 /* get width */
346 s->width = LCDFN(getstringsize)(s->line, &w, &h);
348 /* scroll bidirectional or forward only depending on the string
349 width */
350 if ( LCDFN(scroll_info).bidir_limit ) {
351 s->bidir = s->width < (current_vp->width) *
352 (100 + LCDFN(scroll_info).bidir_limit) / 100;
354 else
355 s->bidir = false;
357 if (!s->bidir) { /* add spaces if scrolling in the round */
358 strcat(s->line, " ");
359 /* get new width incl. spaces */
360 s->width = LCDFN(getstringsize)(s->line, &w, &h);
363 end = strchr(s->line, '\0');
364 strlcpy(end, string, current_vp->width/2);
366 s->vp = current_vp;
367 s->y = y;
368 s->len = utf8length(string);
369 s->offset = offset;
370 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
371 s->backward = false;
373 LCDFN(scroll_info).lines++;
376 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
378 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
381 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
382 int style)
384 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
387 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
388 int offset)
390 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
393 void LCDFN(scroll_fn)(void)
395 struct font* pf;
396 struct scrollinfo* s;
397 int index;
398 int xpos, ypos;
399 struct viewport* old_vp = current_vp;
401 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
402 s = &LCDFN(scroll_info).scroll[index];
404 /* check pause */
405 if (TIME_BEFORE(current_tick, s->start_tick))
406 continue;
408 LCDFN(set_viewport)(s->vp);
410 if (s->backward)
411 s->offset -= LCDFN(scroll_info).step;
412 else
413 s->offset += LCDFN(scroll_info).step;
415 pf = font_get(current_vp->font);
416 xpos = s->startx;
417 ypos = s->y * pf->height;
419 if (s->bidir) { /* scroll bidirectional */
420 if (s->offset <= 0) {
421 /* at beginning of line */
422 s->offset = 0;
423 s->backward = false;
424 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
426 if (s->offset >= s->width - (current_vp->width - xpos)) {
427 /* at end of line */
428 s->offset = s->width - (current_vp->width - xpos);
429 s->backward = true;
430 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
433 else {
434 /* scroll forward the whole time */
435 if (s->offset >= s->width)
436 s->offset %= s->width;
438 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
439 pf->height, s->offset);
440 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
441 pf->height);
443 LCDFN(set_viewport)(old_vp);