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