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)
41 /* compiled-in font */
42 extern struct font sysfont
;
44 /* structure filled in by font_load */
45 static struct font font_ui
;
47 /* system font table, in order of FONT_xxx definition */
48 static struct font
* const sysfonts
[MAXFONTS
] = { &sysfont
, &font_ui
};
50 /* static buffer allocation structures */
51 static unsigned char mbuf
[MAX_FONT_SIZE
];
52 static unsigned char *freeptr
= mbuf
;
53 static unsigned char *fileptr
;
54 static unsigned char *eofptr
;
58 memset(&font_ui
, 0, sizeof(struct font
));
61 static int readshort(unsigned short *sp
)
65 s
= *fileptr
++ & 0xff;
66 *sp
= (*fileptr
++ << 8) | s
;
67 return (fileptr
<= eofptr
);
70 static int readlong(unsigned long *lp
)
74 l
= *fileptr
++ & 0xff;
76 l
|= *fileptr
++ << 16;
77 *lp
= (*fileptr
++ << 24) | l
;
78 return (fileptr
<= eofptr
);
82 static int readstr(char *buf
, int count
)
88 return (fileptr
<= eofptr
)? count
: 0;
93 memset(&font_ui
, 0, sizeof(struct font
));
96 /* read and load font into incore font structure*/
97 struct font
* font_load(const char *path
)
100 unsigned short maxwidth
, height
, ascent
, pad
;
101 unsigned long firstchar
, defaultchar
, size
;
102 unsigned long i
, nbits
, noffset
, nwidth
;
104 struct font
* pf
= &font_ui
;
106 /* open and read entire font file*/
107 fd
= open(path
, O_RDONLY
|O_BINARY
);
109 DEBUGF("Can't open font: %s\n", path
);
115 /* currently, font loading replaces earlier font allocation*/
116 freeptr
= (unsigned char *)(((int)mbuf
+ 3) & ~3);
119 filesize
= read(fd
, fileptr
, MAX_FONT_SIZE
);
120 eofptr
= fileptr
+ filesize
;
122 /* no need for multiple font loads currently*/
123 /*freeptr += filesize;*/
124 /*freeptr = (unsigned char *)(freeptr + 3) & ~3;*/ /* pad freeptr*/
127 if (filesize
== MAX_FONT_SIZE
) {
128 DEBUGF("Font %s too large: %d\n", path
, filesize
);
132 /* read magic and version #*/
133 memset(version
, 0, sizeof(version
));
134 if (readstr(version
, 4) != 4)
136 if (strcmp(version
, VERSION
) != 0)
140 if (!readshort(&maxwidth
))
142 pf
->maxwidth
= maxwidth
;
143 if (!readshort(&height
))
146 if (!readshort(&ascent
))
149 if (!readshort(&pad
))
151 if (!readlong(&firstchar
))
153 pf
->firstchar
= firstchar
;
154 if (!readlong(&defaultchar
))
156 pf
->defaultchar
= defaultchar
;
157 if (!readlong(&size
))
161 /* get variable font data sizes*/
162 /* # words of bitmap_t*/
163 if (!readlong(&nbits
))
166 /* # longs of offset*/
167 if (!readlong(&noffset
))
170 /* # bytes of width*/
171 if (!readlong(&nwidth
))
174 /* variable font data*/
175 pf
->bits
= (unsigned char *)fileptr
;
176 fileptr
+= nbits
*sizeof(unsigned char);
178 /* pad to 16 bit boundary*/
179 fileptr
= (unsigned char *)(((int)fileptr
+ 1) & ~1);
182 pf
->offset
= (unsigned short *)fileptr
;
183 for (i
=0; i
<noffset
; ++i
)
185 unsigned short offset
;
186 if (!readshort(&offset
))
188 ((unsigned short*)(pf
->offset
))[i
] = (unsigned short)offset
;
195 pf
->width
= (unsigned char *)fileptr
;
196 fileptr
+= nwidth
*sizeof(unsigned char);
201 if (fileptr
> eofptr
)
204 return pf
; /* success!*/
208 * Return a pointer to an incore font structure.
209 * If the requested font isn't loaded/compiled-in,
210 * decrement the font number and try again.
212 struct font
* font_get(int font
)
216 if (font
>= MAXFONTS
)
221 if (pf
&& pf
->height
)
228 #endif /* HAVE_LCD_BITMAP */
230 /* -----------------------------------------------------------------
231 * vim: et sw=4 ts=8 sts=4 tw=78