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