@mark1russell7/typeset 0.1.2 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -123,6 +123,27 @@ function coverageIndex(table, glyph) {
123
123
  }
124
124
  throw new Error(`the Coverage format ${format} is unknown`);
125
125
  }
126
+ /** This function gives each glyph of a Coverage table with its coverage index, in the order of the table. */
127
+ function coverageGlyphs(table) {
128
+ const format = table.u16(0);
129
+ const count = table.u16(2);
130
+ const glyphs = [];
131
+ if (format === 1) {
132
+ for (let k = 0; k < count; k++) glyphs.push([table.u16(4 + k * 2), k]);
133
+ return glyphs;
134
+ }
135
+ if (format === 2) {
136
+ for (let k = 0; k < count; k++) {
137
+ const at = 4 + k * 6;
138
+ const start = table.u16(at);
139
+ const end = table.u16(at + 2);
140
+ const index = table.u16(at + 4);
141
+ for (let glyph = start; glyph <= end; glyph++) glyphs.push([glyph, index + glyph - start]);
142
+ }
143
+ return glyphs;
144
+ }
145
+ throw new Error(`the Coverage format ${format} is unknown`);
146
+ }
126
147
  /** The GDEF glyph classes: 1 base, 2 ligature, 3 mark, 4 component. */
127
148
  function readGlyphClasses(gdef) {
128
149
  if (gdef === void 0) return () => 0;
@@ -168,7 +189,7 @@ function pairValue(first, second, skipSecond) {
168
189
  skipSecond
169
190
  };
170
191
  }
171
- /** This function makes the pair function of one PairPos subtable. */
192
+ /** This function makes the functions of one PairPos subtable. */
172
193
  function pairSubtable(table, store) {
173
194
  const format = table.u16(0);
174
195
  const coverage = table.part(table.u16(2));
@@ -177,28 +198,53 @@ function pairSubtable(table, store) {
177
198
  const size1 = valueSize(format1);
178
199
  const size2 = valueSize(format2);
179
200
  const skipSecond = format2 !== 0;
180
- if (format === 1) return (first, second) => {
181
- const index = coverageIndex(coverage, first);
182
- if (index < 0) return void 0;
183
- const set = table.part(table.u16(10 + index * 2));
201
+ if (format === 1) {
184
202
  const recordSize = 2 + size1 + size2;
185
- let low = 0;
186
- let high = set.u16(0) - 1;
187
- while (low <= high) {
188
- const middle = low + high >> 1;
189
- const at = 2 + middle * recordSize;
190
- const glyph = set.u16(at);
191
- if (second < glyph) high = middle - 1;
192
- else if (second > glyph) low = middle + 1;
193
- else return pairValue(xAdvance(set, at + 2, format1, set, store), xAdvance(set, at + 2 + size1, format2, set, store), skipSecond);
194
- }
195
- };
203
+ const pair = (first, second) => {
204
+ const index = coverageIndex(coverage, first);
205
+ if (index < 0) return void 0;
206
+ const set = table.part(table.u16(10 + index * 2));
207
+ let low = 0;
208
+ let high = set.u16(0) - 1;
209
+ while (low <= high) {
210
+ const middle = low + high >> 1;
211
+ const at = 2 + middle * recordSize;
212
+ const glyph = set.u16(at);
213
+ if (second < glyph) high = middle - 1;
214
+ else if (second > glyph) low = middle + 1;
215
+ else return pairValue(xAdvance(set, at + 2, format1, set, store), xAdvance(set, at + 2 + size1, format2, set, store), skipSecond);
216
+ }
217
+ };
218
+ const largest = (glyphs) => {
219
+ let first = 0;
220
+ let second = 0;
221
+ for (const glyph of glyphs) {
222
+ const index = coverageIndex(coverage, glyph);
223
+ if (index < 0) continue;
224
+ const set = table.part(table.u16(10 + index * 2));
225
+ for (let record = 0; record < set.u16(0); record++) {
226
+ const at = 2 + record * recordSize;
227
+ if (!glyphs.has(set.u16(at))) continue;
228
+ first = Math.max(first, xAdvance(set, at + 2, format1, set, store).value);
229
+ second = Math.max(second, xAdvance(set, at + 2 + size1, format2, set, store).value);
230
+ }
231
+ }
232
+ return {
233
+ first,
234
+ second
235
+ };
236
+ };
237
+ return {
238
+ pair,
239
+ largest
240
+ };
241
+ }
196
242
  if (format === 2) {
197
243
  const classes1 = table.part(table.u16(8));
198
244
  const classes2 = table.part(table.u16(10));
199
245
  const count1 = table.u16(12);
200
246
  const count2 = table.u16(14);
201
- return (first, second) => {
247
+ const pair = (first, second) => {
202
248
  if (coverageIndex(coverage, first) < 0) return void 0;
203
249
  const class1 = classOf(classes1, first);
204
250
  const class2 = classOf(classes2, second);
@@ -206,6 +252,30 @@ function pairSubtable(table, store) {
206
252
  const at = 16 + (class1 * count2 + class2) * (size1 + size2);
207
253
  return pairValue(xAdvance(table, at, format1, table, store), xAdvance(table, at + size1, format2, table, store), skipSecond);
208
254
  };
255
+ const largest = (glyphs) => {
256
+ const firsts = /* @__PURE__ */ new Set();
257
+ const seconds = /* @__PURE__ */ new Set();
258
+ for (const glyph of glyphs) {
259
+ if (coverageIndex(coverage, glyph) >= 0) firsts.add(classOf(classes1, glyph));
260
+ seconds.add(classOf(classes2, glyph));
261
+ }
262
+ let first = 0;
263
+ let second = 0;
264
+ for (const class1 of firsts) for (const class2 of seconds) {
265
+ if (class1 >= count1 || class2 >= count2) continue;
266
+ const at = 16 + (class1 * count2 + class2) * (size1 + size2);
267
+ first = Math.max(first, xAdvance(table, at, format1, table, store).value);
268
+ second = Math.max(second, xAdvance(table, at + size1, format2, table, store).value);
269
+ }
270
+ return {
271
+ first,
272
+ second
273
+ };
274
+ };
275
+ return {
276
+ pair,
277
+ largest
278
+ };
209
279
  }
210
280
  throw new Error(`the PairPos format ${format} is unknown`);
211
281
  }
@@ -278,9 +348,16 @@ function readGposKerning(gpos, script, store, filter) {
278
348
  ...skip === void 0 ? {} : { skip },
279
349
  pair(first, second) {
280
350
  for (const subtable of subtables) {
281
- const value = subtable(first, second);
351
+ const value = subtable.pair(first, second);
282
352
  if (value !== void 0) return value;
283
353
  }
354
+ },
355
+ largest(glyphs) {
356
+ const values = subtables.map((subtable) => subtable.largest(glyphs));
357
+ return {
358
+ first: Math.max(...values.map((value) => value.first)),
359
+ second: Math.max(...values.map((value) => value.second))
360
+ };
284
361
  }
285
362
  }];
286
363
  });
