Git init
[framework/uifw/xorg/lib/libxfont.git] / src / bitmap / bdfutils.c
1 /************************************************************************
2 Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts.
3
4                         All Rights Reserved
5
6 Permission to use, copy, modify, and distribute this software and its
7 documentation for any purpose and without fee is hereby granted,
8 provided that the above copyright notice appear in all copies and that
9 both that copyright notice and this permission notice appear in
10 supporting documentation, and that the name of Digital not be
11 used in advertising or publicity pertaining to distribution of the
12 software without specific, written prior permission.
13
14 DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
16 DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
17 ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
19 ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 SOFTWARE.
21
22 ************************************************************************/
23
24 /*
25
26 Copyright 1994, 1998  The Open Group
27
28 Permission to use, copy, modify, distribute, and sell this software and its
29 documentation for any purpose is hereby granted without fee, provided that
30 the above copyright notice appear in all copies and that both that
31 copyright notice and this permission notice appear in supporting
32 documentation.
33
34 The above copyright notice and this permission notice shall be included
35 in all copies or substantial portions of the Software.
36
37 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
38 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
41 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
42 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
43 OTHER DEALINGS IN THE SOFTWARE.
44
45 Except as contained in this notice, the name of The Open Group shall
46 not be used in advertising or otherwise to promote the sale, use or
47 other dealings in this Software without prior written authorization
48 from The Open Group.
49
50 */
51
52 #ifdef HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <ctype.h>
57 #include <stdio.h>
58 #include <stdarg.h>
59
60 #include <X11/fonts/fntfilst.h>
61 #include <X11/fonts/fontstruct.h>
62 /* use bitmap structure */
63 #include <X11/fonts/bitmap.h>
64 #include <X11/fonts/bdfint.h>
65
66 int bdfFileLineNum;
67
68 /***====================================================================***/
69
70 void
71 bdfError(char* message, ...)
72 {
73     va_list args;
74
75     va_start (args, message);
76     fprintf(stderr, "BDF Error on line %d: ", bdfFileLineNum);
77     vfprintf(stderr, message, args);
78     va_end (args);
79 }
80
81 /***====================================================================***/
82
83 void
84 bdfWarning(char *message, ...)
85 {
86     va_list args;
87
88     va_start (args, message);
89     fprintf(stderr, "BDF Warning on line %d: ", bdfFileLineNum);
90     vfprintf(stderr, message, args);
91     va_end (args);
92 }
93
94 /*
95  * read the next (non-comment) line and keep a count for error messages.
96  * Returns buf, or NULL if EOF.
97  */
98
99 unsigned char *
100 bdfGetLine(FontFilePtr file, unsigned char *buf, int len)
101 {
102     int         c;
103     unsigned char *b;
104
105     for (;;) {
106         b = buf;
107         while ((c = FontFileGetc(file)) != FontFileEOF) {
108             if (c == '\r')
109                 continue;
110             if (c == '\n') {
111                 bdfFileLineNum++;
112                 break;
113             }
114             if (b - buf >= (len - 1))
115                 break;
116             *b++ = c;
117         }
118         *b = '\0';
119         if (c == FontFileEOF)
120             return NULL;
121         if (b != buf && !bdfIsPrefix(buf, "COMMENT"))
122             break;
123     }
124     return buf;
125 }
126
127 /***====================================================================***/
128
129 Atom
130 bdfForceMakeAtom(char *str, int *size)
131 {
132     register int len = strlen(str);
133     Atom the_atom;
134
135     if (size != NULL)
136         *size += len + 1;
137     the_atom = MakeAtom(str, len, TRUE);
138     if (the_atom == None)
139       bdfError("Atom allocation failed\n");
140     return the_atom;
141 }
142
143 /***====================================================================***/
144
145 /*
146  * Handle quoted strings.
147  */
148
149 Atom
150 bdfGetPropertyValue(char *s)
151 {
152     register char *p,
153                *pp;
154     char *orig_s = s;
155     Atom        atom;
156
157     /* strip leading white space */
158     while (*s && (*s == ' ' || *s == '\t'))
159         s++;
160     if (*s == 0) {
161         return bdfForceMakeAtom(s, NULL);
162     }
163     if (*s != '"') {
164         pp = s;
165         /* no white space in value */
166         for (pp = s; *pp; pp++)
167             if (*pp == ' ' || *pp == '\t' || *pp == '\015' || *pp == '\n') {
168                 *pp = 0;
169                 break;
170             }
171         return bdfForceMakeAtom(s, NULL);
172     }
173     /* quoted string: strip outer quotes and undouble inner quotes */
174     s++;
175     pp = p = malloc((unsigned) strlen(s) + 1);
176     if (pp == NULL) {
177   bdfError("Couldn't allocate property value string (%d)\n", strlen(s) + 1);
178   return None;
179     }
180     while (*s) {
181         if (*s == '"') {
182             if (*(s + 1) != '"') {
183                 *p++ = 0;
184                 atom = bdfForceMakeAtom(pp, NULL);
185                 free(pp);
186                 return atom;
187             } else {
188                 s++;
189             }
190         }
191         *p++ = *s++;
192     }
193     free (pp);
194     bdfError("unterminated quoted string property: %s\n", (pointer) orig_s);
195     return None;
196 }
197
198 /***====================================================================***/
199
200 /*
201  * return TRUE if string is a valid integer
202  */
203 int
204 bdfIsInteger(char *str)
205 {
206     char        c;
207
208     c = *str++;
209     if (!(isdigit(c) || c == '-' || c == '+'))
210         return (FALSE);
211
212     while ((c = *str++))
213         if (!isdigit(c))
214             return (FALSE);
215
216     return (TRUE);
217 }
218
219 /***====================================================================***/
220
221 /*
222  * make a byte from the first two hex characters in glyph picture
223  */
224
225 unsigned char
226 bdfHexByte(unsigned char *s)
227 {
228     unsigned char b = 0;
229     register char c;
230     int         i;
231
232     for (i = 2; i; i--) {
233         c = *s++;
234         if ((c >= '0') && (c <= '9'))
235             b = (b << 4) + (c - '0');
236         else if ((c >= 'A') && (c <= 'F'))
237             b = (b << 4) + 10 + (c - 'A');
238         else if ((c >= 'a') && (c <= 'f'))
239             b = (b << 4) + 10 + (c - 'a');
240         else
241             bdfError("bad hex char '%c'", c);
242     }
243     return b;
244 }
245
246 /***====================================================================***/
247
248 /*
249  * check for known special property values
250  */
251
252 static char *SpecialAtoms[] = {
253     "FONT_ASCENT",
254 #define BDF_FONT_ASCENT 0
255     "FONT_DESCENT",
256 #define BDF_FONT_DESCENT 1
257     "DEFAULT_CHAR",
258 #define BDF_DEFAULT_CHAR 2
259     "POINT_SIZE",
260 #define BDF_POINT_SIZE 3
261     "RESOLUTION",
262 #define BDF_RESOLUTION 4
263     "X_HEIGHT",
264 #define BDF_X_HEIGHT 5
265     "WEIGHT",
266 #define BDF_WEIGHT 6
267     "QUAD_WIDTH",
268 #define BDF_QUAD_WIDTH 7
269     "FONT",
270 #define BDF_FONT 8
271     "RESOLUTION_X",
272 #define BDF_RESOLUTION_X 9
273     "RESOLUTION_Y",
274 #define BDF_RESOLUTION_Y 10
275     0,
276 };
277
278 Bool
279 bdfSpecialProperty(FontPtr pFont, FontPropPtr prop, 
280                    char isString, bdfFileState *bdfState)
281 {
282     char      **special;
283     char       *name;
284
285     name = NameForAtom(prop->name);
286     for (special = SpecialAtoms; *special; special++)
287         if (!strcmp(name, *special))
288             break;
289
290     switch (special - SpecialAtoms) {
291     case BDF_FONT_ASCENT:
292         if (!isString) {
293             pFont->info.fontAscent = prop->value;
294             bdfState->haveFontAscent = TRUE;
295         }
296         return TRUE;
297     case BDF_FONT_DESCENT:
298         if (!isString) {
299             pFont->info.fontDescent = prop->value;
300             bdfState->haveFontDescent = TRUE;
301         }
302         return TRUE;
303     case BDF_DEFAULT_CHAR:
304         if (!isString) {
305             pFont->info.defaultCh = prop->value;
306             bdfState->haveDefaultCh = TRUE;
307         }
308         return TRUE;
309     case BDF_POINT_SIZE:
310         bdfState->pointSizeProp = prop;
311         return FALSE;
312     case BDF_RESOLUTION:
313         bdfState->resolutionProp = prop;
314         return FALSE;
315     case BDF_X_HEIGHT:
316         bdfState->xHeightProp = prop;
317         return FALSE;
318     case BDF_WEIGHT:
319         bdfState->weightProp = prop;
320         return FALSE;
321     case BDF_QUAD_WIDTH:
322         bdfState->quadWidthProp = prop;
323         return FALSE;
324     case BDF_FONT:
325         bdfState->fontProp = prop;
326         return FALSE;
327     case BDF_RESOLUTION_X:
328         bdfState->resolutionXProp = prop;
329         return FALSE;
330     case BDF_RESOLUTION_Y:
331         bdfState->resolutionYProp = prop;
332         return FALSE;
333     default:
334         return FALSE;
335     }
336 }