FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / firmware / drivers / lcd-bitmap-common.c
blob775b56bb313e7434bb0c9d583f918ff491ac9c29
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 ****************************************************************************/
31 #ifndef LCDFN /* Not compiling for remote - define macros for main LCD. */
32 #define LCDFN(fn) lcd_ ## fn
33 #define FBFN(fn) fb_ ## fn
34 #define LCDM(ma) LCD_ ## ma
35 #define LCDNAME "lcd_"
36 #define MAIN_LCD
37 #endif
39 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
40 /* Fill a rectangle with a gradient */
41 static void lcd_gradient_rect(int x1, int x2, int y, unsigned h,
42 int num_lines, int cur_line)
44 int old_pattern = current_vp->fg_pattern;
45 int step_mul;
46 if (h == 0) return;
48 num_lines *= h;
49 cur_line *= h;
50 step_mul = (1 << 16) / (num_lines);
51 int h_r = RGB_UNPACK_RED(current_vp->lss_pattern);
52 int h_g = RGB_UNPACK_GREEN(current_vp->lss_pattern);
53 int h_b = RGB_UNPACK_BLUE(current_vp->lss_pattern);
54 int rstep = (h_r - RGB_UNPACK_RED(current_vp->lse_pattern)) * step_mul;
55 int gstep = (h_g - RGB_UNPACK_GREEN(current_vp->lse_pattern)) * step_mul;
56 int bstep = (h_b - RGB_UNPACK_BLUE(current_vp->lse_pattern)) * step_mul;
57 h_r = (h_r << 16) + (1 << 15);
58 h_g = (h_g << 16) + (1 << 15);
59 h_b = (h_b << 16) + (1 << 15);
60 if (cur_line)
62 h_r -= cur_line * rstep;
63 h_g -= cur_line * gstep;
64 h_b -= cur_line * bstep;
66 unsigned count;
68 for(count = 0; count < h; count++) {
69 current_vp->fg_pattern = LCD_RGBPACK(h_r >> 16, h_g >> 16, h_b >> 16);
70 lcd_hline(x1, x2, y + count);
71 h_r -= rstep;
72 h_g -= gstep;
73 h_b -= bstep;
76 current_vp->fg_pattern = old_pattern;
78 #endif
80 /* put a string at a given pixel position, skipping first ofs pixel columns */
81 static void LCDFN(putsxyofs)(int x, int y, int ofs, const unsigned char *str)
83 unsigned short ch;
84 unsigned short *ucs;
85 struct font* pf = font_get(current_vp->font);
87 ucs = bidi_l2v(str, 1);
89 while ((ch = *ucs++) != 0 && x < current_vp->width)
91 int width;
92 const unsigned char *bits;
94 /* get proportional width and glyph bits */
95 width = font_get_width(pf, ch);
97 if (ofs > width)
99 ofs -= width;
100 continue;
103 bits = font_get_bits(pf, ch);
105 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
106 if (pf->depth)
107 lcd_alpha_bitmap_part(bits, ofs, 0, width, x, y, width - ofs,
108 pf->height);
109 else
110 #endif
111 LCDFN(mono_bitmap_part)(bits, ofs, 0, width, x, y, width - ofs,
112 pf->height);
114 x += width - ofs;
115 ofs = 0;
118 /* put a string at a given pixel position */
119 void LCDFN(putsxy)(int x, int y, const unsigned char *str)
121 LCDFN(putsxyofs)(x, y, 0, str);
124 static void LCDFN(putsxyofs_style)(int xpos, int ypos,
125 const unsigned char *str, int style,
126 int w, int h, int offset)
128 int lastmode = current_vp->drawmode;
129 int xrect = xpos + MAX(w - offset, 0);
130 #if defined(MAIN_LCD) && defined(HAVE_LCD_COLOR)
131 int oldfgcolor = current_vp->fg_pattern;
132 int oldbgcolor = current_vp->bg_pattern;
133 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
134 DRMODE_INVERSEVID : 0);
135 if (style & STYLE_COLORED) {
136 if (current_vp->drawmode == DRMODE_SOLID)
137 current_vp->fg_pattern = style & STYLE_COLOR_MASK;
138 else
139 current_vp->bg_pattern = style & STYLE_COLOR_MASK;
141 current_vp->drawmode ^= DRMODE_INVERSEVID;
142 if (style & STYLE_GRADIENT) {
143 current_vp->drawmode = DRMODE_FG;
144 lcd_gradient_rect(xpos, current_vp->width, ypos, h,
145 NUMLN_UNPACK(style), CURLN_UNPACK(style));
146 current_vp->fg_pattern = current_vp->lst_pattern;
148 else if (style & STYLE_COLORBAR) {
149 current_vp->drawmode = DRMODE_FG;
150 current_vp->fg_pattern = current_vp->lss_pattern;
151 lcd_fillrect(xpos, ypos, current_vp->width - xpos, h);
152 current_vp->fg_pattern = current_vp->lst_pattern;
154 else {
155 lcd_fillrect(xrect, ypos, current_vp->width - xrect, h);
156 current_vp->drawmode = (style & STYLE_INVERT) ?
157 (DRMODE_SOLID|DRMODE_INVERSEVID) : DRMODE_SOLID;
159 lcd_putsxyofs(xpos, ypos, offset, str);
160 current_vp->fg_pattern = oldfgcolor;
161 current_vp->bg_pattern = oldbgcolor;
162 #else
163 current_vp->drawmode = DRMODE_SOLID | ((style & STYLE_INVERT) ?
164 0 : DRMODE_INVERSEVID);
165 LCDFN(fillrect)(xrect, ypos, current_vp->width - xrect, h);
166 current_vp->drawmode ^= DRMODE_INVERSEVID;
167 LCDFN(putsxyofs)(xpos, ypos, offset, str);
168 #endif
169 current_vp->drawmode = lastmode;
172 /*** Line oriented text output ***/
174 /* put a string at a given char position */
175 void LCDFN(puts_style_offset)(int x, int y, const unsigned char *str,
176 int style, int offset)
178 int xpos, ypos, w, h;
179 LCDFN(scroll_stop_line)(current_vp, y);
180 if(!str || !str[0])
181 return;
182 LCDFN(getstringsize)(str, &w, &h);
183 xpos = x * w / utf8length((char *)str);
184 ypos = y * h;
185 LCDFN(putsxyofs_style)(xpos, ypos, str, style, w, h, offset);
188 void LCDFN(puts)(int x, int y, const unsigned char *str)
190 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, 0);
193 void LCDFN(puts_style)(int x, int y, const unsigned char *str, int style)
195 LCDFN(puts_style_offset)(x, y, str, style, 0);
198 void LCDFN(puts_offset)(int x, int y, const unsigned char *str, int offset)
200 LCDFN(puts_style_offset)(x, y, str, STYLE_DEFAULT, offset);
203 /*** scrolling ***/
205 void LCDFN(puts_scroll_style_offset)(int x, int y, const unsigned char *string,
206 int style, int offset)
208 int w, h;
210 if ((unsigned)y >= (unsigned)current_vp->height)
211 return;
213 /* remove any previously scrolling line at the same location */
214 lcd_scroll_stop_line(current_vp, y);
216 if (LCDFN(scroll_info.lines) >= LCDM(SCROLLABLE_LINES)) return;
217 if (!string)
218 return;
219 LCDFN(puts_style_offset)(x, y, string, style, offset);
221 LCDFN(getstringsize)(string, &w, &h);
223 if (current_vp->width - x * 8 < w) {
224 /* prepare scroll line */
225 struct scrollinfo* s;
226 s = &LCDFN(scroll_info).scroll[LCDFN(scroll_info).lines];
227 s->start_tick = current_tick + LCDFN(scroll_info).delay;
228 s->style = style;
230 char *end;
232 memset(s->line, 0, sizeof s->line);
233 strcpy(s->line, string);
235 /* get width */
236 s->width = LCDFN(getstringsize)(s->line, &w, &h);
238 /* scroll bidirectional or forward only depending on the string
239 width */
240 if ( LCDFN(scroll_info).bidir_limit ) {
241 s->bidir = s->width < (current_vp->width) *
242 (100 + LCDFN(scroll_info).bidir_limit) / 100;
244 else
245 s->bidir = false;
247 if (!s->bidir) { /* add spaces if scrolling in the round */
248 strcat(s->line, " ");
249 /* get new width incl. spaces */
250 s->width = LCDFN(getstringsize)(s->line, &w, &h);
253 end = strchr(s->line, '\0');
254 strlcpy(end, string, current_vp->width/2);
256 s->vp = current_vp;
257 s->y = y;
258 s->len = utf8length(string);
259 s->offset = offset;
260 s->startx = x * s->width / s->len;
261 s->backward = false;
263 LCDFN(scroll_info).lines++;
267 void LCDFN(puts_scroll)(int x, int y, const unsigned char *string)
269 LCDFN(puts_scroll_style)(x, y, string, STYLE_DEFAULT);
272 void LCDFN(puts_scroll_style)(int x, int y, const unsigned char *string,
273 int style)
275 LCDFN(puts_scroll_style_offset)(x, y, string, style, 0);
278 void LCDFN(puts_scroll_offset)(int x, int y, const unsigned char *string,
279 int offset)
281 LCDFN(puts_scroll_style_offset)(x, y, string, STYLE_DEFAULT, offset);
284 void LCDFN(scroll_fn)(void)
286 struct font* pf;
287 struct scrollinfo* s;
288 int index;
289 int xpos, ypos;
290 struct viewport* old_vp = current_vp;
292 for ( index = 0; index < LCDFN(scroll_info).lines; index++ ) {
293 s = &LCDFN(scroll_info).scroll[index];
295 /* check pause */
296 if (TIME_BEFORE(current_tick, s->start_tick))
297 continue;
299 LCDFN(set_viewport)(s->vp);
301 if (s->backward)
302 s->offset -= LCDFN(scroll_info).step;
303 else
304 s->offset += LCDFN(scroll_info).step;
306 pf = font_get(current_vp->font);
307 xpos = s->startx;
308 ypos = s->y * pf->height;
310 if (s->bidir) { /* scroll bidirectional */
311 if (s->offset <= 0) {
312 /* at beginning of line */
313 s->offset = 0;
314 s->backward = false;
315 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
317 if (s->offset >= s->width - (current_vp->width - xpos)) {
318 /* at end of line */
319 s->offset = s->width - (current_vp->width - xpos);
320 s->backward = true;
321 s->start_tick = current_tick + LCDFN(scroll_info).delay * 2;
324 else {
325 /* scroll forward the whole time */
326 if (s->offset >= s->width)
327 s->offset %= s->width;
329 LCDFN(putsxyofs_style)(xpos, ypos, s->line, s->style, s->width,
330 pf->height, s->offset);
331 LCDFN(update_viewport_rect)(xpos, ypos, current_vp->width - xpos,
332 pf->height);
334 LCDFN(set_viewport)(old_vp);