@@ -313,6 +390,14 @@ function readKernTable(kern) {
313
390
  second: 0,
314
391
  skipSecond: false
315
392
  };
393
+ },
394
+ largest(glyphs) {
395
+ let first = 0;
396
+ for (const [key, value] of pairs) if (glyphs.has(Math.floor(key / 65536)) && glyphs.has(key % 65536)) first = Math.max(first, value);
397
+ return {
398
+ first,
399
+ second: 0
400
+ };
316
401
  }
317
402
  }];
318
403
  }
@@ -540,6 +625,81 @@ function records(table, at, count) {
540
625
  lookupIndex: table.u16(at + k * 4 + 2)
541
626
  }));
542
627
  }
628
+ /** This function gives each rule table of the rule sets at `at` of a context subtable of format 1 or 2. An offset 0 is an empty set. */
629
+ function ruleTables(t, at) {
630
+ const tables = [];
631
+ for (let s = 0; s < t.u16(at); s++) {
632
+ const offset = t.u16(at + 2 + s * 2);
633
+ if (offset === 0) continue;
634
+ const set = t.part(offset);
635
+ for (let r = 0; r < set.u16(0); r++) tables.push(set.part(set.u16(2 + r * 2)));
636
+ }
637
+ return tables;
638
+ }
639
+ /** This function gives the records of each rule of a context subtable (lookup type 5). */
640
+ function contextRecords(t) {
641
+ const format = t.u16(0);
642
+ if (format === 3) return records(t, 6 + t.u16(2) * 2, t.u16(4));
643
+ return ruleTables(t, format === 2 ? 6 : 4).flatMap((rule) => records(rule, 4 + (rule.u16(0) - 1) * 2, rule.u16(2)));
644
+ }
645
+ /** This function gives the records of each rule of a chained context subtable (lookup type 6). */
646
+ function chainRecords(t) {
647
+ const format = t.u16(0);
648
+ if (format === 3) {
649
+ const inputAt = 4 + t.u16(2) * 2;
650
+ const aheadAt = inputAt + 2 + t.u16(inputAt) * 2;
651
+ const recordsAt = aheadAt + 2 + t.u16(aheadAt) * 2;
652
+ return records(t, recordsAt + 2, t.u16(recordsAt));
653
+ }
654
+ return ruleTables(t, format === 2 ? 10 : 4).flatMap((rule) => {
655
+ const inputAt = 2 + rule.u16(0) * 2;
656
+ const aheadAt = inputAt + 2 + Math.max(0, rule.u16(inputAt) - 1) * 2;
657
+ const recordsAt = aheadAt + 2 + rule.u16(aheadAt) * 2;
658
+ return records(rule, recordsAt + 2, rule.u16(recordsAt));
659
+ });
660
+ }
661
+ /** This function adds the rules of one subtable of a lookup type to `rules`, and each nested lookup to `nested`. */
662
+ function readRules(type, t, rules, nested) {
663
+ if (type === 5 || type === 6) {
664
+ for (const record of type === 5 ? contextRecords(t) : chainRecords(t)) nested.push(record.lookupIndex);
665
+ return;
666
+ }
667
+ if (type < 1 || type > 8 || type === 7) return;
668
+ const format = t.u16(0);
669
+ for (const [from, index] of coverageGlyphs(t.part(t.u16(2)))) if (type === 1) rules.push({
670
+ from,
671
+ to: [format === 1 ? from + t.i16(4) & 65535 : t.u16(6 + index * 2)]
672
+ });
673
+ else if (type === 2) {
674
+ const sequence = t.part(t.u16(6 + index * 2));
675
+ rules.push({
676
+ from,
677
+ to: Array.from({ length: sequence.u16(0) }, (_, k) => sequence.u16(2 + k * 2))
678
+ });
679
+ } else if (type === 3) {
680
+ const set = t.part(t.u16(6 + index * 2));
681
+ for (let k = 0; k < set.u16(0); k++) rules.push({
682
+ from,
683
+ to: [set.u16(2 + k * 2)]
684
+ });
685
+ } else if (type === 4) {
686
+ const set = t.part(t.u16(6 + index * 2));
687
+ for (let l = 0; l < set.u16(0); l++) {
688
+ const ligature = set.part(set.u16(2 + l * 2));
689
+ rules.push({
690
+ from,
691
+ to: [ligature.u16(0)],
692
+ components: Array.from({ length: ligature.u16(2) - 1 }, (_, k) => ligature.u16(4 + k * 2))
693
+ });
694
+ }
695
+ } else {
696
+ const aheadAt = 6 + t.u16(4) * 2;
697
+ rules.push({
698
+ from,
699
+ to: [t.u16(aheadAt + 2 + t.u16(aheadAt) * 2 + 2 + index * 2)]
700
+ });
701
+ }
702
+ }
543
703
  /** The lookups of one GSUB table. It reads each lookup at its first use. */
