Fix test_codec for r19704 (prefix removed from plugin API).
[kugel-rb.git] / firmware / font.c
blobe02f276efee5cfe9fc4be1329fed9e8f9df7c77c
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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.
27 #include "config.h"
29 #include <stdio.h>
30 #include <string.h>
31 #include "inttypes.h"
32 #include "lcd.h"
33 #include "font.h"
34 #include "file.h"
35 #include "debug.h"
36 #include "panic.h"
37 #include "rbunicode.h"
39 #ifndef BOOTLOADER
40 /* Font cache includes */
41 #include "font_cache.h"
42 #include "lru.h"
43 #endif
45 #ifndef O_BINARY
46 #define O_BINARY 0
47 #endif
49 /* compiled-in font */
50 extern struct font sysfont;
52 #ifndef BOOTLOADER
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);
78 void font_init(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
88 function.
91 static short readshort(void)
93 unsigned short s;
95 s = *fileptr++ & 0xff;
96 s |= (*fileptr++ << 8);
97 return s;
100 static int32_t readlong(void)
102 uint32_t l;
104 l = *fileptr++ & 0xff;
105 l |= *fileptr++ << 8;
106 l |= ((uint32_t)(*fileptr++)) << 16;
107 l |= ((uint32_t)(*fileptr++)) << 24;
108 return l;
111 /* read count bytes*/
112 static void readstr(char *buf, int count)
114 while (count--)
115 *buf++ = *fileptr++;
118 void font_reset(void)
120 memset(&font_ui, 0, sizeof(struct font));
123 static struct font* font_load_header(struct font *pf)
125 char version[4+1];
127 /* Check we have enough data */
128 if (!HAVEBYTES(28))
129 return NULL;
131 /* read magic and version #*/
132 memset(version, 0, sizeof(version));
133 readstr(version, 4);
135 if (strcmp(version, VERSION) != 0)
136 return NULL;
138 /* font info*/
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();
151 return pf;
153 /* Load memory font */
154 static struct font* font_load_in_memory(struct font* pf)
156 int32_t i, noffset, nwidth;
158 if (!HAVEBYTES(4))
159 return NULL;
161 /* # longs of offset*/
162 noffset = readlong();
164 /* # bytes of width*/
165 nwidth = readlong();
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);
176 else
178 /* pad to 32-bit boundary*/
179 fileptr = (unsigned char *)(((intptr_t)fileptr + 3) & ~3);
182 if (noffset)
184 if ( pf->bits_size < 0xFFDB )
186 long_offset = 0;
187 pf->offset = (unsigned short *)fileptr;
189 /* Check we have sufficient buffer */
190 if (!HAVEBYTES(noffset * sizeof(short)))
191 return NULL;
193 for (i=0; i<noffset; ++i)
195 ((unsigned short*)(pf->offset))[i] = (unsigned short)readshort();
198 else
200 long_offset = 1;
201 pf->offset = (unsigned short *)fileptr;
203 /* Check we have sufficient buffer */
204 if (!HAVEBYTES(noffset * sizeof(int32_t)))
205 return NULL;
207 for (i=0; i<noffset; ++i)
209 ((uint32_t*)(pf->offset))[i] = (uint32_t)readlong();
213 else
214 pf->offset = NULL;
216 if (nwidth) {
217 pf->width = (unsigned char *)fileptr;
218 fileptr += nwidth*sizeof(unsigned char);
220 else
221 pf->width = NULL;
223 if (fileptr > eofptr)
224 return NULL;
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)))
236 return NULL;
238 /* # longs of offset*/
239 noffset = readlong();
241 /* # bytes of width*/
242 nwidth = readlong();
244 /* We are now at the bitmap data, this is fixed at 36.. */
245 pf->bits = NULL;
247 /* Calculate offset to offset data */
248 fileptr += pf->bits_size * sizeof(unsigned char);
250 if ( pf->bits_size < 0xFFDB )
252 long_offset = 0;
253 /* pad to 16-bit boundary */
254 fileptr = (unsigned char *)(((intptr_t)fileptr + 1) & ~1);
256 else
258 long_offset = 1;
259 /* pad to 32-bit boundary*/
260 fileptr = (unsigned char *)(((intptr_t)fileptr + 3) & ~3);
263 if (noffset)
264 file_offset_offset = (uint32_t)(fileptr - freeptr);
265 else
266 file_offset_offset = 0;
268 /* Calculate offset to widths data */
269 if ( pf->bits_size < 0xFFDB )
270 fileptr += noffset * sizeof(unsigned short);
271 else
272 fileptr += noffset * sizeof(uint32_t);
274 if (nwidth)
275 file_width_offset = (uint32_t)(fileptr - freeptr);
276 else
277 file_width_offset = 0;
279 fileptr = oldfileptr;
281 /* Create the cache */
282 cache_create(pf->maxwidth, pf->height);
284 return pf;
287 /* read and load font into incore font structure*/
288 struct font* font_load(const char *path)
290 int size;
291 struct font* pf = &font_ui;
293 /* save loaded glyphs */
294 glyph_cache_save();
296 /* Close font file handle */
297 if (fnt_file >= 0)
298 close(fnt_file);
300 /* open and read entire font file*/
301 fnt_file = open(path, O_RDONLY|O_BINARY);
303 if (fnt_file < 0) {
304 DEBUGF("Can't open font: %s\n", path);
305 return NULL;
308 /* Check file size */
309 size = filesize(fnt_file);
311 font_reset();
313 /* currently, font loading replaces earlier font allocation*/
314 freeptr = (unsigned char *)(((intptr_t)mbuf + 3) & ~3);
315 fileptr = freeptr;
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");
326 return NULL;
329 if (!font_load_cached(pf))
331 DEBUGF("Failed font cache load");
332 return NULL;
335 glyph_cache_load();
337 else
339 read(fnt_file, fileptr, MAX_FONT_SIZE);
340 eofptr = fileptr + size;
341 close(fnt_file);
342 fnt_file = -1;
344 if (!font_load_header(pf))
346 DEBUGF("Failed font header load");
347 return NULL;
350 if (!font_load_in_memory(pf))
352 DEBUGF("Failed mem load");
353 return NULL;
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)
371 struct font* pf;
373 if (font >= MAXFONTS)
374 font = 0;
376 while (1) {
377 pf = sysfonts[font];
378 if (pf && pf->height)
379 return pf;
380 if (--font < 0)
381 panicf("No font!");
386 * Reads an entry into cache entry
388 static void
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);
401 else
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);
414 if (long_offset) {
415 read (fnt_file, tmp, 2);
416 bitmap_offset |= (tmp[0] << 16) | (tmp[1] << 24);
419 else
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)
469 bits =
470 (unsigned char*)font_cache_get(&font_cache_ui,char_code,load_cache_entry,pf)->bitmap;
472 else
474 bits = pf->bits + (pf->offset?
475 pf->offset[char_code]:
476 (((pf->height + 7) / 8) * pf->maxwidth * char_code));
479 return bits;
482 static void glyph_file_write(void* data)
484 struct font_cache_entry* p = data;
485 struct font* pf = &font_ui;
486 unsigned short ch;
487 unsigned char tmp[2];
489 ch = p->_char_code + pf->firstchar;
491 if (ch != 0xffff && glyph_file >= 0) {
492 tmp[0] = ch >> 8;
493 tmp[1] = ch & 0xff;
494 if (write(glyph_file, tmp, 2) != 2) {
495 close(glyph_file);
496 glyph_file = -1;
499 return;
502 /* save the char codes of the loaded glyphs to a file */
503 void glyph_cache_save(void)
506 if (fnt_file >= 0) {
507 #ifdef WPSEDITOR
508 glyph_file = open(GLYPH_CACHE_FILE, O_WRONLY|O_CREAT|O_TRUNC);
509 #else
510 glyph_file = creat(GLYPH_CACHE_FILE);
511 #endif
512 if (glyph_file < 0) return;
514 lru_traverse(&font_cache_ui._lru, glyph_file_write);
516 if (glyph_file >= 0)
517 close(glyph_file);
519 return;
522 static void glyph_cache_load(void)
524 if (fnt_file >= 0) {
526 int fd;
527 unsigned char tmp[2];
528 unsigned short ch;
529 struct font* pf = &font_ui;
531 fd = open(GLYPH_CACHE_FILE, O_RDONLY|O_BINARY);
533 if (fd >= 0) {
535 while (read(fd, tmp, 2) == 2) {
536 ch = (tmp[0] << 8) | tmp[1];
537 font_get_bits(pf, ch);
540 close(fd);
541 } else {
542 /* load latin1 chars into cache */
543 ch = 256;
544 while (ch-- > 32)
545 font_get_bits(pf, ch);
548 return;
550 #else /* BOOTLOADER */
552 void font_init(void)
557 * Bootloader only supports the built-in sysfont.
559 struct font* font_get(int font)
561 (void)font;
562 return &sysfont;
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));
591 return bits;
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);
602 unsigned short ch;
603 int width = 0;
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);
611 if ( w )
612 *w = width;
613 if ( h )
614 *h = pf->height;
615 return width;
618 /* -----------------------------------------------------------------
619 * vim: et sw=4 ts=8 sts=4 tw=78