diff options
Diffstat (limited to 'firmware/font.c')
-rw-r--r-- | firmware/font.c | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/firmware/font.c b/firmware/font.c new file mode 100644 index 0000000000..5bde2ff1bc --- /dev/null +++ b/firmware/font.c | |||
@@ -0,0 +1,228 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (c) 2002 by Greg Haerr <greg@censoft.com> | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | /* | ||
20 | * Rockbox startup font initialization | ||
21 | * This file specifies which fonts get compiled-in and | ||
22 | * loaded at startup, as well as their mapping into | ||
23 | * the FONT_SYSFIXED, FONT_UI and FONT_MP3 ids. | ||
24 | */ | ||
25 | #include "config.h" | ||
26 | |||
27 | #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR) | ||
28 | |||
29 | #include <stdio.h> | ||
30 | #include <string.h> | ||
31 | #include "lcd.h" | ||
32 | #include "font.h" | ||
33 | #include "debug.h" | ||
34 | #include "panic.h" | ||
35 | |||
36 | /* available compiled-in fonts*/ | ||
37 | extern MWCFONT font_X5x8; | ||
38 | /*extern MWCFONT font_X6x9; */ | ||
39 | /*extern MWCFONT font_courB08; */ | ||
40 | /*extern MWCFONT font_timR08; */ | ||
41 | |||
42 | /* structure filled in by rbf_load_font*/ | ||
43 | static MWCFONT font_UI; | ||
44 | |||
45 | /* system font table, in order of FONT_xxx definition*/ | ||
46 | struct corefont sysfonts[MAXFONTS] = { | ||
47 | { &font_X5x8, NULL }, /* compiled-in FONT_SYSFIXED*/ | ||
48 | { &font_UI, "/system.fnt" }, /* loaded FONT_UI*/ | ||
49 | { NULL, NULL }, /* no FONT_MP3*/ | ||
50 | }; | ||
51 | |||
52 | void | ||
53 | font_init(void) | ||
54 | { | ||
55 | struct corefont *cfp; | ||
56 | |||
57 | for (cfp=sysfonts; cfp < &sysfonts[MAXFONTS]; ++cfp) { | ||
58 | if (cfp->pf && cfp->diskname) { | ||
59 | cfp->pf = rbf_load_font(cfp->diskname, cfp->pf); | ||
60 | #if defined(DEBUG) || defined(SIMULATOR) | ||
61 | if (!cfp->pf) | ||
62 | DEBUGF("Font load failed: %s\n", cfp->diskname); | ||
63 | #endif | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | /* | ||
69 | * Return a pointer to an incore font structure. | ||
70 | * If the requested font isn't loaded/compiled-in, | ||
71 | * decrement the font number and try again. | ||
72 | */ | ||
73 | PMWCFONT | ||
74 | getfont(int font) | ||
75 | { | ||
76 | PMWCFONT pf; | ||
77 | |||
78 | while (1) { | ||
79 | pf = sysfonts[font].pf; | ||
80 | if (pf && pf->height) | ||
81 | return pf; | ||
82 | if (--font < 0) | ||
83 | panicf("No font!"); | ||
84 | } | ||
85 | } | ||
86 | |||
87 | /* | ||
88 | * Return width and height of a given font. | ||
89 | */ | ||
90 | void lcd_getfontsize(int font, int *width, int *height) | ||
91 | { | ||
92 | PMWCFONT pf = getfont(font); | ||
93 | |||
94 | *width = pf->maxwidth; | ||
95 | *height = pf->height; | ||
96 | } | ||
97 | |||
98 | /* | ||
99 | * Return width and height of a given font. | ||
100 | */ | ||
101 | //FIXME rename to font_gettextsize, add baseline | ||
102 | int | ||
103 | lcd_getstringsize(unsigned char *str, int font, int *w, int *h) | ||
104 | { | ||
105 | PMWCFONT pf = getfont(font); | ||
106 | int ch; | ||
107 | int width = 0; | ||
108 | |||
109 | while((ch = *str++)) { | ||
110 | |||
111 | /* check input range*/ | ||
112 | if (ch < pf->firstchar || ch >= pf->firstchar+pf->size) | ||
113 | ch = pf->defaultchar; | ||
114 | ch -= pf->firstchar; | ||
115 | |||
116 | /* get proportional width and glyph bits*/ | ||
117 | width += pf->width? pf->width[ch]: pf->maxwidth; | ||
118 | } | ||
119 | *w = width; | ||
120 | *h = pf->height; | ||
121 | |||
122 | return width; | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | * Take an MWIMAGEBITS bitmap and convert to Rockbox format. | ||
127 | * Used for converting font glyphs for the time being. | ||
128 | * Can use for standard X11 and Win32 images as well. | ||
129 | * | ||
130 | * Doing it this way keeps fonts in standard formats, | ||
131 | * as well as keeping Rockbox hw bitmap format. | ||
132 | */ | ||
133 | static void | ||
134 | rotleft(unsigned char *dst, MWIMAGEBITS *src, unsigned int width, | ||
135 | unsigned int height) | ||
136 | { | ||
137 | unsigned int i,j; | ||
138 | unsigned int dst_col = 0; /* destination column*/ | ||
139 | unsigned int dst_shift = 0; /* destination shift amount*/ | ||
140 | unsigned int dst_linelen; /* # bytes per output row*/ | ||
141 | unsigned int src_words; /* # words of input image*/ | ||
142 | |||
143 | /* calc bytes per output row*/ | ||
144 | dst_linelen = (height-1)/8+1; | ||
145 | |||
146 | /* calc words of input image*/ | ||
147 | src_words = MWIMAGE_WORDS(width) * height; | ||
148 | |||
149 | /* clear background*/ | ||
150 | memset(dst, 0, dst_linelen*height); | ||
151 | |||
152 | for (i=0; i < src_words; i++) { | ||
153 | MWIMAGEBITS srcmap; /* current src input bit*/ | ||
154 | MWIMAGEBITS dstmap; /* current dst output bit*/ | ||
155 | |||
156 | /* calc src input bit*/ | ||
157 | srcmap = 1 << (sizeof(MWIMAGEBITS)*8-1); | ||
158 | |||
159 | /* calc dst output bit*/ | ||
160 | if (i>0 && (i%8==0)) { | ||
161 | ++dst_col; | ||
162 | dst_shift = 0; | ||
163 | } | ||
164 | dstmap = 1 << dst_shift++; | ||
165 | |||
166 | /* for each input column...*/ | ||
167 | for(j=0; j < width; j++) { | ||
168 | |||
169 | /* calc input bitmask*/ | ||
170 | MWIMAGEBITS bit = srcmap >> j; | ||
171 | if (bit==0) { | ||
172 | srcmap = 1 << (sizeof(MWIMAGEBITS)*8-1); | ||
173 | bit = srcmap >> (j % 16); | ||
174 | } | ||
175 | |||
176 | /* if set in input, set in rotated output*/ | ||
177 | if (bit & src[i]) { | ||
178 | /* input column j becomes output row*/ | ||
179 | dst[j*dst_linelen + dst_col] |= dstmap; | ||
180 | } | ||
181 | //printf((bit & src[i])? "*": "."); | ||
182 | } | ||
183 | //printf("\n"); | ||
184 | } | ||
185 | } | ||
186 | |||
187 | /* | ||
188 | * Put a string at specified bit position | ||
189 | */ | ||
190 | //FIXME rename font_putsxy? | ||
191 | void | ||
192 | lcd_putsxy(int x, int y, unsigned char *str, int font) | ||
193 | { | ||
194 | int ch; | ||
195 | unsigned char *src; | ||
196 | PMWCFONT pf = getfont(font); | ||
197 | |||
198 | while (((ch = *str++) != '\0')) { | ||
199 | MWIMAGEBITS *bits; | ||
200 | int width; | ||
201 | unsigned char outbuf[256]; | ||
202 | |||
203 | /* check input range*/ | ||
204 | if (ch < pf->firstchar || ch >= pf->firstchar+pf->size) | ||
205 | ch = pf->defaultchar; | ||
206 | ch -= pf->firstchar; | ||
207 | |||
208 | /* get proportional width and glyph bits*/ | ||
209 | width = pf->width? pf->width[ch]: pf->maxwidth; | ||
210 | if(x + width > LCD_WIDTH) | ||
211 | break; | ||
212 | bits = pf->bits + (pf->offset? pf->offset[ch]: (pf->height * ch)); | ||
213 | |||
214 | /* rotate left for lcd_bitmap function input*/ | ||
215 | rotleft(outbuf, bits, width, pf->height); | ||
216 | src = outbuf; | ||
217 | |||
218 | lcd_bitmap (src, x, y, width, pf->height, true); | ||
219 | x += width; | ||
220 | } | ||
221 | } | ||
222 | #endif /* HAVE_LCD_BITMAP */ | ||
223 | |||
224 | /* ----------------------------------------------------------------- | ||
225 | * local variables: | ||
226 | * eval: (load-file "rockbox-mode.el") | ||
227 | * end: | ||
228 | */ | ||