544
704
  var Gsub = class {
545
705
  lookups;
@@ -800,6 +960,23 @@ var Gsub = class {
800
960
  return false;
801
961
  });
802
962
  }
963
+ /** This function gives the rules of a lookup and of each lookup that it nests, as `rules` of `SubstitutionLookup` tells. */
964
+ rulesOf(index) {
965
+ const lists = [];
966
+ const seen = /* @__PURE__ */ new Set();
967
+ const visit = (at) => {
968
+ const lookup = this.lookup(at);
969
+ if (seen.has(at) || lookup === void 0) return;
970
+ seen.add(at);
971
+ const rules = [];
972
+ const nested = [];
973
+ for (const subtable of lookup.subtables) readRules(lookup.type, subtable, rules, nested);
974
+ lists.push(rules);
975
+ nested.forEach(visit);
976
+ };
977
+ visit(index);
978
+ return lists;
979
+ }
803
980
  /** This function applies one subtable at `i`. It gives the next position, or -1 when the subtable does not match. */
804
981
  subtable(lookup, t, glyphs, i, depth) {
805
982
  const glyph = glyphs[i];
@@ -977,7 +1154,8 @@ function readSubstitutions(gsub, script, features, filter) {
977
1154
  features: tags,
978
1155
  apply: (glyphs, masks, mask) => table.applyAll(index, glyphs, masks, mask),
979
1156
  applyInfos: (glyphs, infos, mask, perSyllable) => table.applyRun(index, glyphs, infos, mask, perSyllable),
980
- wouldApply: (glyphs) => table.wouldApply(index, glyphs)
1157
+ wouldApply: (glyphs) => table.wouldApply(index, glyphs),
1158
+ rules: () => table.rulesOf(index)
981
1159
  });
