1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
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.
27 #if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
37 #include "rbunicode.h"
38 /* Font cache includes */
39 #include "font_cache.h"
46 /* compiled-in font */
47 extern struct font sysfont
;
49 /* structure filled in by font_load */
50 static struct font font_ui
;
52 /* system font table, in order of FONT_xxx definition */
53 static struct font
* const sysfonts
[MAXFONTS
] = { &sysfont
, &font_ui
};
55 /* static buffer allocation structures */
56 static unsigned char mbuf
[MAX_FONT_SIZE
];
57 static unsigned char *freeptr
= mbuf
;
58 static unsigned char *fileptr
;
59 static unsigned char *eofptr
;
61 /* Font cache structures */
62 static struct font_cache font_cache_ui
;
63 static int fnt_file
= -1; /* >=0 if font is cached */
64 static uint32_t file_width_offset
; /* offset to file width data */
65 static uint32_t file_offset_offset
; /* offset to file offset data */
66 static void cache_create(int maxwidth
, int height
);
67 static int long_offset
= 0;
68 static int glyph_file
;
69 /* End Font cache structures */
71 static void glyph_cache_load(void);
75 memset(&font_ui
, 0, sizeof(struct font
));
78 /* Check if we have x bytes left in the file buffer */
79 #define HAVEBYTES(x) (fileptr + (x) <= eofptr)
81 /* Helper functions to read big-endian unaligned short or long from
82 the file buffer. Bounds-checking must be done in the calling
86 static short readshort(void)
90 s
= *fileptr
++ & 0xff;
91 s
|= (*fileptr
++ << 8);
95 static int32_t readlong(void)
99 l
= *fileptr
++ & 0xff;
100 l
|= *fileptr
++ << 8;
101 l
|= ((uint32_t)(*fileptr
++)) << 16;
102 l
|= ((uint32_t)(*fileptr
++)) << 24;
106 /* read count bytes*/
107 static void readstr(char *buf
, int count
)
113 void font_reset(void)
115 memset(&font_ui
, 0, sizeof(struct font
));
118 static struct font
* font_load_header(struct font
*pf
)
122 /* Check we have enough data */
126 /* read magic and version #*/
127 memset(version
, 0, sizeof(version
));
130 if (strcmp(version
, VERSION
) != 0)
134 pf
->maxwidth
= readshort();
135 pf
->height
= readshort();
136 pf
->ascent
= readshort();
137 fileptr
+= 2; /* Skip padding */
138 pf
->firstchar
= readlong();
139 pf
->defaultchar
= readlong();
140 pf
->size
= readlong();
142 /* get variable font data sizes*/
143 /* # words of bitmap_t*/
144 pf
->bits_size
= readlong();
148 /* Load memory font */
149 static struct font
* font_load_in_memory(struct font
* pf
)
151 int32_t i
, noffset
, nwidth
;
156 /* # longs of offset*/
157 noffset
= readlong();
159 /* # bytes of width*/
162 /* variable font data*/
163 pf
->bits
= (unsigned char *)fileptr
;
164 fileptr
+= pf
->bits_size
*sizeof(unsigned char);
166 if ( pf
->bits_size
< 0xFFDB )
168 /* pad to 16-bit boundary */
169 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 1) & ~1);
173 /* pad to 32-bit boundary*/
174 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 3) & ~3);
179 if ( pf
->bits_size
< 0xFFDB )
182 pf
->offset
= (unsigned short *)fileptr
;
184 /* Check we have sufficient buffer */
185 if (!HAVEBYTES(noffset
* sizeof(short)))
188 for (i
=0; i
<noffset
; ++i
)
190 ((unsigned short*)(pf
->offset
))[i
] = (unsigned short)readshort();
196 pf
->offset
= (unsigned short *)fileptr
;
198 /* Check we have sufficient buffer */
199 if (!HAVEBYTES(noffset
* sizeof(int32_t)))
202 for (i
=0; i
<noffset
; ++i
)
204 ((uint32_t*)(pf
->offset
))[i
] = (uint32_t)readlong();
212 pf
->width
= (unsigned char *)fileptr
;
213 fileptr
+= nwidth
*sizeof(unsigned char);
218 if (fileptr
> eofptr
)
221 return pf
; /* success!*/
224 /* Load cached font */
225 static struct font
* font_load_cached(struct font
* pf
)
227 uint32_t noffset
, nwidth
;
228 unsigned char* oldfileptr
= fileptr
;
230 if (!HAVEBYTES(2 * sizeof(int32_t)))
233 /* # longs of offset*/
234 noffset
= readlong();
236 /* # bytes of width*/
239 /* We are now at the bitmap data, this is fixed at 36.. */
242 /* Calculate offset to offset data */
243 fileptr
+= pf
->bits_size
* sizeof(unsigned char);
245 if ( pf
->bits_size
< 0xFFDB )
248 /* pad to 16-bit boundary */
249 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 1) & ~1);
254 /* pad to 32-bit boundary*/
255 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 3) & ~3);
259 file_offset_offset
= (uint32_t)(fileptr
- freeptr
);
261 file_offset_offset
= 0;
263 /* Calculate offset to widths data */
264 if ( pf
->bits_size
< 0xFFDB )
265 fileptr
+= noffset
* sizeof(unsigned short);
267 fileptr
+= noffset
* sizeof(uint32_t);
270 file_width_offset
= (uint32_t)(fileptr
- freeptr
);
272 file_width_offset
= 0;
274 fileptr
= oldfileptr
;
276 /* Create the cache */
277 cache_create(pf
->maxwidth
, pf
->height
);
282 /* read and load font into incore font structure*/
283 struct font
* font_load(const char *path
)
286 struct font
* pf
= &font_ui
;
288 /* save loaded glyphs */
291 /* Close font file handle */
295 /* open and read entire font file*/
296 fnt_file
= open(path
, O_RDONLY
|O_BINARY
);
299 DEBUGF("Can't open font: %s\n", path
);
303 /* Check file size */
304 size
= filesize(fnt_file
);
308 /* currently, font loading replaces earlier font allocation*/
309 freeptr
= (unsigned char *)(((intptr_t)mbuf
+ 3) & ~3);
313 if (size
> MAX_FONT_SIZE
)
315 read(fnt_file
, fileptr
, FONT_HEADER_SIZE
);
316 eofptr
= fileptr
+ FONT_HEADER_SIZE
;
318 if (!font_load_header(pf
))
320 DEBUGF("Failed font header load");
324 if (!font_load_cached(pf
))
326 DEBUGF("Failed font cache load");
334 read(fnt_file
, fileptr
, MAX_FONT_SIZE
);
335 eofptr
= fileptr
+ size
;
339 if (!font_load_header(pf
))
341 DEBUGF("Failed font header load");
345 if (!font_load_in_memory(pf
))
347 DEBUGF("Failed mem load");
352 /* no need for multiple font loads currently*/
353 /*freeptr += filesize;*/
354 /*freeptr = (unsigned char *)(freeptr + 3) & ~3;*/ /* pad freeptr*/
356 return pf
; /* success!*/
360 * Return a pointer to an incore font structure.
361 * If the requested font isn't loaded/compiled-in,
362 * decrement the font number and try again.
364 struct font
* font_get(int font
)
368 if (font
>= MAXFONTS
)
373 if (pf
&& pf
->height
)
380 * Returns the stringsize of a given string.
382 int font_getstringsize(const unsigned char *str
, int *w
, int *h
, int fontnumber
)
384 struct font
* pf
= font_get(fontnumber
);
388 for (str
= utf8decode(str
, &ch
); ch
!= 0 ; str
= utf8decode(str
, &ch
))
391 /* get proportional width and glyph bits*/
392 width
+= font_get_width(pf
,ch
);
402 * Reads an entry into cache entry
405 load_cache_entry(struct font_cache_entry
* p
, void* callback_data
)
407 struct font
* pf
= callback_data
;
408 unsigned short char_code
= p
->_char_code
;
409 unsigned char tmp
[2];
411 if (file_width_offset
)
413 int width_offset
= file_width_offset
+ char_code
;
414 lseek(fnt_file
, width_offset
, SEEK_SET
);
415 read(fnt_file
, &(p
->width
), 1);
419 p
->width
= pf
->maxwidth
;
422 int32_t bitmap_offset
= 0;
424 if (file_offset_offset
)
426 int32_t offset
= file_offset_offset
+ char_code
* (long_offset
? sizeof(int32_t) : sizeof(short));
427 lseek(fnt_file
, offset
, SEEK_SET
);
428 read (fnt_file
, tmp
, 2);
429 bitmap_offset
= tmp
[0] | (tmp
[1] << 8);
431 read (fnt_file
, tmp
, 2);
432 bitmap_offset
|= (tmp
[0] << 16) | (tmp
[1] << 24);
437 bitmap_offset
= ((pf
->height
+ 7) / 8) * p
->width
* char_code
;
440 int32_t file_offset
= FONT_HEADER_SIZE
+ bitmap_offset
;
441 lseek(fnt_file
, file_offset
, SEEK_SET
);
443 int src_bytes
= p
->width
* ((pf
->height
+ 7) / 8);
444 read(fnt_file
, p
->bitmap
, src_bytes
);
448 * Converts cbuf into a font cache
450 static void cache_create(int maxwidth
, int height
)
452 /* maximum size of rotated bitmap */
453 int bitmap_size
= maxwidth
* ((height
+ 7) / 8);
455 /* Initialise cache */
456 font_cache_create(&font_cache_ui
, mbuf
, MAX_FONT_SIZE
, bitmap_size
);
460 * Returns width of character
462 int font_get_width(struct font
* pf
, unsigned short char_code
)
464 /* check input range*/
465 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
466 char_code
= pf
->defaultchar
;
467 char_code
-= pf
->firstchar
;
469 return (fnt_file
>= 0 && pf
!= &sysfont
)?
470 font_cache_get(&font_cache_ui
,char_code
,load_cache_entry
,pf
)->width
:
471 pf
->width
? pf
->width
[char_code
]: pf
->maxwidth
;
474 const unsigned char* font_get_bits(struct font
* pf
, unsigned short char_code
)
476 const unsigned char* bits
;
478 /* check input range*/
479 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
480 char_code
= pf
->defaultchar
;
481 char_code
-= pf
->firstchar
;
483 if (fnt_file
>= 0 && pf
!= &sysfont
)
486 (unsigned char*)font_cache_get(&font_cache_ui
,char_code
,load_cache_entry
,pf
)->bitmap
;
490 bits
= pf
->bits
+ (pf
->offset
?
491 pf
->offset
[char_code
]:
492 (((pf
->height
+ 7) / 8) * pf
->maxwidth
* char_code
));
498 static void glyph_file_write(void* data
)
500 struct font_cache_entry
* p
= data
;
501 struct font
* pf
= &font_ui
;
503 unsigned char tmp
[2];
505 ch
= p
->_char_code
+ pf
->firstchar
;
507 if (ch
!= 0xffff && glyph_file
>= 0) {
510 if (write(glyph_file
, tmp
, 2) != 2) {
518 /* save the char codes of the loaded glyphs to a file */
519 void glyph_cache_save(void)
524 glyph_file
= creat(GLYPH_CACHE_FILE
);
526 if (glyph_file
< 0) return;
528 lru_traverse(&font_cache_ui
._lru
, glyph_file_write
);
536 static void glyph_cache_load(void)
541 unsigned char tmp
[2];
543 struct font
* pf
= &font_ui
;
545 fd
= open(GLYPH_CACHE_FILE
, O_RDONLY
|O_BINARY
);
549 while (read(fd
, tmp
, 2) == 2) {
550 ch
= (tmp
[0] << 8) | tmp
[1];
551 font_get_bits(pf
, ch
);
556 /* load latin1 chars into cache */
559 font_get_bits(pf
, ch
);
565 #endif /* HAVE_LCD_BITMAP */
567 /* -----------------------------------------------------------------
568 * vim: et sw=4 ts=8 sts=4 tw=78