fontink.c revision a96d7823
1a96d7823Smrg/*
2a96d7823Smrg
3a96d7823SmrgCopyright 1990, 1998  The Open Group
4a96d7823Smrg
5a96d7823SmrgPermission to use, copy, modify, distribute, and sell this software and its
6a96d7823Smrgdocumentation for any purpose is hereby granted without fee, provided that
7a96d7823Smrgthe above copyright notice appear in all copies and that both that
8a96d7823Smrgcopyright notice and this permission notice appear in supporting
9a96d7823Smrgdocumentation.
10a96d7823Smrg
11a96d7823SmrgThe above copyright notice and this permission notice shall be included
12a96d7823Smrgin all copies or substantial portions of the Software.
13a96d7823Smrg
14a96d7823SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15a96d7823SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16a96d7823SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17a96d7823SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18a96d7823SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19a96d7823SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20a96d7823SmrgOTHER DEALINGS IN THE SOFTWARE.
21a96d7823Smrg
22a96d7823SmrgExcept as contained in this notice, the name of The Open Group shall
23a96d7823Smrgnot be used in advertising or otherwise to promote the sale, use or
24a96d7823Smrgother dealings in this Software without prior written authorization
25a96d7823Smrgfrom The Open Group.
26a96d7823Smrg
27a96d7823Smrg*/
28a96d7823Smrg
29a96d7823Smrg/*
30a96d7823Smrg * Author:  Keith Packard, MIT X Consortium
31a96d7823Smrg */
32a96d7823Smrg
33a96d7823Smrg#ifdef HAVE_CONFIG_H
34a96d7823Smrg#include <config.h>
35a96d7823Smrg#endif
36a96d7823Smrg#include "libxfontint.h"
37a96d7823Smrg
38a96d7823Smrg#include <X11/fonts/fntfilst.h>
39a96d7823Smrg#include <X11/fonts/bitmap.h>
40a96d7823Smrg#include <X11/fonts/bdfint.h>
41a96d7823Smrg
42a96d7823Smrgstatic unsigned char ink_mask_msb[8] = {
43a96d7823Smrg    0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01,
44a96d7823Smrg};
45a96d7823Smrg
46a96d7823Smrgstatic unsigned char ink_mask_lsb[8] = {
47a96d7823Smrg    0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80,
48a96d7823Smrg};
49a96d7823Smrg
50a96d7823Smrgvoid
51a96d7823SmrgFontCharInkMetrics(FontPtr pFont, CharInfoPtr pCI, xCharInfo *pInk)
52a96d7823Smrg{
53a96d7823Smrg    int         leftBearing,
54a96d7823Smrg                ascent,
55a96d7823Smrg                descent;
56a96d7823Smrg    register int vpos,
57a96d7823Smrg                hpos,
58a96d7823Smrg                bpos = 0;
59a96d7823Smrg    int         bitmapByteWidth,
60a96d7823Smrg                bitmapByteWidthPadded;
61a96d7823Smrg    int         bitmapBitWidth;
62a96d7823Smrg    int         span;
63a96d7823Smrg    register unsigned char *p;
64a96d7823Smrg    unsigned char *ink_mask = 0;
65a96d7823Smrg    register int bmax;
66a96d7823Smrg    register unsigned char charbits;
67a96d7823Smrg
68a96d7823Smrg    if (pFont->bit == MSBFirst)
69a96d7823Smrg	ink_mask = ink_mask_msb;
70a96d7823Smrg    else if (pFont->bit == LSBFirst)
71a96d7823Smrg	ink_mask = ink_mask_lsb;
72a96d7823Smrg    pInk->characterWidth = pCI->metrics.characterWidth;
73a96d7823Smrg    pInk->attributes = pCI->metrics.attributes;
74a96d7823Smrg
75a96d7823Smrg    leftBearing = pCI->metrics.leftSideBearing;
76a96d7823Smrg    ascent = pCI->metrics.ascent;
77a96d7823Smrg    descent = pCI->metrics.descent;
78a96d7823Smrg    bitmapBitWidth = GLYPHWIDTHPIXELS(pCI);
79a96d7823Smrg    bitmapByteWidth = GLYPHWIDTHBYTES(pCI);
80a96d7823Smrg    bitmapByteWidthPadded = BYTES_PER_ROW(bitmapBitWidth, pFont->glyph);
81a96d7823Smrg    span = bitmapByteWidthPadded - bitmapByteWidth;
82a96d7823Smrg
83a96d7823Smrg    p = (unsigned char *) pCI->bits;
84a96d7823Smrg    for (vpos = descent + ascent; --vpos >= 0;) {
85a96d7823Smrg	for (hpos = bitmapByteWidth; --hpos >= 0;) {
86a96d7823Smrg	    if (*p++ != 0)
87a96d7823Smrg		goto found_ascent;
88a96d7823Smrg	}
89a96d7823Smrg	p += span;
90a96d7823Smrg    }
91a96d7823Smrg    /*
92a96d7823Smrg     * special case -- font with no bits gets all zeros
93a96d7823Smrg     */
94a96d7823Smrg    pInk->leftSideBearing = leftBearing;
95a96d7823Smrg    pInk->rightSideBearing = leftBearing;
96a96d7823Smrg    pInk->ascent = 0;
97a96d7823Smrg    pInk->descent = 0;
98a96d7823Smrg    return;
99a96d7823Smrgfound_ascent:
100a96d7823Smrg    pInk->ascent = vpos - descent + 1;
101a96d7823Smrg
102a96d7823Smrg    p = ((unsigned char *) pCI->bits) + bitmapByteWidthPadded *
103a96d7823Smrg	(descent + ascent - 1) + bitmapByteWidth;
104a96d7823Smrg
105a96d7823Smrg    for (vpos = descent + ascent; --vpos >= 0;) {
106a96d7823Smrg	for (hpos = bitmapByteWidth; --hpos >= 0;) {
107a96d7823Smrg	    if (*--p != 0)
108a96d7823Smrg		goto found_descent;
109a96d7823Smrg	}
110a96d7823Smrg	p -= span;
111a96d7823Smrg    }
112a96d7823Smrgfound_descent:
113a96d7823Smrg    pInk->descent = vpos - ascent + 1;
114a96d7823Smrg
115a96d7823Smrg    bmax = 8;
116a96d7823Smrg    for (hpos = 0; hpos < bitmapByteWidth; hpos++) {
117a96d7823Smrg	charbits = 0;
118a96d7823Smrg	p = (unsigned char *) pCI->bits + hpos;
119a96d7823Smrg	for (vpos = descent + ascent; --vpos >= 0; p += bitmapByteWidthPadded)
120a96d7823Smrg	    charbits |= *p;
121a96d7823Smrg	if (charbits) {
122a96d7823Smrg	    if (hpos == bitmapByteWidth - 1)
123a96d7823Smrg		bmax = bitmapBitWidth - (hpos << 3);
124a96d7823Smrg	    p = ink_mask;
125a96d7823Smrg	    for (bpos = bmax; --bpos >= 0;) {
126a96d7823Smrg		if (charbits & *p++)
127a96d7823Smrg		    goto found_left;
128a96d7823Smrg	    }
129a96d7823Smrg	}
130a96d7823Smrg    }
131a96d7823Smrgfound_left:
132a96d7823Smrg    pInk->leftSideBearing = leftBearing + (hpos << 3) + bmax - bpos - 1;
133a96d7823Smrg
134a96d7823Smrg    bmax = bitmapBitWidth - ((bitmapByteWidth - 1) << 3);
135a96d7823Smrg    for (hpos = bitmapByteWidth; --hpos >= 0;) {
136a96d7823Smrg	charbits = 0;
137a96d7823Smrg	p = (unsigned char *) pCI->bits + hpos;
138a96d7823Smrg	for (vpos = descent + ascent; --vpos >= 0; p += bitmapByteWidthPadded)
139a96d7823Smrg	    charbits |= *p;
140a96d7823Smrg	if (charbits) {
141a96d7823Smrg	    p = ink_mask + bmax;
142a96d7823Smrg	    for (bpos = bmax; --bpos >= 0;) {
143a96d7823Smrg		if (charbits & *--p)
144a96d7823Smrg		    goto found_right;
145a96d7823Smrg	    }
146a96d7823Smrg	}
147a96d7823Smrg	bmax = 8;
148a96d7823Smrg    }
149a96d7823Smrgfound_right:
150a96d7823Smrg    pInk->rightSideBearing = leftBearing + (hpos << 3) + bpos + 1;
151a96d7823Smrg}
152a96d7823Smrg
153a96d7823Smrg#define	ISBITONMSB(x, line)	((line)[(x)/8] & (1 << (7-((x)%8))))
154a96d7823Smrg#define	SETBITMSB(x, line)	((line)[(x)/8] |= (1 << (7-((x)%8))))
155a96d7823Smrg#define	ISBITONLSB(x, line)	((line)[(x)/8] & (1 << ((x)%8)))
156a96d7823Smrg#define	SETBITLSB(x, line)	((line)[(x)/8] |= (1 << ((x)%8)))
157a96d7823Smrg
158a96d7823Smrg#define Min(a,b)    ((a)<(b)?(a):(b))
159a96d7823Smrg#define Max(a,b)    ((a)>(b)?(a):(b))
160a96d7823Smrg
161a96d7823Smrgvoid
162a96d7823SmrgFontCharReshape(FontPtr pFont, CharInfoPtr pSrc, CharInfoPtr pDst)
163a96d7823Smrg{
164a96d7823Smrg    int         x,
165a96d7823Smrg                y;
166a96d7823Smrg    unsigned char *in_line,
167a96d7823Smrg               *out_line;
168a96d7823Smrg    unsigned char *oldglyph,
169a96d7823Smrg               *newglyph;
170a96d7823Smrg    int         inwidth;
171a96d7823Smrg    int         outwidth,
172a96d7823Smrg                outheight;
173a96d7823Smrg    int         out_bytes,
174a96d7823Smrg                in_bytes;
175a96d7823Smrg    int         y_min,
176a96d7823Smrg                y_max,
177a96d7823Smrg                x_min,
178a96d7823Smrg                x_max;
179a96d7823Smrg
180a96d7823Smrg    newglyph = (unsigned char *) pDst->bits;
181a96d7823Smrg    outwidth = pDst->metrics.rightSideBearing - pDst->metrics.leftSideBearing;
182a96d7823Smrg    outheight = pDst->metrics.descent + pDst->metrics.ascent;
183a96d7823Smrg    out_bytes = BYTES_PER_ROW(outwidth, pFont->glyph);
184a96d7823Smrg
185a96d7823Smrg    oldglyph = (unsigned char *) pSrc->bits;
186a96d7823Smrg    inwidth = pSrc->metrics.rightSideBearing - pSrc->metrics.leftSideBearing;
187a96d7823Smrg    in_bytes = BYTES_PER_ROW(inwidth, pFont->glyph);
188a96d7823Smrg
189a96d7823Smrg    bzero(newglyph, out_bytes * outheight);
190a96d7823Smrg    in_line = oldglyph;
191a96d7823Smrg    out_line = newglyph;
192a96d7823Smrg    y_min = Max(-pSrc->metrics.ascent, -pDst->metrics.ascent);
193a96d7823Smrg    y_max = Min(pSrc->metrics.descent, pDst->metrics.descent);
194a96d7823Smrg    x_min = Max(pSrc->metrics.leftSideBearing, pDst->metrics.leftSideBearing);
195a96d7823Smrg    x_max = Min(pSrc->metrics.rightSideBearing, pDst->metrics.rightSideBearing);
196a96d7823Smrg    in_line += (y_min + pSrc->metrics.ascent) * in_bytes;
197a96d7823Smrg    out_line += (y_min + pDst->metrics.ascent) * out_bytes;
198a96d7823Smrg    if (pFont->bit == MSBFirst) {
199a96d7823Smrg	for (y = y_min; y < y_max; y++) {
200a96d7823Smrg	    for (x = x_min; x < x_max; x++) {
201a96d7823Smrg		if (ISBITONMSB(x - pSrc->metrics.leftSideBearing, in_line))
202a96d7823Smrg		    SETBITMSB(x - pDst->metrics.leftSideBearing, out_line);
203a96d7823Smrg	    }
204a96d7823Smrg	    in_line += in_bytes;
205a96d7823Smrg	    out_line += out_bytes;
206a96d7823Smrg	}
207a96d7823Smrg    } else {
208a96d7823Smrg	for (y = y_min; y < y_max; y++) {
209a96d7823Smrg	    for (x = x_min; x < x_max; x++) {
210a96d7823Smrg		if (ISBITONLSB(x - pSrc->metrics.leftSideBearing, in_line))
211a96d7823Smrg		    SETBITLSB(x - pDst->metrics.leftSideBearing, out_line);
212a96d7823Smrg	    }
213a96d7823Smrg	    in_line += in_bytes;
214a96d7823Smrg	    out_line += out_bytes;
215a96d7823Smrg	}
216a96d7823Smrg    }
217a96d7823Smrg}
218