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