chars.c revision 21c2f794
1/* $Xorg: chars.c,v 1.4 2001/02/09 02:05:30 xorgcvs Exp $ */
2/* $XdotOrg: app/fstobdf/chars.c,v 1.3 2005/10/31 16:05:45 alanc Exp $ */
3/*
4
5Copyright 1990, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27 * Copyright 1990 Network Computing Devices;
28 * Portions Copyright 1987 by Digital Equipment Corporation
29 *
30 * Permission to use, copy, modify, distribute, and sell this software and
31 * its documentation for any purpose is hereby granted without fee, provided
32 * that the above copyright notice appear in all copies and that both that
33 * copyright notice and this permission notice appear in supporting
34 * documentation, and that the names of Network Computing Devices, or Digital
35 * not be used in advertising or publicity pertaining to distribution
36 * of the software without specific, written prior permission.
37 *
38 * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
39 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
41 * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
42 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
43 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
45 * THIS SOFTWARE.
46 */
47/* $XFree86: xc/programs/fstobdf/chars.c,v 3.7 2001/03/04 00:16:31 tsi Exp $ */
48
49/* Morten Storgaard Nielsen: chars.c,v 3.2-1 2000/01/30 14:11:19 kat Exp */
50
51#include	<stdio.h>
52#include	<X11/Xlib.h>
53#include	"fstobdf.h"
54
55#define BIT_ORDER	BitmapFormatBitOrderMSB
56#ifdef BYTE_ORDER
57#undef BYTE_ORDER
58#endif
59#define BYTE_ORDER	BitmapFormatByteOrderMSB
60#define SCANLINE_UNIT	BitmapFormatScanlineUnit8
61#define SCANLINE_PAD	BitmapFormatScanlinePad8
62#define EXTENTS		BitmapFormatImageRectMin
63
64#define SCANLINE_PAD_BYTES	1
65
66#define GLWIDTHBYTESPADDED(bits, nBytes)				    \
67	((nBytes) == 1 ? (((bits)  +  7) >> 3)		/* pad to 1 byte  */\
68	:(nBytes) == 2 ? ((((bits) + 15) >> 3) & ~1)	/* pad to 2 bytes */\
69	:(nBytes) == 4 ? ((((bits) + 31) >> 3) & ~3)	/* pad to 4 bytes */\
70	:(nBytes) == 8 ? ((((bits) + 63) >> 3) & ~7)	/* pad to 8 bytes */\
71	: 0)
72
73
74static void
75EmitBitmap(FILE *outFile,
76	   FSXFontInfoHeader *fontHeader,
77	   FSXCharInfo *charInfo,
78	   unsigned int encoding,
79	   int bpr,
80	   unsigned char *data)
81{
82    char       *glyphName;
83    unsigned int row;
84
85    /*-
86     * format:
87     * STARTCHAR name
88     * ENCODING index
89     * SWIDTH scalablewidth 0
90     * DWIDTH pixels 0
91     * BBX width height xoff yoff
92     * ATTRIBUTES xxxx
93     * BITMAP hhhhhhhh ...
94     * ENDCHAR
95     *
96     * where, SWIDTH * (point / 1000) * (yres / 72) = DWIDTH or,
97     *        SWIDTH = 72000 *
98     * DWIDTH / (point * yres)
99     */
100
101    fprintf(outFile, "STARTCHAR ");
102    glyphName = XKeysymToString((KeySym) encoding);
103    if (glyphName)
104	fputs(glyphName, outFile);
105    else
106	fprintf(outFile, (fontHeader->char_range.min_char.low > 0 ?
107			  "C%06o" : "C%03o"), encoding);
108    fputc('\n', outFile);
109    fprintf(outFile, "ENCODING %u\n", encoding);
110    fprintf(outFile, "SWIDTH %ld 0\n",
111	    (((long) charInfo->width) * 72000L) /
112	    (pointSize * yResolution));
113    fprintf(outFile, "DWIDTH %d 0\n", charInfo->width);
114    fprintf(outFile, "BBX %d %d %d %d\n",
115	    charInfo->right - charInfo->left,
116	    charInfo->ascent + charInfo->descent,
117	    charInfo->left,
118	    -charInfo->descent);
119    if (charInfo->attributes)
120	fprintf(outFile, "ATTRIBUTES %04x\n", charInfo->attributes);
121
122    /*
123     * emit the bitmap
124     */
125    fprintf(outFile, "BITMAP\n");
126    for (row = 0; row < (charInfo->ascent + charInfo->descent); row++) {
127	unsigned    byte;
128	unsigned    bit;
129
130	static unsigned maskTab[] =
131	{
132	    (1 << 7), (1 << 6), (1 << 5), (1 << 4),
133	    (1 << 3), (1 << 2), (1 << 1), (1 << 0),
134	};
135
136	byte = 0;
137	for (bit = 0; bit < (charInfo->right - charInfo->left); bit++) {
138	    byte |= maskTab[bit & 7] & data[bit >> 3];
139	    if ((bit & 7) == 7) {
140		fprintf(outFile, "%02x", byte);
141		byte = 0;
142	    }
143	}
144	if ((bit & 7) != 0)
145	    fprintf(outFile, "%02x", byte);
146	fputc('\n', outFile);
147	data += bpr;
148    }
149    fprintf(outFile, "ENDCHAR\n");
150}
151
152
153Bool
154EmitCharacters(FILE *outFile,
155	       FSServer *fontServer,
156	       FSXFontInfoHeader *fontHeader,
157	       Font fontID)
158{
159    FSXCharInfo *extents;
160    FSXCharInfo *charInfo;
161    int         encoding;
162    FSOffset   *offsets;
163    unsigned char *glyph;
164    unsigned char *glyphs;
165    unsigned int nChars;
166    int         firstCharLow;
167    int         firstCharHigh;
168    int         lastCharLow;
169    int         lastCharHigh;
170    int         chLow;
171    int         chHigh;
172    FSBitmapFormat format;
173
174    nChars = 0;
175
176    format = BYTE_ORDER | BIT_ORDER | SCANLINE_UNIT |
177	SCANLINE_PAD | EXTENTS;
178    firstCharLow = fontHeader->char_range.min_char.low;
179    firstCharHigh = fontHeader->char_range.min_char.high;
180    lastCharLow = fontHeader->char_range.max_char.low;
181    lastCharHigh = fontHeader->char_range.max_char.high;
182
183    (void) FSQueryXExtents16(fontServer, fontID, True, (FSChar2b *) 0, 0,
184			     &extents);
185    (void) FSQueryXBitmaps16(fontServer, fontID, format, True, (FSChar2b *) 0,
186			     0, &offsets, &glyphs);
187
188    charInfo = extents;
189    /* calculate the actual number of chars */
190    for (chHigh = 0; chHigh <= (lastCharHigh-firstCharHigh); chHigh++) {
191      for (chLow = 0; chLow <= (lastCharLow-firstCharLow); chLow++) {
192	if ((charInfo->width != 0) || (charInfo->left != charInfo->right))
193	    nChars++;
194	charInfo++;
195      }
196    }
197
198    fprintf(outFile, "CHARS %u\n", nChars);
199
200    /*
201     * actually emit the characters
202     */
203    charInfo = extents;
204    glyph = glyphs;
205    for (chHigh = firstCharHigh; chHigh <= lastCharHigh; chHigh++) {
206      for (chLow = firstCharLow; chLow <= lastCharLow; chLow++) {
207	int         bpr;
208
209	bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
210				 SCANLINE_PAD_BYTES);
211  	  encoding=(chHigh << 8)+chLow;
212	if ((charInfo->width != 0) || (charInfo->right != charInfo->left))
213	    EmitBitmap(outFile, fontHeader, charInfo, encoding, bpr, glyph);
214	glyph = glyphs +
215	    offsets[encoding-((firstCharHigh << 8)+firstCharLow) + 1].position;
216	charInfo++;
217      }
218    }
219    FSFree((char *) extents);
220    FSFree((char *) glyphs);
221    FSFree((char *) offsets);
222    return (True);
223}
224