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