bdfutils.c revision 23a0898a
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#ifndef FONTMODULE
59#include <ctype.h>
60#include <stdio.h>
61#include <stdarg.h>
62#endif
63
64#include <X11/fonts/fntfilst.h>
65#include <X11/fonts/fontstruct.h>
66/* use bitmap structure */
67#include <X11/fonts/bitmap.h>
68#include <X11/fonts/bdfint.h>
69
70int bdfFileLineNum;
71
72/***====================================================================***/
73
74void
75bdfError(char* message, ...)
76{
77    va_list args;
78
79    va_start (args, message);
80    fprintf(stderr, "BDF Error on line %d: ", bdfFileLineNum);
81    vfprintf(stderr, message, args);
82    va_end (args);
83}
84
85/***====================================================================***/
86
87void
88bdfWarning(char *message, ...)
89{
90    va_list args;
91
92    va_start (args, message);
93    fprintf(stderr, "BDF Warning on line %d: ", bdfFileLineNum);
94    vfprintf(stderr, message, args);
95    va_end (args);
96}
97
98/*
99 * read the next (non-comment) line and keep a count for error messages.
100 * Returns buf, or NULL if EOF.
101 */
102
103unsigned char *
104bdfGetLine(FontFilePtr file, unsigned char *buf, int len)
105{
106    int         c;
107    unsigned char *b;
108
109    for (;;) {
110	b = buf;
111	while ((c = FontFileGetc(file)) != FontFileEOF) {
112	    if (c == '\r')
113		continue;
114	    if (c == '\n') {
115		bdfFileLineNum++;
116		break;
117	    }
118	    if (b - buf >= (len - 1))
119		break;
120	    *b++ = c;
121	}
122	*b = '\0';
123	if (c == FontFileEOF)
124	    return NULL;
125	if (b != buf && !bdfIsPrefix(buf, "COMMENT"))
126	    break;
127    }
128    return buf;
129}
130
131/***====================================================================***/
132
133Atom
134bdfForceMakeAtom(char *str, int *size)
135{
136    register int len = strlen(str);
137    Atom the_atom;
138
139    if (size != NULL)
140	*size += len + 1;
141    the_atom = MakeAtom(str, len, TRUE);
142    if (the_atom == None)
143      bdfError("Atom allocation failed\n");
144    return the_atom;
145}
146
147/***====================================================================***/
148
149/*
150 * Handle quoted strings.
151 */
152
153Atom
154bdfGetPropertyValue(char *s)
155{
156    register char *p,
157               *pp;
158    char *orig_s = s;
159    Atom        atom;
160
161    /* strip leading white space */
162    while (*s && (*s == ' ' || *s == '\t'))
163	s++;
164    if (*s == 0) {
165	return bdfForceMakeAtom(s, NULL);
166    }
167    if (*s != '"') {
168	pp = s;
169	/* no white space in value */
170	for (pp = s; *pp; pp++)
171	    if (*pp == ' ' || *pp == '\t' || *pp == '\015' || *pp == '\n') {
172		*pp = 0;
173		break;
174	    }
175	return bdfForceMakeAtom(s, NULL);
176    }
177    /* quoted string: strip outer quotes and undouble inner quotes */
178    s++;
179    pp = p = (char *) xalloc((unsigned) strlen(s) + 1);
180    if (pp == NULL) {
181  bdfError("Couldn't allocate property value string (%d)\n", strlen(s) + 1);
182  return None;
183    }
184    while (*s) {
185	if (*s == '"') {
186	    if (*(s + 1) != '"') {
187		*p++ = 0;
188		atom = bdfForceMakeAtom(pp, NULL);
189		xfree(pp);
190		return atom;
191	    } else {
192		s++;
193	    }
194	}
195	*p++ = *s++;
196    }
197    xfree (pp);
198    bdfError("unterminated quoted string property: %s\n", (pointer) orig_s);
199    return None;
200}
201
202/***====================================================================***/
203
204/*
205 * return TRUE if string is a valid integer
206 */
207int
208bdfIsInteger(char *str)
209{
210    char        c;
211
212    c = *str++;
213    if (!(isdigit(c) || c == '-' || c == '+'))
214	return (FALSE);
215
216    while ((c = *str++))
217	if (!isdigit(c))
218	    return (FALSE);
219
220    return (TRUE);
221}
222
223/***====================================================================***/
224
225/*
226 * make a byte from the first two hex characters in glyph picture
227 */
228
229unsigned char
230bdfHexByte(unsigned char *s)
231{
232    unsigned char b = 0;
233    register char c;
234    int         i;
235
236    for (i = 2; i; i--) {
237	c = *s++;
238	if ((c >= '0') && (c <= '9'))
239	    b = (b << 4) + (c - '0');
240	else if ((c >= 'A') && (c <= 'F'))
241	    b = (b << 4) + 10 + (c - 'A');
242	else if ((c >= 'a') && (c <= 'f'))
243	    b = (b << 4) + 10 + (c - 'a');
244	else
245	    bdfError("bad hex char '%c'", c);
246    }
247    return b;
248}
249
250/***====================================================================***/
251
252/*
253 * check for known special property values
254 */
255
256static char *SpecialAtoms[] = {
257    "FONT_ASCENT",
258#define BDF_FONT_ASCENT	0
259    "FONT_DESCENT",
260#define BDF_FONT_DESCENT 1
261    "DEFAULT_CHAR",
262#define BDF_DEFAULT_CHAR 2
263    "POINT_SIZE",
264#define BDF_POINT_SIZE 3
265    "RESOLUTION",
266#define BDF_RESOLUTION 4
267    "X_HEIGHT",
268#define BDF_X_HEIGHT 5
269    "WEIGHT",
270#define BDF_WEIGHT 6
271    "QUAD_WIDTH",
272#define BDF_QUAD_WIDTH 7
273    "FONT",
274#define BDF_FONT 8
275    "RESOLUTION_X",
276#define BDF_RESOLUTION_X 9
277    "RESOLUTION_Y",
278#define BDF_RESOLUTION_Y 10
279    0,
280};
281
282Bool
283bdfSpecialProperty(FontPtr pFont, FontPropPtr prop,
284		   char isString, bdfFileState *bdfState)
285{
286    char      **special;
287    char       *name;
288
289    name = NameForAtom(prop->name);
290    for (special = SpecialAtoms; *special; special++)
291	if (!strcmp(name, *special))
292	    break;
293
294    switch (special - SpecialAtoms) {
295    case BDF_FONT_ASCENT:
296	if (!isString) {
297	    pFont->info.fontAscent = prop->value;
298	    bdfState->haveFontAscent = TRUE;
299	}
300	return TRUE;
301    case BDF_FONT_DESCENT:
302	if (!isString) {
303	    pFont->info.fontDescent = prop->value;
304	    bdfState->haveFontDescent = TRUE;
305	}
306	return TRUE;
307    case BDF_DEFAULT_CHAR:
308	if (!isString) {
309	    pFont->info.defaultCh = prop->value;
310	    bdfState->haveDefaultCh = TRUE;
311	}
312	return TRUE;
313    case BDF_POINT_SIZE:
314	bdfState->pointSizeProp = prop;
315	return FALSE;
316    case BDF_RESOLUTION:
317	bdfState->resolutionProp = prop;
318	return FALSE;
319    case BDF_X_HEIGHT:
320	bdfState->xHeightProp = prop;
321	return FALSE;
322    case BDF_WEIGHT:
323	bdfState->weightProp = prop;
324	return FALSE;
325    case BDF_QUAD_WIDTH:
326	bdfState->quadWidthProp = prop;
327	return FALSE;
328    case BDF_FONT:
329	bdfState->fontProp = prop;
330	return FALSE;
331    case BDF_RESOLUTION_X:
332	bdfState->resolutionXProp = prop;
333	return FALSE;
334    case BDF_RESOLUTION_Y:
335	bdfState->resolutionYProp = prop;
336	return FALSE;
337    default:
338	return FALSE;
339    }
340}
341