11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1989, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included
121ab64890Smrgin all copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151ab64890SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
161ab64890SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
171ab64890SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
181ab64890SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
191ab64890SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201ab64890SmrgOTHER DEALINGS IN THE SOFTWARE.
211ab64890Smrg
221ab64890SmrgExcept as contained in this notice, the name of The Open Group shall
231ab64890Smrgnot be used in advertising or otherwise to promote the sale, use or
241ab64890Smrgother dealings in this Software without prior written authorization
251ab64890Smrgfrom The Open Group.
261ab64890Smrg
271ab64890Smrg*/
281ab64890Smrg/*
291ab64890Smrg * Copyright 1995 by FUJITSU LIMITED
301ab64890Smrg * This is source code modified by FUJITSU LIMITED under the Joint
311ab64890Smrg * Development Agreement for the CDE/Motif PST.
321ab64890Smrg */
331ab64890Smrg
341ab64890Smrg
351ab64890Smrg#ifdef HAVE_CONFIG_H
361ab64890Smrg#include <config.h>
371ab64890Smrg#endif
381ab64890Smrg#include "Xlibint.h"
391ab64890Smrg
401ab64890Smrg#define min_byte2 min_char_or_byte2
411ab64890Smrg#define max_byte2 max_char_or_byte2
421ab64890Smrg
431ab64890Smrg
4461b2299dSmrg/*
451ab64890Smrg * CI_GET_ROWZERO_CHAR_INFO_2D - do the same thing as CI_GET_CHAR_INFO_1D,
461ab64890Smrg * except that the font has more than one row.  This is special case of more
471ab64890Smrg * general version used in XTextExt16.c since row == 0.  This is used when
481ab64890Smrg * max_byte2 is not zero.  A further optimization would do the check for
491ab64890Smrg * min_byte1 being zero ahead of time.
501ab64890Smrg */
511ab64890Smrg
521ab64890Smrg#define CI_GET_ROWZERO_CHAR_INFO_2D(fs,col,def,cs) \
5307fb9b8fSmrgdo { \
541ab64890Smrg    cs = def; \
551ab64890Smrg    if (fs->min_byte1 == 0 && \
561ab64890Smrg	col >= fs->min_byte2 && col <= fs->max_byte2) { \
571ab64890Smrg	if (fs->per_char == NULL) { \
581ab64890Smrg	    cs = &fs->min_bounds; \
591ab64890Smrg	} else { \
601ab64890Smrg	    cs = &fs->per_char[(col - fs->min_byte2)]; \
611ab64890Smrg	    if (CI_NONEXISTCHAR(cs)) cs = def; \
621ab64890Smrg	} \
631ab64890Smrg    } \
6407fb9b8fSmrg} while (0)
651ab64890Smrg
661ab64890Smrg
671ab64890Smrg/*
681ab64890Smrg * XTextExtents - compute the extents of string given as a sequences of eight
691ab64890Smrg * bit bytes.  Since we know that the input characters will always be from the
701ab64890Smrg * first row of the font (i.e. byte1 == 0), we can do some optimizations beyond
711ab64890Smrg * what is done in XTextExtents16.
721ab64890Smrg */
731ab64890Smrgint
741ab64890SmrgXTextExtents (
751ab64890Smrg    XFontStruct *fs,
761ab64890Smrg    _Xconst char *string,
771ab64890Smrg    int nchars,
781ab64890Smrg    int *dir,           /* RETURN font information */
791ab64890Smrg    int *font_ascent,   /* RETURN font information */
801ab64890Smrg    int *font_descent,  /* RETURN font information */
811ab64890Smrg    register XCharStruct *overall)	/* RETURN character information */
821ab64890Smrg{
831ab64890Smrg    int i;				/* iterator */
841ab64890Smrg    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
851ab64890Smrg    int nfound = 0;			/* number of characters found */
861ab64890Smrg    XCharStruct *def;			/* info about default char */
871ab64890Smrg    unsigned char *us;  		/* be 8bit clean */
881ab64890Smrg
891ab64890Smrg    if (singlerow) {			/* optimization */
901ab64890Smrg	CI_GET_DEFAULT_INFO_1D (fs, def);
911ab64890Smrg    } else {
921ab64890Smrg	CI_GET_DEFAULT_INFO_2D (fs, def);
931ab64890Smrg    }
941ab64890Smrg
951ab64890Smrg    *dir = fs->direction;
961ab64890Smrg    *font_ascent = fs->ascent;
971ab64890Smrg    *font_descent = fs->descent;
981ab64890Smrg
991ab64890Smrg    /*
1001ab64890Smrg     * Iterate over the input string getting the appropriate * char struct.
1011ab64890Smrg     * The default (which may be null if there is no def_char) will be returned
1021ab64890Smrg     * if the character doesn't exist.  On the first time * through the loop,
1031ab64890Smrg     * assign the values to overall; otherwise, compute * the new values.
1041ab64890Smrg     */
1051ab64890Smrg
1061ab64890Smrg    for (i = 0, us = (unsigned char *) string; i < nchars; i++, us++) {
1071ab64890Smrg	register unsigned uc = (unsigned) *us;	/* since about to do macro */
1081ab64890Smrg	register XCharStruct *cs;
1091ab64890Smrg
1101ab64890Smrg	if (singlerow) {		/* optimization */
1111ab64890Smrg	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
1121ab64890Smrg	} else {
1131ab64890Smrg	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
1141ab64890Smrg	}
1151ab64890Smrg
1161ab64890Smrg	if (cs) {
1171ab64890Smrg	    if (nfound++ == 0) {
1181ab64890Smrg		*overall = *cs;
1191ab64890Smrg	    } else {
1201ab64890Smrg		overall->ascent = max (overall->ascent, cs->ascent);
1211ab64890Smrg		overall->descent = max (overall->descent, cs->descent);
12261b2299dSmrg		overall->lbearing = min (overall->lbearing,
1231ab64890Smrg					 overall->width + cs->lbearing);
1241ab64890Smrg		overall->rbearing = max (overall->rbearing,
1251ab64890Smrg					 overall->width + cs->rbearing);
1261ab64890Smrg		overall->width += cs->width;
1271ab64890Smrg	    }
1281ab64890Smrg	}
1291ab64890Smrg    }
1301ab64890Smrg
1311ab64890Smrg    /*
1321ab64890Smrg     * if there were no characters, then set everything to 0
1331ab64890Smrg     */
1341ab64890Smrg    if (nfound == 0) {
13561b2299dSmrg	overall->width = overall->ascent = overall->descent =
1361ab64890Smrg	  overall->lbearing = overall->rbearing = 0;
1371ab64890Smrg    }
1381ab64890Smrg
1391ab64890Smrg    return 0;
1401ab64890Smrg}
1411ab64890Smrg
1421ab64890Smrg
1431ab64890Smrg/*
14461b2299dSmrg * XTextWidth - compute the width of a string of eightbit bytes.  This is a
1451ab64890Smrg * subset of XTextExtents.
1461ab64890Smrg */
1471ab64890Smrgint
1481ab64890SmrgXTextWidth (
1491ab64890Smrg    XFontStruct *fs,
1501ab64890Smrg    _Xconst char *string,
1511ab64890Smrg    int count)
1521ab64890Smrg{
1531ab64890Smrg    int i;				/* iterator */
1541ab64890Smrg    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
1551ab64890Smrg    XCharStruct *def;			/* info about default char */
1561ab64890Smrg    unsigned char *us;  		/* be 8bit clean */
1571ab64890Smrg    int width = 0;			/* RETURN value */
1581ab64890Smrg
1591ab64890Smrg    if (singlerow) {			/* optimization */
1601ab64890Smrg	CI_GET_DEFAULT_INFO_1D (fs, def);
1611ab64890Smrg    } else {
1621ab64890Smrg	CI_GET_DEFAULT_INFO_2D (fs, def);
1631ab64890Smrg    }
1641ab64890Smrg
1651ab64890Smrg    if (def && fs->min_bounds.width == fs->max_bounds.width)
1661ab64890Smrg	return (fs->min_bounds.width * count);
1671ab64890Smrg
1681ab64890Smrg    /*
1691ab64890Smrg     * Iterate over all character in the input string; only consider characters
1701ab64890Smrg     * that exist.
1711ab64890Smrg     */
1721ab64890Smrg    for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
1731ab64890Smrg	register unsigned uc = (unsigned) *us;	/* since about to do macro */
1741ab64890Smrg	register XCharStruct *cs;
1751ab64890Smrg
1761ab64890Smrg	if (singlerow) {		/* optimization */
1771ab64890Smrg	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
1781ab64890Smrg	} else {
1791ab64890Smrg	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
1801ab64890Smrg	}
1811ab64890Smrg
1821ab64890Smrg	if (cs) width += cs->width;
1831ab64890Smrg    }
1841ab64890Smrg
1851ab64890Smrg    return width;
1861ab64890Smrg}
1871ab64890Smrg
1881ab64890Smrg
1891ab64890Smrg
1901ab64890Smrg/*
1911ab64890Smrg * _XTextHeight - compute the height of a string of eightbit bytes.
1921ab64890Smrg */
1931ab64890Smrgint
1941ab64890Smrg_XTextHeight (
1951ab64890Smrg    XFontStruct *fs,
1961ab64890Smrg    _Xconst char *string,
1971ab64890Smrg    int count)
1981ab64890Smrg{
1991ab64890Smrg    int i;				/* iterator */
2001ab64890Smrg    Bool singlerow = (fs->max_byte1 == 0);  /* optimization */
2011ab64890Smrg    XCharStruct *def;			/* info about default char */
2021ab64890Smrg    unsigned char *us;  		/* be 8bit clean */
2031ab64890Smrg    int height = 0;			/* RETURN value */
2041ab64890Smrg
2051ab64890Smrg    if (singlerow) {			/* optimization */
2061ab64890Smrg	CI_GET_DEFAULT_INFO_1D (fs, def);
2071ab64890Smrg    } else {
2081ab64890Smrg	CI_GET_DEFAULT_INFO_2D (fs, def);
2091ab64890Smrg    }
2101ab64890Smrg
2111ab64890Smrg    if (def && (fs->min_bounds.ascent == fs->max_bounds.ascent)
2121ab64890Smrg	    && (fs->min_bounds.descent == fs->max_bounds.descent))
2131ab64890Smrg	return ((fs->min_bounds.ascent + fs->min_bounds.descent) * count);
2141ab64890Smrg
2151ab64890Smrg    /*
2161ab64890Smrg     * Iterate over all character in the input string; only consider characters
2171ab64890Smrg     * that exist.
2181ab64890Smrg     */
2191ab64890Smrg    for (i = 0, us = (unsigned char *) string; i < count; i++, us++) {
2201ab64890Smrg	register unsigned uc = (unsigned) *us;	/* since about to do macro */
2211ab64890Smrg	register XCharStruct *cs;
2221ab64890Smrg
2231ab64890Smrg	if (singlerow) {		/* optimization */
2241ab64890Smrg	    CI_GET_CHAR_INFO_1D (fs, uc, def, cs);
2251ab64890Smrg	} else {
2261ab64890Smrg	    CI_GET_ROWZERO_CHAR_INFO_2D (fs, uc, def, cs);
2271ab64890Smrg	}
2281ab64890Smrg
2291ab64890Smrg	if (cs) height += (cs->ascent + cs->descent);
2301ab64890Smrg    }
2311ab64890Smrg
2321ab64890Smrg    return height;
2331ab64890Smrg}
2341ab64890Smrg
235