chars.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/* Morten Storgaard Nielsen: chars.c,v 3.2-1 2000/01/30 14:11:19 kat Exp */
47
48#include	<stdio.h>
49#include	<X11/Xlib.h>
50#include	"fstobdf.h"
51
52#define BIT_ORDER	BitmapFormatBitOrderMSB
53#ifdef BYTE_ORDER
54#undef BYTE_ORDER
55#endif
56#define BYTE_ORDER	BitmapFormatByteOrderMSB
57#define SCANLINE_UNIT	BitmapFormatScanlineUnit8
58#define SCANLINE_PAD	BitmapFormatScanlinePad8
59#define EXTENTS		BitmapFormatImageRectMin
60
61#define SCANLINE_PAD_BYTES	1
62
63#define GLWIDTHBYTESPADDED(bits, nBytes)				    \
64	((nBytes) == 1 ? (((bits)  +  7) >> 3)		/* pad to 1 byte  */\
65	:(nBytes) == 2 ? ((((bits) + 15) >> 3) & ~1)	/* pad to 2 bytes */\
66	:(nBytes) == 4 ? ((((bits) + 31) >> 3) & ~3)	/* pad to 4 bytes */\
67	:(nBytes) == 8 ? ((((bits) + 63) >> 3) & ~7)	/* pad to 8 bytes */\
68	: 0)
69
70
71static void
72EmitBitmap(FILE *outFile,
73	   FSXFontInfoHeader *fontHeader,
74	   FSXCharInfo *charInfo,
75	   unsigned int encoding,
76	   int bpr,
77	   unsigned char *data)
78{
79    char       *glyphName;
80    unsigned int row;
81
82    /*-
83     * format:
84     * STARTCHAR name
85     * ENCODING index
86     * SWIDTH scalablewidth 0
87     * DWIDTH pixels 0
88     * BBX width height xoff yoff
89     * ATTRIBUTES xxxx
90     * BITMAP hhhhhhhh ...
91     * ENDCHAR
92     *
93     * where, SWIDTH * (point / 1000) * (yres / 72) = DWIDTH or,
94     *        SWIDTH = 72000 *
95     * DWIDTH / (point * yres)
96     */
97
98    fprintf(outFile, "STARTCHAR ");
99    glyphName = XKeysymToString((KeySym) encoding);
100    if (glyphName)
101	fputs(glyphName, outFile);
102    else
103	fprintf(outFile, (fontHeader->char_range.min_char.low > 0 ?
104			  "C%06o" : "C%03o"), encoding);
105    fputc('\n', outFile);
106    fprintf(outFile, "ENCODING %u\n", encoding);
107    fprintf(outFile, "SWIDTH %ld 0\n",
108	    (((long) charInfo->width) * 72000L) /
109	    (pointSize * yResolution));
110    fprintf(outFile, "DWIDTH %d 0\n", charInfo->width);
111    fprintf(outFile, "BBX %d %d %d %d\n",
112	    charInfo->right - charInfo->left,
113	    charInfo->ascent + charInfo->descent,
114	    charInfo->left,
115	    -charInfo->descent);
116    if (charInfo->attributes)
117	fprintf(outFile, "ATTRIBUTES %04x\n", charInfo->attributes);
118
119    /*
120     * emit the bitmap
121     */
122    fprintf(outFile, "BITMAP\n");
123    for (row = 0; row < (charInfo->ascent + charInfo->descent); row++) {
124	unsigned    byte;
125	unsigned    bit;
126
127	static unsigned maskTab[] =
128	{
129	    (1 << 7), (1 << 6), (1 << 5), (1 << 4),
130	    (1 << 3), (1 << 2), (1 << 1), (1 << 0),
131	};
132
133	byte = 0;
134	for (bit = 0; bit < (charInfo->right - charInfo->left); bit++) {
135	    byte |= maskTab[bit & 7] & data[bit >> 3];
136	    if ((bit & 7) == 7) {
137		fprintf(outFile, "%02x", byte);
138		byte = 0;
139	    }
140	}
141	if ((bit & 7) != 0)
142	    fprintf(outFile, "%02x", byte);
143	fputc('\n', outFile);
144	data += bpr;
145    }
146    fprintf(outFile, "ENDCHAR\n");
147}
148
149
150Bool
151EmitCharacters(FILE *outFile,
152	       FSServer *fontServer,
153	       FSXFontInfoHeader *fontHeader,
154	       Font fontID)
155{
156    FSXCharInfo *extents;
157    FSXCharInfo *charInfo;
158    unsigned int encoding;
159    FSOffset   *offsets;
160    unsigned char *glyph;
161    unsigned char *glyphs;
162    unsigned int nChars;
163    unsigned int firstCharLow;
164    unsigned int firstCharHigh;
165    unsigned int lastCharLow;
166    unsigned int lastCharHigh;
167    unsigned int chLow;
168    unsigned int chHigh;
169    FSBitmapFormat format;
170
171    nChars = 0;
172
173    format = BYTE_ORDER | BIT_ORDER | SCANLINE_UNIT |
174	SCANLINE_PAD | EXTENTS;
175    firstCharLow = fontHeader->char_range.min_char.low;
176    firstCharHigh = fontHeader->char_range.min_char.high;
177    lastCharLow = fontHeader->char_range.max_char.low;
178    lastCharHigh = fontHeader->char_range.max_char.high;
179
180    (void) FSQueryXExtents16(fontServer, fontID, True, (FSChar2b *) 0, 0,
181			     &extents);
182    (void) FSQueryXBitmaps16(fontServer, fontID, format, True, (FSChar2b *) 0,
183			     0, &offsets, &glyphs);
184
185    charInfo = extents;
186    /* calculate the actual number of chars */
187    for (chHigh = 0; chHigh <= (lastCharHigh-firstCharHigh); chHigh++) {
188      for (chLow = 0; chLow <= (lastCharLow-firstCharLow); chLow++) {
189	if ((charInfo->width != 0) || (charInfo->left != charInfo->right))
190	    nChars++;
191	charInfo++;
192      }
193    }
194
195    fprintf(outFile, "CHARS %u\n", nChars);
196
197    /*
198     * actually emit the characters
199     */
200    charInfo = extents;
201    glyph = glyphs;
202    for (chHigh = firstCharHigh; chHigh <= lastCharHigh; chHigh++) {
203      for (chLow = firstCharLow; chLow <= lastCharLow; chLow++) {
204	int         bpr;
205
206	bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
207				 SCANLINE_PAD_BYTES);
208  	  encoding=(chHigh << 8)+chLow;
209	if ((charInfo->width != 0) || (charInfo->right != charInfo->left))
210	    EmitBitmap(outFile, fontHeader, charInfo, encoding, bpr, glyph);
211	glyph = glyphs +
212	    offsets[encoding-((firstCharHigh << 8)+firstCharLow) + 1].position;
213	charInfo++;
214      }
215    }
216    FSFree((char *) extents);
217    FSFree((char *) glyphs);
218    FSFree((char *) offsets);
219    return (True);
220}
221