diff options
author | Daniel Stenberg <daniel@haxx.se> | 2002-09-12 13:33:59 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2002-09-12 13:33:59 +0000 |
commit | 93b231c69366563ba441dc4907bfb036fe3b4c55 (patch) | |
tree | 0783ad028211f59e63925a354e4260a7209ffa24 /firmware/loadfont.c | |
parent | 5ed78ea80cdaa0ede4df89568f0781fa477a5738 (diff) | |
download | rockbox-93b231c69366563ba441dc4907bfb036fe3b4c55.tar.gz rockbox-93b231c69366563ba441dc4907bfb036fe3b4c55.zip |
Greg Haerr's new loadable font. No more #ifdef font-style, removed old
propfont and loadable font code. New font file format.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2269 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/loadfont.c')
-rw-r--r-- | firmware/loadfont.c | 202 |
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*/ | ||
40 | static unsigned char mbuf[MAX_FONT_SIZE]; | ||
41 | static unsigned char *freeptr = mbuf; | ||
42 | typedef unsigned char CFILE; | ||
43 | static CFILE *fileptr; | ||
44 | static CFILE *eofptr; | ||
45 | |||
46 | static int | ||
47 | READSHORT(unsigned short *sp) | ||
48 | { | ||
49 | unsigned short s; | ||
50 | |||
51 | s = *fileptr++ & 0xff; | ||
52 | *sp = (*fileptr++ << 8) | s; | ||
53 | return (fileptr <= eofptr); | ||
54 | } | ||
55 | |||
56 | static int | ||
57 | READLONG(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*/ | ||
69 | static int | ||
70 | READSTR(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*/ | ||
81 | static int | ||
82 | READSTRPAD(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*/ | ||
100 | PMWCFONT | ||
101 | rbf_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 | */ | ||