123a0898aSmrg/*
223a0898aSmrg
323a0898aSmrgCopyright 1991, 1998  The Open Group
423a0898aSmrg
523a0898aSmrgPermission to use, copy, modify, distribute, and sell this software and its
623a0898aSmrgdocumentation for any purpose is hereby granted without fee, provided that
723a0898aSmrgthe above copyright notice appear in all copies and that both that
823a0898aSmrgcopyright notice and this permission notice appear in supporting
923a0898aSmrgdocumentation.
1023a0898aSmrg
1123a0898aSmrgThe above copyright notice and this permission notice shall be included in
1223a0898aSmrgall copies or substantial portions of the Software.
1323a0898aSmrg
1423a0898aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1523a0898aSmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1623a0898aSmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
1723a0898aSmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
1823a0898aSmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1923a0898aSmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2023a0898aSmrg
2123a0898aSmrgExcept as contained in this notice, the name of The Open Group shall not be
2223a0898aSmrgused in advertising or otherwise to promote the sale, use or other dealings
2323a0898aSmrgin this Software without prior written authorization from The Open Group.
2423a0898aSmrg
2523a0898aSmrg*/
2623a0898aSmrg
2723a0898aSmrg/*
2823a0898aSmrg * Author:  Keith Packard, MIT X Consortium
2923a0898aSmrg */
3023a0898aSmrg
3123a0898aSmrg#ifdef HAVE_CONFIG_H
3223a0898aSmrg#include <config.h>
3323a0898aSmrg#endif
3423a0898aSmrg
3523a0898aSmrg#include <X11/fonts/fntfilst.h>
3623a0898aSmrg#include <X11/fonts/bitmap.h>
3723a0898aSmrg
3823a0898aSmrgint
3941c30155SmrgbitmapGetGlyphs(FontPtr pFont, unsigned long count, unsigned char *chars,
4041c30155Smrg		FontEncoding charEncoding,
4123a0898aSmrg		unsigned long *glyphCount, 	/* RETURN */
4223a0898aSmrg		CharInfoPtr *glyphs) 		/* RETURN */
4323a0898aSmrg{
4423a0898aSmrg    BitmapFontPtr  bitmapFont;
4523a0898aSmrg    unsigned int firstCol;
4623a0898aSmrg    register unsigned int numCols;
4723a0898aSmrg    unsigned int firstRow;
4823a0898aSmrg    unsigned int numRows;
4923a0898aSmrg    CharInfoPtr *glyphsBase;
5023a0898aSmrg    register unsigned int c;
5123a0898aSmrg    register CharInfoPtr pci;
5223a0898aSmrg    unsigned int r;
5323a0898aSmrg    CharInfoPtr **encoding;
5423a0898aSmrg    CharInfoPtr pDefault;
5523a0898aSmrg
5623a0898aSmrg    bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
5723a0898aSmrg    encoding = bitmapFont->encoding;
5823a0898aSmrg    pDefault = bitmapFont->pDefault;
5923a0898aSmrg    firstCol = pFont->info.firstCol;
6023a0898aSmrg    numCols = pFont->info.lastCol - firstCol + 1;
6123a0898aSmrg    glyphsBase = glyphs;
6223a0898aSmrg    switch (charEncoding) {
6323a0898aSmrg
6423a0898aSmrg    case Linear8Bit:
6523a0898aSmrg    case TwoD8Bit:
660145ab54Ssnj	if (pFont->info.firstRow > 0) {
670145ab54Ssnj            if (pDefault)
680145ab54Ssnj                while (count--)
690145ab54Ssnj                    *glyphs++ = pDefault;
7023a0898aSmrg	    break;
710145ab54Ssnj        }
7223a0898aSmrg	if (pFont->info.allExist && pDefault) {
7323a0898aSmrg	    while (count--) {
7423a0898aSmrg		c = (*chars++) - firstCol;
7523a0898aSmrg		if (c < numCols)
7623a0898aSmrg		    *glyphs++ = ACCESSENCODING(encoding,c);
7723a0898aSmrg		else
7823a0898aSmrg		    *glyphs++ = pDefault;
7923a0898aSmrg	    }
8023a0898aSmrg	} else {
8123a0898aSmrg	    while (count--) {
8223a0898aSmrg		c = (*chars++) - firstCol;
8323a0898aSmrg		if (c < numCols && (pci = ACCESSENCODING(encoding,c)))
8423a0898aSmrg		    *glyphs++ = pci;
8523a0898aSmrg		else if (pDefault)
8623a0898aSmrg		    *glyphs++ = pDefault;
8723a0898aSmrg	    }
8823a0898aSmrg	}
8923a0898aSmrg	break;
9023a0898aSmrg    case Linear16Bit:
9123a0898aSmrg	if (pFont->info.allExist && pDefault) {
9223a0898aSmrg	    while (count--) {
9323a0898aSmrg		c = *chars++ << 8;
9423a0898aSmrg		c = (c | *chars++) - firstCol;
9523a0898aSmrg		if (c < numCols)
9623a0898aSmrg		    *glyphs++ = ACCESSENCODING(encoding,c);
9723a0898aSmrg		else
9823a0898aSmrg		    *glyphs++ = pDefault;
9923a0898aSmrg	    }
10023a0898aSmrg	} else {
10123a0898aSmrg	    while (count--) {
10223a0898aSmrg		c = *chars++ << 8;
10323a0898aSmrg		c = (c | *chars++) - firstCol;
10423a0898aSmrg		if (c < numCols && (pci = ACCESSENCODING(encoding,c)))
10523a0898aSmrg		    *glyphs++ = pci;
10623a0898aSmrg		else if (pDefault)
10723a0898aSmrg		    *glyphs++ = pDefault;
10823a0898aSmrg	    }
10923a0898aSmrg	}
11023a0898aSmrg	break;
11123a0898aSmrg
11223a0898aSmrg    case TwoD16Bit:
11323a0898aSmrg	firstRow = pFont->info.firstRow;
11423a0898aSmrg	numRows = pFont->info.lastRow - firstRow + 1;
11523a0898aSmrg	while (count--) {
11623a0898aSmrg	    r = (*chars++) - firstRow;
11723a0898aSmrg	    c = (*chars++) - firstCol;
11823a0898aSmrg	    if (r < numRows && c < numCols &&
11923a0898aSmrg		    (pci = ACCESSENCODING(encoding, r * numCols + c)))
12023a0898aSmrg		*glyphs++ = pci;
12123a0898aSmrg	    else if (pDefault)
12223a0898aSmrg		*glyphs++ = pDefault;
12323a0898aSmrg	}
12423a0898aSmrg	break;
12523a0898aSmrg    }
12623a0898aSmrg    *glyphCount = glyphs - glyphsBase;
12723a0898aSmrg    return Successful;
12823a0898aSmrg}
12923a0898aSmrg
13023a0898aSmrgstatic CharInfoRec nonExistantChar;
13123a0898aSmrg
13223a0898aSmrgint
13341c30155SmrgbitmapGetMetrics(FontPtr pFont, unsigned long count, unsigned char *chars,
13441c30155Smrg		 FontEncoding charEncoding,
13523a0898aSmrg		 unsigned long *glyphCount,	/* RETURN */
13623a0898aSmrg		 xCharInfo **glyphs)		/* RETURN */
13723a0898aSmrg{
13823a0898aSmrg    int         ret;
13923a0898aSmrg    xCharInfo  *ink_metrics;
14023a0898aSmrg    CharInfoPtr metrics;
14123a0898aSmrg    BitmapFontPtr  bitmapFont;
14223a0898aSmrg    CharInfoPtr	oldDefault;
14323a0898aSmrg    int         i;
14423a0898aSmrg
14523a0898aSmrg    bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
14623a0898aSmrg    oldDefault = bitmapFont->pDefault;
14723a0898aSmrg    bitmapFont->pDefault = &nonExistantChar;
14823a0898aSmrg    ret = bitmapGetGlyphs(pFont, count, chars, charEncoding, glyphCount, (CharInfoPtr *) glyphs);
14923a0898aSmrg    if (ret == Successful) {
15023a0898aSmrg	if (bitmapFont->ink_metrics) {
15123a0898aSmrg	    metrics = bitmapFont->metrics;
15223a0898aSmrg	    ink_metrics = bitmapFont->ink_metrics;
15323a0898aSmrg	    for (i = 0; i < *glyphCount; i++) {
15423a0898aSmrg		if (glyphs[i] != (xCharInfo *) & nonExistantChar)
15523a0898aSmrg		    glyphs[i] = ink_metrics + (((CharInfoPtr) glyphs[i]) - metrics);
15623a0898aSmrg	    }
15723a0898aSmrg	}
15823a0898aSmrg    }
15923a0898aSmrg    bitmapFont->pDefault = oldDefault;
16023a0898aSmrg    return ret;
16123a0898aSmrg}
162