1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
/* $Xorg: fontscale.c,v 1.5 2001/02/09 02:04:03 xorgcvs Exp $ */
/*
Copyright 1991, 1998 The Open Group
Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
*/
/*
* Author: Keith Packard, MIT X Consortium
*/
#include "fntfilst.h"
#ifdef _XOPEN_SOURCE
#include <math.h>
#else
#define _XOPEN_SOURCE /* to get prototype for hypot on some systems */
#include <math.h>
#undef _XOPEN_SOURCE
#endif
Bool
FontFileAddScaledInstance (entry, vals, pFont, bitmapName)
FontEntryPtr entry;
FontScalablePtr vals;
FontPtr pFont;
char *bitmapName;
{
FontScalableEntryPtr scalable;
FontScalableExtraPtr extra;
FontScaledPtr new;
int newsize;
scalable = &entry->u.scalable;
extra = scalable->extra;
if (extra->numScaled == extra->sizeScaled)
{
newsize = extra->sizeScaled + 4;
new = (FontScaledPtr) xrealloc (extra->scaled,
newsize * sizeof (FontScaledRec));
if (!new)
return FALSE;
extra->sizeScaled = newsize;
extra->scaled = new;
}
new = &extra->scaled[extra->numScaled++];
new->vals = *vals;
new->pFont = pFont;
new->bitmap = (FontEntryPtr) bitmapName;
if (pFont)
pFont->fpePrivate = (pointer) entry;
return TRUE;
}
/* Must call this after the directory is sorted */
void
FontFileSwitchStringsToBitmapPointers (dir)
FontDirectoryPtr dir;
{
int s;
int b;
int i;
FontEntryPtr scalable;
FontEntryPtr nonScalable;
FontScaledPtr scaled;
FontScalableExtraPtr extra;
scalable = dir->scalable.entries;
nonScalable = dir->nonScalable.entries;
for (s = 0; s < dir->scalable.used; s++)
{
extra = scalable[s].u.scalable.extra;
scaled = extra->scaled;
for (i = 0; i < extra->numScaled; i++)
for (b = 0; b < dir->nonScalable.used; b++)
if (nonScalable[b].name.name == (char *) scaled[i].bitmap)
scaled[i].bitmap = &nonScalable[b];
}
}
void
FontFileRemoveScaledInstance (entry, pFont)
FontEntryPtr entry;
FontPtr pFont;
{
FontScalableEntryPtr scalable;
FontScalableExtraPtr extra;
int i;
scalable = &entry->u.scalable;
extra = scalable->extra;
for (i = 0; i < extra->numScaled; i++)
{
if (extra->scaled[i].pFont == pFont)
{
if (extra->scaled[i].vals.ranges)
xfree (extra->scaled[i].vals.ranges);
extra->numScaled--;
for (; i < extra->numScaled; i++)
extra->scaled[i] = extra->scaled[i+1];
}
}
}
Bool
FontFileCompleteXLFD (vals, def)
register FontScalablePtr vals;
FontScalablePtr def;
{
FontResolutionPtr res;
int num_res;
double sx, sy, temp_matrix[4];
double pixel_setsize_adjustment = 1.0;
/*
* If two of the three vertical scale values are specified, compute the
* third. If all three are specified, make sure they are consistent
* (within a pixel)
*
* One purpose of this procedure is to complete XLFD names in a
* repeatable manner. That is, if the user partially specifies
* a name (say, pixelsize but not pointsize), the results generated
* here result in a fully specified name that will result in the
* same font.
*/
res = GetClientResolutions(&num_res);
if (!(vals->values_supplied & PIXELSIZE_MASK) ||
!(vals->values_supplied & POINTSIZE_MASK))
{
/* If resolution(s) unspecified and cannot be computed from
pixelsize and pointsize, get appropriate defaults. */
if (num_res)
{
if (vals->x <= 0)
vals->x = res->x_resolution;
if (vals->y <= 0)
vals->y = res->y_resolution;
}
if (vals->x <= 0)
vals->x = def->x;
if (vals->y <= 0)
vals->y = def->y;
}
else
{
/* If needed, compute resolution values from the pixel and
pointsize information we were given. This problem is
overdetermined (four equations, two unknowns), but we don't
check for inconsistencies here. If they exist, they will
show up in later tests for the point and pixel sizes. */
if (vals->y <= 0)
{
double x = hypot(vals->pixel_matrix[1], vals->pixel_matrix[3]);
double y = hypot(vals->point_matrix[1], vals->point_matrix[3]);
if (y < EPS) return FALSE;
vals->y = (int)(x * 72.27 / y + .5);
}
if (vals->x <= 0)
{
/* If the pixelsize was given as an array, or as a scalar that
has been normalized for the pixel shape, we have enough
information to compute a separate horizontal resolution */
if ((vals->values_supplied & PIXELSIZE_MASK) == PIXELSIZE_ARRAY ||
(vals->values_supplied & PIXELSIZE_MASK) ==
PIXELSIZE_SCALAR_NORMALIZED)
{
double x = hypot(vals->pixel_matrix[0], vals->pixel_matrix[2]);
double y = hypot(vals->point_matrix[0], vals->point_matrix[2]);
if (y < EPS) return FALSE;
vals->x = (int)(x * 72.27 / y + .5);
}
else
{
/* Not enough information in the pixelsize array. Just
assume the pixels are square. */
vals->x = vals->y;
}
}
}
if (vals->x <= 0 || vals->y <= 0) return FALSE;
/* If neither pixelsize nor pointsize is defined, take the pointsize
from the defaults structure we've been passed. */
if (!(vals->values_supplied & PIXELSIZE_MASK) &&
!(vals->values_supplied & POINTSIZE_MASK))
{
if (num_res)
{
vals->point_matrix[0] =
vals->point_matrix[3] = (double)res->point_size / 10.0;
vals->point_matrix[1] =
vals->point_matrix[2] = 0;
vals->values_supplied = (vals->values_supplied & ~POINTSIZE_MASK) |
POINTSIZE_SCALAR;
}
else if (def->values_supplied & POINTSIZE_MASK)
{
vals->point_matrix[0] = def->point_matrix[0];
vals->point_matrix[1] = def->point_matrix[1];
vals->point_matrix[2] = def->point_matrix[2];
vals->point_matrix[3] = def->point_matrix[3];
vals->values_supplied = (vals->values_supplied & ~POINTSIZE_MASK) |
(def->values_supplied & POINTSIZE_MASK);
}
else return FALSE;
}
/* At this point, at least two of the three vertical scale values
should be specified. Our job now is to compute the missing ones
and check for agreement between overspecified values */
/* If pixelsize was specified by a scalar, we need to fix the matrix
now that we know the resolutions. */
if ((vals->values_supplied & PIXELSIZE_MASK) == PIXELSIZE_SCALAR)
{
/* pixel_setsize_adjustment used below to modify permissible
error in pixel/pointsize matching, since multiplying a
number rounded to integer changes the amount of the error
caused by the rounding */
pixel_setsize_adjustment = (double)vals->x / (double)vals->y;
vals->pixel_matrix[0] *= pixel_setsize_adjustment;
vals->values_supplied = vals->values_supplied & ~PIXELSIZE_MASK |
PIXELSIZE_SCALAR_NORMALIZED;
}
sx = (double)vals->x / 72.27;
sy = (double)vals->y / 72.27;
/* If a pointsize was specified, make sure pixelsize is consistent
to within 1 pixel, then replace pixelsize with a consistent
floating-point value. */
if (vals->values_supplied & POINTSIZE_MASK)
{
recompute_pixelsize: ;
temp_matrix[0] = vals->point_matrix[0] * sx;
temp_matrix[1] = vals->point_matrix[1] * sy;
temp_matrix[2] = vals->point_matrix[2] * sx;
temp_matrix[3] = vals->point_matrix[3] * sy;
if (vals->values_supplied & PIXELSIZE_MASK)
{
if (fabs(vals->pixel_matrix[0] - temp_matrix[0]) >
pixel_setsize_adjustment ||
fabs(vals->pixel_matrix[1] - temp_matrix[1]) > 1 ||
fabs(vals->pixel_matrix[2] - temp_matrix[2]) > 1 ||
fabs(vals->pixel_matrix[3] - temp_matrix[3]) > 1)
return FALSE;
}
if ((vals->values_supplied & PIXELSIZE_MASK) == PIXELSIZE_ARRAY &&
(vals->values_supplied & POINTSIZE_MASK) == POINTSIZE_SCALAR)
{
/* In the special case that pixelsize came as an array and
pointsize as a scalar, recompute the pointsize matrix
from the pixelsize matrix. */
goto recompute_pointsize;
}
/* Refresh pixel matrix with precise values computed from
pointsize and resolution. */
vals->pixel_matrix[0] = temp_matrix[0];
vals->pixel_matrix[1] = temp_matrix[1];
vals->pixel_matrix[2] = temp_matrix[2];
vals->pixel_matrix[3] = temp_matrix[3];
/* Set values_supplied for pixel to match that for point */
vals->values_supplied =
(vals->values_supplied & ~PIXELSIZE_MASK) |
(((vals->values_supplied & POINTSIZE_MASK) == POINTSIZE_ARRAY) ?
PIXELSIZE_ARRAY : PIXELSIZE_SCALAR_NORMALIZED);
}
else
{
/* Pointsize unspecified... compute from pixel size and
resolutions */
recompute_pointsize: ;
if (fabs(sx) < EPS || fabs(sy) < EPS) return FALSE;
vals->point_matrix[0] = vals->pixel_matrix[0] / sx;
vals->point_matrix[1] = vals->pixel_matrix[1] / sy;
vals->point_matrix[2] = vals->pixel_matrix[2] / sx;
vals->point_matrix[3] = vals->pixel_matrix[3] / sy;
/* Set values_supplied for pixel to match that for point */
vals->values_supplied =
(vals->values_supplied & ~POINTSIZE_MASK) |
(((vals->values_supplied & PIXELSIZE_MASK) == PIXELSIZE_ARRAY) ?
POINTSIZE_ARRAY : POINTSIZE_SCALAR);
/* If we computed scalar pointsize from scalar pixelsize, round
pointsize to decipoints and recompute pixelsize so we end up
with a repeatable name */
if ((vals->values_supplied & POINTSIZE_MASK) == POINTSIZE_SCALAR)
{
/* Off-diagonal elements should be zero since no matrix was
specified. */
vals->point_matrix[0] =
(double)(int)(vals->point_matrix[0] * 10.0 + .5) / 10.0;
vals->point_matrix[3] =
(double)(int)(vals->point_matrix[3] * 10.0 + .5) / 10.0;
goto recompute_pixelsize;
}
}
/* We've succeeded. Round everything to a few decimal places
for repeatability. */
vals->pixel_matrix[0] = xlfd_round_double(vals->pixel_matrix[0]);
vals->pixel_matrix[1] = xlfd_round_double(vals->pixel_matrix[1]);
vals->pixel_matrix[2] = xlfd_round_double(vals->pixel_matrix[2]);
vals->pixel_matrix[3] = xlfd_round_double(vals->pixel_matrix[3]);
vals->point_matrix[0] = xlfd_round_double(vals->point_matrix[0]);
vals->point_matrix[1] = xlfd_round_double(vals->point_matrix[1]);
vals->point_matrix[2] = xlfd_round_double(vals->point_matrix[2]);
vals->point_matrix[3] = xlfd_round_double(vals->point_matrix[3]);
/* Fill in the deprecated fields for the benefit of rasterizers
that do not handle the matrices. */
vals->point = vals->point_matrix[3] * 10;
vals->pixel = vals->pixel_matrix[3];
return TRUE;
}
static Bool
MatchScalable (a, b)
FontScalablePtr a, b;
{
int i;
/* Some asymmetry here: we assume that the first argument (a) is
the table entry and the second (b) the item we're trying to match
(the key). We'll consider the fonts matched if the relevant
metrics match *and* if a) the table entry doesn't have charset
subsetting or b) the table entry has identical charset subsetting
to that in the key. We could add logic to check if the table
entry has a superset of the charset required by the key, but
we'll resist the urge for now. */
#define EQUAL(a,b) ((a)[0] == (b)[0] && \
(a)[1] == (b)[1] && \
(a)[2] == (b)[2] && \
(a)[3] == (b)[3])
if (!(a->x == b->x &&
a->y == b->y &&
(a->width == b->width || a->width == 0 || b->width == 0) &&
(!(b->values_supplied & PIXELSIZE_MASK) ||
(a->values_supplied & PIXELSIZE_MASK) ==
(b->values_supplied & PIXELSIZE_MASK) &&
EQUAL(a->pixel_matrix, b->pixel_matrix)) &&
(!(b->values_supplied & POINTSIZE_MASK) ||
(a->values_supplied & POINTSIZE_MASK) ==
(b->values_supplied & POINTSIZE_MASK) &&
EQUAL(a->point_matrix, b->point_matrix)) &&
(a->nranges == 0 || a->nranges == b->nranges)))
return FALSE;
for (i = 0; i < a->nranges; i++)
if (a->ranges[i].min_char_low != b->ranges[i].min_char_low ||
a->ranges[i].min_char_high != b->ranges[i].min_char_high ||
a->ranges[i].max_char_low != b->ranges[i].max_char_low ||
a->ranges[i].max_char_high != b->ranges[i].max_char_high)
return FALSE;
return TRUE;
}
FontScaledPtr
FontFileFindScaledInstance (entry, vals, noSpecificSize)
FontEntryPtr entry;
FontScalablePtr vals;
{
FontScalableEntryPtr scalable;
FontScalableExtraPtr extra;
FontScalablePtr mvals;
int dist, i;
int mini;
double mindist;
register double temp, sum=0.0;
#define NORMDIFF(a, b) ( \
temp = (a)[0] - (b)[0], \
sum = temp * temp, \
temp = (a)[1] - (b)[1], \
sum += temp * temp, \
temp = (a)[2] - (b)[2], \
sum += temp * temp, \
temp = (a)[3] - (b)[3], \
sum + temp * temp )
scalable = &entry->u.scalable;
extra = scalable->extra;
if (noSpecificSize && extra->numScaled)
{
mini = 0;
mindist = NORMDIFF(extra->scaled[0].vals.point_matrix,
vals->point_matrix);
for (i = 1; i < extra->numScaled; i++)
{
if (extra->scaled[i].pFont &&
!extra->scaled[i].pFont->info.cachable) continue;
mvals = &extra->scaled[i].vals;
dist = NORMDIFF(mvals->point_matrix, vals->point_matrix);
if (dist < mindist)
{
mindist = dist;
mini = i;
}
}
if (extra->scaled[mini].pFont &&
!extra->scaled[mini].pFont->info.cachable) return 0;
return &extra->scaled[mini];
}
else
{
/* See if we've scaled to this value yet */
for (i = 0; i < extra->numScaled; i++)
{
if (extra->scaled[i].pFont &&
!extra->scaled[i].pFont->info.cachable) continue;
if (MatchScalable (&extra->scaled[i].vals, vals))
return &extra->scaled[i];
}
}
return 0;
}
|