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