Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/absfont/absfont.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * Abstract font handling.
7 */
8
9 #include "absfont.h"
10 #include "dictops.h"
11
12 #include <stdlib.h>
13 #include <string.h>
14 #include <math.h>
15
16 #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
17
18 /* Hint zone pair */
19 typedef struct
20 {
21 float lo;
22 float hi;
23 int top; /* Flags top zone */
24 } ZonePair;
25
26 static abfString unsetString =
27 {
28 ABF_UNSET_PTR,
29 ABF_UNSET_INT,
30 };
31
32 /* Initialize top dictionary. */
33 void abfInitTopDict(abfTopDict *top) {
34 top->version = unsetString;
35 top->Notice = unsetString;
36 top->Copyright = unsetString;
37 top->FullName = unsetString;
38 top->FamilyName = unsetString;
39 top->Weight = unsetString;
40 top->isFixedPitch = cff_DFLT_isFixedPitch;
41 top->ItalicAngle = cff_DFLT_ItalicAngle;
42 top->UnderlinePosition = cff_DFLT_UnderlinePosition;
43 top->UnderlineThickness = cff_DFLT_UnderlineThickness;
44 top->UniqueID = ABF_UNSET_INT;
45 top->FontBBox[0] = 0.0;
46 top->FontBBox[1] = 0.0;
47 top->FontBBox[2] = 0.0;
48 top->FontBBox[3] = 0.0;
49 top->StrokeWidth = cff_DFLT_StrokeWidth;
50 top->XUID.cnt = ABF_EMPTY_ARRAY;
51
52 /* CFF and Acrobat extensions */
53 top->PostScript = unsetString;
54 top->BaseFontName = unsetString;
55 top->BaseFontBlend.cnt = ABF_EMPTY_ARRAY;
56 top->FSType = ABF_UNSET_INT;
57 top->OrigFontType = ABF_UNSET_INT;
58 top->WasEmbedded = 0;
59
60 /* Synthetic font */
61 top->SynBaseFontName = unsetString;
62
63 /* CID-keyed font extensions */
64 top->cid.FontMatrix.cnt = ABF_EMPTY_ARRAY;
65 top->cid.CIDFontName = unsetString;
66 top->cid.Registry = unsetString;
67 top->cid.Ordering = unsetString;
68 top->cid.Supplement = ABF_UNSET_INT;
69 top->cid.CIDFontVersion = cff_DFLT_CIDFontVersion;
70 top->cid.CIDFontRevision = cff_DFLT_CIDFontRevision;
71 top->cid.CIDCount = cff_DFLT_CIDCount;
72 top->cid.UIDBase = ABF_UNSET_INT;
73
74 /* Supplementary data */
75 top->sup.flags = 0;
76 top->sup.srcFontType = ABF_UNSET_INT;
77 top->sup.filename = ABF_UNSET_PTR;
78 top->sup.UnitsPerEm = 1000;
79 top->sup.nGlyphs = ABF_UNSET_INT;
80
81 /* CFF2 data */
82 top->maxstack = 0;
83 top->varStore = NULL;
84 }
85
86 /* Initialize Private dictionary. */
87 static void initPrivateDict(abfPrivateDict *priv) {
88 priv->BlueValues.cnt = ABF_EMPTY_ARRAY;
89 priv->OtherBlues.cnt = ABF_EMPTY_ARRAY;
90 priv->FamilyBlues.cnt = ABF_EMPTY_ARRAY;
91 priv->FamilyOtherBlues.cnt = ABF_EMPTY_ARRAY;
92 priv->BlueScale = (float)cff_DFLT_BlueScale;
93 priv->BlueShift = cff_DFLT_BlueShift;
94 priv->BlueFuzz = cff_DFLT_BlueFuzz;
95 priv->StdHW = ABF_UNSET_REAL;
96 priv->StdVW = ABF_UNSET_REAL;
97 priv->StemSnapH.cnt = ABF_EMPTY_ARRAY;
98 priv->StemSnapV.cnt = ABF_EMPTY_ARRAY;
99 priv->ForceBold = cff_DFLT_ForceBold;
100 priv->LanguageGroup = cff_DFLT_LanguageGroup;
101 priv->ExpansionFactor = (float)cff_DFLT_ExpansionFactor;
102 priv->initialRandomSeed = cff_DFLT_initialRandomSeed;
103 priv->vsindex = ABF_UNSET_INT;
104 priv->numRegions = ABF_UNSET_INT;
105 memset(&priv->blendValues, 0, sizeof(priv->blendValues));
106 }
107
108 /* Initialize font dictionary. */
109 void abfInitFontDict(abfFontDict *font) {
110 font->FontName = unsetString;
111 font->PaintType = cff_DFLT_PaintType;
112 font->FontMatrix.cnt = ABF_EMPTY_ARRAY;
113 initPrivateDict(&font->Private);
114 }
115
116 /* Initialize all dictionary data. */
117 void abfInitAllDicts(abfTopDict *top) {
118 long i;
119 abfInitTopDict(top);
120 for (i = 0; i < top->FDArray.cnt; i++)
121 abfInitFontDict(&top->FDArray.array[i]);
122 }
123
124 /* Initialize glyph info structure. */
125 void abfInitGlyphInfo(abfGlyphInfo *info) {
126 info->flags = 0;
127 info->tag = 0;
128 info->gname.ptr = ABF_UNSET_PTR;
129 info->gname.impl = ABF_UNSET_INT;
130 info->encoding.code = ABF_GLYPH_UNENC;
131 info->encoding.next = ABF_UNSET_PTR;
132 info->cid = 0;
133 info->iFD = 0;
134 info->sup.begin = ABF_UNSET_INT;
135 info->sup.end = ABF_UNSET_INT;
136 info->blendInfo.numRegions = 0;
137 info->blendInfo.vsindex = 0;
138 }
139
140 /* Check UniqueID values is within 24-bit unsigned range. */
141 static void checkUniqueID(abfErrCallbacks *cb, long value, int err_code) {
142 if (value == ABF_UNSET_INT)
143 return;
144 if (value < 0 || value > 16777215L)
145 cb->report_error(cb, err_code, -1);
146 }
147
148 /* Validate top dict. */
149 static void checkTopDict(abfErrCallbacks *cb, abfTopDict *top) {
150 if (cb == NULL)
151 return; /* Reporting turned off; nothing to do */
152
153 /* Check reserved FSType bits are unset */
154 if (top->FSType != ABF_UNSET_INT && (top->FSType & 0xfcf1))
155 cb->report_error(cb, abfErrBadFSType, -1);
156
157 /* Check UniqueID ranges */
158 checkUniqueID(cb, top->UniqueID, abfErrBadUniqueID);
159 if (top->sup.flags & ABF_CID_FONT)
160 checkUniqueID(cb, top->cid.UIDBase, abfErrBadUIDBase);
161
162 /* Check UnderlinePosition and UnderlineThickness */
163 if (top->UnderlinePosition > 0)
164 cb->report_error(cb, abfErrBadULPosition, -1);
165 if (top->UnderlineThickness <= 0)
166 cb->report_error(cb, abfErrBadULThickness, -1);
167 }
168
169 /* Check and repair bad boolean values. */
170 static void checkBooleanValue(abfErrCallbacks *cb, long *value,
171 int err_code, int iFD) {
172 if (*value == 0 || *value == 1)
173 return;
174
175 /* Invalid value; force to 0 */
176 *value = 0;
177 if (cb != NULL)
178 cb->report_error(cb, err_code, iFD);
179 }
180
181 /* Validate hint zones array. If zones are invalid try
182 * to continue and let the interpreter workaround the
183 * problem unless the optional flag is set. If the
184 * optional flag is set to 1 it means that this is an
185 * optional keyword in the font so if an error is found
186 * the array cnt is set to ABF_EMPTY_ARRAY which means
187 * the array is not included in the font.
188 */
189 static int checkZoneArray(abfErrCallbacks *cb,
190 int badarray, int bigvalue, long *cnt, float *array,
191 abfTopDict *top, int iFD, long optional) {
192 if (*cnt == ABF_EMPTY_ARRAY)
193 return 0;
194 else if (*cnt & 1) {
195 /* Unpaired value */
196 cb->report_error(cb, badarray, iFD);
197 if (optional) *cnt = ABF_EMPTY_ARRAY;
198 return 0;
199 } else {
200 long i;
201 for (i = 0; i < *cnt; i += 2)
202 if (array[i + 0] > array[i + 1]) {
203 /* Misordered pair */
204 cb->report_error(cb, badarray, iFD);
205 if (optional) *cnt = ABF_EMPTY_ARRAY;
206 return 0;
207 }
208 for (i = 0; i < *cnt; i++)
209 if (fabs(array[i]) > top->sup.UnitsPerEm * 4.0f)
210 /* Suspiciously large value */
211 cb->report_error(cb, bigvalue, iFD);
212 return 0;
213 }
214 }
215
216 /* Validate StemSnap array and Std value. */
217 static void checkStemSnap(abfErrCallbacks *cb, abfTopDict *top,
218 int nostd, int badstd, int badstemsnap,
219 float *std, long *cnt, float *array, int iFD) {
220 long i;
221
222 if (*cnt != ABF_EMPTY_ARRAY) { /* Check that StemSnap elements are in order */
223 for (i = 1; i < *cnt; i++)
224 if (array[i - 1] >= array[i]) {
225 cb->report_error(cb, badstemsnap, iFD);
226 /* Don't write out this array if it's invalid. */
227 *cnt = ABF_EMPTY_ARRAY;
228 break;
229 }
230 }
231
232 if (*std == ABF_UNSET_REAL) {
233 if (*cnt != ABF_EMPTY_ARRAY) {
234 cb->report_error(cb, nostd, iFD);
235 *std = array[0];
236 }
237 return;
238 } else if (*std < 0 || *std > top->sup.UnitsPerEm / 2.0f) {
239 cb->report_error(cb, badstd, iFD);
240 *std = (*cnt != ABF_EMPTY_ARRAY) ? array[0] : 0; /* force stem value to a legal size */
241 }
242
243 if (*cnt == ABF_EMPTY_ARRAY)
244 return;
245
246 /* Check std value represented */
247 for (i = 0; i < *cnt; i++)
248 if (array[i] == *std)
249 return;
250
251 /* Std value isn't in StemSnap array so force
252 it to the value of the first element. */
253 *std = array[0];
254
255 cb->report_error(cb, badstd, iFD);
256 }
257
258 /* Compare zone pair. */
259 static int CTL_CDECL cmpZonePair(const void *first, const void *second) {
260 const ZonePair *a = (const ZonePair *)first;
261 const ZonePair *b = (const ZonePair *)second;
262 if (a->lo < b->lo)
263 return -1;
264 else if (a->lo > b->lo)
265 return 1;
266 else
267 return 0;
268 }
269
270 /* Validate hint zones. */
271 static void checkZones(abfErrCallbacks *cb,
272 int overlap,
273 long bluescnt, float *bluesarray,
274 long othercnt, float *otherarray,
275 abfTopDict *top, abfPrivateDict *priv, int iFD) {
276 long i;
277 long cnt;
278 ZonePair *last;
279 ZonePair array[7 + 5];
280 float max;
281
282 if (bluescnt == ABF_EMPTY_ARRAY && othercnt == ABF_EMPTY_ARRAY)
283 return;
284
285 /* Combine and sort arrays by increasing pairs */
286 cnt = 0;
287 if (((unsigned long)(cnt + bluescnt / 2)) > sizeof(array) / sizeof(array[0])) {
288 cb->report_error(cb, abfErrBadBlueValues, iFD);
289 return;
290 }
291 for (i = 0; i + 1 < bluescnt; i += 2) {
292 ZonePair *pair = &array[cnt++];
293 pair->lo = bluesarray[i + 0];
294 pair->hi = bluesarray[i + 1];
295 pair->top = i > 0;
296 }
297 if (((unsigned long)(cnt + othercnt / 2)) > sizeof(array) / sizeof(array[0])) {
298 cb->report_error(cb, abfErrBadOtherBlues, iFD);
299 return;
300 }
301 for (i = 0; i + 1 < othercnt; i += 2) {
302 ZonePair *pair = &array[cnt++];
303 pair->lo = otherarray[i + 0];
304 pair->hi = otherarray[i + 1];
305 pair->top = 0;
306 }
307 qsort(array, cnt, sizeof(ZonePair), cmpZonePair);
308
309 /* Check BlueScale against maximum zone height. */
310 max = 0;
311 for (i = 0; i < cnt; i++) {
312 float height = array[i].hi - array[i].lo;
313 if (height > max)
314 max = height;
315 }
316 if (max > 0 && (priv->BlueScale * max > 1.0 || priv->BlueScale * max < 0.5))
317 cb->report_error(cb, abfErrBadBlueScale, iFD);
318
319 /* Check for overlapping zones */
320 last = &array[0];
321 for (i = 1; i < cnt; i++) {
322 ZonePair *curr = &array[i];
323 if (last->top == curr->top &&
324 last->hi + 2 * priv->BlueFuzz + 1 > curr->lo) {
325 cb->report_error(cb, overlap, iFD);
326 break;
327 }
328 last = curr;
329 }
330 }
331
332 /* Validate font dict. Currently only the contents of Private dict is
333 checked. */
334 static void checkFontDict(abfErrCallbacks *cb,
335 abfTopDict *top, abfFontDict *font, int iFD) {
336 abfPrivateDict *priv = &font->Private;
337
338 /* Check booleans */
339 checkBooleanValue(cb, &top->isFixedPitch, abfErrBadisFixedPitch, iFD);
340 checkBooleanValue(cb, &priv->ForceBold, abfErrBadForceBold, iFD);
341 checkBooleanValue(cb, &priv->LanguageGroup, abfErrBadLanguageGroup, iFD);
342
343 if (cb == NULL)
344 return; /* Reporting turned off; nothing more to do */
345
346 /* Check StemSnap arrays */
347 checkStemSnap(cb, top,
348 abfErrNoStdHW,
349 abfErrBadStdHW,
350 abfErrBadStemSnapH,
351 &priv->StdHW,
352 &priv->StemSnapH.cnt,
353 priv->StemSnapH.array,
354 iFD);
355 checkStemSnap(cb, top,
356 abfErrNoStdVW,
357 abfErrBadStdVW,
358 abfErrBadStemSnapV,
359 &priv->StdVW,
360 &priv->StemSnapV.cnt,
361 priv->StemSnapV.array,
362 iFD);
363
364 if (priv->BlueValues.cnt == ABF_EMPTY_ARRAY &&
365 priv->OtherBlues.cnt == ABF_EMPTY_ARRAY &&
366 priv->FamilyBlues.cnt == ABF_EMPTY_ARRAY &&
367 priv->FamilyOtherBlues.cnt == ABF_EMPTY_ARRAY) {
368 /* Report non-default Blue-key values when no zones */
369 if (priv->BlueScale != (float)cff_DFLT_BlueScale)
370 cb->report_error(cb, abfErrUselessBlueScale, iFD);
371 if (priv->BlueShift != cff_DFLT_BlueShift)
372 cb->report_error(cb, abfErrUselessBlueShift, iFD);
373 if (priv->BlueFuzz != cff_DFLT_BlueFuzz)
374 cb->report_error(cb, abfErrUselessBlueFuzz, iFD);
375 return;
376 }
377
378 /* Check each hint zone array for errors */
379 if (checkZoneArray(cb,
380 abfErrBadBlueValues,
381 abfErrBigBlueValues,
382 &priv->BlueValues.cnt,
383 priv->BlueValues.array,
384 top, iFD, 0) ||
385 checkZoneArray(cb,
386 abfErrBadOtherBlues,
387 abfErrBigOtherBlues,
388 &priv->OtherBlues.cnt,
389 priv->OtherBlues.array,
390 top, iFD, 1) ||
391 checkZoneArray(cb,
392 abfErrBadFamilyBlues,
393 abfErrBigFamilyBlues,
394 &priv->FamilyBlues.cnt,
395 priv->FamilyBlues.array,
396 top, iFD, 1) ||
397 checkZoneArray(cb,
398 abfErrBadFamilyOtherBlues,
399 abfErrBigFamilyOtherBlues,
400 &priv->FamilyOtherBlues.cnt,
401 priv->FamilyOtherBlues.array,
402 top, iFD, 1))
403 return; /* Can't continue with malformed zones */
404
405 /* Check regular and family zones for overlap */
406 checkZones(cb,
407 abfErrRegularOverlap,
408 priv->BlueValues.cnt, priv->BlueValues.array,
409 priv->OtherBlues.cnt, priv->OtherBlues.array,
410 top, priv, iFD);
411 checkZones(cb,
412 abfErrFamilyOverlap,
413 priv->FamilyBlues.cnt, priv->FamilyBlues.array,
414 priv->FamilyOtherBlues.cnt, priv->FamilyOtherBlues.array,
415 top, priv, iFD);
416 }
417
418 /* Validate all dictionaries. */
419 void abfCheckAllDicts(abfErrCallbacks *cb, abfTopDict *top) {
420 checkTopDict(cb, top);
421 if (top->FDArray.cnt == 1)
422 checkFontDict(cb, top, &top->FDArray.array[0], -1);
423 else {
424 int i;
425 for (i = 0; i < top->FDArray.cnt; i++)
426 checkFontDict(cb, top, &top->FDArray.array[i], i);
427 }
428 }
429
430 /* Return 1 if "FontMatrix" is the default matrix otherwise return 0. */
431 int abfIsDefaultFontMatrix(const abfFontMatrix *FontMatrix) {
432 return (FontMatrix->cnt == ABF_EMPTY_ARRAY ||
433 (FontMatrix->array[0] == 0.001f &&
434 FontMatrix->array[1] == 0.0 &&
435 FontMatrix->array[2] == 0.0 &&
436 FontMatrix->array[3] == 0.001f &&
437 FontMatrix->array[4] == 0.0 &&
438 FontMatrix->array[5] == 0.0));
439 }
440
441 /* Map error code to error string. */
442 char *abfErrStr(int err_code) {
443 static char *errstrs[] =
444 {
445 #undef CTL_DCL_ERR
446 #define CTL_DCL_ERR(name, string) string,
447 #include "abferr.h"
448 };
449 return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
450 }
451
452 /* Get version numbers of libraries. */
453 void abfGetVersion(ctlVersionCallbacks *cb) {
454 if (cb->called & 1 << ABF_LIB_ID)
455 return; /* Already enumerated */
456
457 cb->getversion(cb, ABF_VERSION, "absfont");
458
459 /* Record this call */
460 cb->called |= 1 << ABF_LIB_ID;
461 }