123a0898aSmrg/*
223a0898aSmrg
323a0898aSmrgCopyright 1990, 1994, 1998  The Open Group
423a0898aSmrg
523a0898aSmrgPermission to use, copy, modify, distribute, and sell this software and its
623a0898aSmrgdocumentation for any purpose is hereby granted without fee, provided that
723a0898aSmrgthe above copyright notice appear in all copies and that both that
823a0898aSmrgcopyright notice and this permission notice appear in supporting
923a0898aSmrgdocumentation.
1023a0898aSmrg
1123a0898aSmrgThe above copyright notice and this permission notice shall be included
1223a0898aSmrgin all copies or substantial portions of the Software.
1323a0898aSmrg
1423a0898aSmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1523a0898aSmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1623a0898aSmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
1723a0898aSmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
1823a0898aSmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1923a0898aSmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2023a0898aSmrgOTHER DEALINGS IN THE SOFTWARE.
2123a0898aSmrg
2223a0898aSmrgExcept as contained in this notice, the name of The Open Group shall
2323a0898aSmrgnot be used in advertising or otherwise to promote the sale, use or
2423a0898aSmrgother dealings in this Software without prior written authorization
2523a0898aSmrgfrom The Open Group.
2623a0898aSmrg
2723a0898aSmrg*/
2823a0898aSmrg
2923a0898aSmrg#ifdef HAVE_CONFIG_H
3023a0898aSmrg#include <config.h>
3123a0898aSmrg#endif
3223a0898aSmrg
3323a0898aSmrg#include <X11/fonts/fntfilst.h>
3423a0898aSmrg#include <X11/fonts/bitmap.h>
3523a0898aSmrg#include <X11/fonts/bdfint.h>
3623a0898aSmrg
3723a0898aSmrg#ifndef MAXSHORT
3823a0898aSmrg#define MAXSHORT    32767
3923a0898aSmrg#endif
4023a0898aSmrg
4123a0898aSmrg#ifndef MINSHORT
4223a0898aSmrg#define MINSHORT    -32768
4323a0898aSmrg#endif
4423a0898aSmrg
4523a0898aSmrgstatic xCharInfo initMinMetrics = {
4623a0898aSmrgMAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, 0xFFFF};
4723a0898aSmrgstatic xCharInfo initMaxMetrics = {
4823a0898aSmrgMINSHORT, MINSHORT, MINSHORT, MINSHORT, MINSHORT, 0x0000};
4923a0898aSmrg
5023a0898aSmrg#define MINMAX(field,ci) \
5123a0898aSmrg	if (minbounds->field > (ci)->field) \
5223a0898aSmrg	     minbounds->field = (ci)->field; \
5323a0898aSmrg	if (maxbounds->field < (ci)->field) \
5423a0898aSmrg	     maxbounds->field = (ci)->field;
5523a0898aSmrg
5623a0898aSmrg#define COMPUTE_MINMAX(ci) \
5723a0898aSmrg    if ((ci)->ascent || (ci)->descent || \
5823a0898aSmrg	(ci)->leftSideBearing || (ci)->rightSideBearing || \
5923a0898aSmrg	(ci)->characterWidth) \
6023a0898aSmrg    { \
6123a0898aSmrg	MINMAX(ascent, (ci)); \
6223a0898aSmrg	MINMAX(descent, (ci)); \
6323a0898aSmrg	MINMAX(leftSideBearing, (ci)); \
6423a0898aSmrg	MINMAX(rightSideBearing, (ci)); \
6523a0898aSmrg	MINMAX(characterWidth, (ci)); \
6623a0898aSmrg    }
6723a0898aSmrg
6823a0898aSmrgvoid
6923a0898aSmrgbitmapComputeFontBounds(FontPtr pFont)
7023a0898aSmrg{
7123a0898aSmrg    BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
7223a0898aSmrg    int         nchars;
7323a0898aSmrg    int         r,
7423a0898aSmrg                c;
7523a0898aSmrg    CharInfoPtr ci;
7623a0898aSmrg    int         maxOverlap;
7723a0898aSmrg    int         overlap;
7823a0898aSmrg    xCharInfo  *minbounds,
7923a0898aSmrg               *maxbounds;
8023a0898aSmrg    int         i;
8123a0898aSmrg    int		numneg = 0, numpos = 0;
8223a0898aSmrg
8323a0898aSmrg    if (bitmapFont->bitmapExtra) {
8423a0898aSmrg	minbounds = &bitmapFont->bitmapExtra->info.minbounds;
8523a0898aSmrg	maxbounds = &bitmapFont->bitmapExtra->info.maxbounds;
8623a0898aSmrg    } else {
8723a0898aSmrg	minbounds = &pFont->info.minbounds;
8823a0898aSmrg	maxbounds = &pFont->info.maxbounds;
8923a0898aSmrg    }
9023a0898aSmrg    *minbounds = initMinMetrics;
9123a0898aSmrg    *maxbounds = initMaxMetrics;
9223a0898aSmrg    maxOverlap = MINSHORT;
9323a0898aSmrg    nchars = bitmapFont->num_chars;
9423a0898aSmrg    for (i = 0, ci = bitmapFont->metrics; i < nchars; i++, ci++) {
9523a0898aSmrg	COMPUTE_MINMAX(&ci->metrics);
9623a0898aSmrg	if (ci->metrics.characterWidth < 0)
9723a0898aSmrg	    numneg++;
9823a0898aSmrg	else
9923a0898aSmrg	    numpos++;
10023a0898aSmrg	minbounds->attributes &= ci->metrics.attributes;
10123a0898aSmrg	maxbounds->attributes |= ci->metrics.attributes;
10223a0898aSmrg	overlap = ci->metrics.rightSideBearing - ci->metrics.characterWidth;
10323a0898aSmrg	if (maxOverlap < overlap)
10423a0898aSmrg	    maxOverlap = overlap;
10523a0898aSmrg    }
10623a0898aSmrg    if (bitmapFont->bitmapExtra) {
10723a0898aSmrg	if (numneg > numpos)
10823a0898aSmrg	    bitmapFont->bitmapExtra->info.drawDirection = RightToLeft;
10923a0898aSmrg	else
11023a0898aSmrg	    bitmapFont->bitmapExtra->info.drawDirection = LeftToRight;
11123a0898aSmrg	bitmapFont->bitmapExtra->info.maxOverlap = maxOverlap;
11223a0898aSmrg	minbounds = &pFont->info.minbounds;
11323a0898aSmrg	maxbounds = &pFont->info.maxbounds;
11423a0898aSmrg	*minbounds = initMinMetrics;
11523a0898aSmrg	*maxbounds = initMaxMetrics;
11623a0898aSmrg        i = 0;
11723a0898aSmrg	maxOverlap = MINSHORT;
11823a0898aSmrg	for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
11923a0898aSmrg	    for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
12023a0898aSmrg		ci = ACCESSENCODING(bitmapFont->encoding, i);
12123a0898aSmrg		if (ci) {
12223a0898aSmrg		    COMPUTE_MINMAX(&ci->metrics);
12323a0898aSmrg		    if (ci->metrics.characterWidth < 0)
12423a0898aSmrg			numneg++;
12523a0898aSmrg		    else
12623a0898aSmrg			numpos++;
12723a0898aSmrg		    minbounds->attributes &= ci->metrics.attributes;
12823a0898aSmrg		    maxbounds->attributes |= ci->metrics.attributes;
12923a0898aSmrg		    overlap = ci->metrics.rightSideBearing -
13023a0898aSmrg			ci->metrics.characterWidth;
13123a0898aSmrg		    if (maxOverlap < overlap)
13223a0898aSmrg			maxOverlap = overlap;
13323a0898aSmrg		}
13423a0898aSmrg                i++;
13523a0898aSmrg	    }
13623a0898aSmrg	}
13723a0898aSmrg    }
13823a0898aSmrg    if (numneg > numpos)
13923a0898aSmrg	pFont->info.drawDirection = RightToLeft;
14023a0898aSmrg    else
14123a0898aSmrg	pFont->info.drawDirection = LeftToRight;
14223a0898aSmrg    pFont->info.maxOverlap = maxOverlap;
14323a0898aSmrg}
14423a0898aSmrg
14523a0898aSmrgvoid
14623a0898aSmrgbitmapComputeFontInkBounds(FontPtr pFont)
14723a0898aSmrg{
14823a0898aSmrg    BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
14923a0898aSmrg    int         nchars;
15023a0898aSmrg    int         r,
15123a0898aSmrg                c;
15223a0898aSmrg    CharInfoPtr cit;
15323a0898aSmrg    xCharInfo  *ci;
15423a0898aSmrg    int         offset;
15523a0898aSmrg    xCharInfo  *minbounds,
15623a0898aSmrg               *maxbounds;
15723a0898aSmrg    int         i;
15823a0898aSmrg
15923a0898aSmrg    if (!bitmapFont->ink_metrics) {
16023a0898aSmrg	if (bitmapFont->bitmapExtra) {
16123a0898aSmrg	    bitmapFont->bitmapExtra->info.ink_minbounds = bitmapFont->bitmapExtra->info.minbounds;
16223a0898aSmrg	    bitmapFont->bitmapExtra->info.ink_maxbounds = bitmapFont->bitmapExtra->info.maxbounds;
16323a0898aSmrg	}
16423a0898aSmrg	pFont->info.ink_minbounds = pFont->info.minbounds;
16523a0898aSmrg	pFont->info.ink_maxbounds = pFont->info.maxbounds;
16623a0898aSmrg    } else {
16723a0898aSmrg	if (bitmapFont->bitmapExtra) {
16823a0898aSmrg	    minbounds = &bitmapFont->bitmapExtra->info.ink_minbounds;
16923a0898aSmrg	    maxbounds = &bitmapFont->bitmapExtra->info.ink_maxbounds;
17023a0898aSmrg	} else {
17123a0898aSmrg	    minbounds = &pFont->info.ink_minbounds;
17223a0898aSmrg	    maxbounds = &pFont->info.ink_maxbounds;
17323a0898aSmrg	}
17423a0898aSmrg	*minbounds = initMinMetrics;
17523a0898aSmrg	*maxbounds = initMaxMetrics;
17623a0898aSmrg	nchars = bitmapFont->num_chars;
17723a0898aSmrg	for (i = 0, ci = bitmapFont->ink_metrics; i < nchars; i++, ci++) {
17823a0898aSmrg	    COMPUTE_MINMAX(ci);
17923a0898aSmrg	    minbounds->attributes &= ci->attributes;
18023a0898aSmrg	    maxbounds->attributes |= ci->attributes;
18123a0898aSmrg	}
18223a0898aSmrg	if (bitmapFont->bitmapExtra) {
18323a0898aSmrg	    minbounds = &pFont->info.ink_minbounds;
18423a0898aSmrg	    maxbounds = &pFont->info.ink_maxbounds;
18523a0898aSmrg	    *minbounds = initMinMetrics;
18623a0898aSmrg	    *maxbounds = initMaxMetrics;
18723a0898aSmrg            i=0;
18823a0898aSmrg	    for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
18923a0898aSmrg		for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
19023a0898aSmrg		    cit = ACCESSENCODING(bitmapFont->encoding, i);
19123a0898aSmrg		    if (cit) {
19223a0898aSmrg			offset = cit - bitmapFont->metrics;
19323a0898aSmrg			ci = &bitmapFont->ink_metrics[offset];
19423a0898aSmrg			COMPUTE_MINMAX(ci);
19523a0898aSmrg			minbounds->attributes &= ci->attributes;
19623a0898aSmrg			maxbounds->attributes |= ci->attributes;
19723a0898aSmrg		    }
19823a0898aSmrg                    i++;
19923a0898aSmrg		}
20023a0898aSmrg	    }
20123a0898aSmrg	}
20223a0898aSmrg    }
20323a0898aSmrg}
20423a0898aSmrg
20523a0898aSmrgBool
20623a0898aSmrgbitmapAddInkMetrics(FontPtr pFont)
20723a0898aSmrg{
20823a0898aSmrg    BitmapFontPtr  bitmapFont;
20923a0898aSmrg    int         i;
21023a0898aSmrg
21123a0898aSmrg    bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
2127f7f5e4eSmrg    bitmapFont->ink_metrics = malloc(bitmapFont->num_chars * sizeof(xCharInfo));
21323a0898aSmrg    if (!bitmapFont->ink_metrics) {
21423a0898aSmrg      fprintf(stderr, "Error: Couldn't allocate ink_metrics (%d*%ld)\n",
21523a0898aSmrg	      bitmapFont->num_chars, (unsigned long)sizeof(xCharInfo));
21623a0898aSmrg	return FALSE;
21723a0898aSmrg    }
21823a0898aSmrg    for (i = 0; i < bitmapFont->num_chars; i++)
21923a0898aSmrg	FontCharInkMetrics(pFont, &bitmapFont->metrics[i], &bitmapFont->ink_metrics[i]);
22023a0898aSmrg    pFont->info.inkMetrics = TRUE;
22123a0898aSmrg    return TRUE;
22223a0898aSmrg}
22323a0898aSmrg
22423a0898aSmrg/* ARGSUSED */
22523a0898aSmrgint
22623a0898aSmrgbitmapComputeWeight(FontPtr pFont)
22723a0898aSmrg{
22823a0898aSmrg    return 10;
22923a0898aSmrg}
230