1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (c) 2002 by Greg Haerr <greg@censoft.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 * Rockbox startup font initialization
23 * This file specifies which fonts get compiled-in and
24 * loaded at startup, as well as their mapping into
25 * the FONT_SYSFIXED, FONT_UI and FONT_MP3 ids.
37 #include "rbunicode.h"
40 /* Font cache includes */
41 #include "font_cache.h"
49 /* compiled-in font */
50 extern struct font sysfont
;
54 /* structure filled in by font_load */
55 static struct font font_ui
;
57 /* system font table, in order of FONT_xxx definition */
58 static struct font
* const sysfonts
[MAXFONTS
] = { &sysfont
, &font_ui
};
60 /* static buffer allocation structures */
61 static unsigned char mbuf
[MAX_FONT_SIZE
];
62 static unsigned char *freeptr
= mbuf
;
63 static unsigned char *fileptr
;
64 static unsigned char *eofptr
;
66 /* Font cache structures */
67 static struct font_cache font_cache_ui
;
68 static int fnt_file
= -1; /* >=0 if font is cached */
69 static uint32_t file_width_offset
; /* offset to file width data */
70 static uint32_t file_offset_offset
; /* offset to file offset data */
71 static void cache_create(int maxwidth
, int height
);
72 static int long_offset
= 0;
73 static int glyph_file
;
74 /* End Font cache structures */
76 static void glyph_cache_load(void);
80 memset(&font_ui
, 0, sizeof(struct font
));
83 /* Check if we have x bytes left in the file buffer */
84 #define HAVEBYTES(x) (fileptr + (x) <= eofptr)
86 /* Helper functions to read big-endian unaligned short or long from
87 the file buffer. Bounds-checking must be done in the calling
91 static short readshort(void)
95 s
= *fileptr
++ & 0xff;
96 s
|= (*fileptr
++ << 8);
100 static int32_t readlong(void)
104 l
= *fileptr
++ & 0xff;
105 l
|= *fileptr
++ << 8;
106 l
|= ((uint32_t)(*fileptr
++)) << 16;
107 l
|= ((uint32_t)(*fileptr
++)) << 24;
111 /* read count bytes*/
112 static void readstr(char *buf
, int count
)
118 void font_reset(void)
120 memset(&font_ui
, 0, sizeof(struct font
));
123 static struct font
* font_load_header(struct font
*pf
)
127 /* Check we have enough data */
131 /* read magic and version #*/
132 memset(version
, 0, sizeof(version
));
135 if (strcmp(version
, VERSION
) != 0)
139 pf
->maxwidth
= readshort();
140 pf
->height
= readshort();
141 pf
->ascent
= readshort();
142 fileptr
+= 2; /* Skip padding */
143 pf
->firstchar
= readlong();
144 pf
->defaultchar
= readlong();
145 pf
->size
= readlong();
147 /* get variable font data sizes*/
148 /* # words of bitmap_t*/
149 pf
->bits_size
= readlong();
153 /* Load memory font */
154 static struct font
* font_load_in_memory(struct font
* pf
)
156 int32_t i
, noffset
, nwidth
;
161 /* # longs of offset*/
162 noffset
= readlong();
164 /* # bytes of width*/
167 /* variable font data*/
168 pf
->bits
= (unsigned char *)fileptr
;
169 fileptr
+= pf
->bits_size
*sizeof(unsigned char);
171 if ( pf
->bits_size
< 0xFFDB )
173 /* pad to 16-bit boundary */
174 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 1) & ~1);
178 /* pad to 32-bit boundary*/
179 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 3) & ~3);
184 if ( pf
->bits_size
< 0xFFDB )
187 pf
->offset
= (unsigned short *)fileptr
;
189 /* Check we have sufficient buffer */
190 if (!HAVEBYTES(noffset
* sizeof(short)))
193 for (i
=0; i
<noffset
; ++i
)
195 ((unsigned short*)(pf
->offset
))[i
] = (unsigned short)readshort();
201 pf
->offset
= (unsigned short *)fileptr
;
203 /* Check we have sufficient buffer */
204 if (!HAVEBYTES(noffset
* sizeof(int32_t)))
207 for (i
=0; i
<noffset
; ++i
)
209 ((uint32_t*)(pf
->offset
))[i
] = (uint32_t)readlong();
217 pf
->width
= (unsigned char *)fileptr
;
218 fileptr
+= nwidth
*sizeof(unsigned char);
223 if (fileptr
> eofptr
)
226 return pf
; /* success!*/
229 /* Load cached font */
230 static struct font
* font_load_cached(struct font
* pf
)
232 uint32_t noffset
, nwidth
;
233 unsigned char* oldfileptr
= fileptr
;
235 if (!HAVEBYTES(2 * sizeof(int32_t)))
238 /* # longs of offset*/
239 noffset
= readlong();
241 /* # bytes of width*/
244 /* We are now at the bitmap data, this is fixed at 36.. */
247 /* Calculate offset to offset data */
248 fileptr
+= pf
->bits_size
* sizeof(unsigned char);
250 if ( pf
->bits_size
< 0xFFDB )
253 /* pad to 16-bit boundary */
254 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 1) & ~1);
259 /* pad to 32-bit boundary*/
260 fileptr
= (unsigned char *)(((intptr_t)fileptr
+ 3) & ~3);
264 file_offset_offset
= (uint32_t)(fileptr
- freeptr
);
266 file_offset_offset
= 0;
268 /* Calculate offset to widths data */
269 if ( pf
->bits_size
< 0xFFDB )
270 fileptr
+= noffset
* sizeof(unsigned short);
272 fileptr
+= noffset
* sizeof(uint32_t);
275 file_width_offset
= (uint32_t)(fileptr
- freeptr
);
277 file_width_offset
= 0;
279 fileptr
= oldfileptr
;
281 /* Create the cache */
282 cache_create(pf
->maxwidth
, pf
->height
);
287 /* read and load font into incore font structure*/
288 struct font
* font_load(const char *path
)
291 struct font
* pf
= &font_ui
;
293 /* save loaded glyphs */
296 /* Close font file handle */
300 /* open and read entire font file*/
301 fnt_file
= open(path
, O_RDONLY
|O_BINARY
);
304 DEBUGF("Can't open font: %s\n", path
);
308 /* Check file size */
309 size
= filesize(fnt_file
);
313 /* currently, font loading replaces earlier font allocation*/
314 freeptr
= (unsigned char *)(((intptr_t)mbuf
+ 3) & ~3);
318 if (size
> MAX_FONT_SIZE
)
320 read(fnt_file
, fileptr
, FONT_HEADER_SIZE
);
321 eofptr
= fileptr
+ FONT_HEADER_SIZE
;
323 if (!font_load_header(pf
))
325 DEBUGF("Failed font header load");
329 if (!font_load_cached(pf
))
331 DEBUGF("Failed font cache load");
339 read(fnt_file
, fileptr
, MAX_FONT_SIZE
);
340 eofptr
= fileptr
+ size
;
344 if (!font_load_header(pf
))
346 DEBUGF("Failed font header load");
350 if (!font_load_in_memory(pf
))
352 DEBUGF("Failed mem load");
357 /* no need for multiple font loads currently*/
358 /*freeptr += filesize;*/
359 /*freeptr = (unsigned char *)(freeptr + 3) & ~3;*/ /* pad freeptr*/
361 return pf
; /* success!*/
365 * Return a pointer to an incore font structure.
366 * If the requested font isn't loaded/compiled-in,
367 * decrement the font number and try again.
369 struct font
* font_get(int font
)
373 if (font
>= MAXFONTS
)
378 if (pf
&& pf
->height
)
386 * Reads an entry into cache entry
389 load_cache_entry(struct font_cache_entry
* p
, void* callback_data
)
391 struct font
* pf
= callback_data
;
392 unsigned short char_code
= p
->_char_code
;
393 unsigned char tmp
[2];
395 if (file_width_offset
)
397 int width_offset
= file_width_offset
+ char_code
;
398 lseek(fnt_file
, width_offset
, SEEK_SET
);
399 read(fnt_file
, &(p
->width
), 1);
403 p
->width
= pf
->maxwidth
;
406 int32_t bitmap_offset
= 0;
408 if (file_offset_offset
)
410 int32_t offset
= file_offset_offset
+ char_code
* (long_offset
? sizeof(int32_t) : sizeof(short));
411 lseek(fnt_file
, offset
, SEEK_SET
);
412 read (fnt_file
, tmp
, 2);
413 bitmap_offset
= tmp
[0] | (tmp
[1] << 8);
415 read (fnt_file
, tmp
, 2);
416 bitmap_offset
|= (tmp
[0] << 16) | (tmp
[1] << 24);
421 bitmap_offset
= ((pf
->height
+ 7) / 8) * p
->width
* char_code
;
424 int32_t file_offset
= FONT_HEADER_SIZE
+ bitmap_offset
;
425 lseek(fnt_file
, file_offset
, SEEK_SET
);
427 int src_bytes
= p
->width
* ((pf
->height
+ 7) / 8);
428 read(fnt_file
, p
->bitmap
, src_bytes
);
432 * Converts cbuf into a font cache
434 static void cache_create(int maxwidth
, int height
)
436 /* maximum size of rotated bitmap */
437 int bitmap_size
= maxwidth
* ((height
+ 7) / 8);
439 /* Initialise cache */
440 font_cache_create(&font_cache_ui
, mbuf
, MAX_FONT_SIZE
, bitmap_size
);
444 * Returns width of character
446 int font_get_width(struct font
* pf
, unsigned short char_code
)
448 /* check input range*/
449 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
450 char_code
= pf
->defaultchar
;
451 char_code
-= pf
->firstchar
;
453 return (fnt_file
>= 0 && pf
!= &sysfont
)?
454 font_cache_get(&font_cache_ui
,char_code
,load_cache_entry
,pf
)->width
:
455 pf
->width
? pf
->width
[char_code
]: pf
->maxwidth
;
458 const unsigned char* font_get_bits(struct font
* pf
, unsigned short char_code
)
460 const unsigned char* bits
;
462 /* check input range*/
463 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
464 char_code
= pf
->defaultchar
;
465 char_code
-= pf
->firstchar
;
467 if (fnt_file
>= 0 && pf
!= &sysfont
)
470 (unsigned char*)font_cache_get(&font_cache_ui
,char_code
,load_cache_entry
,pf
)->bitmap
;
474 bits
= pf
->bits
+ (pf
->offset
?
475 pf
->offset
[char_code
]:
476 (((pf
->height
+ 7) / 8) * pf
->maxwidth
* char_code
));
482 static void glyph_file_write(void* data
)
484 struct font_cache_entry
* p
= data
;
485 struct font
* pf
= &font_ui
;
487 unsigned char tmp
[2];
489 ch
= p
->_char_code
+ pf
->firstchar
;
491 if (ch
!= 0xffff && glyph_file
>= 0) {
494 if (write(glyph_file
, tmp
, 2) != 2) {
502 /* save the char codes of the loaded glyphs to a file */
503 void glyph_cache_save(void)
508 glyph_file
= open(GLYPH_CACHE_FILE
, O_WRONLY
|O_CREAT
|O_TRUNC
);
510 glyph_file
= creat(GLYPH_CACHE_FILE
);
512 if (glyph_file
< 0) return;
514 lru_traverse(&font_cache_ui
._lru
, glyph_file_write
);
522 static void glyph_cache_load(void)
527 unsigned char tmp
[2];
529 struct font
* pf
= &font_ui
;
531 fd
= open(GLYPH_CACHE_FILE
, O_RDONLY
|O_BINARY
);
535 while (read(fd
, tmp
, 2) == 2) {
536 ch
= (tmp
[0] << 8) | tmp
[1];
537 font_get_bits(pf
, ch
);
542 /* load latin1 chars into cache */
545 font_get_bits(pf
, ch
);
550 #else /* BOOTLOADER */
557 * Bootloader only supports the built-in sysfont.
559 struct font
* font_get(int font
)
566 * Returns width of character
568 int font_get_width(struct font
* pf
, unsigned short char_code
)
570 /* check input range*/
571 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
572 char_code
= pf
->defaultchar
;
573 char_code
-= pf
->firstchar
;
575 return pf
->width
? pf
->width
[char_code
]: pf
->maxwidth
;
578 const unsigned char* font_get_bits(struct font
* pf
, unsigned short char_code
)
580 const unsigned char* bits
;
582 /* check input range*/
583 if (char_code
< pf
->firstchar
|| char_code
>= pf
->firstchar
+pf
->size
)
584 char_code
= pf
->defaultchar
;
585 char_code
-= pf
->firstchar
;
587 bits
= pf
->bits
+ (pf
->offset
?
588 pf
->offset
[char_code
]:
589 (((pf
->height
+ 7) / 8) * pf
->maxwidth
* char_code
));
594 #endif /* BOOTLOADER */
597 * Returns the stringsize of a given string.
599 int font_getstringsize(const unsigned char *str
, int *w
, int *h
, int fontnumber
)
601 struct font
* pf
= font_get(fontnumber
);
605 for (str
= utf8decode(str
, &ch
); ch
!= 0 ; str
= utf8decode(str
, &ch
))
608 /* get proportional width and glyph bits*/
609 width
+= font_get_width(pf
,ch
);
618 /* -----------------------------------------------------------------
619 * vim: et sw=4 ts=8 sts=4 tw=78