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