summaryrefslogtreecommitdiff
path: root/firmware/loadfont.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/loadfont.c')
-rw-r--r--firmware/loadfont.c202
1 files changed, 202 insertions, 0 deletions
diff --git a/firmware/loadfont.c b/firmware/loadfont.c
new file mode 100644
index 0000000000..7f572a4841
--- /dev/null
+++ b/firmware/loadfont.c
@@ -0,0 +1,202 @@
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 * Load an rbf font, store in incore format.
21 */
22#include "config.h"
23
24#if defined(HAVE_LCD_BITMAP) || defined(SIMULATOR)
25
26#include <stdio.h>
27#include <string.h>
28#include "font.h"
29#include "file.h"
30
31#ifndef DEBUGF
32#include "debug.h"
33#endif
34
35#ifndef O_BINARY
36#define O_BINARY 0
37#endif
38
39/* static buffer allocation structures*/
40static unsigned char mbuf[MAX_FONT_SIZE];
41static unsigned char *freeptr = mbuf;
42typedef unsigned char CFILE;
43static CFILE *fileptr;
44static CFILE *eofptr;
45
46static int
47READSHORT(unsigned short *sp)
48{
49 unsigned short s;
50
51 s = *fileptr++ & 0xff;
52 *sp = (*fileptr++ << 8) | s;
53 return (fileptr <= eofptr);
54}
55
56static int
57READLONG(unsigned long *lp)
58{
59 unsigned long l;
60
61 l = *fileptr++ & 0xff;
62 l |= *fileptr++ << 8;
63 l |= *fileptr++ << 16;
64 *lp = (*fileptr++ << 24) | l;
65 return (fileptr <= eofptr);
66}
67
68/* read count bytes*/
69static int
70READSTR(char *buf, int count)
71{
72 int n = count;
73
74 while (--n >= 0)
75 *buf++ = *fileptr++;
76 return (fileptr <= eofptr)? count: 0;
77}
78
79/* read totlen bytes, return NUL terminated string*/
80/* may write 1 past buf[totlen]; removes blank pad*/
81static int
82READSTRPAD(char *buf, int totlen)
83{
84 char *p = buf;
85 int n = totlen;
86
87 while (--n >= 0)
88 *p++ = *fileptr++;
89 if (fileptr > eofptr)
90 return 0;
91
92 p = &buf[totlen];
93 *p-- = 0;
94 while (*p == ' ' && p >= buf)
95 *p-- = '\0';
96 return totlen;
97}
98
99/* read and load font into incore font structure*/
100PMWCFONT
101rbf_load_font(char *path, PMWCFONT pf)
102{
103 int fd, filesize;
104 unsigned short maxwidth, height, ascent;
105 unsigned long firstchar, defaultchar, size;
106 unsigned long nbits, noffset, nwidth;
107 char version[4+1];
108 char copyright[256+1];
109
110 memset(pf, 0, sizeof(MWCFONT));
111
112 /* open and read entire font file*/
113 fd = open(path, O_RDONLY|O_BINARY);
114 if (fd < 0) {
115 DEBUGF("Can't open font: %s\n", path);
116 return NULL;
117 }
118 fileptr = freeptr;
119 filesize = read(fd, fileptr, MAX_FONT_SIZE);
120 freeptr += filesize;
121 eofptr = fileptr + filesize;
122 close(fd);
123 if (filesize == MAX_FONT_SIZE) {
124 DEBUGF("Font %s too large: %d\n", path, filesize);
125 return NULL;
126 }
127
128 /* read magic and version #*/
129 memset(version, 0, sizeof(version));
130 if (READSTR(version, 4) != 4)
131 return NULL;
132 if (strcmp(version, VERSION) != 0)
133 return NULL;
134
135 /* internal font name*/
136 pf->name = fileptr;
137 if (READSTRPAD(pf->name, 64) != 64)
138 return NULL;
139
140 /* copyright, not currently stored*/
141 if (READSTRPAD(copyright, 256) != 256)
142 return NULL;
143
144 /* font info*/
145 if (!READSHORT(&maxwidth))
146 return NULL;
147 pf->maxwidth = maxwidth;
148 if (!READSHORT(&height))
149 return NULL;
150 pf->height = height;
151 if (!READSHORT(&ascent))
152 return NULL;
153 pf->ascent = ascent;
154 if (!READLONG(&firstchar))
155 return NULL;
156 pf->firstchar = firstchar;
157 if (!READLONG(&defaultchar))
158 return NULL;
159 pf->defaultchar = defaultchar;
160 if (!READLONG(&size))
161 return NULL;
162 pf->size = size;
163
164 /* get variable font data sizes*/
165 /* # words of MWIMAGEBITS*/
166 if (!READLONG(&nbits))
167 return NULL;
168 pf->bits_size = nbits;
169
170 /* # longs of offset*/
171 if (!READLONG(&noffset))
172 return NULL;
173
174 /* # bytes of width*/
175 if (!READLONG(&nwidth))
176 return NULL;
177
178 /* variable font data*/
179 pf->bits = (MWIMAGEBITS *)fileptr;
180 fileptr += nbits*sizeof(MWIMAGEBITS);
181
182 if (noffset) {
183 pf->offset = (unsigned long *)fileptr;
184 fileptr += noffset*sizeof(unsigned long);
185 } else pf->offset = NULL;
186
187 if (nwidth) {
188 pf->width = (unsigned char *)fileptr;
189 fileptr += noffset*sizeof(unsigned char);
190 } else pf->width = NULL;
191
192 if (fileptr > eofptr)
193 return NULL;
194 return pf; /* success!*/
195}
196#endif /* HAVE_LCD_BITMAP */
197
198/* -----------------------------------------------------------------
199 * local variables:
200 * eval: (load-file "rockbox-mode.el")
201 * end:
202 */