1a96d7823Smrg/*
2a96d7823Smrg
3a96d7823SmrgCopyright 1990, 1994, 1998  The Open Group
4a96d7823Smrg
5a96d7823SmrgPermission to use, copy, modify, distribute, and sell this software and its
6a96d7823Smrgdocumentation for any purpose is hereby granted without fee, provided that
7a96d7823Smrgthe above copyright notice appear in all copies and that both that
8a96d7823Smrgcopyright notice and this permission notice appear in supporting
9a96d7823Smrgdocumentation.
10a96d7823Smrg
11a96d7823SmrgThe above copyright notice and this permission notice shall be included
12a96d7823Smrgin all copies or substantial portions of the Software.
13a96d7823Smrg
14a96d7823SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15a96d7823SmrgOR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16a96d7823SmrgMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17a96d7823SmrgIN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18a96d7823SmrgOTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19a96d7823SmrgARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20a96d7823SmrgOTHER DEALINGS IN THE SOFTWARE.
21a96d7823Smrg
22a96d7823SmrgExcept as contained in this notice, the name of The Open Group shall
23a96d7823Smrgnot be used in advertising or otherwise to promote the sale, use or
24a96d7823Smrgother dealings in this Software without prior written authorization
25a96d7823Smrgfrom The Open Group.
26a96d7823Smrg
27a96d7823Smrg*/
28a96d7823Smrg
29a96d7823Smrg#ifdef HAVE_CONFIG_H
30a96d7823Smrg#include <config.h>
31a96d7823Smrg#endif
32a96d7823Smrg#include "libxfontint.h"
33c7b4381aSmrg#include "src/util/replace.h"
34a96d7823Smrg
35a96d7823Smrg#include <X11/fonts/fntfilst.h>
36a96d7823Smrg#include <X11/fonts/bitmap.h>
37a96d7823Smrg#include <X11/fonts/bdfint.h>
38a96d7823Smrg
39a96d7823Smrg#ifndef MAXSHORT
40a96d7823Smrg#define MAXSHORT    32767
41a96d7823Smrg#endif
42a96d7823Smrg
43a96d7823Smrg#ifndef MINSHORT
44a96d7823Smrg#define MINSHORT    -32768
45a96d7823Smrg#endif
46a96d7823Smrg
47a96d7823Smrgstatic xCharInfo initMinMetrics = {
48a96d7823SmrgMAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, MAXSHORT, 0xFFFF};
49a96d7823Smrgstatic xCharInfo initMaxMetrics = {
50a96d7823SmrgMINSHORT, MINSHORT, MINSHORT, MINSHORT, MINSHORT, 0x0000};
51a96d7823Smrg
52a96d7823Smrg#define MINMAX(field,ci) \
53a96d7823Smrg	if (minbounds->field > (ci)->field) \
54a96d7823Smrg	     minbounds->field = (ci)->field; \
55a96d7823Smrg	if (maxbounds->field < (ci)->field) \
56a96d7823Smrg	     maxbounds->field = (ci)->field;
57a96d7823Smrg
58a96d7823Smrg#define COMPUTE_MINMAX(ci) \
59a96d7823Smrg    if ((ci)->ascent || (ci)->descent || \
60a96d7823Smrg	(ci)->leftSideBearing || (ci)->rightSideBearing || \
61a96d7823Smrg	(ci)->characterWidth) \
62a96d7823Smrg    { \
63a96d7823Smrg	MINMAX(ascent, (ci)); \
64a96d7823Smrg	MINMAX(descent, (ci)); \
65a96d7823Smrg	MINMAX(leftSideBearing, (ci)); \
66a96d7823Smrg	MINMAX(rightSideBearing, (ci)); \
67a96d7823Smrg	MINMAX(characterWidth, (ci)); \
68a96d7823Smrg    }
69a96d7823Smrg
70a96d7823Smrgvoid
71a96d7823SmrgbitmapComputeFontBounds(FontPtr pFont)
72a96d7823Smrg{
73a96d7823Smrg    BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
74a96d7823Smrg    int         nchars;
75a96d7823Smrg    int         r,
76a96d7823Smrg                c;
77a96d7823Smrg    CharInfoPtr ci;
78a96d7823Smrg    int         maxOverlap;
79a96d7823Smrg    int         overlap;
80a96d7823Smrg    xCharInfo  *minbounds,
81a96d7823Smrg               *maxbounds;
82a96d7823Smrg    int         i;
83a96d7823Smrg    int		numneg = 0, numpos = 0;
84a96d7823Smrg
85a96d7823Smrg    if (bitmapFont->bitmapExtra) {
86a96d7823Smrg	minbounds = &bitmapFont->bitmapExtra->info.minbounds;
87a96d7823Smrg	maxbounds = &bitmapFont->bitmapExtra->info.maxbounds;
88a96d7823Smrg    } else {
89a96d7823Smrg	minbounds = &pFont->info.minbounds;
90a96d7823Smrg	maxbounds = &pFont->info.maxbounds;
91a96d7823Smrg    }
92a96d7823Smrg    *minbounds = initMinMetrics;
93a96d7823Smrg    *maxbounds = initMaxMetrics;
94a96d7823Smrg    maxOverlap = MINSHORT;
95a96d7823Smrg    nchars = bitmapFont->num_chars;
96a96d7823Smrg    for (i = 0, ci = bitmapFont->metrics; i < nchars; i++, ci++) {
97a96d7823Smrg	COMPUTE_MINMAX(&ci->metrics);
98a96d7823Smrg	if (ci->metrics.characterWidth < 0)
99a96d7823Smrg	    numneg++;
100a96d7823Smrg	else
101a96d7823Smrg	    numpos++;
102a96d7823Smrg	minbounds->attributes &= ci->metrics.attributes;
103a96d7823Smrg	maxbounds->attributes |= ci->metrics.attributes;
104a96d7823Smrg	overlap = ci->metrics.rightSideBearing - ci->metrics.characterWidth;
105a96d7823Smrg	if (maxOverlap < overlap)
106a96d7823Smrg	    maxOverlap = overlap;
107a96d7823Smrg    }
108a96d7823Smrg    if (bitmapFont->bitmapExtra) {
109a96d7823Smrg	if (numneg > numpos)
110a96d7823Smrg	    bitmapFont->bitmapExtra->info.drawDirection = RightToLeft;
111a96d7823Smrg	else
112a96d7823Smrg	    bitmapFont->bitmapExtra->info.drawDirection = LeftToRight;
113a96d7823Smrg	bitmapFont->bitmapExtra->info.maxOverlap = maxOverlap;
114a96d7823Smrg	minbounds = &pFont->info.minbounds;
115a96d7823Smrg	maxbounds = &pFont->info.maxbounds;
116a96d7823Smrg	*minbounds = initMinMetrics;
117a96d7823Smrg	*maxbounds = initMaxMetrics;
118a96d7823Smrg        i = 0;
119a96d7823Smrg	maxOverlap = MINSHORT;
120a96d7823Smrg	for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
121a96d7823Smrg	    for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
122a96d7823Smrg		ci = ACCESSENCODING(bitmapFont->encoding, i);
123a96d7823Smrg		if (ci) {
124a96d7823Smrg		    COMPUTE_MINMAX(&ci->metrics);
125a96d7823Smrg		    if (ci->metrics.characterWidth < 0)
126a96d7823Smrg			numneg++;
127a96d7823Smrg		    else
128a96d7823Smrg			numpos++;
129a96d7823Smrg		    minbounds->attributes &= ci->metrics.attributes;
130a96d7823Smrg		    maxbounds->attributes |= ci->metrics.attributes;
131a96d7823Smrg		    overlap = ci->metrics.rightSideBearing -
132a96d7823Smrg			ci->metrics.characterWidth;
133a96d7823Smrg		    if (maxOverlap < overlap)
134a96d7823Smrg			maxOverlap = overlap;
135a96d7823Smrg		}
136a96d7823Smrg                i++;
137a96d7823Smrg	    }
138a96d7823Smrg	}
139a96d7823Smrg    }
140a96d7823Smrg    if (numneg > numpos)
141a96d7823Smrg	pFont->info.drawDirection = RightToLeft;
142a96d7823Smrg    else
143a96d7823Smrg	pFont->info.drawDirection = LeftToRight;
144a96d7823Smrg    pFont->info.maxOverlap = maxOverlap;
145a96d7823Smrg}
146a96d7823Smrg
147a96d7823Smrgvoid
148a96d7823SmrgbitmapComputeFontInkBounds(FontPtr pFont)
149a96d7823Smrg{
150a96d7823Smrg    BitmapFontPtr  bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
151a96d7823Smrg    int         nchars;
152a96d7823Smrg    int         r,
153a96d7823Smrg                c;
154a96d7823Smrg    CharInfoPtr cit;
155a96d7823Smrg    xCharInfo  *ci;
156a96d7823Smrg    int         offset;
157a96d7823Smrg    xCharInfo  *minbounds,
158a96d7823Smrg               *maxbounds;
159a96d7823Smrg    int         i;
160a96d7823Smrg
161a96d7823Smrg    if (!bitmapFont->ink_metrics) {
162a96d7823Smrg	if (bitmapFont->bitmapExtra) {
163a96d7823Smrg	    bitmapFont->bitmapExtra->info.ink_minbounds = bitmapFont->bitmapExtra->info.minbounds;
164a96d7823Smrg	    bitmapFont->bitmapExtra->info.ink_maxbounds = bitmapFont->bitmapExtra->info.maxbounds;
165a96d7823Smrg	}
166a96d7823Smrg	pFont->info.ink_minbounds = pFont->info.minbounds;
167a96d7823Smrg	pFont->info.ink_maxbounds = pFont->info.maxbounds;
168a96d7823Smrg    } else {
169a96d7823Smrg	if (bitmapFont->bitmapExtra) {
170a96d7823Smrg	    minbounds = &bitmapFont->bitmapExtra->info.ink_minbounds;
171a96d7823Smrg	    maxbounds = &bitmapFont->bitmapExtra->info.ink_maxbounds;
172a96d7823Smrg	} else {
173a96d7823Smrg	    minbounds = &pFont->info.ink_minbounds;
174a96d7823Smrg	    maxbounds = &pFont->info.ink_maxbounds;
175a96d7823Smrg	}
176a96d7823Smrg	*minbounds = initMinMetrics;
177a96d7823Smrg	*maxbounds = initMaxMetrics;
178a96d7823Smrg	nchars = bitmapFont->num_chars;
179a96d7823Smrg	for (i = 0, ci = bitmapFont->ink_metrics; i < nchars; i++, ci++) {
180a96d7823Smrg	    COMPUTE_MINMAX(ci);
181a96d7823Smrg	    minbounds->attributes &= ci->attributes;
182a96d7823Smrg	    maxbounds->attributes |= ci->attributes;
183a96d7823Smrg	}
184a96d7823Smrg	if (bitmapFont->bitmapExtra) {
185a96d7823Smrg	    minbounds = &pFont->info.ink_minbounds;
186a96d7823Smrg	    maxbounds = &pFont->info.ink_maxbounds;
187a96d7823Smrg	    *minbounds = initMinMetrics;
188a96d7823Smrg	    *maxbounds = initMaxMetrics;
189a96d7823Smrg            i=0;
190a96d7823Smrg	    for (r = pFont->info.firstRow; r <= pFont->info.lastRow; r++) {
191a96d7823Smrg		for (c = pFont->info.firstCol; c <= pFont->info.lastCol; c++) {
192a96d7823Smrg		    cit = ACCESSENCODING(bitmapFont->encoding, i);
193a96d7823Smrg		    if (cit) {
194a96d7823Smrg			offset = cit - bitmapFont->metrics;
195a96d7823Smrg			ci = &bitmapFont->ink_metrics[offset];
196a96d7823Smrg			COMPUTE_MINMAX(ci);
197a96d7823Smrg			minbounds->attributes &= ci->attributes;
198a96d7823Smrg			maxbounds->attributes |= ci->attributes;
199a96d7823Smrg		    }
200a96d7823Smrg                    i++;
201a96d7823Smrg		}
202a96d7823Smrg	    }
203a96d7823Smrg	}
204a96d7823Smrg    }
205a96d7823Smrg}
206a96d7823Smrg
207a96d7823SmrgBool
208a96d7823SmrgbitmapAddInkMetrics(FontPtr pFont)
209a96d7823Smrg{
210a96d7823Smrg    BitmapFontPtr  bitmapFont;
211a96d7823Smrg    int         i;
212a96d7823Smrg
213a96d7823Smrg    bitmapFont = (BitmapFontPtr) pFont->fontPrivate;
214c7b4381aSmrg    bitmapFont->ink_metrics = mallocarray(bitmapFont->num_chars, sizeof(xCharInfo));
215a96d7823Smrg    if (!bitmapFont->ink_metrics) {
216a96d7823Smrg      fprintf(stderr, "Error: Couldn't allocate ink_metrics (%d*%ld)\n",
217a96d7823Smrg	      bitmapFont->num_chars, (unsigned long)sizeof(xCharInfo));
218a96d7823Smrg	return FALSE;
219a96d7823Smrg    }
220a96d7823Smrg    for (i = 0; i < bitmapFont->num_chars; i++)
221a96d7823Smrg	FontCharInkMetrics(pFont, &bitmapFont->metrics[i], &bitmapFont->ink_metrics[i]);
222a96d7823Smrg    pFont->info.inkMetrics = TRUE;
223a96d7823Smrg    return TRUE;
224a96d7823Smrg}
225a96d7823Smrg
226a96d7823Smrg/* ARGSUSED */
227a96d7823Smrgint
228a96d7823SmrgbitmapComputeWeight(FontPtr pFont)
229a96d7823Smrg{
230a96d7823Smrg    return 10;
231a96d7823Smrg}
232