982
1160
  }
983
1161
  return result;
@@ -1016,7 +1194,7 @@ function readAxes(fvar) {
1016
1194
  * This function gives the normalized coordinates of an instance. Each coordinate is in -1 to 1, on the 2.14 grid. The
1017
1195
  * segment maps of `avar` change it when the font has them. A value outside the range of an axis is clamped to the range.
1018
1196
  */
1019
- function normalize$1(axes, avar, instance) {
1197
+ function normalize(axes, avar, instance) {
1020
1198
  const coordinates = axes.map((axis) => {
1021
1199
  const value = Math.min(axis.max, Math.max(axis.min, instance[axis.tag] ?? axis.default));
1022
1200
  if (value < axis.default) return toF2dot14(axis.default === axis.min ? 0 : (value - axis.default) / (axis.default - axis.min));
@@ -1343,6 +1521,144 @@ function gvarAdvanceDeltas(gvar, pointCounts, coordinates, deltas) {
1343
1521
  }
1344
1522
  return result;
1345
1523
  }
1524
+ // src/fonts/normalize.ts
1525
+ /**
1526
+ * The Unicode normalization of HarfBuzz, from `hb-ot-shape-normalize.cc`. HarfBuzz decomposes each cluster of a base and its marks.
1527
+ * It then sorts the marks, and composes each mark with its base when the font has the glyph of the composite.
1528
+ */
1529
+ const unicodeMark = /\p{M}/u;
1530
+ /**
1531
+ * The classes of HarfBuzz for the marks of Hebrew and Arabic, from `hb-unicode.hh`. Their order is not the canonical order:
1532
+ * for example the shadda comes first. Each other pair of marks uses the canonical order.
1533
+ */
1534
+ const sortClasses = /* @__PURE__ */ new Map([
1535
+ [1456, 22],
1536
+ [1457, 15],
1537
+ [1458, 16],
1538
+ [1459, 17],
1539
+ [1460, 23],
1540
+ [1461, 18],
1541
+ [1462, 19],
1542
+ [1463, 20],
1543
+ [1464, 21],
1544
+ [1465, 14],
1545
+ [1466, 14],
1546
+ [1467, 24],
1547
+ [1468, 12],
1548
+ [1469, 25],
1549
+ [1471, 13],
1550
+ [1473, 10],
1551
+ [1474, 11],
1552
+ [1479, 21],
1553
+ [64286, 26],
1554
+ [1560, 31],
1555
+ [1561, 32],
1556
+ [1562, 33],
1557
+ [1611, 28],
1558
+ [1612, 29],
1559
+ [1613, 30],
1560
+ [1614, 31],
1561
+ [1615, 32],
1562
+ [1616, 33],
1563
+ [1617, 27],
1564
+ [1618, 34],
1565
+ [1648, 35],
1566
+ [2288, 28],
1567
+ [2289, 29],
1568
+ [2290, 30]
1569
+ ]);
1570
+ /** This function tells if a code point is a mark of Unicode. The first mark of Unicode is U+0300. */
1571
+ function isMark(codePoint) {
1572
+ return codePoint >= 768 && unicodeMark.test(String.fromCodePoint(codePoint));
1573
+ }
1574
+ /** This function tells if a text has a mark of Unicode. */
1575
+ function hasMark(text) {
1576
+ return unicodeMark.test(text);
1577
+ }
1578
+ function decomposed(codePoint) {
1579
+ return String.fromCodePoint(codePoint).normalize("NFD");
1580
+ }
1581
+ /**
1582
+ * This function tells if the mark `second` sorts before the mark `first`. That is true when the class of `first` is larger
1583
+ * than the class of `second`, and neither class is 0. The form NFD of JavaScript gives the canonical order, so no table is needed.
1584
+ */
1585
+ function reorders(first, second) {
1586
+ const firstClass = sortClasses.get(first);
1587
+ const secondClass = sortClasses.get(second);
1588
+ if (firstClass !== void 0 && secondClass !== void 0) return firstClass > secondClass;
1589
+ const text = decomposed(first) + decomposed(second);
1590
+ return text.normalize("NFD") !== text;
1591
+ }
1592
+ /** This function tells if a code point has the combining class 0. U+0345 has the largest class of Unicode. */
1593
+ function isStarter(codePoint) {
1594
+ return !isMark(codePoint) || codePoint !== 837 && !reorders(837, codePoint);
1595
+ }
1596
+ /** This function gives the canonical composite of two code points, or `undefined`. A mark does not compose with a mark. */
1597
+ function compose(first, second) {
1598
+ if (isMark(first)) return void 0;
1599
+ const [single, ...rest] = [...String.fromCodePoint(first, second).normalize("NFC")];
1600
+ return rest.length === 0 && single !== void 0 ? single.codePointAt(0) : void 0;
1601
+ }
1602
+ /**
1603
+ * This function gives the code points of a character after the decomposition. A character keeps its form when the font has
1604
+ * no glyph for one of its parts, or when the set `kept` has the character.
1605
+ */
1606
+ function decomposition(codePoint, hasGlyph, kept) {
1607
+ const parts = kept.has(codePoint) ? [codePoint] : Array.from(decomposed(codePoint), (part) => part.codePointAt(0));
1608
+ return (parts.length > 1 || parts[0] !== codePoint) && parts.every(hasGlyph) ? parts : [codePoint];
1609
+ }
1610
+ /**
1611
+ * This function normalizes code points as HarfBuzz does. It decomposes the characters by `rules`. A text with a mark after
1612
+ * its first character then gets more steps. The function sorts the marks, and composes each mark with its starter again
1613
+ * when the font has the glyph of the composite. A mark between them with the same class or a larger class blocks this.
1614
+ */
1615
+ function normalizeCodePoints(codePoints, rules) {
1616
+ const { hasGlyph, shortCircuit, kept = /* @__PURE__ */ new Set() } = rules;
1617
+ const marked = codePoints.map(isMark);
1618
+ const parts = codePoints.flatMap((codePoint, i) => {
1619
+ const simple = marked[i + 1] !== true && !(i > 0 && marked[i]);
1620
+ return shortCircuit && simple && hasGlyph(codePoint) ? [codePoint] : decomposition(codePoint, hasGlyph, kept);
1621
+ });
1622
+ if (!marked.slice(1).includes(true)) return parts;
1623
+ for (let i = 1; i < parts.length; i++) for (let j = i; j > 0 && isMark(parts[j - 1]) && isMark(parts[j]) && reorders(parts[j - 1], parts[j]); j--) [parts[j - 1], parts[j]] = [parts[j], parts[j - 1]];
1624
+ const out = parts.slice(0, 1);
1625
+ let starter = 0;
1626
+ for (let i = 1; i < parts.length; i++) {
1627
+ const codePoint = parts[i];
1628
+ if (isMark(codePoint) && (starter === out.length - 1 || reorders(codePoint, out[out.length - 1]))) {
1629
+ const composed = compose(out[starter], codePoint);
1630
+ if (composed !== void 0 && hasGlyph(composed)) {
1631
+ out[starter] = composed;
1632
+ continue;
1633
+ }
1634
+ }
1635
+ out.push(codePoint);
1636
+ if (isStarter(codePoint)) starter = out.length - 1;
1637
+ }
1638
+ return out;
1639
+ }
1640
+ /** This function gives the forms of a set of characters. It repeats the compositions until the set of code points is stable. */
1641
+ function normalForms(characters, hasGlyph, kept = /* @__PURE__ */ new Set()) {
1642
+ const parts = new Map([...characters].map((codePoint) => [codePoint, decomposition(codePoint, hasGlyph, kept)]));
1643
+ const codePoints = /* @__PURE__ */ new Set([...characters, ...[...parts.values()].flat()]);
1644
+ const composites = /* @__PURE__ */ new Map();
1645
+ for (let size = 0; size !== codePoints.size;) {
1646
+ size = codePoints.size;
1647
+ const marks = [...codePoints].filter(isMark);
1648
+ for (const starter of codePoints) for (const mark of marks) {
1649
+ const composed = compose(starter, mark);
1650
+ if (composed === void 0 || !hasGlyph(composed)) continue;
1651
+ const known = composites.get(starter) ?? [];
1652
+ if (!known.includes(composed)) composites.set(starter, [...known, composed]);
1653
+ codePoints.add(composed);
1654
+ }
1655
+ }
1656
+ return {
1657
+ parts,
1658
+ composites,
1659
+ codePoints
1660
+ };
1661
+ }
1346
1662
  // src/fonts/indic.ts
1347
1663
  /** The OpenType script tags that use the Indic shaper in this model. */
1348
1664
  const indicScripts = ["deva"];
@@ -1597,70 +1913,8 @@ function indicProperties(codePoint) {
1597
1913
  }
1598
1914
  return [X, END];
1599
1915
  }
1600
- /**
1601
- * The canonical combining classes of the marks of the Devanagari blocks that are not 0. The normalization uses them to
1602
- * sort the marks and to block a composition. A mark of another block has the class 0 in this model.
1603
- */
1604
- const combiningClasses = [
1605
- 2364,
1606
- 2364,
1607
- 7,
1608
- 2381,
1609
- 2381,
1610
- 9,
1611
- 2385,
1612
- 2385,
1613
- 230,
1614
- 2386,
1615
- 2386,
1616
- 220,
1617
- 2387,
1618
- 2388,
1619
- 230,
1620
- 7376,
1621
- 7378,
1622
- 230,
1623
- 7380,
1624
- 7380,
1625
- 1,
1626
- 7381,
1627
- 7385,
1628
- 220,
1629
- 7386,
1630
- 7387,
1631
- 230,
1632
- 7388,
1633
- 7391,
1634
- 220,
1635
- 7392,
1636
- 7392,
1637
- 230,
1638
- 7394,
1639
- 7400,
1640
- 1,
1641
- 7405,
1642
- 7405,
1643
- 220,
1644
- 7412,
1645
- 7412,
1646
- 230,
1647
- 7416,
1648
- 7417,
1649
- 230,
1650
- 43232,
1651
- 43249,
1652
- 230
1653
- ];
1654
- function combiningClass(codePoint) {
1655
- for (let i = 0; i < combiningClasses.length; i += 3) if (codePoint >= combiningClasses[i] && codePoint <= combiningClasses[i + 1]) return combiningClasses[i + 2];
1656
- return 0;
1657
- }
1658
- const unicodeMark = /\p{M}/u;
1659
1916
  /** The general categories from `FORMAT` to `NON_SPACING_MARK` of HarfBuzz: a letter, a mark, or a format or other code point. */
1660
1917
  const letterOrMark = /[\p{L}\p{M}\p{Cf}\p{Cn}\p{Co}\p{Cs}]/u;
1661
- function isMark(codePoint) {
1662
- return unicodeMark.test(String.fromCodePoint(codePoint));
1663
- }
1664
1918
  /**
1665
1919
  * This function inserts a dotted circle in each vowel sequence of Devanagari that looks like another vowel, as
1666
1920
  * `_hb_preprocess_text_vowel_constraints` does.
@@ -1709,53 +1963,11 @@ function vowelConstraints(codePoints) {
1709
1963
  while (i < codePoints.length) out.push(codePoints[i++]);
1710
1964
  return out;
1711
1965
  }
1712
- /**
1713
- * This function normalizes the code points as HarfBuzz does in the mode `COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT`. It
1714
- * decomposes each character that has a canonical decomposition when the font has the glyphs of its parts. The letter
1715
- * U+0931 does not decompose. A text with a mark after its first character then gets more steps. The function sorts the
1716
- * marks, and composes each mark with its starter again when the font has the glyph of the composite.
1717
- */
1718
- function normalize(codePoints, hasGlyph) {
1719
- const allSimple = !codePoints.some((codePoint, i) => i > 0 && isMark(codePoint));
1720
- const decomposed = [];
1721
- for (const codePoint of codePoints) {
1722
- const parts = codePoint === 2353 ? [codePoint] : [...String.fromCodePoint(codePoint).normalize("NFD")].map((part) => part.codePointAt(0));
1723
- if (parts.length > 1 || parts[0] !== codePoint) {
1724
- if (parts.every(hasGlyph)) {
1725
- decomposed.push(...parts);
1726
- continue;
1727
- }
1728
- }
1729
- decomposed.push(codePoint);
1730
- }
1731
- if (allSimple) return decomposed;
1732
- for (let i = 1; i < decomposed.length; i++) for (let j = i; j > 0; j--) {
1733
- const before = combiningClass(decomposed[j - 1]);
1734
- const after = combiningClass(decomposed[j]);
1735
- if (after === 0 || before <= after) break;
1736
- [decomposed[j - 1], decomposed[j]] = [decomposed[j], decomposed[j - 1]];
1737
- }
1738
- const out = decomposed.slice(0, 1);
1739
- let starter = 0;
1740
- for (let i = 1; i < decomposed.length; i++) {
1741
- const codePoint = decomposed[i];
1742
- if (isMark(codePoint) && (starter === out.length - 1 || combiningClass(out[out.length - 1]) < combiningClass(codePoint))) {
1743
- const composed = compose(out[starter], codePoint);
1744
- if (composed !== void 0 && hasGlyph(composed)) {
1745
- out[starter] = composed;
1746
- continue;
1747
- }
1748
- }
1749
- out.push(codePoint);
1750
- if (combiningClass(codePoint) === 0) starter = out.length - 1;
1751
- }
1752
- return out;
1753
- }
1754
- /** This function gives the canonical composite of two code points, or `undefined`. A mark does not compose with a mark. */
1755
- function compose(first, second) {
1756
- if (isMark(first)) return void 0;
1757
- const [single, ...rest] = [...[(String.fromCodePoint(first) + String.fromCodePoint(second)).normalize("NFC")][0]];
1758
- return rest.length === 0 && single !== void 0 ? single.codePointAt(0) : void 0;
1966
+ /** The code points of Devanagari that do not decompose. */
1967
+ const kept = /* @__PURE__ */ new Set([2353]);
1968
+ /** This function gives the code points that `normalizeCodePoints` and `vowelConstraints` can give for a set of characters. */
1969
+ function indicForms(characters, hasGlyph) {
1970
+ return normalForms(/* @__PURE__ */ new Set([...characters, 9676]), hasGlyph, kept);
1759
1971
  }
1760
1972
  const letters = /* @__PURE__ */ new Map([
1761
1973
  [X, "x"],
@@ -1869,6 +2081,24 @@ const finalFeatures = [
1869
2081
  "psts",
1870
2082
  "haln"
1871
2083
  ];
2084
+ /** This function gives the features of the page that the last stage applies. */
2085
+ function pageFeatures(features) {
2086
+ return features.filter((tag) => ![
2087
+ "ccmp",
2088
+ "locl",
2089
+ "liga"
2090
+ ].includes(tag) && !finalFeatures.includes(tag));
2091
+ }
2092
+ /** This function gives all the features that `indicShaper` applies for the features `features` of the page. */
2093
+ function indicFeatures(features) {
2094
+ return [
2095
+ "locl",
2096
+ "ccmp",
2097
+ ...basicFeatures.map(([tag]) => tag),
2098
+ ...finalFeatures,
2099
+ ...pageFeatures(features)
2100
+ ];
2101
+ }
1872
2102
  /** The mask bit of the global features. Each glyph has it. */
1873
2103
  const GLOBAL = 1;
1874
2104
  /** The mask bits of the features that apply only to some glyphs of a syllable. */
@@ -1940,12 +2170,7 @@ function indicShaper(font, script, features) {
1940
2170
  lookups: lookupsOf([tag]),
1941
2171
  mask: global ? GLOBAL : featureBits.get(tag)
1942
2172
  }));
1943
- const extra = features.filter((tag) => ![
1944
- "ccmp",
1945
- "locl",
1946
- "liga"
1947
- ].includes(tag) && !finalFeatures.includes(tag));
1948
- const last = lookupsOf([...finalFeatures, ...extra]).map((lookup) => ({
2173
+ const last = lookupsOf([...finalFeatures, ...pageFeatures(features)]).map((lookup) => ({
1949
2174
  lookup,
1950
2175
  mask: lookup.features.reduce((mask, tag) => mask | (tag === "init" ? initMask : GLOBAL), 0),
1951
2176
  perSyllable: lookup.features.some((tag) => finalFeatures.includes(tag))
@@ -2136,7 +2361,11 @@ function indicShaper(font, script, features) {
2136
2361
  if (infos[start].position === PRE_M && (start === 0 || !letterOrMark.test(String.fromCodePoint(infos[start - 1].codePoint)))) infos[start].mask |= initMask;
2137
2362
  };
2138
2363
  return (word) => {
2139
- const codePoints = normalize(vowelConstraints([...word].map((character) => character.codePointAt(0))), (codePoint) => font.glyph(codePoint) !== 0);
2364
+ const codePoints = normalizeCodePoints(vowelConstraints([...word].map((character) => character.codePointAt(0))), {
2365
+ hasGlyph: (codePoint) => font.glyph(codePoint) !== 0,
2366
+ shortCircuit: false,
2367
+ kept
2368
+ });
2140
2369
  const run = {
2141
2370
  glyphs: codePoints.map((codePoint) => font.glyph(codePoint)),
2142
2371
  infos: []
@@ -3289,7 +3518,7 @@ function build(parsed, requested, modes) {
3289
3518
  const { tables, header, characters, glyphClass, axes } = parsed;
3290
3519
  const { deltas } = modes;
3291
3520
  const instance = Object.fromEntries(axes.map((axis) => [axis.tag, Math.min(axis.max, Math.max(axis.min, requested[axis.tag] ?? axis.default))]));
3292
- const coordinates = normalize$1(axes, tables.get("avar"), instance);
3521
+ const coordinates = normalize(axes, tables.get("avar"), instance);
3293
3522
  const variable = coordinates.some((value) => value !== 0);
3294
3523
  const hvar = tables.get("HVAR");
3295
3524
  const gvar = tables.get("gvar");
@@ -3401,24 +3630,49 @@ function shapingWords(text) {
3401
3630
  if (word !== "") words.push(word);
3402
3631
  return words;
3403
3632
  }
3404
- /** The GSUB pass of one set of options: a function that gives the glyphs of one word, and the kerning lookups. */
3405
- function shaperOf(font, options) {
3633
+ /** This function gives the choices of a set of options: the script, the features, the shaper and the kerning lookups. */
3634
+ function shapingOf(font, options) {
3406
3635
  const script = options.script ?? "DFLT";
3407
3636
  const features = options.features ?? defaultFeatures;
3408
3637
  const kerning = options.kerning === false ? [] : font.kerning(script);
3409
3638
  if (options.indic !== false && indicScripts.includes(script)) return {
3639
+ script,
3640
+ features,
3641
+ shaper: "indic",
3642
+ kerning
3643
+ };
3644
+ return {
3645
+ script,
3646
+ features,
3647
+ shaper: options.joining !== false && joiningScripts.includes(script) ? "joining" : "plain",
3648
+ kerning
3649
+ };
3650
+ }
3651
+ /** The GSUB pass of one set of options: a function that gives the glyphs of one word, and the kerning lookups. */
3652
+ function shaperOf(font, options) {
3653
+ const { script, features, shaper, kerning } = shapingOf(font, options);
3654
+ if (shaper === "indic") return {
3410
3655
  substitute: indicShaper(font, script, features),
3411
3656
  kerning
3412
3657
  };
3413
- const joins = options.joining !== false && joiningScripts.includes(script);
3658
+ const joins = shaper === "joining";
3414
3659
  const lookups = joins ? [] : font.substitutions(script, features);
3415
3660
  const stages = joins ? joiningStages(features, features.includes("rclt") && font.substitutions(script, ["rclt"]).length > 0).map((stage) => ({
3416
3661
  stage,
3417
3662
  lookups: font.substitutions(script, stage.features)
3418
3663
  })) : [];
3664
+ const normalizes = script !== "hang";
3665
+ const rules = {
3666
+ hasGlyph: (codePoint) => font.glyph(codePoint) !== 0,
3667
+ shortCircuit: true
3668
+ };
3419
3669
  const substitute = (word) => {
3420
- const codePoints = [...word].map((character) => character.codePointAt(0));
3421
- const wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
3670
+ let codePoints = [...word].map((character) => character.codePointAt(0));
3671
+ let wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
3672
+ if (normalizes && (hasMark(word) || wordGlyphs.includes(0))) {
3673
+ codePoints = normalizeCodePoints(codePoints, rules);
3674
+ wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
3675
+ }
3422
3676
  for (const lookup of lookups) lookup.apply(wordGlyphs);
3423
3677
  if (joins) {
3424
3678
  const masks = joiningForms(codePoints).map((form) => 128 | (form === void 0 ? 0 : 1 << joiningFeatures.indexOf(form)));
@@ -3545,6 +3799,6 @@ function measureWidth(font, text, size, options = {}) {
3545
3799
  for (const advance of advances) units += advance;
3546
3800
  return units * size / font.unitsPerEm;
3547
3801
  }
3548
- export { canvasWords as a, shapingWords as i, measureWidth as n, defaultFeatures as o, readFont as r, readTables as s, glyphAdvances as t };
3802
+ export { shapingWords as a, joiningStages as c, normalForms as d, defaultFeatures as f, shapingOf as i, indicFeatures as l, measureWidth as n, canvasSegments as o, readTables as p, readFont as r, canvasWords as s, glyphAdvances as t, indicForms as u };
3549
3803
 
3550
3804
  //# sourceMappingURL=font.js.map