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