Debian Codegraph @

texk/web2c/mfluadir/otfcc/lib/otf-writer/stat.c

raw
1 #include "stat.h"
2
3 #include <time.h>
4 #include <float.h>
5 #include "support/util.h"
6
7 #define OTFCC_CHR(a,b,c,d) ( ((a)<<24) | ((b)<<16) | ((c)<<8) | (d) )
8
9 // Stating
10 // Calculate necessary values for SFNT
11
12 typedef enum { stat_not_started = 0, stat_doing = 1, stat_completed = 2 } stat_status;
13
14 static glyf_GlyphStat stat_single_glyph(table_glyf *table, glyf_ComponentReference *gr,
15 stat_status *stated, uint8_t depth, glyphid_t topj,
16 const otfcc_Options *options) {
17 glyf_GlyphStat stat = {0, 0, 0, 0, 0, 0, 0, 0, 0};
18 glyphid_t j = gr->glyph.index;
19 if (depth >= 0xFF) return stat;
20 if (stated[j] == stat_doing) {
21 // We have a circular reference
22 logWarning("[Stat] Circular glyph reference found in gid %d to gid %d. The reference will "
23 "be dropped.\n",
24 topj, j);
25 stated[j] = stat_completed;
26 return stat;
27 }
28
29 glyf_Glyph *g = table->items[j];
30 stated[j] = stat_doing;
31 pos_t xmin = POS_MAX;
32 pos_t xmax = -POS_MAX;
33 pos_t ymin = POS_MAX;
34 pos_t ymax = -POS_MAX;
35 uint16_t nestDepth = 0;
36 uint16_t nPoints = 0;
37 uint16_t nCompositePoints = 0;
38 uint16_t nCompositeContours = 0;
39 // Stat xmin, xmax, ymin, ymax
40 for (shapeid_t c = 0; c < g->contours.length; c++) {
41 for (shapeid_t pj = 0; pj < g->contours.items[c].length; pj++) {
42 // Stat point coordinates USING the matrix transformation
43 glyf_Point *p = &(g->contours.items[c].items[pj]);
44 pos_t x = round(iVQ.getStill(gr->x) + gr->a * iVQ.getStill(p->x) +
45 gr->b * iVQ.getStill(p->y));
46 pos_t y = round(iVQ.getStill(gr->y) + gr->c * iVQ.getStill(p->x) +
47 gr->d * iVQ.getStill(p->y));
48 if (x < xmin) xmin = x;
49 if (x > xmax) xmax = x;
50 if (y < ymin) ymin = y;
51 if (y > ymax) ymax = y;
52 nPoints += 1;
53 }
54 }
55 nCompositePoints = nPoints;
56 nCompositeContours = g->contours.length;
57 for (shapeid_t r = 0; r < g->references.length; r++) {
58 glyf_ComponentReference ref;
59 glyf_iComponentReference.init(&ref);
60 glyf_ComponentReference *rr = &(g->references.items[r]);
61 // composite affine transformations
62 Handle.replace(&ref.glyph, Handle.fromIndex(g->references.items[r].glyph.index));
63 ref.a = gr->a * rr->a + rr->b * gr->c;
64 ref.b = rr->a * gr->b + rr->b * gr->d;
65 ref.c = gr->a * rr->c + gr->c * rr->d;
66 ref.d = gr->b * rr->c + rr->d * gr->d;
67 iVQ.replace(&ref.x, iVQ.createStill(iVQ.getStill(rr->x) + rr->a * iVQ.getStill(gr->x) +
68 rr->b * iVQ.getStill(gr->y)));
69 iVQ.replace(&ref.y, iVQ.createStill(iVQ.getStill(rr->y) + rr->c * iVQ.getStill(gr->x) +
70 rr->d * iVQ.getStill(gr->y)));
71
72 glyf_GlyphStat thatstat = stat_single_glyph(table, &ref, stated, depth + 1, topj, options);
73 if (thatstat.xMin < xmin) xmin = thatstat.xMin;
74 if (thatstat.xMax > xmax) xmax = thatstat.xMax;
75 if (thatstat.yMin < ymin) ymin = thatstat.yMin;
76 if (thatstat.yMax > ymax) ymax = thatstat.yMax;
77 if (thatstat.nestDepth + 1 > nestDepth) nestDepth = thatstat.nestDepth + 1;
78 nCompositePoints += thatstat.nCompositePoints;
79 nCompositeContours += thatstat.nCompositeContours;
80 }
81 // deal with space glyphs
82 if (xmin > xmax) xmin = xmax = 0;
83 if (ymin > ymax) ymin = ymax = 0;
84
85 // write back
86 stat.xMin = xmin;
87 stat.xMax = xmax;
88 stat.yMin = ymin;
89 stat.yMax = ymax;
90 stat.nestDepth = nestDepth;
91 stat.nPoints = nPoints;
92 stat.nContours = g->contours.length;
93 stat.nCompositePoints = nCompositePoints;
94 stat.nCompositeContours = nCompositeContours;
95 stated[j] = stat_completed;
96 return stat;
97 }
98
99 static void statGlyf(otfcc_Font *font, const otfcc_Options *options) {
100 stat_status *stated;
101 NEW(stated, font->glyf->length);
102 pos_t xmin = 0xFFFFFFFF;
103 pos_t xmax = -0xFFFFFFFF;
104 pos_t ymin = 0xFFFFFFFF;
105 pos_t ymax = -0xFFFFFFFF;
106 for (glyphid_t j = 0; j < font->glyf->length; j++) {
107 glyf_ComponentReference gr;
108 gr.glyph = Handle.fromIndex(j);
109 gr.x = iVQ.createStill(0);
110 gr.y = iVQ.createStill(0);
111 gr.a = 1;
112 gr.b = 0;
113 gr.c = 0;
114 gr.d = 1;
115 glyf_GlyphStat thatstat = font->glyf->items[j]->stat =
116 stat_single_glyph(font->glyf, &gr, stated, 0, j, options);
117 if (thatstat.xMin < xmin) xmin = thatstat.xMin;
118 if (thatstat.xMax > xmax) xmax = thatstat.xMax;
119 if (thatstat.yMin < ymin) ymin = thatstat.yMin;
120 if (thatstat.yMax > ymax) ymax = thatstat.yMax;
121 }
122 font->head->xMin = xmin;
123 font->head->xMax = xmax;
124 font->head->yMin = ymin;
125 font->head->yMax = ymax;
126 FREE(stated);
127 }
128
129 static void statMaxp(otfcc_Font *font) {
130 uint16_t nestDepth = 0;
131 uint16_t nPoints = 0;
132 uint16_t nContours = 0;
133 uint16_t nComponents = 0;
134 uint16_t nCompositePoints = 0;
135 uint16_t nCompositeContours = 0;
136 uint16_t instSize = 0;
137 for (glyphid_t j = 0; j < font->glyf->length; j++) {
138 glyf_Glyph *g = font->glyf->items[j];
139 if (g->contours.length > 0) {
140 if (g->stat.nPoints > nPoints) nPoints = g->stat.nPoints;
141 if (g->stat.nContours > nContours) nContours = g->stat.nContours;
142 } else if (g->references.length > 0) {
143 if (g->stat.nCompositePoints > nCompositePoints)
144 nCompositePoints = g->stat.nCompositePoints;
145 if (g->stat.nCompositeContours > nCompositeContours)
146 nCompositeContours = g->stat.nCompositeContours;
147 if (g->stat.nestDepth > nestDepth) nestDepth = g->stat.nestDepth;
148 if (g->references.length > nComponents) nComponents = g->references.length;
149 }
150 if (g->instructionsLength > instSize) instSize = g->instructionsLength;
151 }
152 font->maxp->maxPoints = nPoints;
153 font->maxp->maxContours = nContours;
154 font->maxp->maxCompositePoints = nCompositePoints;
155 font->maxp->maxCompositeContours = nCompositeContours;
156 font->maxp->maxComponentDepth = nestDepth;
157 font->maxp->maxComponentElements = nComponents;
158 font->maxp->maxSizeOfInstructions = instSize;
159 }
160
161 static void statHmtx(otfcc_Font *font, const otfcc_Options *options) {
162 if (!font->glyf) return;
163 table_hmtx *hmtx;
164 NEW(hmtx);
165 glyphid_t count_a = font->glyf->length;
166 glyphid_t count_k = 0;
167 bool lsbAtX_0 = true;
168 if (font->subtype == FONTTYPE_CFF) {
169 // pass
170 } else {
171 while (count_a > 2 && iVQ.getStill(font->glyf->items[count_a - 1]->advanceWidth) ==
172 iVQ.getStill(font->glyf->items[count_a - 2]->advanceWidth)) {
173 count_a--;
174 }
175 count_k = font->glyf->length - count_a;
176 }
177 NEW(hmtx->metrics, count_a);
178 NEW(hmtx->leftSideBearing, count_k);
179
180 pos_t minLSB = 0x7FFF;
181 pos_t minRSB = 0x7FFF;
182 pos_t maxExtent = -0x8000;
183 length_t maxWidth = 0;
184 for (glyphid_t j = 0; j < font->glyf->length; j++) {
185 glyf_Glyph *g = font->glyf->items[j];
186 if (iVQ.isZero(g->horizontalOrigin, 1.0 / 1000.0)) {
187 iVQ.replace(&g->horizontalOrigin, iVQ.neutral());
188 } else {
189 lsbAtX_0 = false;
190 }
191 const pos_t hori = iVQ.getStill(g->horizontalOrigin);
192 const pos_t advw = iVQ.getStill(g->advanceWidth);
193 const pos_t lsb = (g->stat.xMin) - hori;
194 const pos_t rsb = advw + hori - (g->stat.xMax);
195
196 if (j < count_a) {
197 hmtx->metrics[j].advanceWidth = advw;
198 hmtx->metrics[j].lsb = lsb;
199 } else {
200 hmtx->leftSideBearing[j - count_a] = lsb;
201 }
202
203 if (advw > maxWidth) maxWidth = advw;
204 if (lsb < minLSB) minLSB = lsb;
205 if (rsb < minRSB) minRSB = rsb;
206 if (g->stat.xMax - hori > maxExtent) { maxExtent = g->stat.xMax - hori; }
207 }
208 font->hhea->numberOfMetrics = count_a;
209 font->hhea->minLeftSideBearing = minLSB;
210 font->hhea->minRightSideBearing = minRSB;
211 font->hhea->xMaxExtent = maxExtent;
212 font->hhea->advanceWidthMax = maxWidth;
213 font->hmtx = hmtx;
214 // set bit 1 in head.flags
215 font->head->flags = (font->head->flags & (~0x2)) | (lsbAtX_0 ? 0x2 : 0);
216 }
217 static void statVmtx(otfcc_Font *font, const otfcc_Options *options) {
218 if (!font->glyf) return;
219 table_vmtx *vmtx;
220 NEW(vmtx);
221 glyphid_t count_a = font->glyf->length;
222 glyphid_t count_k = 0;
223 if (font->subtype == FONTTYPE_CFF && !options->cff_short_vmtx) {
224 // pass
225 } else {
226 while (count_a > 2 && iVQ.getStill(font->glyf->items[count_a - 1]->advanceHeight) ==
227 iVQ.getStill(font->glyf->items[count_a - 2]->advanceHeight)) {
228 count_a--;
229 }
230 count_k = font->glyf->length - count_a;
231 }
232 NEW(vmtx->metrics, count_a);
233 NEW(vmtx->topSideBearing, count_k);
234
235 pos_t minTSB = 0x7FFF;
236 pos_t minBSB = 0x7FFF;
237 pos_t maxExtent = -0x8000;
238 length_t maxHeight = 0;
239 for (glyphid_t j = 0; j < font->glyf->length; j++) {
240 glyf_Glyph *g = font->glyf->items[j];
241 const pos_t vori = iVQ.getStill(g->verticalOrigin);
242 const pos_t advh = iVQ.getStill(g->advanceHeight);
243 const pos_t tsb = (vori) - (g->stat.yMax);
244 const pos_t bsb = (g->stat.yMin) - (vori) + (advh);
245 if (j < count_a) {
246 vmtx->metrics[j].advanceHeight = advh;
247 vmtx->metrics[j].tsb = tsb;
248 } else {
249 vmtx->topSideBearing[j - count_a] = tsb;
250 }
251 if (advh > maxHeight) maxHeight = advh;
252 if (tsb < minTSB) minTSB = tsb;
253 if (bsb < minBSB) minBSB = bsb;
254 if (vori - g->stat.yMin > maxExtent) { maxExtent = vori - g->stat.yMin; }
255 }
256 font->vhea->numOfLongVerMetrics = count_a;
257 font->vhea->minTop = minTSB;
258 font->vhea->minBottom = minBSB;
259 font->vhea->yMaxExtent = maxExtent;
260 font->vhea->advanceHeightMax = maxHeight;
261 font->vmtx = vmtx;
262 }
263 static void statOS_2UnicodeRanges(otfcc_Font *font, const otfcc_Options *options) {
264 cmap_Entry *item;
265 // Stat for OS/2.ulUnicodeRange.
266 uint32_t u1 = 0;
267 uint32_t u2 = 0;
268 uint32_t u3 = 0;
269 uint32_t u4 = 0;
270 int32_t minUnicode = 0xFFFF;
271 int32_t maxUnicode = 0;
272 foreach_hash(item, font->cmap->unicodes) {
273 int u = item->unicode;
274 // Stat for minimium and maximium unicode
275 if (u < minUnicode) minUnicode = u;
276 if (u > maxUnicode) maxUnicode = u;
277 // Reference: https://www.microsoft.com/typography/otspec/os2.htm#ur
278 if ((u >= 0x0000 && u <= 0x007F)) { u1 |= (1 << 0); }
279 if ((u >= 0x0080 && u <= 0x00FF)) { u1 |= (1 << 1); }
280 if ((u >= 0x0100 && u <= 0x017F)) { u1 |= (1 << 2); }
281 if ((u >= 0x0180 && u <= 0x024F)) { u1 |= (1 << 3); }
282 if ((u >= 0x0250 && u <= 0x02AF) || (u >= 0x1D00 && u <= 0x1D7F) ||
283 (u >= 0x1D80 && u <= 0x1DBF)) {
284 u1 |= (1 << 4);
285 }
286 if ((u >= 0x02B0 && u <= 0x02FF) || (u >= 0xA700 && u <= 0xA71F)) { u1 |= (1 << 5); }
287 if ((u >= 0x0300 && u <= 0x036F) || (u >= 0x1DC0 && u <= 0x1DFF)) { u1 |= (1 << 6); }
288 if ((u >= 0x0370 && u <= 0x03FF)) { u1 |= (1 << 7); }
289 if ((u >= 0x2C80 && u <= 0x2CFF)) { u1 |= (1 << 8); }
290 if ((u >= 0x0400 && u <= 0x04FF) || (u >= 0x0500 && u <= 0x052F) ||
291 (u >= 0x2DE0 && u <= 0x2DFF) || (u >= 0xA640 && u <= 0xA69F)) {
292 u1 |= (1 << 9);
293 }
294 if ((u >= 0x0530 && u <= 0x058F)) { u1 |= (1 << 10); }
295 if ((u >= 0x0590 && u <= 0x05FF)) { u1 |= (1 << 11); }
296 if ((u >= 0xA500 && u <= 0xA63F)) { u1 |= (1 << 12); }
297 if ((u >= 0x0600 && u <= 0x06FF) || (u >= 0x0750 && u <= 0x077F)) { u1 |= (1 << 13); }
298 if ((u >= 0x07C0 && u <= 0x07FF)) { u1 |= (1 << 14); }
299 if ((u >= 0x0900 && u <= 0x097F)) { u1 |= (1 << 15); }
300 if ((u >= 0x0980 && u <= 0x09FF)) { u1 |= (1 << 16); }
301 if ((u >= 0x0A00 && u <= 0x0A7F)) { u1 |= (1 << 17); }
302 if ((u >= 0x0A80 && u <= 0x0AFF)) { u1 |= (1 << 18); }
303 if ((u >= 0x0B00 && u <= 0x0B7F)) { u1 |= (1 << 19); }
304 if ((u >= 0x0B80 && u <= 0x0BFF)) { u1 |= (1 << 20); }
305 if ((u >= 0x0C00 && u <= 0x0C7F)) { u1 |= (1 << 21); }
306 if ((u >= 0x0C80 && u <= 0x0CFF)) { u1 |= (1 << 22); }
307 if ((u >= 0x0D00 && u <= 0x0D7F)) { u1 |= (1 << 23); }
308 if ((u >= 0x0E00 && u <= 0x0E7F)) { u1 |= (1 << 24); }
309 if ((u >= 0x0E80 && u <= 0x0EFF)) { u1 |= (1 << 25); }
310 if ((u >= 0x10A0 && u <= 0x10FF) || (u >= 0x2D00 && u <= 0x2D2F)) { u1 |= (1 << 26); }
311 if ((u >= 0x1B00 && u <= 0x1B7F)) { u1 |= (1 << 27); }
312 if ((u >= 0x1100 && u <= 0x11FF)) { u1 |= (1 << 28); }
313 if ((u >= 0x1E00 && u <= 0x1EFF) || (u >= 0x2C60 && u <= 0x2C7F) ||
314 (u >= 0xA720 && u <= 0xA7FF)) {
315 u1 |= (1 << 29);
316 }
317 if ((u >= 0x1F00 && u <= 0x1FFF)) { u1 |= (1 << 30); }
318 if ((u >= 0x2000 && u <= 0x206F) || (u >= 0x2E00 && u <= 0x2E7F)) { u1 |= (1 << 31); }
319 if ((u >= 0x2070 && u <= 0x209F)) { u2 |= (1 << 0); }
320 if ((u >= 0x20A0 && u <= 0x20CF)) { u2 |= (1 << 1); }
321 if ((u >= 0x20D0 && u <= 0x20FF)) { u2 |= (1 << 2); }
322 if ((u >= 0x2100 && u <= 0x214F)) { u2 |= (1 << 3); }
323 if ((u >= 0x2150 && u <= 0x218F)) { u2 |= (1 << 4); }
324 if ((u >= 0x2190 && u <= 0x21FF) || (u >= 0x27F0 && u <= 0x27FF) ||
325 (u >= 0x2900 && u <= 0x297F) || (u >= 0x2B00 && u <= 0x2BFF)) {
326 u2 |= (1 << 5);
327 }
328 if ((u >= 0x2200 && u <= 0x22FF) || (u >= 0x2A00 && u <= 0x2AFF) ||
329 (u >= 0x27C0 && u <= 0x27EF) || (u >= 0x2980 && u <= 0x29FF)) {
330 u2 |= (1 << 6);
331 }
332 if ((u >= 0x2300 && u <= 0x23FF)) { u2 |= (1 << 7); }
333 if ((u >= 0x2400 && u <= 0x243F)) { u2 |= (1 << 8); }
334 if ((u >= 0x2440 && u <= 0x245F)) { u2 |= (1 << 9); }
335 if ((u >= 0x2460 && u <= 0x24FF)) { u2 |= (1 << 10); }
336 if ((u >= 0x2500 && u <= 0x257F)) { u2 |= (1 << 11); }
337 if ((u >= 0x2580 && u <= 0x259F)) { u2 |= (1 << 12); }
338 if ((u >= 0x25A0 && u <= 0x25FF)) { u2 |= (1 << 13); }
339 if ((u >= 0x2600 && u <= 0x26FF)) { u2 |= (1 << 14); }
340 if ((u >= 0x2700 && u <= 0x27BF)) { u2 |= (1 << 15); }
341 if ((u >= 0x3000 && u <= 0x303F)) { u2 |= (1 << 16); }
342 if ((u >= 0x3040 && u <= 0x309F)) { u2 |= (1 << 17); }
343 if ((u >= 0x30A0 && u <= 0x30FF) || (u >= 0x31F0 && u <= 0x31FF)) { u2 |= (1 << 18); }
344 if ((u >= 0x3100 && u <= 0x312F) || (u >= 0x31A0 && u <= 0x31BF)) { u2 |= (1 << 19); }
345 if ((u >= 0x3130 && u <= 0x318F)) { u2 |= (1 << 20); }
346 if ((u >= 0xA840 && u <= 0xA87F)) { u2 |= (1 << 21); }
347 if ((u >= 0x3200 && u <= 0x32FF)) { u2 |= (1 << 22); }
348 if ((u >= 0x3300 && u <= 0x33FF)) { u2 |= (1 << 23); }
349 if ((u >= 0xAC00 && u <= 0xD7AF)) { u2 |= (1 << 24); }
350 if ((u >= 0xD800 && u <= 0xDFFF) || u > 0xFFFF) { u2 |= (1 << 25); }
351 if ((u >= 0x10900 && u <= 0x1091F)) { u2 |= (1 << 26); }
352 if ((u >= 0x4E00 && u <= 0x9FFF) || (u >= 0x2E80 && u <= 0x2EFF) ||
353 (u >= 0x2F00 && u <= 0x2FDF) || (u >= 0x2FF0 && u <= 0x2FFF) ||
354 (u >= 0x3400 && u <= 0x4DBF) || (u >= 0x20000 && u <= 0x2F7FF) ||
355 (u >= 0x3190 && u <= 0x319F)) {
356 u2 |= (1 << 27);
357 }
358 if ((u >= 0xE000 && u <= 0xF8FF)) { u2 |= (1 << 28); }
359 if ((u >= 0x31C0 && u <= 0x31EF) || (u >= 0xF900 && u <= 0xFAFF) ||
360 (u >= 0x2F800 && u <= 0x2FA1F)) {
361 u2 |= (1 << 29);
362 }
363 if ((u >= 0xFB00 && u <= 0xFB4F)) { u2 |= (1 << 30); }
364 if ((u >= 0xFB50 && u <= 0xFDFF)) { u2 |= (1 << 31); }
365 if ((u >= 0xFE20 && u <= 0xFE2F)) { u3 |= (1 << 0); }
366 if ((u >= 0xFE10 && u <= 0xFE1F) || (u >= 0xFE30 && u <= 0xFE4F)) { u3 |= (1 << 1); }
367 if ((u >= 0xFE50 && u <= 0xFE6F)) { u3 |= (1 << 2); }
368 if ((u >= 0xFE70 && u <= 0xFEFF)) { u3 |= (1 << 3); }
369 if ((u >= 0xFF00 && u <= 0xFFEF)) { u3 |= (1 << 4); }
370 if ((u >= 0xFFF0 && u <= 0xFFFF)) { u3 |= (1 << 5); }
371 if ((u >= 0x0F00 && u <= 0x0FFF)) { u3 |= (1 << 6); }
372 if ((u >= 0x0700 && u <= 0x074F)) { u3 |= (1 << 7); }
373 if ((u >= 0x0780 && u <= 0x07BF)) { u3 |= (1 << 8); }
374 if ((u >= 0x0D80 && u <= 0x0DFF)) { u3 |= (1 << 9); }
375 if ((u >= 0x1000 && u <= 0x109F)) { u3 |= (1 << 10); }
376 if ((u >= 0x1200 && u <= 0x137F) || (u >= 0x1380 && u <= 0x139F) ||
377 (u >= 0x2D80 && u <= 0x2DDF)) {
378 u3 |= (1 << 11);
379 }
380 if ((u >= 0x13A0 && u <= 0x13FF)) { u3 |= (1 << 12); }
381 if ((u >= 0x1400 && u <= 0x167F)) { u3 |= (1 << 13); }
382 if ((u >= 0x1680 && u <= 0x169F)) { u3 |= (1 << 14); }
383 if ((u >= 0x16A0 && u <= 0x16FF)) { u3 |= (1 << 15); }
384 if ((u >= 0x1780 && u <= 0x17FF) || (u >= 0x19E0 && u <= 0x19FF)) { u3 |= (1 << 16); }
385 if ((u >= 0x1800 && u <= 0x18AF)) { u3 |= (1 << 17); }
386 if ((u >= 0x2800 && u <= 0x28FF)) { u3 |= (1 << 18); }
387 if ((u >= 0xA000 && u <= 0xA48F) || (u >= 0xA490 && u <= 0xA4CF)) { u3 |= (1 << 19); }
388 if ((u >= 0x1700 && u <= 0x171F) || (u >= 0x1720 && u <= 0x173F) ||
389 (u >= 0x1740 && u <= 0x175F) || (u >= 0x1760 && u <= 0x177F)) {
390 u3 |= (1 << 20);
391 }
392 if ((u >= 0x10300 && u <= 0x1032F)) { u3 |= (1 << 21); }
393 if ((u >= 0x10330 && u <= 0x1034F)) { u3 |= (1 << 22); }
394 if ((u >= 0x10400 && u <= 0x1044F)) { u3 |= (1 << 23); }
395 if ((u >= 0x1D000 && u <= 0x1D0FF) || (u >= 0x1D100 && u <= 0x1D1FF) ||
396 (u >= 0x1D200 && u <= 0x1D24F)) {
397 u3 |= (1 << 24);
398 }
399 if ((u >= 0x1D400 && u <= 0x1D7FF)) { u3 |= (1 << 25); }
400 if ((u >= 0xFF000 && u <= 0xFFFFD) || (u >= 0x100000 && u <= 0x10FFFD)) { u3 |= (1 << 26); }
401 if ((u >= 0xFE00 && u <= 0xFE0F) || (u >= 0xE0100 && u <= 0xE01EF)) { u3 |= (1 << 27); }
402 if ((u >= 0xE0000 && u <= 0xE007F)) { u3 |= (1 << 28); }
403 if ((u >= 0x1900 && u <= 0x194F)) { u3 |= (1 << 29); }
404 if ((u >= 0x1950 && u <= 0x197F)) { u3 |= (1 << 30); }
405 if ((u >= 0x1980 && u <= 0x19DF)) { u3 |= (1 << 31); }
406 if ((u >= 0x1A00 && u <= 0x1A1F)) { u4 |= (1 << 0); }
407 if ((u >= 0x2C00 && u <= 0x2C5F)) { u4 |= (1 << 1); }
408 if ((u >= 0x2D30 && u <= 0x2D7F)) { u4 |= (1 << 2); }
409 if ((u >= 0x4DC0 && u <= 0x4DFF)) { u4 |= (1 << 3); }
410 if ((u >= 0xA800 && u <= 0xA82F)) { u4 |= (1 << 4); }
411 if ((u >= 0x10000 && u <= 0x1007F) || (u >= 0x10080 && u <= 0x100FF) ||
412 (u >= 0x10100 && u <= 0x1013F)) {
413 u4 |= (1 << 5);
414 }
415 if ((u >= 0x10140 && u <= 0x1018F)) { u4 |= (1 << 6); }
416 if ((u >= 0x10380 && u <= 0x1039F)) { u4 |= (1 << 7); }
417 if ((u >= 0x103A0 && u <= 0x103DF)) { u4 |= (1 << 8); }
418 if ((u >= 0x10450 && u <= 0x1047F)) { u4 |= (1 << 9); }
419 if ((u >= 0x10480 && u <= 0x104AF)) { u4 |= (1 << 10); }
420 if ((u >= 0x10800 && u <= 0x1083F)) { u4 |= (1 << 11); }
421 if ((u >= 0x10A00 && u <= 0x10A5F)) { u4 |= (1 << 12); }
422 if ((u >= 0x1D300 && u <= 0x1D35F)) { u4 |= (1 << 13); }
423 if ((u >= 0x12000 && u <= 0x123FF) || (u >= 0x12400 && u <= 0x1247F)) { u4 |= (1 << 14); }
424 if ((u >= 0x1D360 && u <= 0x1D37F)) { u4 |= (1 << 15); }
425 if ((u >= 0x1B80 && u <= 0x1BBF)) { u4 |= (1 << 16); }
426 if ((u >= 0x1C00 && u <= 0x1C4F)) { u4 |= (1 << 17); }
427 if ((u >= 0x1C50 && u <= 0x1C7F)) { u4 |= (1 << 18); }
428 if ((u >= 0xA880 && u <= 0xA8DF)) { u4 |= (1 << 19); }
429 if ((u >= 0xA900 && u <= 0xA92F)) { u4 |= (1 << 20); }
430 if ((u >= 0xA930 && u <= 0xA95F)) { u4 |= (1 << 21); }
431 if ((u >= 0xAA00 && u <= 0xAA5F)) { u4 |= (1 << 22); }
432 if ((u >= 0x10190 && u <= 0x101CF)) { u4 |= (1 << 23); }
433 if ((u >= 0x101D0 && u <= 0x101FF)) { u4 |= (1 << 24); }
434 if ((u >= 0x102A0 && u <= 0x102DF) || (u >= 0x10280 && u <= 0x1029F) ||
435 (u >= 0x10920 && u <= 0x1093F)) {
436 u4 |= (1 << 25);
437 }
438 if ((u >= 0x1F030 && u <= 0x1F09F) || (u >= 0x1F000 && u <= 0x1F02F)) { u4 |= (1 << 26); }
439 }
440 if (!options->keep_unicode_ranges) {
441 font->OS_2->ulUnicodeRange1 = u1;
442 font->OS_2->ulUnicodeRange2 = u2;
443 font->OS_2->ulUnicodeRange3 = u3;
444 font->OS_2->ulUnicodeRange4 = u4;
445 }
446 if (minUnicode < 0x10000) {
447 font->OS_2->usFirstCharIndex = minUnicode;
448 } else {
449 font->OS_2->usFirstCharIndex = 0xFFFF;
450 }
451 if (maxUnicode < 0x10000) {
452 font->OS_2->usLastCharIndex = maxUnicode;
453 } else {
454 font->OS_2->usLastCharIndex = 0xFFFF;
455 }
456 }
457 static void statOS_2AverageWidth(otfcc_Font *font, const otfcc_Options *options) {
458 if (options->keep_average_char_width) return;
459 uint32_t totalWidth = 0;
460 for (glyphid_t j = 0; j < font->glyf->length; j++) {
461 const pos_t adw = iVQ.getStill(font->glyf->items[j]->advanceWidth);
462 if (adw > 0) { totalWidth += adw; }
463 }
464 font->OS_2->xAvgCharWidth = totalWidth / font->glyf->length;
465 }
466 static uint16_t statMaxContextOTL(const table_OTL *table) {
467 uint16_t maxc = 1;
468 foreach (otl_Lookup **lookup_, table->lookups) {
469 otl_Lookup *lookup = *lookup_;
470 switch (lookup->type) {
471 case otl_type_gpos_pair:
472 case otl_type_gpos_markToBase:
473 case otl_type_gpos_markToLigature:
474 case otl_type_gpos_markToMark:
475 if (maxc < 2) maxc = 2;
476 break;
477 case otl_type_gsub_ligature:
478 foreach (otl_Subtable **subtable_, lookup->subtables) {
479 subtable_gsub_ligature *subtable = (subtable_gsub_ligature *)*subtable_;
480 foreach (otl_GsubLigatureEntry *entry, *subtable) {
481 if (maxc < entry->from->numGlyphs) { maxc = entry->from->numGlyphs; }
482 };
483 }
484 break;
485 case otl_type_gsub_chaining:
486 case otl_type_gpos_chaining:
487 foreach (otl_Subtable **subtable_, lookup->subtables) {
488 subtable_chaining *subtable = (subtable_chaining *)*subtable_;
489 if (maxc < subtable->rule.matchCount) maxc = subtable->rule.matchCount;
490 }
491 break;
492 case otl_type_gsub_reverse:
493 foreach (otl_Subtable **subtable_, lookup->subtables) {
494 subtable_gsub_reverse *subtable = (subtable_gsub_reverse *)*subtable_;
495 if (maxc < subtable->matchCount) maxc = subtable->matchCount;
496 }
497 break;
498 default:;
499 }
500 }
501 return maxc;
502 }
503 static void statMaxContext(otfcc_Font *font, const otfcc_Options *options) {
504 uint16_t maxc = 1;
505 if (font->GSUB) {
506 uint16_t maxc_gsub = statMaxContextOTL(font->GSUB);
507 if (maxc_gsub > maxc) maxc = maxc_gsub;
508 }
509 if (font->GPOS) {
510 uint16_t maxc_gpos = statMaxContextOTL(font->GPOS);
511 if (maxc_gpos > maxc) maxc = maxc_gpos;
512 }
513 font->OS_2->usMaxContext = maxc;
514 }
515 static void statOS_2(otfcc_Font *font, const otfcc_Options *options) {
516 statOS_2UnicodeRanges(font, options);
517 statOS_2AverageWidth(font, options);
518 statMaxContext(font, options);
519 }
520
521 #define MAX_STAT_METRIC 4096
522 static void statCFFWidths(otfcc_Font *font) {
523 if (!font->glyf || !font->CFF_) return;
524 // Stat the most frequent character width
525 uint32_t *frequency;
526 NEW(frequency, MAX_STAT_METRIC);
527 for (glyphid_t j = 0; j < font->glyf->length; j++) {
528 uint16_t intWidth = (uint16_t)iVQ.getStill(font->glyf->items[j]->advanceWidth);
529 if (intWidth < MAX_STAT_METRIC) { frequency[intWidth] += 1; }
530 }
531 uint16_t maxfreq = 0;
532 uint16_t maxj = 0;
533 for (uint16_t j = 0; j < MAX_STAT_METRIC; j++) {
534 if (frequency[j] > maxfreq) {
535 maxfreq = frequency[j];
536 maxj = j;
537 }
538 }
539 // stat nominalWidthX
540 uint16_t nn = 0;
541 uint32_t nnsum = 0;
542 for (glyphid_t j = 0; j < font->glyf->length; j++) {
543 const pos_t adw = iVQ.getStill(font->glyf->items[j]->advanceWidth);
544 if (adw != maxj) { nn += 1, nnsum += adw; }
545 }
546 int16_t nominalWidthX = 0;
547 if (nn > 0) nominalWidthX = nnsum / nn;
548 if (font->CFF_->privateDict) {
549 font->CFF_->privateDict->defaultWidthX = maxj;
550 if (nn != 0) { font->CFF_->privateDict->nominalWidthX = nominalWidthX; }
551 }
552 if (font->CFF_->fdArray) {
553 for (tableid_t j = 0; j < font->CFF_->fdArrayCount; j++) {
554 font->CFF_->fdArray[j]->privateDict->defaultWidthX = maxj;
555 font->CFF_->fdArray[j]->privateDict->nominalWidthX = nominalWidthX;
556 }
557 }
558 FREE(frequency);
559 }
560
561 static void statVORG(otfcc_Font *font) {
562 if (!font->glyf || !font->CFF_ || !font->vhea || !font->vmtx) return;
563 uint32_t *frequency;
564 NEW(frequency, MAX_STAT_METRIC);
565 for (glyphid_t j = 0; j < font->glyf->length; j++) {
566 const pos_t vori = iVQ.getStill(font->glyf->items[j]->verticalOrigin);
567 if (vori >= 0 && vori < MAX_STAT_METRIC) { frequency[(uint16_t)(vori)] += 1; }
568 }
569 // stat VORG.defaultVerticalOrigin
570 uint32_t maxfreq = 0;
571 glyphid_t maxj = 0;
572 for (glyphid_t j = 0; j < MAX_STAT_METRIC; j++) {
573 if (frequency[j] > maxfreq) {
574 maxfreq = frequency[j];
575 maxj = j;
576 }
577 }
578
579 table_VORG *vorg;
580 NEW(vorg);
581 vorg->defaultVerticalOrigin = maxj;
582
583 glyphid_t nVertOrigs = 0;
584 for (glyphid_t j = 0; j < font->glyf->length; j++) {
585 const pos_t vori = iVQ.getStill(font->glyf->items[j]->verticalOrigin);
586 if (vori != maxj) { nVertOrigs += 1; }
587 }
588 vorg->numVertOriginYMetrics = nVertOrigs;
589 NEW(vorg->entries, nVertOrigs);
590
591 glyphid_t jj = 0;
592 for (glyphid_t j = 0; j < font->glyf->length; j++) {
593 const pos_t vori = iVQ.getStill(font->glyf->items[j]->verticalOrigin);
594 if (vori != maxj) {
595 vorg->entries[jj].gid = j;
596 vorg->entries[jj].verticalOrigin = vori;
597 jj += 1;
598 }
599 }
600 FREE(frequency);
601 font->VORG = vorg;
602 }
603
604 static void statLTSH(otfcc_Font *font) {
605 if (!font->glyf) return;
606 bool needLTSH = false;
607 for (glyphid_t j = 0; j < font->glyf->length; j++)
608 if (font->glyf->items[j]->yPel > 1) { needLTSH = true; }
609 if (!needLTSH) return;
610
611 table_LTSH *ltsh;
612 NEW(ltsh);
613 ltsh->numGlyphs = font->glyf->length;
614 NEW(ltsh->yPels, ltsh->numGlyphs);
615 for (glyphid_t j = 0; j < font->glyf->length; j++) {
616 ltsh->yPels[j] = font->glyf->items[j]->yPel;
617 }
618 font->LTSH = ltsh;
619 }
620
621 void otfcc_statFont(otfcc_Font *font, const otfcc_Options *options) {
622 if (font->glyf && font->head) {
623 statGlyf(font, options);
624 if (!options->keep_modified_time) {
625 font->head->modified = 2082844800 + (int64_t)time(NULL);
626 }
627 }
628 if (font->head && font->CFF_) {
629 table_CFF *cff = font->CFF_;
630 if (cff->fontBBoxBottom > font->head->yMin) cff->fontBBoxBottom = font->head->yMin;
631 if (cff->fontBBoxTop < font->head->yMax) cff->fontBBoxTop = font->head->yMax;
632 if (cff->fontBBoxLeft < font->head->xMin) cff->fontBBoxLeft = font->head->xMin;
633 if (cff->fontBBoxRight < font->head->xMax) cff->fontBBoxRight = font->head->xMax;
634 if (font->glyf && cff->isCID) { cff->cidCount = (uint32_t)font->glyf->length; }
635 if (cff->isCID) {
636 if (cff->fontMatrix) {
637 iVQ.dispose(&cff->fontMatrix->x);
638 iVQ.dispose(&cff->fontMatrix->y);
639 FREE(cff->fontMatrix);
640 cff->fontMatrix = NULL;
641 }
642 for (tableid_t j = 0; j < cff->fdArrayCount; j++) {
643 table_CFF *fd = cff->fdArray[j];
644 if (fd->fontMatrix) {
645 iVQ.dispose(&fd->fontMatrix->x);
646 iVQ.dispose(&fd->fontMatrix->y);
647 FREE(fd->fontMatrix);
648 fd->fontMatrix = NULL;
649 }
650 if (font->head->unitsPerEm == 1000) {
651 fd->fontMatrix = NULL;
652 } else {
653 NEW(fd->fontMatrix);
654 fd->fontMatrix->a = 1.0 / font->head->unitsPerEm;
655 fd->fontMatrix->b = 0.0;
656 fd->fontMatrix->c = 0.0;
657 fd->fontMatrix->d = 1.0 / font->head->unitsPerEm;
658 fd->fontMatrix->x = iVQ.neutral();
659 fd->fontMatrix->y = iVQ.neutral();
660 }
661 }
662 } else {
663 if (font->head->unitsPerEm == 1000) {
664 cff->fontMatrix = NULL;
665 } else {
666 NEW(cff->fontMatrix);
667 cff->fontMatrix->a = 1.0 / font->head->unitsPerEm;
668 cff->fontMatrix->b = 0.0;
669 cff->fontMatrix->c = 0.0;
670 cff->fontMatrix->d = 1.0 / font->head->unitsPerEm;
671 cff->fontMatrix->x = iVQ.neutral();
672 cff->fontMatrix->y = iVQ.neutral();
673 }
674 }
675
676 statCFFWidths(font);
677 }
678 if (font->glyf && font->maxp) { font->maxp->numGlyphs = (uint16_t)font->glyf->length; }
679 if (font->glyf && font->post) { font->post->maxMemType42 = (uint32_t)font->glyf->length; }
680 if (font->glyf && font->maxp && font->maxp->version == 0x10000) {
681 statMaxp(font);
682 if (font->fpgm && font->fpgm->length > font->maxp->maxSizeOfInstructions) {
683 font->maxp->maxSizeOfInstructions = font->fpgm->length;
684 }
685 if (font->prep && font->prep->length > font->maxp->maxSizeOfInstructions) {
686 font->maxp->maxSizeOfInstructions = font->prep->length;
687 }
688 }
689 if (font->OS_2 && font->cmap && font->glyf) statOS_2(font, options);
690 if (font->subtype == FONTTYPE_TTF) {
691 if (font->maxp) font->maxp->version = 0x00010000;
692 } else {
693 if (font->maxp) font->maxp->version = 0x00005000;
694 }
695 if (font->glyf && font->hhea) { statHmtx(font, options); }
696 if (font->glyf && font->vhea) { statVmtx(font, options), statVORG(font); }
697 statLTSH(font);
698 }
699
700 void otfcc_unstatFont(otfcc_Font *font, const otfcc_Options *options) {
701 otfcc_iFont.deleteTable(font, OTFCC_CHR('h','d','m','x'));
702 otfcc_iFont.deleteTable(font, OTFCC_CHR('h','m','t','x'));
703 otfcc_iFont.deleteTable(font, OTFCC_CHR('V','O','R','G'));
704 otfcc_iFont.deleteTable(font, OTFCC_CHR('v','m','t','x'));
705 otfcc_iFont.deleteTable(font, OTFCC_CHR('L','T','S','H'));
706 }