Debian Codegraph @

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

raw
1 /* Copyright 2016 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 #include "varread.h"
5 #include "ctlshare.h"
6 #include "ctutil.h"
7 #include "cffread.h"
8 #include "dynarr.h"
9 #include "supportfp.h"
10 #include "txops.h"
11 #include "supportexcept.h"
12
13 #include <string.h>
14
15 /* ----------------------------- fixed number constants, types, macros ---------------------------- */
16
17 #define FIXED_ZERO 0
18 #define FIXED_ONE 0x00010000
19 #define FIXED_MINUS_ONE (-FIXED_ONE)
20 #define F2DOT14_ZERO 0
21 #define F2DOT14_ONE (1 << 14)
22 #define F2DOT14_MINUS_ONE (-F2DOT14_ONE)
23
24 #define F2DOT14_TO_FIXED(v) (((Fixed)(v)) << 2)
25 #define FIXED_TO_F2DOT14(v) ((var_F2dot14)(((Fixed)(v) + 0x00000002) >> 2))
26
27 /* ----------------------------- variation font table constants ---------------------------- */
28
29 #define HHEA_TABLE_TAG CTL_TAG('h', 'h', 'e', 'a')
30 #define VHEA_TABLE_TAG CTL_TAG('v', 'h', 'e', 'a')
31 #define HMTX_TABLE_TAG CTL_TAG('h', 'm', 't', 'x')
32 #define VMTX_TABLE_TAG CTL_TAG('v', 'm', 't', 'x')
33 #define VORG_TABLE_TAG CTL_TAG('V', 'O', 'R', 'G')
34 #define AVAR_TABLE_TAG CTL_TAG('a', 'v', 'a', 'r')
35 #define FVAR_TABLE_TAG CTL_TAG('f', 'v', 'a', 'r')
36 #define HVAR_TABLE_TAG CTL_TAG('H', 'V', 'A', 'R')
37 #define VVAR_TABLE_TAG CTL_TAG('V', 'V', 'A', 'R')
38 #define MVAR_TABLE_TAG CTL_TAG('M', 'V', 'A', 'R')
39
40 #define HHEA_TABLE_VERSION 0x00010000
41 #define VHEA_TABLE_VERSION 0x00010000
42 #define VHEA_TABLE_VERSION_1_1 0x00011000
43 #define VORG_TABLE_VERSION 0x00010000
44 #define AVAR_TABLE_VERSION 0x00010000
45 #define FVAR_TABLE_VERSION 0x00010000
46 #define HVAR_TABLE_VERSION 0x00010000
47 #define VVAR_TABLE_VERSION 0x00010000
48 #define MVAR_TABLE_VERSION 0x00010000
49
50 #define HHEA_TABLE_HEADER_SIZE 36
51 #define VHEA_TABLE_HEADER_SIZE 36
52 #define VORG_TABLE_HEADER_SIZE 8
53 #define AVAR_TABLE_HEADER_SIZE 6
54 #define FVAR_TABLE_HEADER_SIZE 16
55 #define HVAR_TABLE_HEADER_SIZE 20
56 #define VVAR_TABLE_HEADER_SIZE 24
57 #define MVAR_TABLE_HEADER_SIZE 12
58 #define MVAR_TABLE_RECORD_SIZE 8
59
60 #define AVAR_SEGMENT_MAP_SIZE (2 + 4 * 3)
61 #define AVAR_AXIS_VALUE_MAP_SIZE 4
62
63 #define FVAR_OFFSET_TO_AXES_ARRAY 16
64 #define FVAR_COUNT_SIZE_PAIRS 2
65 #define FVAR_AXIS_SIZE 20
66 #define FVAR_INSTANCE_SIZE 4
67 #define FVAR_INSTANCE_WITH_NAME_SIZE 6
68
69 #define ITEM_VARIATION_STORE_TABLE_FORMAT 1
70 #define IVS_SUBTABLE_HEADER_SIZE 12
71 #define IVS_VARIATION_REGION_LIST_HEADER_SIZE 4
72
73 #define REGION_AXIS_COORDINATES_SIZE (2 * 3)
74 #define ITEM_VARIATION_DATA_HEADER_SIZE (2 * 3)
75
76 #define DELTA_SET_INDEX_MAP_HEADER_SIZE (2 * 2)
77
78 #define INNER_INDEX_BIT_COUNT_MASK 0x000F
79 #define MAP_ENTRY_SIZE_MASK 0x0030
80 #define MAP_ENTRY_SIZE_SHIFT 4
81
82 /* ----------------------------- variation font table data ---------------------------- */
83
84 /* avar table */
85 struct var_avar_;
86 typedef struct var_avar_ *var_avar;
87
88 /* fvar table */
89 struct var_fvar_;
90 typedef struct var_fvar_ *var_fvar;
91
92 /* avar table */
93
94 typedef struct axisValueMap_ {
95 Fixed fromCoord;
96 Fixed toCoord;
97 } axisValueMap;
98
99 typedef dnaDCL(axisValueMap, axisValueMapArray);
100
101 typedef struct segmentMap_ {
102 unsigned short positionMapCount;
103 axisValueMapArray valueMaps;
104 } segmentMap;
105
106 typedef dnaDCL(segmentMap, segmentMapArray);
107
108 struct var_avar_ {
109 unsigned short axisCount;
110 segmentMapArray segmentMaps;
111 };
112
113 /* fvar table */
114
115 typedef struct variationAxis_ {
116 ctlTag tag;
117 Fixed minValue;
118 Fixed defaultValue;
119 Fixed maxValue;
120 unsigned short flags;
121 unsigned short nameID;
122 } variationAxis;
123
124 typedef dnaDCL(variationAxis, variationAxesArray);
125
126 typedef dnaDCL(float, coordinatesArray);
127
128 typedef struct variationInstance_ {
129 unsigned short subfamilyNameID;
130 unsigned short flags;
131 coordinatesArray coordinates;
132 unsigned short postScriptNameID;
133 } variationInstance;
134
135 typedef dnaDCL(variationInstance, variationInstancesArray);
136
137 struct var_fvar_ {
138 unsigned short axisCount;
139 unsigned short instanceCount;
140 variationAxesArray axes;
141 variationInstancesArray instances;
142 };
143
144 /* variable font axis tables */
145
146 struct var_axes_ {
147 var_avar avar;
148 var_fvar fvar;
149 };
150
151 typedef dnaDCL(var_glyphMetrics, var_glyphMetricsArray);
152
153 /* index pair */
154
155 typedef struct indexPair_ {
156 unsigned short outerIndex;
157 unsigned short innerIndex;
158 } indexPair;
159
160 typedef struct indexMap_ {
161 unsigned long offset;
162 dnaDCL(indexPair, map);
163 } indexMap;
164
165 /* horizontal metrics tables: hhea, hmtx, HVAR */
166
167 typedef struct var_hhea_ {
168 Fixed version;
169 short ascender;
170 short descender;
171 short lineGap;
172 unsigned short advanceWidthMax;
173 short minLeftSideBearing;
174 short minRightSideBearing;
175 short xMaxExtent;
176 short caretSlopeRise;
177 short caretSlopeRun;
178 short caretOffset;
179 short reserved[4];
180 short metricDataFormat;
181 unsigned short numberOfHMetrics;
182 } var_hhea;
183
184 struct var_hmtx_ {
185 var_hhea header;
186 var_glyphMetricsArray defaultMetrics;
187
188 /* HHVAR */
189 var_itemVariationStore ivs;
190 indexMap widthMap;
191 indexMap lsbMap;
192 indexMap rsbMap;
193 };
194 typedef struct var_hmtx_ *var_hmtx;
195
196 /* horizontal metrics tables: hhea, hmtx, HVAR */
197
198 typedef struct var_vhea_ {
199 Fixed version;
200 short vertTypoAscender;
201 short vertTypoDescender;
202 short vertTypoLineGap;
203 short advanceHeightMax;
204 short minTop;
205 short minBottom;
206 short yMaxExtent;
207 short caretSlopeRise;
208 short caretSlopeRun;
209 short caretOffset;
210 short reserved[4];
211 short metricDataFormat;
212 unsigned short numOfLongVertMetrics;
213 } var_vhea;
214
215 /* vertical metrics tables: vhea, vmtx, VORG, VVAR */
216
217 struct var_vmtx_ {
218 var_vhea header;
219 var_itemVariationStore ivs;
220 var_glyphMetricsArray defaultMetrics;
221 dnaDCL(short, vertOriginY);
222 indexMap widthMap;
223 indexMap tsbMap;
224 indexMap bsbMap;
225 indexMap vorgMap;
226 };
227 typedef struct var_vmtx_ *var_vmtx;
228
229 /* MVAR table */
230
231 typedef struct mvarValueRecord_ {
232 ctlTag valueTag;
233 indexPair pair;
234 } mvarValueRecord;
235
236 typedef dnaDCL(mvarValueRecord, mvarValueArray);
237
238 struct var_MVAR_ {
239 var_itemVariationStore ivs;
240 unsigned short axisCount;
241 unsigned short valueRecordCount;
242 mvarValueArray values;
243 };
244
245 /* ----------------------------- math utility functions ---------------------------- */
246
247 float var_F2dot14ToFloat(var_F2dot14 v) {
248 return (float)v / (1 << 14);
249 }
250
251 /* ----------------------------- variation font table loading functions ---------------------------- */
252
253 /* avar table */
254
255 static void var_freeavar(ctlSharedStmCallbacks *sscb, var_avar avar) {
256 if (avar) {
257 unsigned short i;
258
259 for (i = 0; i < avar->axisCount; i++) {
260 dnaFREE(avar->segmentMaps.array[i].valueMaps);
261 }
262 dnaFREE(avar->segmentMaps);
263
264 sscb->memFree(sscb, avar);
265 }
266 }
267
268 static var_avar var_loadavar(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
269 var_avar avar = NULL;
270 int success = 0;
271 unsigned short axisCount = 0;
272 unsigned short i;
273
274 sfrTable *table = sfrGetTableByTag(sfr, AVAR_TABLE_TAG);
275 if (table == NULL)
276 return NULL;
277
278 DURING
279 sscb->seek(sscb, table->offset);
280
281 /* Read and validate table version */
282 if (sscb->read4(sscb) != AVAR_TABLE_VERSION) {
283 sscb->message(sscb, "invalid avar table version");
284 goto cleanup;
285 }
286
287 if (table->length < AVAR_TABLE_HEADER_SIZE) {
288 sscb->message(sscb, "invalid avar table size");
289 goto cleanup;
290 }
291
292 avar = (var_avar)sscb->memNew(sscb, sizeof(*avar));
293 memset(avar, 0, sizeof(*avar));
294 dnaINIT(sscb->dna, avar->segmentMaps, 0, 1);
295
296 i = sscb->read2(sscb); /* skip reserved short */
297 axisCount = sscb->read2(sscb);
298
299 if (table->length < AVAR_TABLE_HEADER_SIZE + (unsigned long)AVAR_SEGMENT_MAP_SIZE * axisCount) {
300 sscb->message(sscb, "invalid avar table size or axis/instance count/size");
301 goto cleanup;
302 }
303
304 if (dnaSetCnt(&avar->segmentMaps, DNA_ELEM_SIZE_(avar->segmentMaps), axisCount) < 0)
305 goto cleanup;
306 avar->axisCount = axisCount;
307 for (i = 0; i < avar->axisCount; i++) {
308 segmentMap *seg = &avar->segmentMaps.array[i];
309 dnaINIT(sscb->dna, seg->valueMaps, 0, 1);
310 }
311
312 for (i = 0; i < avar->axisCount; i++) {
313 segmentMap *seg = &avar->segmentMaps.array[i];
314 unsigned short j;
315 int hasZeroMap = 0;
316 seg->positionMapCount = sscb->read2(sscb);
317 if (table->length < sscb->tell(sscb) - table->offset + AVAR_AXIS_VALUE_MAP_SIZE * seg->positionMapCount) {
318 sscb->message(sscb, "avar axis value map out of bounds");
319 goto cleanup;
320 }
321
322 if (dnaSetCnt(&seg->valueMaps, DNA_ELEM_SIZE_(seg->valueMaps), seg->positionMapCount) < 0)
323 goto cleanup;
324 for (j = 0; j < seg->positionMapCount; j++) {
325 Fixed fromCoord = F2DOT14_TO_FIXED((var_F2dot14)sscb->read2(sscb));
326 Fixed toCoord = F2DOT14_TO_FIXED((var_F2dot14)sscb->read2(sscb));
327
328 if (j > 0 && j < seg->positionMapCount - 1 && fromCoord == 0 && toCoord == 0) {
329 hasZeroMap = 1;
330 }
331 seg->valueMaps.array[j].fromCoord = fromCoord;
332 seg->valueMaps.array[j].toCoord = toCoord;
333 }
334 if (seg->positionMapCount < 3
335 || seg->valueMaps.array[0].fromCoord != FIXED_MINUS_ONE
336 || seg->valueMaps.array[0].toCoord != FIXED_MINUS_ONE
337 || !hasZeroMap
338 || seg->valueMaps.array[seg->positionMapCount - 1].fromCoord != FIXED_ONE
339 || seg->valueMaps.array[seg->positionMapCount - 1].toCoord != FIXED_ONE) {
340 seg->positionMapCount = 0; /* incomplete value maps: invalidate the maps entirely for this axis */
341 }
342 }
343 success = 1;
344 cleanup:;
345 HANDLER
346 END_HANDLER
347
348 if (!success) {
349 var_freeavar(sscb, avar);
350 avar = 0;
351 }
352
353 return avar;
354 }
355
356 /* fvar table */
357
358 static void var_freefvar(ctlSharedStmCallbacks *sscb, var_fvar fvar) {
359 if (fvar) {
360 long i;
361
362 for (i = 0; i < fvar->instances.cnt; i++) {
363 dnaFREE(fvar->instances.array[i].coordinates);
364 }
365 dnaFREE(fvar->instances);
366 dnaFREE(fvar->axes);
367
368 sscb->memFree(sscb, fvar);
369 }
370 }
371
372 static var_fvar var_loadfvar(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
373 var_fvar fvar = NULL;
374 int success = 0;
375 unsigned short offsetToAxesArray;
376 unsigned short countSizePairs;
377 unsigned short axisSize;
378 unsigned short instanceSize;
379 unsigned short i;
380
381 sfrTable *table = sfrGetTableByTag(sfr, FVAR_TABLE_TAG);
382 if (table == NULL)
383 return NULL;
384
385 DURING
386 sscb->seek(sscb, table->offset);
387
388 /* Read and validate table version */
389 if (sscb->read4(sscb) != FVAR_TABLE_VERSION) {
390 sscb->message(sscb, "invalid fvar table version");
391 goto cleanup;
392 }
393
394 if (table->length < FVAR_TABLE_HEADER_SIZE) {
395 sscb->message(sscb, "invalid fvar table size");
396 goto cleanup;
397 }
398
399 fvar = (var_fvar)sscb->memNew(sscb, sizeof(*fvar));
400 memset(fvar, 0, sizeof(*fvar));
401
402 offsetToAxesArray = sscb->read2(sscb);
403 countSizePairs = sscb->read2(sscb);
404 fvar->axisCount = sscb->read2(sscb);
405 axisSize = sscb->read2(sscb);
406 fvar->instanceCount = sscb->read2(sscb);
407 instanceSize = sscb->read2(sscb);
408
409 /* sanity check of values */
410 if (offsetToAxesArray < FVAR_OFFSET_TO_AXES_ARRAY
411 || countSizePairs < FVAR_COUNT_SIZE_PAIRS
412 || axisSize < FVAR_AXIS_SIZE) {
413 sscb->message(sscb, "invalid values in fvar table header");
414 goto cleanup;
415 }
416
417 if (table->length < offsetToAxesArray + (unsigned long)axisSize * fvar->axisCount + (unsigned long)instanceSize * fvar->instanceCount
418 || instanceSize < FVAR_INSTANCE_SIZE + 4 * fvar->axisCount) {
419 sscb->message(sscb, "invalid fvar table size or axis/instance count/size");
420 goto cleanup;
421 }
422
423 sscb->seek(sscb, table->offset + offsetToAxesArray);
424
425 dnaINIT(sscb->dna, fvar->axes, 0, 1);
426 dnaINIT(sscb->dna, fvar->instances, 0, 1);
427 if (dnaSetCnt(&fvar->axes, DNA_ELEM_SIZE_(fvar->axes), fvar->axisCount) < 0)
428 goto cleanup;
429 memset(fvar->axes.array, 0, sizeof(fvar->axes.array[0]) * fvar->axes.cnt);
430
431 if (dnaSetCnt(&fvar->instances, DNA_ELEM_SIZE_(fvar->instances), fvar->instanceCount) < 0)
432 goto cleanup;
433 memset(fvar->instances.array, 0, sizeof(fvar->instances.array[0]) * fvar->instances.cnt);
434
435 for (i = 0; i < fvar->axisCount; i++) {
436 fvar->axes.array[i].tag = sscb->read4(sscb);
437 fvar->axes.array[i].minValue = (Fixed)sscb->read4(sscb);
438 fvar->axes.array[i].defaultValue = (Fixed)sscb->read4(sscb);
439 fvar->axes.array[i].maxValue = (Fixed)sscb->read4(sscb);
440 fvar->axes.array[i].flags = sscb->read2(sscb);
441 fvar->axes.array[i].nameID = sscb->read2(sscb);
442 }
443
444 for (i = 0; i < fvar->instanceCount; i++) {
445 dnaINIT(sscb->dna, fvar->instances.array[i].coordinates, 0, 1);
446 }
447
448 for (i = 0; i < fvar->instanceCount; i++) {
449 variationInstance *inst = &fvar->instances.array[i];
450 unsigned short j;
451 inst->subfamilyNameID = sscb->read2(sscb);
452 inst->flags = sscb->read2(sscb);
453 if (dnaSetCnt(&inst->coordinates, DNA_ELEM_SIZE_(inst->coordinates), fvar->axisCount) < 0)
454 goto cleanup;
455 for (j = 0; j < fvar->axisCount; j++) {
456 fixtopflt((Fixed)sscb->read4(sscb), &inst->coordinates.array[j]);
457 }
458 if (instanceSize >= FVAR_INSTANCE_WITH_NAME_SIZE + 4 * fvar->axisCount)
459 inst->postScriptNameID = sscb->read2(sscb);
460 else
461 inst->postScriptNameID = 0; /* indicating an unspecified PostScript name ID */
462 }
463 success = 1;
464 cleanup:;
465 HANDLER
466 END_HANDLER
467
468 if (!success) {
469 var_freefvar(sscb, fvar);
470 fvar = NULL;
471 }
472
473 return fvar;
474 }
475
476 /* load font axis tables */
477 var_axes var_loadaxes(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
478 var_axes axis_tables = NULL;
479 int success = 0;
480
481 axis_tables = (var_axes)sscb->memNew(sscb, sizeof(*axis_tables));
482 memset(axis_tables, 0, sizeof(*axis_tables));
483
484 DURING
485 axis_tables->fvar = var_loadfvar(sfr, sscb);
486 if (!axis_tables->fvar)
487 goto cleanup;
488
489 axis_tables->avar = var_loadavar(sfr, sscb);
490 if (axis_tables->avar && axis_tables->fvar->axisCount != axis_tables->avar->axisCount) {
491 sscb->message(sscb, "mismatching axis counts in fvar and avar");
492 var_freeavar(sscb, axis_tables->avar);
493 axis_tables->avar = NULL;
494 }
495 success = 1;
496 cleanup:;
497 HANDLER
498 END_HANDLER
499
500 if (!success) {
501 var_freeaxes(sscb, axis_tables);
502 axis_tables = NULL;
503 }
504
505 return axis_tables;
506 }
507
508 /* free font axis tables */
509 void var_freeaxes(ctlSharedStmCallbacks *sscb, var_axes axes) {
510 if (axes) {
511 if (axes->avar)
512 var_freeavar(sscb, axes->avar);
513 if (axes->fvar)
514 var_freefvar(sscb, axes->fvar);
515 sscb->memFree(sscb, axes);
516 }
517 }
518
519 unsigned short var_getAxisCount(var_axes axes) {
520 if (axes && axes->fvar) {
521 return axes->fvar->axisCount;
522 } else
523 return 0;
524 }
525
526 int var_getAxis(var_axes axes, unsigned short index, unsigned long *tag, Fixed *minValue, Fixed *defaultValue, Fixed *maxValue, unsigned short *flags) {
527 if (axes && axes->fvar && index < axes->fvar->axisCount) {
528 variationAxis *axis = &axes->fvar->axes.array[index];
529 if (tag) *tag = axis->tag;
530 if (minValue) *minValue = axis->minValue;
531 if (defaultValue) *defaultValue = axis->defaultValue;
532 if (maxValue) *maxValue = axis->maxValue;
533 if (flags) *flags = axis->flags;
534 return 0;
535 } else
536 return 1;
537 }
538
539 static Fixed var_defaultNormalizeAxis(var_fvar fvar, unsigned short axisIndex, Fixed userValue) {
540 variationAxis *axis = &fvar->axes.array[axisIndex];
541
542 if (userValue < axis->defaultValue) {
543 if (userValue < axis->minValue)
544 return FIXED_MINUS_ONE;
545
546 return fixdiv(-(axis->defaultValue - userValue), axis->defaultValue - axis->minValue);
547 } else if (userValue > axis->defaultValue) {
548 if (userValue > axis->maxValue)
549 return FIXED_ONE;
550
551 return (fixdiv(userValue - axis->defaultValue, axis->maxValue - axis->defaultValue));
552 } else
553 return FIXED_ZERO;
554 }
555
556 static Fixed applySegmentMap(segmentMap *seg, Fixed value) {
557 long i;
558 axisValueMap *map = NULL;
559 Fixed endFromVal, endToVal, startFromVal, startToVal;
560
561 if (seg->valueMaps.cnt <= 0)
562 return value;
563
564 for (i = 0; i < seg->valueMaps.cnt; i++) {
565 map = &seg->valueMaps.array[i];
566 if (value < map->fromCoord) {
567 break;
568 }
569 }
570 if (i <= 0) /* value is at min axis value */
571 return seg->valueMaps.array[0].toCoord;
572
573 if (i >= seg->valueMaps.cnt) /* value is at max axis value */
574 return seg->valueMaps.array[seg->valueMaps.cnt - 1].toCoord;
575
576 endFromVal = map[0].fromCoord;
577 endToVal = map[0].toCoord;
578
579 if (value == endFromVal)
580 return endToVal;
581
582 startFromVal = map[-1].fromCoord;
583 startToVal = map[-1].toCoord;
584
585 return startToVal + fixmul(endToVal - startToVal, fixdiv(value - startFromVal, endFromVal - startFromVal));
586 }
587
588 int var_normalizeCoords(ctlSharedStmCallbacks *sscb, var_axes axes, Fixed *userCoords, Fixed *normCoords) {
589 unsigned short i;
590 unsigned short axisCount;
591
592 if (!axes || !axes->fvar) {
593 sscb->message(sscb, "var_normalizeCoords: invalid axis table");
594 return 1;
595 }
596
597 axisCount = axes->fvar->axisCount;
598
599 /* default normalization */
600 for (i = 0; i < axisCount; i++) {
601 normCoords[i] = var_defaultNormalizeAxis(axes->fvar, i, userCoords[i]);
602 }
603
604 /* modify the default normalized Coords with the axis variations table */
605 if (axes->avar) {
606 for (i = 0; i < axisCount; i++) {
607 segmentMap *seg = &axes->avar->segmentMaps.array[i];
608 if (seg->positionMapCount > 0)
609 normCoords[i] = applySegmentMap(seg, normCoords[i]);
610 }
611 }
612
613 return 0;
614 }
615
616 /* get the number of named instances */
617 unsigned short var_getInstanceCount(var_axes axes) {
618 if (axes && axes->fvar)
619 return axes->fvar->instanceCount;
620 else
621 return 0;
622 }
623
624 /* get a named instance */
625 int var_findInstance(var_axes axes, float *userCoords, unsigned short axisCount, unsigned short *subfamilyID, unsigned short *postscriptID) {
626 unsigned short i;
627 var_fvar fvar;
628
629 if (!axes || !axes->fvar)
630 return -1;
631
632 fvar = axes->fvar;
633 if (axisCount != fvar->axisCount)
634 return -1;
635
636 for (i = 0; i < fvar->instanceCount; i++) {
637 int match = 1;
638 variationInstance *instance = &fvar->instances.array[i];
639 unsigned short axis;
640
641 for (axis = 0; axis < axisCount; axis++) {
642 if (userCoords[axis] != instance->coordinates.array[axis]) {
643 match = 0;
644 break;
645 }
646 }
647 if (match) {
648 *subfamilyID = instance->subfamilyNameID;
649 *postscriptID = instance->postScriptNameID;
650 return (int)i;
651 }
652 }
653 return -1;
654 }
655
656 /* item variation store sub-table */
657
658 var_itemVariationStore var_loadItemVariationStore(ctlSharedStmCallbacks *sscb, unsigned long tableOffset, unsigned long tableLength, unsigned long ivsOffset) {
659 unsigned long regionListOffset;
660 unsigned short ivdSubtableCount;
661 var_itemVariationStore ivs = NULL;
662 int success = 0;
663 unsigned short i, axis;
664 dnaDCL(unsigned long, ivdSubtablesOffsets);
665 variationRegion *region;
666
667 dnaINIT(sscb->dna, ivdSubtablesOffsets, 5, 10);
668
669 DURING
670 if (ivsOffset + IVS_SUBTABLE_HEADER_SIZE > tableLength) {
671 sscb->message(sscb, "item variation store offset not within table range");
672 goto cleanup;
673 }
674
675 sscb->seek(sscb, tableOffset + ivsOffset);
676
677 /* load table header */
678 if (sscb->read2(sscb) != ITEM_VARIATION_STORE_TABLE_FORMAT) {
679 sscb->message(sscb, "invalid item variation store table format");
680 goto cleanup;
681 }
682 regionListOffset = sscb->read4(sscb);
683 ivdSubtableCount = sscb->read2(sscb);
684
685 /* allocate and initialize item variation data store structure */
686 ivs = (var_itemVariationStore)sscb->memNew(sscb, sizeof(*ivs));
687 if (ivs == NULL)
688 goto cleanup;
689 memset(ivs, 0, sizeof(*ivs));
690 dnaINIT(sscb->dna, ivs->regionList.regions, 0, 1);
691 dnaINIT(sscb->dna, ivs->dataList.ivdSubtables, 0, 1);
692
693 /* load item variation data offsets into a temporary array */
694 if (dnaSetCnt(&ivs->dataList.ivdSubtables, DNA_ELEM_SIZE_(ivs->dataList.ivdSubtables), ivdSubtableCount) < 0)
695 goto cleanup;
696
697 memset(DNA_ELEM_ADDR_(ivs->dataList.ivdSubtables, 0), 0, DNA_ELEM_SIZE_(ivs->dataList.ivdSubtables) * ivdSubtableCount);
698
699 if (dnaSetCnt(&ivdSubtablesOffsets, DNA_ELEM_SIZE_(ivdSubtablesOffsets), ivdSubtableCount) < 0)
700 goto cleanup;
701
702 for (i = 0; i < ivdSubtableCount; i++) {
703 ivdSubtablesOffsets.array[i] = sscb->read4(sscb);
704 }
705
706 /* load variation region list */
707 if (ivsOffset + regionListOffset + IVS_VARIATION_REGION_LIST_HEADER_SIZE > tableLength) {
708 sscb->message(sscb, "invalid item variation region offset");
709 goto cleanup;
710 }
711 sscb->seek(sscb, tableOffset + ivsOffset + regionListOffset);
712
713 ivs->regionList.axisCount = sscb->read2(sscb);
714
715 if (ivs->regionList.axisCount > CFF2_MAX_AXES) {
716 sscb->message(sscb, "invalid axis count in item variation region list");
717 goto cleanup;
718 }
719
720 ivs->regionList.regionCount = sscb->read2(sscb);
721
722 /* cff2.scalars and cff2.regionIndices have size CFF2_MAX_MASTERS,
723 so regionList.regionCount should not exceed CFF2_MAX_MASTERS */
724 if (ivs->regionList.regionCount > CFF2_MAX_MASTERS) {
725 sscb->message(sscb, "invalid region count in item variation region list");
726 goto cleanup;
727 }
728
729 if (dnaSetCnt(&ivs->regionList.regions, DNA_ELEM_SIZE_(ivs->regionList.regions), (long)ivs->regionList.axisCount * ivs->regionList.regionCount) < 0)
730 goto cleanup;
731
732 if (ivsOffset + regionListOffset + IVS_VARIATION_REGION_LIST_HEADER_SIZE
733 + REGION_AXIS_COORDINATES_SIZE * ivs->regionList.regions.cnt > tableLength) {
734 sscb->message(sscb, "item variation region list out of bounds");
735 goto cleanup;
736 }
737
738 region = &ivs->regionList.regions.array[0];
739 for (i = 0; i < ivs->regionList.regionCount; i++) {
740 for (axis = 0; axis < ivs->regionList.axisCount; axis++) {
741 region->startCoord = F2DOT14_TO_FIXED((var_F2dot14)sscb->read2(sscb));
742 region->peakCoord = F2DOT14_TO_FIXED((var_F2dot14)sscb->read2(sscb));
743 region->endCoord = F2DOT14_TO_FIXED((var_F2dot14)sscb->read2(sscb));
744 region++;
745
746 /* TODO: coord range check? */
747 }
748 }
749
750 /* load item variation data list */
751 for (i = 0; i < ivdSubtableCount; i++) {
752 unsigned long ivdSubtablesOffset;
753 unsigned short shortDeltaCount;
754 itemVariationDataSubtable *ivd;
755 unsigned short r, t, j;
756
757 ivdSubtablesOffset = ivdSubtablesOffsets.array[i];
758 if (ivsOffset + ivdSubtablesOffset + ITEM_VARIATION_DATA_HEADER_SIZE > tableLength) {
759 sscb->message(sscb, "item variation data offset out of bounds");
760 goto cleanup;
761 }
762
763 /* load item variation data sub-table header */
764 sscb->seek(sscb, tableOffset + ivsOffset + ivdSubtablesOffset);
765
766 ivd = &ivs->dataList.ivdSubtables.array[i];
767 ivd->itemCount = sscb->read2(sscb);
768 shortDeltaCount = sscb->read2(sscb);
769 ivd->regionCount = sscb->read2(sscb);
770 if (ivd->regionCount > CFF2_MAX_MASTERS)
771 goto cleanup;
772
773 dnaINIT(sscb->dna, ivd->regionIndices, ivd->regionCount, 1);
774 dnaINIT(sscb->dna, ivd->deltaValues, (size_t)ivd->itemCount * ivd->regionCount, 1);
775 if ((dnaSetCnt(&ivd->regionIndices, DNA_ELEM_SIZE_(ivd->regionIndices), ivd->regionCount) < 0)
776 || (dnaSetCnt(&ivd->deltaValues, DNA_ELEM_SIZE_(ivd->deltaValues), (long)ivd->itemCount * ivd->regionCount) < 0))
777 goto cleanup;
778
779 /* load region indices */
780 for (r = 0; r < ivd->regionCount; r++) {
781 ivd->regionIndices.array[r] = (short)sscb->read2(sscb);
782 }
783
784 /* load two-dimensional delta values array */
785 j = 0;
786 for (t = 0; t < ivd->itemCount; t++) {
787 for (r = 0; r < ivd->regionCount; r++) {
788 ivd->deltaValues.array[j++] = (short)((r < shortDeltaCount) ? sscb->read2(sscb) : (char)sscb->read1(sscb));
789 }
790 }
791 }
792 success = 1;
793 cleanup:;
794 HANDLER
795 END_HANDLER
796
797 if (!success) {
798 var_freeItemVariationStore(sscb, ivs);
799 ivs = NULL;
800 }
801 dnaFREE(ivdSubtablesOffsets);
802
803 return ivs;
804 }
805
806 void var_freeItemVariationStore(ctlSharedStmCallbacks *sscb, var_itemVariationStore ivs) {
807 if (ivs) {
808 long i;
809
810 for (i = 0; i < ivs->dataList.ivdSubtables.cnt; i++) {
811 itemVariationDataSubtable *ivd = &ivs->dataList.ivdSubtables.array[i];
812 dnaFREE(ivd->regionIndices);
813 dnaFREE(ivd->deltaValues);
814 }
815
816 dnaFREE(ivs->regionList.regions);
817 dnaFREE(ivs->dataList.ivdSubtables);
818 sscb->memFree(sscb, ivs);
819 }
820 }
821
822 unsigned short var_getIVSRegionCount(var_itemVariationStore ivs) {
823 return (unsigned short)ivs->regionList.regionCount;
824 }
825
826 /* get the region count for the ItemVariationData table with a given variation store index */
827 unsigned short var_getIVDRegionCountForIndex(var_itemVariationStore ivs, unsigned short vsIndex) {
828 if (!ivs || vsIndex >= ivs->dataList.ivdSubtables.cnt)
829 return 0;
830
831 return ivs->dataList.ivdSubtables.array[vsIndex].regionCount;
832 }
833
834 /* calculate scalars for all regions given a normalized design vector. */
835 void var_calcRegionScalars(ctlSharedStmCallbacks *sscb, var_itemVariationStore ivs, unsigned short *fvarAxisCount, Fixed *instCoords, float *scalars) {
836 variationRegionList *regionList = &ivs->regionList;
837 long regionCount = regionList->regionCount;
838 long i;
839 unsigned short axis;
840 unsigned short axisCount = *fvarAxisCount;
841
842 if (axisCount != regionList->axisCount) {
843 sscb->message(sscb, "axis count in variation font region list does not match axis count in fvar table");
844 *fvarAxisCount = axisCount = regionList->axisCount;
845 for (i = 0; i < regionCount; i++)
846 scalars[i] = .0f;
847 return;
848 }
849
850 for (i = 0; i < regionCount; i++) {
851 float scalar = 1.0f;
852
853 for (axis = 0; axis < axisCount; axis++) {
854 float axisScalar;
855 variationRegion *r = &regionList->regions.array[i * axisCount + axis];
856
857 if (r->startCoord > r->peakCoord || r->peakCoord > r->endCoord)
858 axisScalar = 1.0f;
859 else if (r->startCoord < FIXED_ZERO && r->endCoord > FIXED_ZERO && r->peakCoord != FIXED_ZERO)
860 axisScalar = 1.0f;
861 else if (r->peakCoord == FIXED_ZERO)
862 axisScalar = 1.0f;
863 else if (instCoords[axis] < r->startCoord || instCoords[axis] > r->endCoord)
864 axisScalar = .0f;
865 else {
866 if (instCoords[axis] == r->peakCoord)
867 axisScalar = 1.0f;
868 else if (instCoords[axis] < r->peakCoord)
869 axisScalar = (float)(instCoords[axis] - r->startCoord) / (r->peakCoord - r->startCoord);
870 else /* instCoords[axis] > r->peakCoord */
871 axisScalar = (float)(r->endCoord - instCoords[axis]) / (r->endCoord - r->peakCoord);
872 }
873 scalar = scalar * axisScalar;
874 }
875
876 scalars[i] = scalar;
877 }
878 return;
879 }
880
881 static int loadIndexMap(ctlSharedStmCallbacks *sscb, sfrTable *table, unsigned long indexOffset, indexMap *ima) {
882 unsigned short entryFormat;
883 unsigned short mapCount;
884 unsigned short entrySize;
885 unsigned short innerIndexEntryMask;
886 unsigned short outerIndexEntryShift;
887 unsigned long mapDataSize;
888 unsigned short i, j;
889
890 ima->offset = indexOffset;
891 if (indexOffset == 0) /* No index map */
892 return 1;
893
894 if (indexOffset + DELTA_SET_INDEX_MAP_HEADER_SIZE > table->offset) {
895 sscb->message(sscb, "invalid delta set index map table header");
896 return 0;
897 }
898
899 sscb->seek(sscb, table->offset + indexOffset);
900 entryFormat = sscb->read2(sscb);
901 mapCount = sscb->read2(sscb);
902
903 entrySize = ((entryFormat & MAP_ENTRY_SIZE_MASK) >> MAP_ENTRY_SIZE_SHIFT) + 1;
904 innerIndexEntryMask = (1 << ((entryFormat & INNER_INDEX_BIT_COUNT_MASK) + 1)) - 1;
905 outerIndexEntryShift = (entryFormat & INNER_INDEX_BIT_COUNT_MASK) + 1;
906
907 mapDataSize = (unsigned long)mapCount * entrySize;
908
909 if (mapCount == 0 || indexOffset + DELTA_SET_INDEX_MAP_HEADER_SIZE + mapDataSize > table->length) {
910 sscb->message(sscb, "invalid delta set index map table size");
911 return 0;
912 }
913
914 if (dnaSetCnt(&ima->map, DNA_ELEM_SIZE_(ima->map), mapCount) < 0)
915 return 0;
916
917 for (i = 0; i < mapCount; i++) {
918 unsigned short entry = 0;
919 for (j = 0; j < entrySize; j++) {
920 entry <<= 8;
921 entry += sscb->read1(sscb);
922 }
923 ima->map.array[i].innerIndex = (entry & innerIndexEntryMask);
924 ima->map.array[i].outerIndex = (entry >> outerIndexEntryShift);
925 }
926
927 return 1;
928 }
929
930 static void lookupIndexMap(indexMap *map, unsigned short gid, indexPair *index) {
931 if (map->map.cnt <= gid) {
932 if (map->map.cnt == 0) {
933 index->innerIndex = gid;
934 index->outerIndex = 0;
935 } else {
936 *index = map->map.array[map->map.cnt - 1];
937 }
938 } else {
939 *index = map->map.array[gid];
940 }
941 }
942
943 long var_getIVSRegionIndices(var_itemVariationStore ivs, unsigned short vsIndex, unsigned short *regionIndices, long regionListCount) {
944 itemVariationDataSubtableList *dataList = &ivs->dataList;
945 itemVariationDataSubtable *subtable;
946 long i;
947
948 if (!ivs || vsIndex >= dataList->ivdSubtables.cnt)
949 return 0;
950
951 subtable = &dataList->ivdSubtables.array[vsIndex];
952
953 if (regionListCount < subtable->regionIndices.cnt)
954 return 0;
955
956 for (i = 0; i < subtable->regionIndices.cnt; i++) {
957 unsigned short index = subtable->regionIndices.array[i];
958 if (index >= regionListCount) {
959 return 0;
960 }
961 regionIndices[i] = index;
962 }
963
964 return subtable->regionIndices.cnt;
965 }
966
967 static float var_applyDeltasForIndexPair(ctlSharedStmCallbacks *sscb, var_itemVariationStore ivs, indexPair *pair, float *scalars, long regionListCount) {
968 itemVariationDataSubtableList *dataList = &ivs->dataList;
969 float netAdjustment = .0f;
970 long i, deltaSetIndex;
971 itemVariationDataSubtable *subtable;
972 unsigned short regionIndices[CFF2_MAX_MASTERS];
973 long subRegionCount;
974
975 if ((long)pair->outerIndex >= dataList->ivdSubtables.cnt) {
976 sscb->message(sscb, "invalid outer index in index map");
977 return netAdjustment;
978 }
979
980 subtable = &dataList->ivdSubtables.array[pair->outerIndex];
981
982 /* If specific glyphs do not have any variation, they may be
983 referenced by an ivdSubtable with a region count of 0. This is
984 valid.
985 */
986 if (subtable->regionCount == 0)
987 return netAdjustment;
988
989 if (subtable->regionCount > regionListCount) {
990 sscb->message(sscb, "out of range region count in item variation store subtable");
991 return netAdjustment;
992 }
993
994 deltaSetIndex = (long)subtable->regionCount * pair->innerIndex;
995
996 if ((long)pair->innerIndex >= subtable->itemCount || deltaSetIndex + subtable->regionCount > subtable->deltaValues.cnt) {
997 sscb->message(sscb, "invalid inner index in index map");
998 return netAdjustment;
999 }
1000
1001 subRegionCount = var_getIVSRegionIndices(ivs, pair->outerIndex, regionIndices, regionListCount);
1002 if (subRegionCount == 0) {
1003 sscb->message(sscb, "out of range region index found in item variation store subtable");
1004 }
1005
1006 for (i = 0; i < subRegionCount; i++) {
1007 unsigned short regionIndex = regionIndices[i];
1008
1009 if (scalars[regionIndex])
1010 netAdjustment += scalars[regionIndex] * subtable->deltaValues.array[deltaSetIndex + i];
1011 }
1012
1013 return netAdjustment;
1014 }
1015
1016 static float var_applyDeltasForGid(ctlSharedStmCallbacks *sscb, var_itemVariationStore ivs, indexMap *map, unsigned short gid, float *scalars, long regionListCount) {
1017 indexPair pair;
1018
1019 /* Use (0,gid) as the default index pair if the index map table is missing */
1020 if (map->map.cnt == 0) {
1021 pair.outerIndex = 0;
1022 pair.innerIndex = gid;
1023 } else
1024 lookupIndexMap(map, gid, &pair);
1025
1026 return var_applyDeltasForIndexPair(sscb, ivs, &pair, scalars, regionListCount);
1027 }
1028
1029 /* HVAR / vmtx tables */
1030
1031 var_hmtx var_loadhmtx(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
1032 var_hmtx hmtx = NULL;
1033 int success = 0;
1034 sfrTable *table = NULL;
1035 unsigned long ivsOffset;
1036 unsigned long widthMapOffset;
1037 unsigned long lsbMapOffset;
1038 unsigned long rsbMapOffset;
1039 float defaultWidth;
1040 long i;
1041 long numGlyphs;
1042
1043 hmtx = (var_hmtx)sscb->memNew(sscb, sizeof(*hmtx));
1044 memset(hmtx, 0, sizeof(*hmtx));
1045
1046 DURING
1047 /* read hhea table */
1048 table = sfrGetTableByTag(sfr, HHEA_TABLE_TAG);
1049 if (table == NULL || table->length < HHEA_TABLE_HEADER_SIZE) {
1050 sscb->message(sscb, "invalid/missing hhea table");
1051 goto cleanup;
1052 }
1053
1054 sscb->seek(sscb, table->offset);
1055
1056 hmtx->header.version = (Fixed)sscb->read4(sscb);
1057 if (hmtx->header.version != HHEA_TABLE_VERSION) {
1058 sscb->message(sscb, "invalid hhea table version");
1059 goto cleanup;
1060 }
1061
1062 hmtx->header.ascender = (short)sscb->read2(sscb);
1063 hmtx->header.descender = (short)sscb->read2(sscb);
1064 hmtx->header.lineGap = (short)sscb->read2(sscb);
1065 hmtx->header.advanceWidthMax = (unsigned short)sscb->read2(sscb);
1066 hmtx->header.minLeftSideBearing = (short)sscb->read2(sscb);
1067 hmtx->header.minRightSideBearing = (short)sscb->read2(sscb);
1068 hmtx->header.xMaxExtent = (short)sscb->read2(sscb);
1069 hmtx->header.caretSlopeRise = (short)sscb->read2(sscb);
1070 hmtx->header.caretSlopeRun = (short)sscb->read2(sscb);
1071 hmtx->header.caretOffset = (short)sscb->read2(sscb);
1072 for (i = 0; i < 4; i++) hmtx->header.reserved[i] = (short)sscb->read2(sscb);
1073 hmtx->header.metricDataFormat = (short)sscb->read2(sscb);
1074 hmtx->header.numberOfHMetrics = (unsigned short)sscb->read2(sscb);
1075 if (hmtx->header.numberOfHMetrics == 0) {
1076 sscb->message(sscb, "invalid numberOfHMetrics value in hhea table");
1077 goto cleanup;
1078 }
1079
1080 /* read hmtx table */
1081 table = sfrGetTableByTag(sfr, HMTX_TABLE_TAG);
1082 if (table == NULL)
1083 goto cleanup;
1084
1085 /* estimate the number of glyphs from the table size instead of reading the head table */
1086 numGlyphs = (table->length / 2) - hmtx->header.numberOfHMetrics;
1087 if ((numGlyphs < hmtx->header.numberOfHMetrics) || (numGlyphs > 65535)) {
1088 sscb->message(sscb, "invalid hmtx table size");
1089 goto cleanup;
1090 }
1091
1092 sscb->seek(sscb, table->offset);
1093
1094 dnaINIT(sscb->dna, hmtx->defaultMetrics, numGlyphs, 1);
1095 if (dnaSetCnt(&hmtx->defaultMetrics, DNA_ELEM_SIZE_(hmtx->defaultMetrics), numGlyphs) < 0)
1096 goto cleanup;
1097 for (i = 0; i < hmtx->header.numberOfHMetrics; i++) {
1098 hmtx->defaultMetrics.array[i].width = (unsigned short)sscb->read2(sscb);
1099 hmtx->defaultMetrics.array[i].sideBearing = (short)sscb->read2(sscb);
1100 }
1101 defaultWidth = hmtx->defaultMetrics.array[i - 1].width;
1102 for (; i < numGlyphs; i++) {
1103 hmtx->defaultMetrics.array[i].width = defaultWidth;
1104 hmtx->defaultMetrics.array[i].sideBearing = (short)sscb->read2(sscb);
1105 }
1106
1107 table = sfrGetTableByTag(sfr, HVAR_TABLE_TAG);
1108 if (table == NULL) { /* HVAR table is optional */
1109 success = 1;
1110 goto cleanup;
1111 }
1112
1113 sscb->seek(sscb, table->offset);
1114
1115 if (table->length < HVAR_TABLE_HEADER_SIZE) {
1116 sscb->message(sscb, "invalid HVAR table size");
1117 goto cleanup;
1118 }
1119
1120 /* Read and validate HVAR table version */
1121 if (sscb->read4(sscb) != HVAR_TABLE_VERSION) {
1122 sscb->message(sscb, "invalid HVAR table version");
1123 goto cleanup;
1124 }
1125
1126 ivsOffset = sscb->read4(sscb);
1127 widthMapOffset = sscb->read4(sscb);
1128 lsbMapOffset = sscb->read4(sscb);
1129 rsbMapOffset = sscb->read4(sscb);
1130
1131 if (ivsOffset == 0) {
1132 sscb->message(sscb, "item variation store offset in HVAR is NULL");
1133 goto cleanup;
1134 }
1135
1136 hmtx->ivs = var_loadItemVariationStore(sscb, table->offset, table->length, ivsOffset);
1137 if (hmtx->ivs == NULL)
1138 goto cleanup;
1139
1140 dnaINIT(sscb->dna, hmtx->widthMap.map, 0, 1);
1141 dnaINIT(sscb->dna, hmtx->lsbMap.map, 0, 1);
1142 dnaINIT(sscb->dna, hmtx->rsbMap.map, 0, 1);
1143
1144 if (!loadIndexMap(sscb, table, widthMapOffset, &hmtx->widthMap))
1145 goto cleanup;
1146
1147 if (!loadIndexMap(sscb, table, lsbMapOffset, &hmtx->lsbMap))
1148 goto cleanup;
1149
1150 if (!loadIndexMap(sscb, table, rsbMapOffset, &hmtx->rsbMap))
1151 goto cleanup;
1152
1153 success = 1;
1154
1155 cleanup:;
1156 HANDLER
1157 END_HANDLER
1158
1159 if (!success) {
1160 var_freehmtx(sscb, hmtx);
1161 hmtx = NULL;
1162 }
1163
1164 return hmtx;
1165 }
1166
1167 void var_freehmtx(ctlSharedStmCallbacks *sscb, var_hmtx hmtx) {
1168 if (hmtx) {
1169 dnaFREE(hmtx->defaultMetrics);
1170 var_freeItemVariationStore(sscb, hmtx->ivs);
1171 dnaFREE(hmtx->widthMap.map);
1172 dnaFREE(hmtx->lsbMap.map);
1173 dnaFREE(hmtx->rsbMap.map);
1174
1175 sscb->memFree(sscb, hmtx);
1176 }
1177 }
1178
1179 int var_lookuphmtx(ctlSharedStmCallbacks *sscb, var_hmtx hmtx, unsigned short axisCount, Fixed *instCoords, unsigned short gid, var_glyphMetrics *metrics) {
1180 if (!hmtx) {
1181 sscb->message(sscb, "invalid HVAR table data");
1182 return 1;
1183 }
1184
1185 if (gid >= hmtx->defaultMetrics.cnt) {
1186 sscb->message(sscb, "var_lookuphmtx: invalid glyph ID");
1187 return 1;
1188 }
1189
1190 *metrics = hmtx->defaultMetrics.array[gid];
1191
1192 /* modify the default metrics if the font has variable font tables */
1193 if (hmtx->ivs && instCoords && (axisCount > 0)) {
1194 long regionListCount = hmtx->ivs->regionList.regionCount;
1195 float scalars[CFF2_MAX_MASTERS];
1196
1197 var_calcRegionScalars(sscb, hmtx->ivs, &axisCount, instCoords, scalars);
1198 metrics->width += var_applyDeltasForGid(sscb, hmtx->ivs, &hmtx->widthMap, gid, scalars, regionListCount);
1199 if (hmtx->lsbMap.offset > 0) /* if side bearing variation data are provided, index map must exist */
1200 metrics->sideBearing += var_applyDeltasForGid(sscb, hmtx->ivs, &hmtx->lsbMap, gid, scalars, regionListCount);
1201 }
1202
1203 return 0;
1204 }
1205
1206 var_vmtx var_loadvmtx(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
1207 var_vmtx vmtx = NULL;
1208 int success = 0;
1209 sfrTable *table = NULL;
1210 unsigned long ivsOffset;
1211 unsigned long widthMapOffset;
1212 unsigned long tsbMapOffset;
1213 unsigned long bsbMapOffset;
1214 unsigned long vorgMapOffset;
1215 float defaultWidth;
1216 long i;
1217 long numGlyphs;
1218
1219 vmtx = (var_vmtx)sscb->memNew(sscb, sizeof(*vmtx));
1220 memset(vmtx, 0, sizeof(*vmtx));
1221
1222 DURING
1223 /* read hhea table */
1224 table = sfrGetTableByTag(sfr, VHEA_TABLE_TAG);
1225 if (table == NULL || table->length < VHEA_TABLE_HEADER_SIZE) {
1226 sscb->message(sscb, "invalid/missing vhea table");
1227 goto cleanup;
1228 }
1229
1230 sscb->seek(sscb, table->offset);
1231
1232 vmtx->header.version = (Fixed)sscb->read4(sscb);
1233 if (vmtx->header.version != VHEA_TABLE_VERSION && vmtx->header.version != VHEA_TABLE_VERSION_1_1) {
1234 sscb->message(sscb, "invalid hhea table version");
1235 goto cleanup;
1236 }
1237
1238 vmtx->header.vertTypoAscender = (short)sscb->read2(sscb);
1239 vmtx->header.vertTypoDescender = (short)sscb->read2(sscb);
1240 vmtx->header.vertTypoLineGap = (short)sscb->read2(sscb);
1241 vmtx->header.advanceHeightMax = (unsigned short)sscb->read2(sscb);
1242 vmtx->header.minTop = (short)sscb->read2(sscb);
1243 vmtx->header.minBottom = (short)sscb->read2(sscb);
1244 vmtx->header.caretSlopeRise = (short)sscb->read2(sscb);
1245 vmtx->header.caretSlopeRun = (short)sscb->read2(sscb);
1246 vmtx->header.caretOffset = (short)sscb->read2(sscb);
1247 for (i = 0; i < 4; i++) vmtx->header.reserved[i] = (short)sscb->read2(sscb);
1248 vmtx->header.metricDataFormat = (short)sscb->read2(sscb);
1249 vmtx->header.numOfLongVertMetrics = (unsigned short)sscb->read2(sscb);
1250 if (vmtx->header.numOfLongVertMetrics == 0) {
1251 sscb->message(sscb, "invalid numOfLongVertMetrics value in vhea table");
1252 goto cleanup;
1253 }
1254
1255 /* read vmtx table */
1256 table = sfrGetTableByTag(sfr, VMTX_TABLE_TAG);
1257 if (table == NULL)
1258 goto cleanup;
1259
1260 /* estimate the number of glyphs from the table size instead of reading the head table */
1261 numGlyphs = (table->length / 2) - vmtx->header.numOfLongVertMetrics;
1262 if (numGlyphs < vmtx->header.numOfLongVertMetrics) {
1263 sscb->message(sscb, "invalid vmtx table size");
1264 goto cleanup;
1265 }
1266
1267 sscb->seek(sscb, table->offset);
1268
1269 if (dnaSetCnt(&vmtx->defaultMetrics, DNA_ELEM_SIZE_(vmtx->defaultMetrics), numGlyphs) < 0)
1270 goto cleanup;
1271 for (i = 0; i < vmtx->header.numOfLongVertMetrics; i++) {
1272 vmtx->defaultMetrics.array[i].width = (unsigned short)sscb->read2(sscb);
1273 vmtx->defaultMetrics.array[i].sideBearing = (short)sscb->read2(sscb);
1274 }
1275 defaultWidth = vmtx->defaultMetrics.array[i - 1].width;
1276 for (; i < numGlyphs; i++) {
1277 vmtx->defaultMetrics.array[i].width = defaultWidth;
1278 vmtx->defaultMetrics.array[i].sideBearing = (short)sscb->read2(sscb);
1279 }
1280
1281 /* read optional VORG table */
1282 dnaINIT(sscb->dna, vmtx->vertOriginY, 0, 1);
1283
1284 table = sfrGetTableByTag(sfr, VORG_TABLE_TAG);
1285 if (table != NULL) {
1286 short defaultVertOriginY;
1287 unsigned short numVertOriginYMetrics;
1288
1289 sscb->seek(sscb, table->offset);
1290
1291 if (dnaSetCnt(&vmtx->vertOriginY, DNA_ELEM_SIZE_(vmtx->vertOriginY), numGlyphs) < 0)
1292 goto cleanup;
1293
1294 if (table->length < VORG_TABLE_HEADER_SIZE) {
1295 sscb->message(sscb, "invalid VVAR table size");
1296 goto cleanup;
1297 }
1298
1299 if (sscb->read4(sscb) != VORG_TABLE_VERSION) {
1300 sscb->message(sscb, "invalid VORG table version");
1301 goto cleanup;
1302 }
1303
1304 defaultVertOriginY = (short)sscb->read2(sscb);
1305 numVertOriginYMetrics = (unsigned short)sscb->read2(sscb);
1306 if (table->length < (unsigned long)(VORG_TABLE_HEADER_SIZE + 4 * numVertOriginYMetrics)) {
1307 sscb->message(sscb, "invalid VORG table size");
1308 goto cleanup;
1309 }
1310
1311 for (i = 0; i < numGlyphs; i++) {
1312 vmtx->vertOriginY.array[i] = defaultVertOriginY;
1313 }
1314
1315 for (i = 0; i < numVertOriginYMetrics; i++) {
1316 unsigned short glyphIndex = (unsigned short)sscb->read2(sscb);
1317 short vertOriginY = (short)sscb->read2(sscb);
1318
1319 if (glyphIndex >= numGlyphs) {
1320 sscb->message(sscb, "invalid glyph index in VORG table");
1321 goto cleanup;
1322 }
1323 vmtx->vertOriginY.array[glyphIndex] = vertOriginY;
1324 }
1325 }
1326
1327 table = sfrGetTableByTag(sfr, VVAR_TABLE_TAG);
1328 if (table == NULL) { /* VVAR table is optional */
1329 success = 1;
1330 goto cleanup;
1331 }
1332
1333 sscb->seek(sscb, table->offset);
1334
1335 /* Read and validate HVAR/VVAR table version */
1336 if (table->length < VVAR_TABLE_HEADER_SIZE) {
1337 sscb->message(sscb, "invalid VVAR table size");
1338 goto cleanup;
1339 }
1340
1341 if (sscb->read4(sscb) != VVAR_TABLE_VERSION) {
1342 sscb->message(sscb, "invalid VVAR table version");
1343 goto cleanup;
1344 }
1345
1346 ivsOffset = sscb->read4(sscb);
1347 widthMapOffset = sscb->read4(sscb);
1348 tsbMapOffset = sscb->read4(sscb);
1349 bsbMapOffset = sscb->read4(sscb);
1350 vorgMapOffset = sscb->read4(sscb);
1351
1352 if (ivsOffset == 0) {
1353 sscb->message(sscb, "item variation store offset in VVAR is NULL");
1354 goto cleanup;
1355 }
1356
1357 vmtx->ivs = var_loadItemVariationStore(sscb, table->offset, table->length, ivsOffset);
1358 if (vmtx->ivs == NULL)
1359 goto cleanup;
1360
1361 dnaINIT(sscb->dna, vmtx->widthMap.map, 0, 1);
1362 dnaINIT(sscb->dna, vmtx->vorgMap.map, 0, 1);
1363
1364 if (!loadIndexMap(sscb, table, widthMapOffset, &vmtx->widthMap))
1365 goto cleanup;
1366
1367 if (!loadIndexMap(sscb, table, tsbMapOffset, &vmtx->tsbMap))
1368 goto cleanup;
1369
1370 if (!loadIndexMap(sscb, table, bsbMapOffset, &vmtx->bsbMap))
1371 goto cleanup;
1372
1373 if (!loadIndexMap(sscb, table, vorgMapOffset, &vmtx->vorgMap))
1374 goto cleanup;
1375
1376 success = 1;
1377
1378 cleanup:;
1379 HANDLER
1380 END_HANDLER
1381
1382 if (!success) {
1383 var_freevmtx(sscb, vmtx);
1384 vmtx = NULL;
1385 }
1386
1387 return vmtx;
1388 }
1389
1390 void var_freevmtx(ctlSharedStmCallbacks *sscb, var_vmtx vmtx) {
1391 if (vmtx) {
1392 dnaFREE(vmtx->defaultMetrics);
1393 dnaFREE(vmtx->vertOriginY);
1394 var_freeItemVariationStore(sscb, vmtx->ivs);
1395 dnaFREE(vmtx->widthMap.map);
1396 dnaFREE(vmtx->tsbMap.map);
1397 dnaFREE(vmtx->bsbMap.map);
1398 dnaFREE(vmtx->vorgMap.map);
1399
1400 sscb->memFree(sscb, vmtx);
1401 }
1402 }
1403
1404 int var_lookupvmtx(ctlSharedStmCallbacks *sscb, var_vmtx vmtx, unsigned short axisCount, Fixed *instCoords, unsigned short gid, var_glyphMetrics *metrics) {
1405 if (!vmtx) {
1406 sscb->message(sscb, "invalid VVAR table data");
1407 return 1;
1408 }
1409
1410 if (gid >= vmtx->defaultMetrics.cnt) {
1411 sscb->message(sscb, "var_lookupvmtx: invalid glyph ID");
1412 return 1;
1413 }
1414
1415 *metrics = vmtx->defaultMetrics.array[gid];
1416
1417 /* modify the default metrics if the font has variable font tables */
1418 if (vmtx->ivs && instCoords && (axisCount > 0)) {
1419 long regionListCount = vmtx->ivs->regionList.regionCount;
1420 float scalars[CFF2_MAX_MASTERS];
1421
1422 var_calcRegionScalars(sscb, vmtx->ivs, &axisCount, instCoords, scalars);
1423 metrics->width += var_applyDeltasForGid(sscb, vmtx->ivs, &vmtx->widthMap, gid, scalars, regionListCount);
1424 if (vmtx->tsbMap.offset > 0) /* if side bearing variation data are provided, index map must exist */
1425 metrics->sideBearing += var_applyDeltasForGid(sscb, vmtx->ivs, &vmtx->tsbMap, gid, scalars, regionListCount);
1426 }
1427
1428 return 0;
1429 }
1430
1431 /* MVAR table */
1432
1433 var_MVAR var_loadMVAR(sfrCtx sfr, ctlSharedStmCallbacks *sscb) {
1434 var_MVAR mvar = NULL;
1435 unsigned long ivsOffset;
1436 unsigned short valueRecordSize;
1437 unsigned short i;
1438
1439 sfrTable *table = sfrGetTableByTag(sfr, MVAR_TABLE_TAG);
1440 if (table == NULL)
1441 return NULL;
1442
1443 sscb->seek(sscb, table->offset);
1444
1445 /* Read and validate table version */
1446 if (table->length < MVAR_TABLE_HEADER_SIZE) {
1447 sscb->message(sscb, "invalid MVAR table size");
1448 return NULL;
1449 }
1450
1451 if (sscb->read4(sscb) != MVAR_TABLE_VERSION) {
1452 sscb->message(sscb, "invalid MVAR table version");
1453 return NULL;
1454 }
1455
1456 mvar = (var_MVAR)sscb->memNew(sscb, sizeof(*mvar));
1457 memset(mvar, 0, sizeof(*mvar));
1458
1459 mvar->axisCount = sscb->read2(sscb);
1460 valueRecordSize = sscb->read2(sscb);
1461 mvar->valueRecordCount = sscb->read2(sscb);
1462 ivsOffset = (unsigned long)sscb->read2(sscb);
1463
1464 if (ivsOffset == 0) {
1465 sscb->message(sscb, "item variation store offset in MVAR is NULL");
1466 return NULL;
1467 }
1468
1469 if (valueRecordSize < MVAR_TABLE_RECORD_SIZE) {
1470 if ((valueRecordSize > 0) || (mvar->valueRecordCount > 0)) {
1471 /* It is OK to have valueRecordSize != MVAR_TABLE_RECORD_SIZE if it is zero because there are no records. */
1472 sscb->message(sscb, "invalid MVAR record size");
1473 return NULL;
1474 }
1475 }
1476 if (table->length < MVAR_TABLE_HEADER_SIZE + (unsigned long)valueRecordSize * mvar->valueRecordCount) {
1477 sscb->message(sscb, "invalid MVAR table size");
1478 return NULL;
1479 }
1480
1481 dnaINIT(sscb->dna, mvar->values, 0, 1);
1482 if (dnaSetCnt(&mvar->values, DNA_ELEM_SIZE_(mvar->values), mvar->valueRecordCount) < 0)
1483 goto cleanup;
1484
1485 for (i = 0; i < mvar->valueRecordCount; i++) {
1486 unsigned short j;
1487
1488 mvar->values.array[i].valueTag = sscb->read4(sscb);
1489 mvar->values.array[i].pair.outerIndex = sscb->read2(sscb);
1490 mvar->values.array[i].pair.innerIndex = sscb->read2(sscb);
1491 for (j = MVAR_TABLE_RECORD_SIZE; j < valueRecordSize; j++)
1492 (void)sscb->read1(sscb);
1493 }
1494
1495 mvar->ivs = var_loadItemVariationStore(sscb, table->offset, table->length, ivsOffset);
1496 if (mvar->ivs == NULL)
1497 goto cleanup;
1498
1499 return mvar;
1500
1501 cleanup:
1502 var_freeMVAR(sscb, mvar);
1503
1504 return NULL;
1505 }
1506
1507 int var_lookupMVAR(ctlSharedStmCallbacks *sscb, var_MVAR mvar, unsigned short axisCount, Fixed *instCoords, ctlTag tag, float *value) {
1508 long top, bot, index;
1509 mvarValueRecord *rec = NULL;
1510 int found = 0;
1511 float scalars[CFF2_MAX_MASTERS];
1512
1513 if (!mvar || !mvar->ivs) {
1514 sscb->message(sscb, "invalid MVAR table data");
1515 return 1;
1516 }
1517
1518 if (instCoords == 0 || axisCount == 0) {
1519 sscb->message(sscb, "zero instCoords/axis count specified for MVAR");
1520 return 1;
1521 }
1522
1523 bot = 0;
1524 top = (long)mvar->valueRecordCount - 1;
1525 while (bot <= top) {
1526 index = (top + bot) / 2;
1527 rec = &mvar->values.array[index];
1528 if (rec->valueTag == tag) {
1529 found = 1;
1530 break;
1531 }
1532 if (tag < rec->valueTag)
1533 top = index - 1;
1534 else
1535 bot = index + 1;
1536 }
1537
1538 if (!found) {
1539 /* Specified tag was not found. */
1540 return 1;
1541 }
1542
1543 var_calcRegionScalars(sscb, mvar->ivs, &axisCount, instCoords, scalars);
1544
1545 /* Blend the metric value using the IVS table */
1546 *value = var_applyDeltasForIndexPair(sscb, mvar->ivs, &rec->pair, scalars, mvar->ivs->regionList.regionCount);
1547
1548 return 0;
1549 }
1550
1551 void var_freeMVAR(ctlSharedStmCallbacks *sscb, var_MVAR mvar) {
1552 if (mvar) {
1553 var_freeItemVariationStore(sscb, mvar->ivs);
1554 dnaFREE(mvar->values);
1555
1556 sscb->memFree(sscb, mvar);
1557 }
1558 }
1559 /* Get version numbers of libraries. */
1560 void varreadGetVersion(ctlVersionCallbacks *cb) {
1561 if (cb->called & 1 << VAR_LIB_ID)
1562 return; /* Already enumerated */
1563
1564 /* This library */
1565 cb->getversion(cb, VARREAD_VERSION, "varread");
1566
1567 /* Record this call */
1568 cb->called |= 1 << VAR_LIB_ID;
1569 }