as3514: detail bit of AS3543_DAC_IF
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob8f0b61ef6a5977352aa90e3e17a4c0b79527a457
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, x + base_ofs, 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 if (str[0])
258 lcd_putsxyofs(xpos, ypos, offset, str);
259 current_vp->fg_pattern = oldfgcolor;
260 current_vp->bg_pattern = oldbgcolor;
261 #else
262 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
263 0 : DRMODE_INVERSEVID);
264 LCDFN(fillrect)(x, ypos, current_vp->width - xrect, h);
265 current_vp->drawmode ^= DRMODE_INVERSEVID;
266 if (str[0])
267 LCDFN(putsxyofs)(xpos, ypos, offset, str);
268 #endif
269 current_vp->drawmode = lastmode;
272 /*** Line oriented text output ***/
274 /* put a string at a given char position */
275 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
276 int style, int offset)
278 int xpos, ypos, w, h;
279 LCDFN(scroll_stop_line)(current_vp, y);
280 if(!str)
281 return;
283 LCDFN(getstringsize)(str, &w, &h);
284 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
285 ypos = y * h;
286 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, offset);
289 void LCDFN(puts)(int x, int y, const unsigned char *str)
291 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
294 /* Formatting version of LCDFN(puts) */
295 void LCDFN(putsf)(int x, int y, const unsigned char *fmt, ...)
297 va_list ap;
298 char buf[256];
299 va_start(ap, fmt);
300 vsnprintf(buf, sizeof (buf), fmt, ap);
301 va_end(ap);
302 LCDFN(puts)(x, y, buf);
305 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
307 LCDFN(puts_style_offset)(x, y, str, style, 0);
310 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
312 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
315 /*** scrolling ***/
317 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
318 int style, int offset)
320 struct scrollinfo* s;
321 char *end;
322 int w, h;
324 if ((unsigned)y >= (unsigned)current_vp->height)
325 return;
327 /* remove any previously scrolling line at the same location */
328 LCDFN(scroll_stop_line)(current_vp, y);
330 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
331 if (!string)
332 return;
333 LCDFN(puts_style_offset)(x, y, string, style, offset);
335 LCDFN(getstringsize)(string, &w, &h);
337 if (current_vp->width - x * 8 >= w)
338 return;
340 /* prepare scroll line */
341 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
342 s->start_tick = current_tick + LCDFN(scroll_info).delay;
343 s->style = style;
345 strlcpy(s->line, string, sizeof s->line);
347 /* get width */
348 s->width = LCDFN(getstringsize)(s->line, &w, &h);
350 /* scroll bidirectional or forward only depending on the string
351 width */
352 if ( LCDFN(scroll_info).bidir_limit ) {
353 s->bidir = s->width < (current_vp->width) *
354 (100 + LCDFN(scroll_info).bidir_limit) / 100;
356 else
357 s->bidir = false;
359 if (!s->bidir) { /* add spaces if scrolling in the round */
360 strcat(s->line, " ");
361 /* get new width incl. spaces */
362 s->width = LCDFN(getstringsize)(s->line, &w, &h);
365 end = strchr(s->line, '\0');
366 strlcpy(end, string, current_vp->width/2);
368 s->vp = current_vp;
369 s->y = y;
370 s->offset = offset;
371 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
372 s->backward = false;
374 LCDFN(scroll_info).lines++;
377 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
379 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
382 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
383 int style)
385 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
388 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
389 int offset)
391 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
394 void LCDFN(scroll_fn)(void)
396 struct font* pf;
397 struct scrollinfo* s;
398 int index;
399 int xpos, ypos;
400 struct viewport* old_vp = current_vp;
402 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
403 s = &LCDFN(scroll_info).scroll[index];
405 /* check pause */
406 if (TIME_BEFORE(current_tick, s->start_tick))
407 continue;
409 LCDFN(set_viewport)(s->vp);
411 if (s->backward)
412 s->offset -= LCDFN(scroll_info).step;
413 else
414 s->offset += LCDFN(scroll_info).step;
416 pf = font_get(current_vp->font);
417 xpos = s->startx;
418 ypos = s->y * pf->height;
420 if (s->bidir) { /* scroll bidirectional */
421 if (s->offset <= 0) {
422 /* at beginning of line */
423 s->offset = 0;
424 s->backward = false;
425 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
427 if (s->offset >= s->width - (current_vp->width - xpos)) {
428 /* at end of line */
429 s->offset = s->width - (current_vp->width - xpos);
430 s->backward = true;
431 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
434 else {
435 /* scroll forward the whole time */
436 if (s->offset >= s->width)
437 s->offset %= s->width;
439 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
440 pf->height, s->offset);
441 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
442 pf->height);
444 LCDFN(set_viewport)(old_vp);