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    "COMMENT  \n"
57    "COMMENT  WARNING:  This bdf file was generated from a font server using\n"
58    "COMMENT  fstobdf.  The resulting font is subject to the same copyright,\n"
59    "COMMENT  license, and trademark restrictions as the original font.  The\n"
60    "COMMENT  authors and distributors of fstobdf disclaim all liability for\n"
61    "COMMENT  misuse of the program or its output.\n"
62    "COMMENT  \n";
63
64static char *
65FindStringProperty(const char *propName,
66                   unsigned int *propLength,
67                   FSPropInfo *propInfo,
68                   FSPropOffset *propOffsets,
69                   unsigned char *propData)
70{
71    FSPropOffset *propOffset;
72    size_t length;
73
74    propOffset = &propOffsets[0];
75    length = strlen(propName);
76    for (unsigned int i = propInfo->num_offsets; i--; propOffset++) {
77        if (propOffset->type == PropTypeString) {
78
79#ifdef DEBUG
80            char pname[256];
81
82            memcpy(pname, propData + propOffset->name.position,
83                   propOffset->name.length);
84            pname[propOffset->name.length] = '\0';
85            fprintf(stderr, "prop name: %s (len %d)\n",
86                    pname, propOffset->name.length);
87#endif
88
89            if ((propOffset->name.length == length) &&
90                !strncmp((char *) propData + propOffset->name.position,
91                         propName, length)) {
92                *propLength = propOffset->value.length;
93                return (char *) (propData + propOffset->value.position);
94            }
95        }
96    }
97    *propLength = 0;
98    return (NULL);
99}
100
101static int
102FindNumberProperty(const char *propName,
103                   unsigned long *propValue,
104                   FSPropInfo *propInfo,
105                   FSPropOffset *propOffsets,
106                   unsigned char *propData)
107{
108    FSPropOffset *propOffset;
109    size_t length;
110
111    propOffset = &propOffsets[0];
112    length = strlen(propName);
113    for (unsigned int i = propInfo->num_offsets; i--; propOffset++) {
114        if ((propOffset->type == PropTypeSigned) ||
115            (propOffset->type == PropTypeUnsigned)) {
116            if ((propOffset->name.length == length) &&
117                !strncmp((char *) propData + propOffset->name.position,
118                         propName, length)) {
119                *propValue = propOffset->value.position;
120                return (propOffset->type);
121            }
122        }
123    }
124    return (-1);
125}
126
127/*
128 * EmitHeader - print STARTFONT, COMMENT lines, FONT, SIZE, and
129 * FONTBOUNDINGBOX lines
130 */
131Bool
132EmitHeader(FILE *outFile,
133           FSXFontInfoHeader *fontHeader,
134           FSPropInfo *propInfo,
135           FSPropOffset *propOffsets,
136           unsigned char *propData)
137{
138    unsigned int len;
139    int type;
140    char *cp;
141    unsigned long xResolution;
142
143    fprintf(outFile, "STARTFONT 2.1\n");
144
145    /*
146     * find COPYRIGHT message and print it first, followed by warning
147     */
148    cp = FindStringProperty("COPYRIGHT", &len, propInfo, propOffsets, propData);
149    if (cp) {
150        fprintf(outFile, "COMMENT  \nCOMMENT  ");
151        fwrite(cp, 1, len, outFile);
152        fputc('\n', outFile);
153    }
154    fputs(warning, outFile);
155
156    /*
157     * FONT name
158     */
159    cp = FindStringProperty("FONT", &len, propInfo, propOffsets, propData);
160    if (cp) {
161        fprintf(outFile, "FONT ");
162        fwrite(cp, 1, len, outFile);
163        fputc('\n', outFile);
164    }
165    else {
166        fprintf(stderr, "unable to find FONT property\n");
167        return (False);
168    }
169
170    /*
171     * SIZE point xres yres
172     *
173     * Get XLFD values if possible, else fake it
174     */
175    type = FindNumberProperty("RESOLUTION_X", &xResolution, propInfo,
176                              propOffsets, propData);
177    if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
178        xResolution = 72;
179
180    type = FindNumberProperty("RESOLUTION_Y", &yResolution, propInfo,
181                              propOffsets, propData);
182    if ((type != PropTypeUnsigned) && (type != PropTypeSigned))
183        yResolution = 72;
184
185    type = FindNumberProperty("POINT_SIZE", &pointSize, propInfo,
186                              propOffsets, propData);
187    if ((type == PropTypeUnsigned) || (type == PropTypeSigned))
188        pointSize = (pointSize + 5) / 10;
189    else
190        pointSize = ((fontHeader->font_ascent + fontHeader->font_descent)
191                     * 72) / yResolution;
192
193    fprintf(outFile, "SIZE %lu %lu %lu\n", pointSize, xResolution, yResolution);
194
195    /*
196     * FONTBOUNDINGBOX width height xoff yoff
197     *
198     */
199    fprintf(outFile, "FONTBOUNDINGBOX %d %d %d %d\n",
200            fontHeader->max_bounds.right - fontHeader->min_bounds.left,
201            fontHeader->max_bounds.ascent + fontHeader->max_bounds.descent,
202            fontHeader->min_bounds.left,
203            -fontHeader->max_bounds.descent);
204    return (True);
205}
206