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