TextExt.c revision 07fb9b8f
1/*
2
3Copyright 1989, 1998  The Open Group
4
5Permission to use, copy, modify, distribute, and sell this software and its
6documentation for any purpose is hereby granted without fee, provided that
7the above copyright notice appear in all copies and that both that
8copyright notice and this permission notice appear in supporting
9documentation.
10
11The above copyright notice and this permission notice shall be included
12in all copies or substantial portions of the Software.
13
14THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall
23not be used in advertising or otherwise to promote the sale, use or
24other dealings in this Software without prior written authorization
25from The Open Group.
26
27*/
28/*
29 * Copyright 1995 by FUJITSU LIMITED
30 * This is source code modified by FUJITSU LIMITED under the Joint
31 * Development Agreement for the CDE/Motif PST.
32 */
33
34
35#ifdef HAVE_CONFIG_H
36#include <config.h>
37#endif
38#include "Xlibint.h"
39
40#define min_byte2 min_char_or_byte2
41#define max_byte2 max_char_or_byte2
42
43
44/*
45 * CI_GET_ROWZERO_CHAR_INFO_2D - do the same thing as CI_GET_CHAR_INFO_1D,
46 * except that the font has more than one row.  This is special case of more
47 * general version used in XTextExt16.c since row == 0.  This is used when
48 * max_byte2 is not zero.  A further optimization would do the check for
49 * min_byte1 being zero ahead of time.
50 */
51
52#define CI_GET_ROWZERO_CHAR_INFO_2D(fs,col,def,cs) \
53do { \
54    cs = def; \
55    if (fs->min_byte1 == 0 && \
56	col >= fs->min_byte2 && col <= fs->max_byte2) { \
57	if (fs->per_char == NULL) { \
58	    cs = &fs->min_bounds; \
59	} else { \
60	    cs = &fs->per_char[(col - fs->min_byte2)]; \
61	    if (CI_NONEXISTCHAR(cs)) cs = def; \
62	} \
63    } \
64} while (0)
65
66
67/*
68 * XTextExtents - compute the extents of string given as a sequences of eight
69 * bit bytes.  Since we know that the input characters will always be from the
70 * first row of the font (i.e. byte1 == 0), we can do some optimizations beyond
71 * what is done in XTextExtents16.
72 */
73int
74XTextExtents (
75    XFontStruct *fs,
76    _Xconst char *string,
77    int nchars,
78    int *dir,           /* RETURN font information */
79    int *font_ascent,   /* RETURN font information */
80    int *font_descent,  /* RETURN font information */
81    register XCharStruct *overall)	/* RETURN character information */
82{
83    int i;				/* iterator */
84    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
85    int nfound = 0;			/* number of characters found */
86    XCharStruct *def;			/* info about default char */
87    unsigned char *us;  		/* be 8bit clean */
88
89    if (singlerow) {			/* optimization */
90	CI_GET_DEFAULT_INFO_1D (fs, def);
91    } else {
92	CI_GET_DEFAULT_INFO_2D (fs, def);
93    }
94
95    *dir = fs->direction;
96    *font_ascent = fs->ascent;
97    *font_descent = fs->descent;
98
99    /*
100     * Iterate over the input string getting the appropriate * char struct.
101     * The default (which may be null if there is no def_char) will be returned
102     * if the character doesn't exist.  On the first time * through the loop,
103     * assign the values to overall; otherwise, compute * the new values.
104     */
105
106    for (i = 0, us = (unsigned char *) string; i < nchars; i++, us++) {
107	register unsigned uc = (unsigned) *us;	/* since about to do macro */
108	register XCharStruct *cs;
109
110	if (singlerow) {		/* optimization */
111	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
112	} else {
113	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
114	}
115
116	if (cs) {
117	    if (nfound++ == 0) {
118		*overall = *cs;
119	    } else {
120		overall->ascent = max (overall->ascent, cs->ascent);
121		overall->descent = max (overall->descent, cs->descent);
122		overall->lbearing = min (overall->lbearing,
123					 overall->width + cs->lbearing);
124		overall->rbearing = max (overall->rbearing,
125					 overall->width + cs->rbearing);
126		overall->width += cs->width;
127	    }
128	}
129    }
130
131    /*
132     * if there were no characters, then set everything to 0
133     */
134    if (nfound == 0) {
135	overall->width = overall->ascent = overall->descent =
136	  overall->lbearing = overall->rbearing = 0;
137    }
138
139    return 0;
140}
141
142
143/*
144 * XTextWidth - compute the width of a string of eightbit bytes.  This is a
145 * subset of XTextExtents.
146 */
147int
148XTextWidth (
149    XFontStruct *fs,
150    _Xconst char *string,
151    int count)
152{
153    int i;				/* iterator */
154    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
155    XCharStruct *def;			/* info about default char */
156    unsigned char *us;  		/* be 8bit clean */
157    int width = 0;			/* RETURN value */
158
159    if (singlerow) {			/* optimization */
160	CI_GET_DEFAULT_INFO_1D (fs, def);
161    } else {
162	CI_GET_DEFAULT_INFO_2D (fs, def);
163    }
164
165    if (def && fs->min_bounds.width == fs->max_bounds.width)
166	return (fs->min_bounds.width * count);
167
168    /*
169     * Iterate over all character in the input string; only consider characters
170     * that exist.
171     */
172    for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
173	register unsigned uc = (unsigned) *us;	/* since about to do macro */
174	register XCharStruct *cs;
175
176	if (singlerow) {		/* optimization */
177	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
178	} else {
179	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
180	}
181
182	if (cs) width += cs->width;
183    }
184
185    return width;
186}
187
188
189
190/*
191 * _XTextHeight - compute the height of a string of eightbit bytes.
192 */
193int
194_XTextHeight (
195    XFontStruct *fs,
196    _Xconst char *string,
197    int count)
198{
199    int i;				/* iterator */
200    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
201    XCharStruct *def;			/* info about default char */
202    unsigned char *us;  		/* be 8bit clean */
203    int height = 0;			/* RETURN value */
204
205    if (singlerow) {			/* optimization */
206	CI_GET_DEFAULT_INFO_1D (fs, def);
207    } else {
208	CI_GET_DEFAULT_INFO_2D (fs, def);
209    }
210
211    if (def && (fs->min_bounds.ascent == fs->max_bounds.ascent)
212	    && (fs->min_bounds.descent == fs->max_bounds.descent))
213	return ((fs->min_bounds.ascent + fs->min_bounds.descent) * count);
214
215    /*
216     * Iterate over all character in the input string; only consider characters
217     * that exist.
218     */
219    for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
220	register unsigned uc = (unsigned) *us;	/* since about to do macro */
221	register XCharStruct *cs;
222
223	if (singlerow) {		/* optimization */
224	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
225	} else {
226	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
227	}
228
229	if (cs) height += (cs->ascent + cs->descent);
230    }
231
232    return height;
233}
234
235