Move c/h files implementing/defining standard library stuff into a new libc directory...
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob1b31ee8a184219afb92cece1636e8ab46600a968
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
84 /* put a string at a given pixel position, skipping first ofs pixel columns */
85 static void LCDFN(putsxyofs)(int x, int y, int ofs, const unsigned char *str)
87 unsigned short *ucs;
88 struct font* pf = font_get(current_vp->font);
89 int vp_flags = current_vp->flags;
90 int rtl_next_non_diac_width, last_non_diacritic_width;
92 if ((vp_flags & VP_FLAG_ALIGNMENT_MASK) != 0)
94 int w;
96 LCDFN(getstringsize)(str, &w, NULL);
97 /* center takes precedence */
98 if (vp_flags & VP_FLAG_ALIGN_CENTER)
100 x = ((current_vp->width - w)/ 2) + x;
101 if (x < 0)
102 x = 0;
104 else
106 x = current_vp->width - w - x;
107 x += ofs;
108 ofs = 0;
112 rtl_next_non_diac_width = 0;
113 last_non_diacritic_width = 0;
114 /* Mark diacritic and rtl flags for each character */
115 for (ucs = bidi_l2v(str, 1); *ucs; ucs++)
117 bool is_rtl, is_diac;
118 const unsigned char *bits;
119 int width, base_width, drawmode = 0, base_ofs = 0;
120 const unsigned short next_ch = ucs[1];
122 if (x >= current_vp->width)
123 break;
125 is_diac = is_diacritic(*ucs, &is_rtl);
127 /* Get proportional width and glyph bits */
128 width = font_get_width(pf, *ucs);
130 /* Calculate base width */
131 if (is_rtl)
133 /* Forward-seek the next non-diacritic character for base width */
134 if (is_diac)
136 if (!rtl_next_non_diac_width)
138 const unsigned short *u;
140 /* Jump to next non-diacritic char, and calc its width */
141 for (u = &ucs[1]; *u && is_diacritic(*u, NULL); u++);
143 rtl_next_non_diac_width = *u ? font_get_width(pf, *u) : 0;
145 base_width = rtl_next_non_diac_width;
147 else
149 rtl_next_non_diac_width = 0; /* Mark */
150 base_width = width;
153 else
155 if (!is_diac)
156 last_non_diacritic_width = width;
158 base_width = last_non_diacritic_width;
161 if (ofs > width)
163 ofs -= width;
164 continue;
167 if (is_diac)
169 /* XXX: Suggested by amiconn:
170 * This will produce completely wrong results if the original
171 * drawmode is DRMODE_COMPLEMENT. We need to pre-render the current
172 * character with all its diacritics at least (in mono) and then
173 * finally draw that. And we'll need an extra buffer that can hold
174 * one char's bitmap. Basically we can't just change the draw mode
175 * to something else irrespective of the original mode and expect
176 * the result to look as intended and with DRMODE_COMPLEMENT (which
177 * means XORing pixels), overdrawing this way will cause odd results
178 * if the diacritics and the base char both have common pixels set.
179 * So we need to combine the char and its diacritics in a temp
180 * buffer using OR, and then draw the final bitmap instead of the
181 * chars, without touching the drawmode
183 drawmode = current_vp->drawmode;
184 current_vp->drawmode = DRMODE_FG;
186 base_ofs = (base_width - width) / 2;
189 bits = font_get_bits(pf, *ucs);
190 LCDFN(mono_bitmap_part)(bits, ofs, 0, width, x + base_ofs, y,
191 width - ofs, pf->height);
193 if (is_diac)
195 current_vp->drawmode = drawmode;
198 if (next_ch)
200 bool next_is_rtl;
201 bool next_is_diacritic = is_diacritic(next_ch, &next_is_rtl);
203 /* Increment if:
204 * LTR: Next char is not diacritic,
205 * RTL: Current char is non-diacritic and next char is diacritic */
206 if ((is_rtl && !is_diac) ||
207 (!is_rtl && (!next_is_diacritic || next_is_rtl)))
209 x += base_width - ofs;
210 ofs = 0;
216 /* put a string at a given pixel position */
217 void LCDFN(putsxy)(int x, int y, const unsigned char *str)
219 LCDFN(putsxyofs)(x, y, 0, str);
222 static void LCDFN(putsxyofs_style)(int xpos, int ypos,
223 const unsigned char *str, int style,
224 int w, int h, int offset)
226 int lastmode = current_vp->drawmode;
227 int xrect = xpos + MAX(w - offset, 0);
228 int x = VP_IS_RTL(current_vp) ? xpos : xrect;
229 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
230 int oldfgcolor = current_vp->fg_pattern;
231 int oldbgcolor = current_vp->bg_pattern;
232 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
233 DRMODE_INVERSEVID : 0);
234 if (style & STYLE_COLORED) {
235 if (current_vp->drawmode == DRMODE_SOLID)
236 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
237 else
238 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
240 current_vp->drawmode ^= DRMODE_INVERSEVID;
241 if (style & STYLE_GRADIENT) {
242 current_vp->drawmode = DRMODE_FG;
243 lcd_gradient_rect(xpos, current_vp->width, ypos, h,
244 NUMLN_UNPACK(style), CURLN_UNPACK(style));
245 current_vp->fg_pattern = current_vp->lst_pattern;
247 else if (style & STYLE_COLORBAR) {
248 current_vp->drawmode = DRMODE_FG;
249 current_vp->fg_pattern = current_vp->lss_pattern;
250 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
251 current_vp->fg_pattern = current_vp->lst_pattern;
253 else {
254 lcd_fillrect(x, ypos, current_vp->width - xrect, h);
255 current_vp->drawmode = (style & STYLE_INVERT) ?
256 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
258 if (str[0])
259 lcd_putsxyofs(xpos, ypos, offset, str);
260 current_vp->fg_pattern = oldfgcolor;
261 current_vp->bg_pattern = oldbgcolor;
262 #else
263 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
264 0 : DRMODE_INVERSEVID);
265 LCDFN(fillrect)(x, ypos, current_vp->width - xrect, h);
266 current_vp->drawmode ^= DRMODE_INVERSEVID;
267 if (str[0])
268 LCDFN(putsxyofs)(xpos, ypos, offset, str);
269 #endif
270 current_vp->drawmode = lastmode;
273 /*** Line oriented text output ***/
275 /* put a string at a given char position */
276 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
277 int style, int offset)
279 int xpos, ypos, w, h;
280 LCDFN(scroll_stop_line)(current_vp, y);
281 if(!str)
282 return;
284 LCDFN(getstringsize)(str, &w, &h);
285 xpos = x * LCDFN(getstringsize)(" ", NULL, NULL);
286 ypos = y * h;
287 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, offset);
290 void LCDFN(puts)(int x, int y, const unsigned char *str)
292 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
295 /* Formatting version of LCDFN(puts) */
296 void LCDFN(putsf)(int x, int y, const unsigned char *fmt, ...)
298 va_list ap;
299 char buf[256];
300 va_start(ap, fmt);
301 vsnprintf(buf, sizeof (buf), fmt, ap);
302 va_end(ap);
303 LCDFN(puts)(x, y, buf);
306 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
308 LCDFN(puts_style_offset)(x, y, str, style, 0);
311 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
313 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
316 /*** scrolling ***/
318 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
319 int style, int offset)
321 struct scrollinfo* s;
322 char *end;
323 int w, h;
325 if ((unsigned)y >= (unsigned)current_vp->height)
326 return;
328 /* remove any previously scrolling line at the same location */
329 LCDFN(scroll_stop_line)(current_vp, y);
331 if (LCDFN(scroll_info).lines >= LCDM(SCROLLABLE_LINES)) return;
332 if (!string)
333 return;
334 LCDFN(puts_style_offset)(x, y, string, style, offset);
336 LCDFN(getstringsize)(string, &w, &h);
338 if (current_vp->width - x * 8 >= w)
339 return;
341 /* prepare scroll line */
342 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
343 s->start_tick = current_tick + LCDFN(scroll_info).delay;
344 s->style = style;
346 strlcpy(s->line, string, sizeof s->line);
348 /* get width */
349 s->width = LCDFN(getstringsize)(s->line, &w, &h);
351 /* scroll bidirectional or forward only depending on the string
352 width */
353 if ( LCDFN(scroll_info).bidir_limit ) {
354 s->bidir = s->width < (current_vp->width) *
355 (100 + LCDFN(scroll_info).bidir_limit) / 100;
357 else
358 s->bidir = false;
360 if (!s->bidir) { /* add spaces if scrolling in the round */
361 strcat(s->line, " ");
362 /* get new width incl. spaces */
363 s->width = LCDFN(getstringsize)(s->line, &w, &h);
366 end = strchr(s->line, '\0');
367 strlcpy(end, string, current_vp->width/2);
369 s->vp = current_vp;
370 s->y = y;
371 s->offset = offset;
372 s->startx = x * LCDFN(getstringsize)(" ", NULL, NULL);
373 s->backward = false;
375 LCDFN(scroll_info).lines++;
378 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
380 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
383 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
384 int style)
386 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
389 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
390 int offset)
392 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
395 void LCDFN(scroll_fn)(void)
397 struct font* pf;
398 struct scrollinfo* s;
399 int index;
400 int xpos, ypos;
401 struct viewport* old_vp = current_vp;
403 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
404 s = &LCDFN(scroll_info).scroll[index];
406 /* check pause */
407 if (TIME_BEFORE(current_tick, s->start_tick))
408 continue;
410 LCDFN(set_viewport)(s->vp);
412 if (s->backward)
413 s->offset -= LCDFN(scroll_info).step;
414 else
415 s->offset += LCDFN(scroll_info).step;
417 pf = font_get(current_vp->font);
418 xpos = s->startx;
419 ypos = s->y * pf->height;
421 if (s->bidir) { /* scroll bidirectional */
422 if (s->offset <= 0) {
423 /* at beginning of line */
424 s->offset = 0;
425 s->backward = false;
426 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
428 if (s->offset >= s->width - (current_vp->width - xpos)) {
429 /* at end of line */
430 s->offset = s->width - (current_vp->width - xpos);
431 s->backward = true;
432 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
435 else {
436 /* scroll forward the whole time */
437 if (s->offset >= s->width)
438 s->offset %= s->width;
440 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
441 pf->height, s->offset);
442 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
443 pf->height);
445 LCDFN(set_viewport)(old_vp);