chars.c revision 82e12b1b
121c2f794Smrg/*
282e12b1bSmrg
321c2f794SmrgCopyright 1990, 1998  The Open Group
421c2f794Smrg
521c2f794SmrgPermission to use, copy, modify, distribute, and sell this software and its
621c2f794Smrgdocumentation for any purpose is hereby granted without fee, provided that
721c2f794Smrgthe above copyright notice appear in all copies and that both that
821c2f794Smrgcopyright notice and this permission notice appear in supporting
921c2f794Smrgdocumentation.
1021c2f794Smrg
1121c2f794SmrgThe above copyright notice and this permission notice shall be included in
1221c2f794Smrgall copies or substantial portions of the Software.
1321c2f794Smrg
1421c2f794SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1521c2f794SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1621c2f794SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1721c2f794SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1821c2f794SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1921c2f794SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2021c2f794Smrg
2121c2f794SmrgExcept as contained in this notice, the name of The Open Group shall not be
2221c2f794Smrgused in advertising or otherwise to promote the sale, use or other dealings
2321c2f794Smrgin this Software without prior written authorization from The Open Group.
2421c2f794Smrg
2521c2f794Smrg * Copyright 1990 Network Computing Devices;
2682e12b1bSmrg * Portions Copyright 1987 by Digital Equipment Corporation
2721c2f794Smrg *
2821c2f794Smrg * Permission to use, copy, modify, distribute, and sell this software and
2921c2f794Smrg * its documentation for any purpose is hereby granted without fee, provided
3021c2f794Smrg * that the above copyright notice appear in all copies and that both that
3121c2f794Smrg * copyright notice and this permission notice appear in supporting
3221c2f794Smrg * documentation, and that the names of Network Computing Devices, or Digital
3321c2f794Smrg * not be used in advertising or publicity pertaining to distribution
3421c2f794Smrg * of the software without specific, written prior permission.
3521c2f794Smrg *
3621c2f794Smrg * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
3721c2f794Smrg * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
3821c2f794Smrg * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
3921c2f794Smrg * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
4021c2f794Smrg * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
4121c2f794Smrg * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
4221c2f794Smrg * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
4321c2f794Smrg * THIS SOFTWARE.
4421c2f794Smrg */
4521c2f794Smrg
4621c2f794Smrg/* Morten Storgaard Nielsen: chars.c,v 3.2-1 2000/01/30 14:11:19 kat Exp */
4721c2f794Smrg
4821c2f794Smrg#include	<stdio.h>
4921c2f794Smrg#include	<X11/Xlib.h>
5021c2f794Smrg#include	"fstobdf.h"
5121c2f794Smrg
5221c2f794Smrg#define BIT_ORDER	BitmapFormatBitOrderMSB
5321c2f794Smrg#ifdef BYTE_ORDER
5421c2f794Smrg#undef BYTE_ORDER
5521c2f794Smrg#endif
5621c2f794Smrg#define BYTE_ORDER	BitmapFormatByteOrderMSB
5721c2f794Smrg#define SCANLINE_UNIT	BitmapFormatScanlineUnit8
5821c2f794Smrg#define SCANLINE_PAD	BitmapFormatScanlinePad8
5921c2f794Smrg#define EXTENTS		BitmapFormatImageRectMin
6021c2f794Smrg
6121c2f794Smrg#define SCANLINE_PAD_BYTES	1
6221c2f794Smrg
6321c2f794Smrg#define GLWIDTHBYTESPADDED(bits, nBytes)				    \
6421c2f794Smrg	((nBytes) == 1 ? (((bits)  +  7) >> 3)		/* pad to 1 byte  */\
6521c2f794Smrg	:(nBytes) == 2 ? ((((bits) + 15) >> 3) & ~1)	/* pad to 2 bytes */\
6621c2f794Smrg	:(nBytes) == 4 ? ((((bits) + 31) >> 3) & ~3)	/* pad to 4 bytes */\
6721c2f794Smrg	:(nBytes) == 8 ? ((((bits) + 63) >> 3) & ~7)	/* pad to 8 bytes */\
6821c2f794Smrg	: 0)
6921c2f794Smrg
7021c2f794Smrg
7121c2f794Smrgstatic void
7282e12b1bSmrgEmitBitmap(FILE *outFile,
7382e12b1bSmrg	   FSXFontInfoHeader *fontHeader,
7482e12b1bSmrg	   FSXCharInfo *charInfo,
7582e12b1bSmrg	   unsigned int encoding,
7682e12b1bSmrg	   int bpr,
7721c2f794Smrg	   unsigned char *data)
7821c2f794Smrg{
7921c2f794Smrg    char       *glyphName;
8021c2f794Smrg    unsigned int row;
8121c2f794Smrg
8221c2f794Smrg    /*-
8321c2f794Smrg     * format:
8421c2f794Smrg     * STARTCHAR name
8521c2f794Smrg     * ENCODING index
8621c2f794Smrg     * SWIDTH scalablewidth 0
8721c2f794Smrg     * DWIDTH pixels 0
8821c2f794Smrg     * BBX width height xoff yoff
8921c2f794Smrg     * ATTRIBUTES xxxx
9021c2f794Smrg     * BITMAP hhhhhhhh ...
9121c2f794Smrg     * ENDCHAR
9221c2f794Smrg     *
9321c2f794Smrg     * where, SWIDTH * (point / 1000) * (yres / 72) = DWIDTH or,
9421c2f794Smrg     *        SWIDTH = 72000 *
9521c2f794Smrg     * DWIDTH / (point * yres)
9621c2f794Smrg     */
9721c2f794Smrg
9821c2f794Smrg    fprintf(outFile, "STARTCHAR ");
9921c2f794Smrg    glyphName = XKeysymToString((KeySym) encoding);
10021c2f794Smrg    if (glyphName)
10121c2f794Smrg	fputs(glyphName, outFile);
10221c2f794Smrg    else
10321c2f794Smrg	fprintf(outFile, (fontHeader->char_range.min_char.low > 0 ?
10421c2f794Smrg			  "C%06o" : "C%03o"), encoding);
10521c2f794Smrg    fputc('\n', outFile);
10621c2f794Smrg    fprintf(outFile, "ENCODING %u\n", encoding);
10721c2f794Smrg    fprintf(outFile, "SWIDTH %ld 0\n",
10821c2f794Smrg	    (((long) charInfo->width) * 72000L) /
10921c2f794Smrg	    (pointSize * yResolution));
11021c2f794Smrg    fprintf(outFile, "DWIDTH %d 0\n", charInfo->width);
11121c2f794Smrg    fprintf(outFile, "BBX %d %d %d %d\n",
11221c2f794Smrg	    charInfo->right - charInfo->left,
11321c2f794Smrg	    charInfo->ascent + charInfo->descent,
11421c2f794Smrg	    charInfo->left,
11521c2f794Smrg	    -charInfo->descent);
11621c2f794Smrg    if (charInfo->attributes)
11721c2f794Smrg	fprintf(outFile, "ATTRIBUTES %04x\n", charInfo->attributes);
11821c2f794Smrg
11921c2f794Smrg    /*
12021c2f794Smrg     * emit the bitmap
12121c2f794Smrg     */
12221c2f794Smrg    fprintf(outFile, "BITMAP\n");
12321c2f794Smrg    for (row = 0; row < (charInfo->ascent + charInfo->descent); row++) {
12421c2f794Smrg	unsigned    byte;
12521c2f794Smrg	unsigned    bit;
12621c2f794Smrg
12721c2f794Smrg	static unsigned maskTab[] =
12821c2f794Smrg	{
12921c2f794Smrg	    (1 << 7), (1 << 6), (1 << 5), (1 << 4),
13021c2f794Smrg	    (1 << 3), (1 << 2), (1 << 1), (1 << 0),
13121c2f794Smrg	};
13221c2f794Smrg
13321c2f794Smrg	byte = 0;
13421c2f794Smrg	for (bit = 0; bit < (charInfo->right - charInfo->left); bit++) {
13521c2f794Smrg	    byte |= maskTab[bit & 7] & data[bit >> 3];
13621c2f794Smrg	    if ((bit & 7) == 7) {
13721c2f794Smrg		fprintf(outFile, "%02x", byte);
13821c2f794Smrg		byte = 0;
13921c2f794Smrg	    }
14021c2f794Smrg	}
14121c2f794Smrg	if ((bit & 7) != 0)
14221c2f794Smrg	    fprintf(outFile, "%02x", byte);
14321c2f794Smrg	fputc('\n', outFile);
14421c2f794Smrg	data += bpr;
14521c2f794Smrg    }
14621c2f794Smrg    fprintf(outFile, "ENDCHAR\n");
14721c2f794Smrg}
14821c2f794Smrg
14921c2f794Smrg
15021c2f794SmrgBool
15182e12b1bSmrgEmitCharacters(FILE *outFile,
15282e12b1bSmrg	       FSServer *fontServer,
15382e12b1bSmrg	       FSXFontInfoHeader *fontHeader,
15421c2f794Smrg	       Font fontID)
15521c2f794Smrg{
15621c2f794Smrg    FSXCharInfo *extents;
15721c2f794Smrg    FSXCharInfo *charInfo;
15821c2f794Smrg    int         encoding;
15921c2f794Smrg    FSOffset   *offsets;
16021c2f794Smrg    unsigned char *glyph;
16121c2f794Smrg    unsigned char *glyphs;
16221c2f794Smrg    unsigned int nChars;
16321c2f794Smrg    int         firstCharLow;
16421c2f794Smrg    int         firstCharHigh;
16521c2f794Smrg    int         lastCharLow;
16621c2f794Smrg    int         lastCharHigh;
16721c2f794Smrg    int         chLow;
16821c2f794Smrg    int         chHigh;
16921c2f794Smrg    FSBitmapFormat format;
17021c2f794Smrg
17121c2f794Smrg    nChars = 0;
17221c2f794Smrg
17321c2f794Smrg    format = BYTE_ORDER | BIT_ORDER | SCANLINE_UNIT |
17421c2f794Smrg	SCANLINE_PAD | EXTENTS;
17582e12b1bSmrg    firstCharLow = fontHeader->char_range.min_char.low;
17682e12b1bSmrg    firstCharHigh = fontHeader->char_range.min_char.high;
17721c2f794Smrg    lastCharLow = fontHeader->char_range.max_char.low;
17821c2f794Smrg    lastCharHigh = fontHeader->char_range.max_char.high;
17921c2f794Smrg
18021c2f794Smrg    (void) FSQueryXExtents16(fontServer, fontID, True, (FSChar2b *) 0, 0,
18121c2f794Smrg			     &extents);
18221c2f794Smrg    (void) FSQueryXBitmaps16(fontServer, fontID, format, True, (FSChar2b *) 0,
18321c2f794Smrg			     0, &offsets, &glyphs);
18421c2f794Smrg
18521c2f794Smrg    charInfo = extents;
18621c2f794Smrg    /* calculate the actual number of chars */
18721c2f794Smrg    for (chHigh = 0; chHigh <= (lastCharHigh-firstCharHigh); chHigh++) {
18821c2f794Smrg      for (chLow = 0; chLow <= (lastCharLow-firstCharLow); chLow++) {
18921c2f794Smrg	if ((charInfo->width != 0) || (charInfo->left != charInfo->right))
19021c2f794Smrg	    nChars++;
19121c2f794Smrg	charInfo++;
19221c2f794Smrg      }
19321c2f794Smrg    }
19421c2f794Smrg
19521c2f794Smrg    fprintf(outFile, "CHARS %u\n", nChars);
19621c2f794Smrg
19721c2f794Smrg    /*
19821c2f794Smrg     * actually emit the characters
19921c2f794Smrg     */
20021c2f794Smrg    charInfo = extents;
20121c2f794Smrg    glyph = glyphs;
20221c2f794Smrg    for (chHigh = firstCharHigh; chHigh <= lastCharHigh; chHigh++) {
20321c2f794Smrg      for (chLow = firstCharLow; chLow <= lastCharLow; chLow++) {
20421c2f794Smrg	int         bpr;
20521c2f794Smrg
20621c2f794Smrg	bpr = GLWIDTHBYTESPADDED((charInfo->right - charInfo->left),
20721c2f794Smrg				 SCANLINE_PAD_BYTES);
20821c2f794Smrg  	  encoding=(chHigh << 8)+chLow;
20921c2f794Smrg	if ((charInfo->width != 0) || (charInfo->right != charInfo->left))
21021c2f794Smrg	    EmitBitmap(outFile, fontHeader, charInfo, encoding, bpr, glyph);
21121c2f794Smrg	glyph = glyphs +
21221c2f794Smrg	    offsets[encoding-((firstCharHigh << 8)+firstCharLow) + 1].position;
21321c2f794Smrg	charInfo++;
21421c2f794Smrg      }
21521c2f794Smrg    }
21621c2f794Smrg    FSFree((char *) extents);
21721c2f794Smrg    FSFree((char *) glyphs);
21821c2f794Smrg    FSFree((char *) offsets);
21921c2f794Smrg    return (True);
22021c2f794Smrg}
221