header.c revision 1b2353db
1/*
2
3Copyright 1990, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included in
12all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21Except as contained in this notice, the name of The Open Group shall not be
22used in advertising or otherwise to promote the sale, use or other dealings
23in this Software without prior written authorization from The Open Group.
24
25 * Copyright 1990 Network Computing Devices;
26 * Portions Copyright 1987 by Digital Equipment Corporation
27 *
28 * Permission to use, copy, modify, distribute, and sell this software and
29 * its documentation for any purpose is hereby granted without fee, provided
30 * that the above copyright notice appear in all copies and that both that
31 * copyright notice and this permission notice appear in supporting
32 * documentation, and that the names of Network Computing Devices, or Digital
33 * not be used in advertising or publicity pertaining to distribution
34 * of the software without specific, written prior permission.
35 *
36 * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
37 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
38 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
39 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
40 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
41 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
42 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
43 * THIS SOFTWARE.
44 */
45
46#include	<stdio.h>
47#include	<X11/Xosdefs.h>
48#include	<stdlib.h>
49#include	<string.h>
50#include	"fstobdf.h"
51
52unsigned long        pointSize;
53unsigned long        yResolution;
54
55static const char *warning[] =
56{
57    "COMMENT  ",
58    "COMMENT  WARNING:  This bdf file was generated from a font server using",
59    "COMMENT  fstobdf.  The resulting font is subject to the same copyright,",
60    "COMMENT  license, and trademark restrictions as the original font.  The",
61    "COMMENT  authors and distributors of fstobdf disclaim all liability for",
62    "COMMENT  misuse of the program or its output.",
63    "COMMENT  ",
64    NULL
65};
66
67static char *
68FindStringProperty(const char *propName,
69		   unsigned int *propLength,
70		   FSPropInfo *propInfo,
71		   FSPropOffset *propOffsets,
72		   unsigned char *propData)
73{
74    FSPropOffset *propOffset;
75    unsigned int  length;
76    unsigned int  i;
77
78    propOffset = &propOffsets[0];
79    length = (unsigned int) strlen(propName);
80    for (i = propInfo->num_offsets; i--; propOffset++) {
81	if (propOffset->type == PropTypeString) {
82
83#ifdef DEBUG
84	    char        pname[256];
85
86	    memmove( pname, propData + propOffset->name.position,
87		  propOffset->name.length);
88	    pname[propOffset->name.length] = '\0';
89	    fprintf(stderr, "prop name: %s (len %d)\n",
90		    pname, propOffset->name.length);
91#endif
92
93	    if ((propOffset->name.length == length) &&
94		    !strncmp((char*)propData + propOffset->name.position, propName, length)) {
95		*propLength = propOffset->value.length;
96		return (char *)(propData + propOffset->value.position);
97	    }
98	}
99    }
100    *propLength = 0;
101    return (NULL);
102}
103
104static int
105FindNumberProperty(const char *propName,
106		   unsigned long *propValue,
107		   FSPropInfo *propInfo,
108		   FSPropOffset *propOffsets,
109		   unsigned char *propData)
110{
111    FSPropOffset *propOffset;
112    unsigned int  i;
113    unsigned int  length;
114
115    propOffset = &propOffsets[0];
116    length = (unsigned int) strlen(propName);
117    for (i = propInfo->num_offsets; i--; propOffset++) {
118	if ((propOffset->type == PropTypeSigned) ||
119		(propOffset->type == PropTypeUnsigned)) {
120	    if ((propOffset->name.length == length) &&
121		    !strncmp((char*)propData + propOffset->name.position, propName, length)) {
122		*propValue = propOffset->value.position;
123		return (propOffset->type);
124	    }
125	}
126    }
127    return (-1);
128}
129
130/*
131 * EmitHeader - print STARTFONT, COMMENT lines, FONT, SIZE, and
132 * FONTBOUNDINGBOX lines
133 */
134Bool
135EmitHeader(FILE *outFile,
136	   FSXFontInfoHeader *fontHeader,
137	   FSPropInfo *propInfo,
138	   FSPropOffset *propOffsets,
139	   unsigned char *propData)
140{
141    unsigned int len;
142    int         type;
143    char       *cp;
144    const char **cpp;
145    unsigned long xResolution;
146
147    fprintf(outFile, "STARTFONT 2.1\n");
148
149    /*
150     * find COPYRIGHT message and print it first, followed by warning
151     */
152    cp = FindStringProperty("COPYRIGHT", &len, propInfo, propOffsets,
153			    propData);
154    if (cp) {
155	fprintf(outFile, "COMMENT  \nCOMMENT  ");
156	fwrite(cp, 1, len, outFile);
157	fputc('\n', outFile);
158    }
159    for (cpp = warning; *cpp; cpp++)
160	fprintf(outFile, "%s\n", *cpp);
161
162    /*
163     * FONT name
164     */
165    cp = FindStringProperty("FONT", &len, propInfo, propOffsets, propData);
166    if (cp) {
167	fprintf(outFile, "FONT ");
168	fwrite(cp, 1, len, outFile);
169	fputc('\n', outFile);
170    } else {
171	fprintf(stderr, "unable to find FONT property\n");
172	return (False);
173    }
174
175    /*
176     * SIZE point xres yres
177     *
178     * Get XLFD values if possible, else fake it
179     */
180    type = FindNumberProperty("RESOLUTION_X", &xResolution, propInfo,
181			      propOffsets, propData);
182    if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
183	xResolution = 72;
184
185    type = FindNumberProperty("RESOLUTION_Y", &yResolution, propInfo,
186			      propOffsets, propData);
187    if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
188	yResolution = 72;
189
190    type = FindNumberProperty("POINT_SIZE", &pointSize, propInfo,
191			      propOffsets, propData);
192    if ((type == PropTypeUnsigned) || (type == PropTypeSigned))
193	pointSize = (pointSize + 5) / 10;
194    else
195	pointSize = ((fontHeader->font_ascent + fontHeader->font_descent)
196		     * 72) / yResolution;
197
198    fprintf(outFile, "SIZE %lu %lu %lu\n", pointSize, xResolution,
199	    yResolution);
200
201    /*
202     * FONTBOUNDINGBOX width height xoff yoff
203     *
204     */
205    fprintf(outFile, "FONTBOUNDINGBOX %d %d %d %d\n",
206	    fontHeader->max_bounds.right - fontHeader->min_bounds.left,
207	    fontHeader->max_bounds.ascent + fontHeader->max_bounds.descent,
208	    fontHeader->min_bounds.left,
209	    -fontHeader->max_bounds.descent);
210    return (True);
211}
212