@mark1russell7/typeset 0.1.4 → 0.1.6

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.
Files changed (48) hide show
  1. package/build/chunks/breaking.js +9 -2
  2. package/build/chunks/breaking.js.map +1 -1
  3. package/build/chunks/engines.js +46 -1
  4. package/build/chunks/engines.js.map +1 -1
  5. package/build/chunks/font.d.ts +6 -0
  6. package/build/chunks/font.d.ts.map +1 -1
  7. package/build/chunks/font.js +543 -315
  8. package/build/chunks/font.js.map +1 -1
  9. package/build/chunks/fonts.d.ts +12 -2
  10. package/build/chunks/fonts.d.ts.map +1 -1
  11. package/build/chunks/fonts.js +3 -2
  12. package/build/chunks/fonts.js.map +1 -1
  13. package/build/chunks/measurer.d.ts +6 -2
  14. package/build/chunks/measurer.d.ts.map +1 -1
  15. package/build/chunks/measurer.js +45 -8
  16. package/build/chunks/measurer.js.map +1 -1
  17. package/build/chunks/paragraph.d.ts +5 -0
  18. package/build/chunks/paragraph.d.ts.map +1 -1
  19. package/build/fonts-inspect.d.ts +11 -1
  20. package/build/fonts-inspect.d.ts.map +1 -1
  21. package/build/fonts-inspect.js +1 -1
  22. package/build/fonts.d.ts +2 -2
  23. package/build/fonts.js +2 -2
  24. package/build/measurer.d.ts +2 -2
  25. package/build/segmentation.d.ts +19 -0
  26. package/build/segmentation.d.ts.map +1 -0
  27. package/build/segmentation.js +267 -0
  28. package/build/segmentation.js.map +1 -0
  29. package/package.json +7 -3
  30. package/src/breaking/paragraph.ts +8 -2
  31. package/src/fonts/context.ts +48 -0
  32. package/src/fonts/font.ts +52 -13
  33. package/src/fonts/index.ts +1 -0
  34. package/src/fonts/indic.ts +51 -42
  35. package/src/fonts/joining.ts +2 -2
  36. package/src/fonts/kerning.ts +48 -34
  37. package/src/fonts/machine.ts +133 -0
  38. package/src/fonts/normalize.ts +27 -0
  39. package/src/fonts/risks.ts +48 -0
  40. package/src/fonts/substitution.ts +15 -44
  41. package/src/fonts-inspect.ts +2 -1
  42. package/src/fonts.ts +1 -0
  43. package/src/measurer/advance.ts +46 -1
  44. package/src/measurer.ts +1 -1
  45. package/src/segmentation/line-table.ts +5 -0
  46. package/src/segmentation/line.ts +203 -0
  47. package/src/segmentation/segments.ts +18 -0
  48. package/src/segmentation.ts +3 -0
@@ -291,26 +291,41 @@ function scriptTags(script) {
291
291
  /** The GPOS features with pair lookups that HarfBuzz applies to horizontal text. */
292
292
  const pairFeatures = /* @__PURE__ */ new Set(["kern", "dist"]);
293
293
  /** The lookup flag that selects a mark filtering set of GDEF. */
294
- const USE_MARK_FILTERING_SET$1 = 16;
295
- /**
296
- * This function gives the pair lookups of the features `kern` and `dist` of GPOS for a script, in the order of the lookup
297
- * list. The store gives the deltas of a variable font at its instance. The filter gives the mark filtering sets of GDEF.
298
- */
299
- function readGposKerning(gpos, script, store, filter) {
300
- const scripts = gpos.part(gpos.u16(4));
301
- const features = gpos.part(gpos.u16(6));
302
- const lookups = gpos.part(gpos.u16(8));
303
- const scriptCount = scripts.u16(0);
304
- let langSys;
305
- for (const wanted of scriptTags(script)) {
306
- for (let i = 0; i < scriptCount && langSys === void 0; i++) {
307
- if (scripts.tag(2 + i * 6) !== wanted) continue;
308
- const table = scripts.part(scripts.u16(2 + i * 6 + 4));
309
- const fallback = table.u16(0);
310
- if (fallback !== 0) langSys = table.part(fallback);
311
- }
312
- if (langSys !== void 0) break;
294
+ const USE_MARK_FILTERING_SET = 16;
295
+ /** This function gives the test of the glyphs that a lookup skips. */
296
+ function lookupSkip(lookup, flags, filter) {
297
+ const markSet = (flags & USE_MARK_FILTERING_SET) !== 0 ? lookup.u16(6 + lookup.u16(4) * 2) : -1;
298
+ return (glyph) => {
299
+ const kind = filter.glyphClass(glyph);
300
+ if ((flags & 2) !== 0 && kind === 1) return true;
301
+ if ((flags & 4) !== 0 && kind === 2) return true;
302
+ if (kind !== 3) return false;
303
+ if ((flags & 8) !== 0) return true;
304
+ if (markSet >= 0) return !filter.inMarkSet(markSet, glyph);
305
+ const attach = flags >> 8;
306
+ return attach !== 0 && filter.markAttachClass(glyph) !== attach;
307
+ };
308
+ }
309
+ /** This function gives the subtables of a lookup with their types, after each extension of the type `extension`. */
310
+ function subtablesOf(lookup, extension) {
311
+ return Array.from({ length: lookup.u16(4) }, (_, s) => {
312
+ const subtable = lookup.part(lookup.u16(6 + s * 2));
313
+ return lookup.u16(0) === extension ? [subtable.u16(2), subtable.part(subtable.u32(4))] : [lookup.u16(0), subtable];
314
+ });
315
+ }
316
+ /** This function gives the default language system of a table for a script. */
317
+ function languageSystem(table, script) {
318
+ const scripts = table.part(table.u16(4));
319
+ for (const wanted of scriptTags(script)) for (let i = 0; i < scripts.u16(0); i++) {
320
+ if (scripts.tag(2 + i * 6) !== wanted) continue;
321
+ const entry = scripts.part(scripts.u16(2 + i * 6 + 4));
322
+ if (entry.u16(0) !== 0) return entry.part(entry.u16(0));
313
323
  }
324
+ }
325
+ /** This function gives the indices of the lookups of `kern` and `dist` for a script, in the order of the lookup list. */
326
+ function pairLookupIndices(gpos, script) {
327
+ const features = gpos.part(gpos.u16(6));
328
+ const langSys = languageSystem(gpos, script);
314
329
  if (langSys === void 0) return [];
315
330
  const indices = /* @__PURE__ */ new Set();
316
331
  const featureCount = langSys.u16(4);
@@ -320,29 +335,21 @@ function readGposKerning(gpos, script, store, filter) {
320
335
  const feature = features.part(features.u16(record + 4));
321
336
  for (let k = 0; k < feature.u16(2); k++) indices.add(feature.u16(4 + k * 2));
322
337
  }
323
- return [...indices].sort((a, b) => a - b).flatMap((index) => {
338
+ return [...indices].sort((a, b) => a - b);
339
+ }
340
+ /**
341
+ * This function gives the pair lookups of the features `kern` and `dist` of GPOS for a script, in the order of the lookup
342
+ * list. The store gives the deltas of a variable font at its instance. The filter gives the mark filtering sets of GDEF.
343
+ */
344
+ function readGposKerning(gpos, script, store, filter) {
345
+ const lookups = gpos.part(gpos.u16(8));
346
+ return pairLookupIndices(gpos, script).flatMap((index) => {
324
347
  const lookup = lookups.part(lookups.u16(2 + index * 2));
325
348
  const subtables = [];
326
- const type = lookup.u16(0);
327
- for (let s = 0; s < lookup.u16(4); s++) {
328
- let subtable = lookup.part(lookup.u16(6 + s * 2));
329
- let subtype = type;
330
- if (type === 9) {
331
- subtype = subtable.u16(2);
332
- subtable = subtable.part(subtable.u32(4));
333
- }
334
- if (subtype === 2) subtables.push(pairSubtable(subtable, store));
335
- }
349
+ for (const [subtype, subtable] of subtablesOf(lookup, 9)) if (subtype === 2) subtables.push(pairSubtable(subtable, store));
336
350
  if (subtables.length === 0) return [];
337
351
  const flags = lookup.u16(2);
338
- const markSet = (flags & USE_MARK_FILTERING_SET$1) !== 0 && filter !== void 0 ? lookup.u16(6 + lookup.u16(4) * 2) : -1;
339
- const skip = markSet < 0 ? void 0 : (glyph) => {
340
- const kind = filter.glyphClass(glyph);
341
- if ((flags & 2) !== 0 && kind === 1) return true;
342
- if ((flags & 4) !== 0 && kind === 2) return true;
343
- if (kind !== 3) return false;
344
- return (flags & 8) !== 0 || !filter.inMarkSet(markSet, glyph);
345
- };
352
+ const skip = (flags & USE_MARK_FILTERING_SET) !== 0 && filter !== void 0 ? lookupSkip(lookup, flags, filter) : void 0;
346
353
  return [{
347
354
  flags,
348
355
  ...skip === void 0 ? {} : { skip },
@@ -401,191 +408,6 @@ function readKernTable(kern) {
401
408
  }
402
409
  }];
403
410
  }
404
- // src/fonts/binary.ts
405
- /** A big-endian reader over a part of a font file. The offsets are relative to the start of the part. */
406
- var Reader = class Reader {
407
- bytes;
408
- view;
409
- constructor(bytes) {
410
- this.bytes = bytes;
411
- this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
412
- }
413
- get length() {
414
- return this.bytes.byteLength;
415
- }
416
- u8(offset) {
417
- return this.view.getUint8(offset);
418
- }
419
- u16(offset) {
420
- return this.view.getUint16(offset);
421
- }
422
- i16(offset) {
423
- return this.view.getInt16(offset);
424
- }
425
- u32(offset) {
426
- return this.view.getUint32(offset);
427
- }
428
- tag(offset) {
429
- return String.fromCharCode(this.u8(offset), this.u8(offset + 1), this.u8(offset + 2), this.u8(offset + 3));
430
- }
431
- /** This method gives a reader over the bytes from `offset`, with the length `length` or to the end. */
432
- part(offset, length) {
433
- const end = length === void 0 ? this.bytes.byteLength : offset + length;
434
- if (offset < 0 || end > this.bytes.byteLength || offset > end) throw new RangeError(`the part ${offset}..${end} is outside ${this.bytes.byteLength} bytes`);
435
- return new Reader(this.bytes.subarray(offset, end));
436
- }
437
- };
438
- // src/fonts/sfnt.ts
439
- /** The tags of the WOFF2 table directory, in the order of the WOFF2 specification. */
440
- const knownTags = [
441
- "cmap",
442
- "head",
443
- "hhea",
444
- "hmtx",
445
- "maxp",
446
- "name",
447
- "OS/2",
448
- "post",
449
- "cvt ",
450
- "fpgm",
451
- "glyf",
452
- "loca",
453
- "prep",
454
- "CFF ",
455
- "VORG",
456
- "EBDT",
457
- "EBLC",
458
- "gasp",
459
- "hdmx",
460
- "kern",
461
- "LTSH",
462
- "PCLT",
463
- "VDMX",
464
- "vhea",
465
- "vmtx",
466
- "BASE",
467
- "GDEF",
468
- "GPOS",
469
- "GSUB",
470
- "EBSC",
471
- "JSTF",
472
- "MATH",
473
- "CBDT",
474
- "CBLC",
475
- "COLR",
476
- "CPAL",
477
- "SVG ",
478
- "sbix",
479
- "acnt",
480
- "avar",
481
- "bdat",
482
- "bloc",
483
- "bsln",
484
- "cvar",
485
- "fdsc",
486
- "feat",
487
- "fmtx",
488
- "fvar",
489
- "gvar",
490
- "hsty",
491
- "just",
492
- "lcar",
493
- "mort",
494
- "morx",
495
- "opbd",
496
- "prop",
497
- "trak",
498
- "Zapf",
499
- "Silf",
500
- "Glat",
501
- "Gloc",
502
- "Feat",
503
- "Sill"
504
- ];
505
- /** This function gives the tables of a TrueType, OpenType or WOFF2 file. */
506
- function readTables(bytes, decompress) {
507
- const file = new Reader(bytes);
508
- if (file.length < 12) throw new RangeError("the file is too short for a font");
509
- const signature = file.tag(0);
510
- if (signature === "wOF2") {
511
- if (decompress === void 0) throw new Error("a WOFF2 file needs a Brotli decompress function");
512
- return readWoff2(file, decompress);
513
- }
514
- if (signature === "wOFF") throw new Error("WOFF 1 files are not supported");
515
- if (signature === "ttcf") throw new Error("font collections are not supported");
516
- if (file.u32(0) !== 65536 && signature !== "OTTO" && signature !== "true") throw new Error(`the signature ${JSON.stringify(signature)} is not a font`);
517
- const tables = /* @__PURE__ */ new Map();
518
- const count = file.u16(4);
519
- for (let i = 0; i < count; i++) {
520
- const record = 12 + i * 16;
521
- tables.set(file.tag(record), file.part(file.u32(record + 8), file.u32(record + 12)));
522
- }
523
- return tables;
524
- }
525
- /** This function reads a UIntBase128 number of WOFF2 and gives the number and the next offset. */
526
- function base128(file, offset) {
527
- let value = 0;
528
- for (let i = 0; i < 5; i++) {
529
- const byte = file.u8(offset + i);
530
- if (i === 0 && byte === 128) throw new RangeError("a UIntBase128 has a leading zero");
531
- value = value * 128 + (byte & 127);
532
- if ((byte & 128) === 0) return [value, offset + i + 1];
533
- }
534
- throw new RangeError("a UIntBase128 is longer than 5 bytes");
535
- }
536
- /**
537
- * This function gives the tables of a WOFF2 file. The `glyf` and `loca` tables stay in their transformed form,
538
- * because the width does not use them. A transformed `hmtx` table becomes a plain table again.
539
- */
540
- function readWoff2(file, decompress) {
541
- if (file.u32(4) === 1953784678) throw new Error("WOFF2 font collections are not supported");
542
- const count = file.u16(12);
543
- const compressedLength = file.u32(20);
544
- let offset = 48;
545
- const entries = [];
546
- for (let i = 0; i < count; i++) {
547
- const flags = file.u8(offset++);
548
- let tag;
549
- if ((flags & 63) === 63) {
550
- tag = file.tag(offset);
551
- offset += 4;
552
- } else {
553
- const known = knownTags[flags & 63];
554
- if (known === void 0) throw new RangeError(`the WOFF2 tag index ${flags & 63} is unknown`);
555
- tag = known;
556
- }
557
- const version = flags >> 6 & 3;
558
- let length;
559
- [length, offset] = base128(file, offset);
560
- const transformed = tag === "glyf" || tag === "loca" ? version === 0 : version !== 0;
561
- if (transformed) [length, offset] = base128(file, offset);
562
- entries.push({
563
- tag,
564
- length,
565
- transformed
566
- });
567
- }
568
- const data = new Reader(decompress(file.bytes.subarray(offset, offset + compressedLength)));
569
- const tables = /* @__PURE__ */ new Map();
570
- let at = 0;
571
- for (const entry of entries) {
572
- tables.set(entry.tag, data.part(at, entry.length));
573
- at += entry.length;
574
- }
575
- if (entries.find((entry) => entry.tag === "hmtx")?.transformed === true) tables.set("hmtx", untransformHmtx(tables));
576
- return tables;
577
- }
578
- /** This function gives a plain `hmtx` table from the transform of WOFF2. Only the advances matter, so each side bearing is 0. */
579
- function untransformHmtx(tables) {
580
- const source = tables.get("hmtx");
581
- const hhea = tables.get("hhea");
582
- if (hhea === void 0) throw new Error("a transformed hmtx needs hhea");
583
- const metrics = hhea.u16(34);
584
- const plain = new Uint8Array(metrics * 4);
585
- const view = new DataView(plain.buffer);
586
- for (let i = 0; i < metrics; i++) view.setUint16(i * 4, source.u16(1 + i * 2));
587
- return new Reader(plain);
588
- }
589
411
  // src/fonts/substitution.ts
590
412
  /**
591
413
  * The GSUB features that the default model applies: the features that HarfBuzz and Chromium apply to horizontal text
@@ -600,8 +422,6 @@ const defaultFeatures = [
600
422
  "calt",
601
423
  "rclt"
602
424
  ];
603
- /** The lookup flag that selects a mark filtering set of GDEF. */
604
- const USE_MARK_FILTERING_SET = 16;
605
425
  /** The largest depth of nested lookups, as in HarfBuzz. */
606
426
  const MAX_NESTING = 64;
607
427
  /** This function reads the glyph classes, the mark attachment classes and the mark filtering sets of GDEF. */
@@ -714,35 +534,14 @@ var Gsub = class {
714
534
  let found = this.cache.get(index);
715
535
  if (found === void 0) {
716
536
  const table = this.lookups.part(this.lookups.u16(2 + index * 2));
717
- const type = table.u16(0);
718
537
  const flags = table.u16(2);
719
- const count = table.u16(4);
720
- const markSet = (flags & USE_MARK_FILTERING_SET) !== 0 ? table.u16(6 + count * 2) : -1;
721
- const subtables = [];
722
- let subtype = type;
723
- for (let s = 0; s < count; s++) {
724
- const subtable = table.part(table.u16(6 + s * 2));
725
- if (type === 7) {
726
- subtype = subtable.u16(2);
727
- subtables.push(subtable.part(subtable.u32(4)));
728
- } else subtables.push(subtable);
729
- }
730
- const { glyphClass, markAttachClass, inMarkSet } = this.filter;
731
- const skip = (glyph) => {
732
- const kind = glyphClass(glyph);
733
- if ((flags & 2) !== 0 && kind === 1) return true;
734
- if ((flags & 4) !== 0 && kind === 2) return true;
735
- if (kind !== 3) return false;
736
- if ((flags & 8) !== 0) return true;
737
- if (markSet >= 0) return !inMarkSet(markSet, glyph);
738
- const attach = flags >> 8;
739
- return attach !== 0 && markAttachClass(glyph) !== attach;
740
- };
538
+ const parts = subtablesOf(table, 7);
539
+ const subtables = parts.map((part) => part[1]);
741
540
  found = {
742
- type: subtype,
541
+ type: parts.at(-1)?.[0] ?? table.u16(0),
743
542
  flags,
744
543
  subtables,
745
- skip
544
+ skip: lookupSkip(table, flags, this.filter)
746
545
  };
747
546
  this.cache.set(index, found);
748
547
  }
@@ -846,8 +645,9 @@ var Gsub = class {
846
645
  for (let j = from; j < glyphs.length; j++) if (!lookup.skip(glyphs[j])) return j;
847
646
  return -1;
848
647
  }
849
- previousOf(lookup, glyphs, from) {
850
- for (let j = from; j >= 0; j--) if (!lookup.skip(glyphs[j])) return j;
648
+ /** This function finds the next glyph of a backtrack or a lookahead. As in HarfBuzz, a joiner does not stop the context. */
649
+ contextOf(lookup, glyphs, from, step) {
650
+ for (let j = from; j >= 0 && j < glyphs.length; j += step) if (!lookup.skip(glyphs[j]) && !this.filter.joiners?.includes(glyphs[j])) return j;
851
651
  return -1;
852
652
  }
853
653
  /**
@@ -867,7 +667,7 @@ var Gsub = class {
867
667
  matchBacktrack(lookup, glyphs, i, count, test) {
868
668
  let at = i;
869
669
  for (let k = 0; k < count; k++) {
870
- at = this.previousOf(lookup, glyphs, at - 1);
670
+ at = this.contextOf(lookup, glyphs, at - 1, -1);
871
671
  if (at < 0 || !this.admits(at, i, false) || !test(k, glyphs[at])) return false;
872
672
  }
873
673
  return true;
@@ -875,7 +675,7 @@ var Gsub = class {
875
675
  matchLookahead(lookup, glyphs, first, last, count, test) {
876
676
  let at = last;
877
677
  for (let k = 0; k < count; k++) {
878
- at = this.nextOf(lookup, glyphs, at + 1);
678
+ at = this.contextOf(lookup, glyphs, at + 1, 1);
879
679
  if (at < 0 || !this.admits(at, first, false) || !test(k, glyphs[at])) return false;
880
680
  }
881
681
  return true;
@@ -1110,19 +910,8 @@ var Gsub = class {
1110
910
  * HarfBuzz applies them. The lookups include the lookups of the necessary feature of the language system.
1111
911
  */
1112
912
  function readSubstitutions(gsub, script, features, filter) {
1113
- const scripts = gsub.part(gsub.u16(4));
1114
913
  const featureList = gsub.part(gsub.u16(6));
1115
- const scriptCount = scripts.u16(0);
1116
- let langSys;
1117
- for (const wanted of scriptTags(script)) {
1118
- for (let i = 0; i < scriptCount && langSys === void 0; i++) {
1119
- if (scripts.tag(2 + i * 6) !== wanted) continue;
1120
- const table = scripts.part(scripts.u16(2 + i * 6 + 4));
1121
- const fallback = table.u16(0);
1122
- if (fallback !== 0) langSys = table.part(fallback);
1123
- }
1124
- if (langSys !== void 0) break;
1125
- }
914
+ const langSys = languageSystem(gsub, script);
1126
915
  if (langSys === void 0 || features.length === 0) return [];
1127
916
  const wanted = new Set(features);
1128
917
  const indices = /* @__PURE__ */ new Map();
@@ -1160,6 +949,232 @@ function readSubstitutions(gsub, script, features, filter) {
1160
949
  }
1161
950
  return result;
1162
951
  }
952
+ // src/fonts/context.ts
953
+ /**
954
+ * This function gives a test for each such lookup that can change an advance. A chained rule of format 3 must match.
955
+ * Another rule needs only its coverage. With `zeroesMarks`, HarfBuzz zeroes a mark after GPOS, so a value on a mark changes no width.
956
+ */
957
+ function readContextKerning(gpos, script, filter, zeroesMarks) {
958
+ const lookups = gpos.part(gpos.u16(8));
959
+ const lookupAt = (index) => lookups.part(lookups.u16(2 + index * 2));
960
+ const changes = (type, t, depth) => {
961
+ if (type === 7 || type === 8) return depth < 8 && (type === 7 ? contextRecords(t) : chainRecords(t)).some((record) => subtablesOf(lookupAt(record.lookupIndex), 9).some(([nested, table]) => changes(nested, table, depth + 1)));
962
+ return type === 3 || (type === 1 || type === 2) && ((t.u16(4) | (type === 2 ? t.u16(6) : 0)) & 68) !== 0;
963
+ };
964
+ const applies = (index, glyphs, at, depth) => {
965
+ const lookup = lookupAt(index);
966
+ const skip = lookupSkip(lookup, lookup.u16(2), filter);
967
+ return !skip(glyphs[at]) && subtablesOf(lookup, 9).some(([type, t]) => {
968
+ if (depth === 0 && type === 2 || !changes(type, t, depth) || type < 7 && zeroesMarks && filter.glyphClass(glyphs[at]) === 3) return false;
969
+ const covers = (offset, glyph) => coverageIndex(t.part(t.u16(offset)), glyph) >= 0;
970
+ if (type !== 8 || t.u16(0) !== 3) return covers(type === 7 && t.u16(0) === 3 ? 6 : 2, glyphs[at]);
971
+ const walk = (from, step, offset, count, seen = []) => {
972
+ let i = from;
973
+ for (let k = 0; k < count; k++) {
974
+ do
975
+ i += step;
976
+ while (i >= 0 && i < glyphs.length && skip(glyphs[i]));
977
+ if (i < 0 || i >= glyphs.length || !covers(offset + k * 2, glyphs[i])) return -1;
978
+ seen.push(i);
979
+ }
980
+ return i;
981
+ };
982
+ const inputAt = 4 + t.u16(2) * 2;
983
+ const aheadAt = inputAt + 2 + t.u16(inputAt) * 2;
984
+ const positions = [at];
985
+ if (!covers(inputAt + 2, glyphs[at]) || walk(at, -1, 4, t.u16(2)) < 0) return false;
986
+ const last = walk(at, 1, inputAt + 4, t.u16(inputAt) - 1, positions);
987
+ if (last < 0 || walk(last, 1, aheadAt + 2, t.u16(aheadAt)) < 0) return false;
988
+ return chainRecords(t).some((record) => record.sequenceIndex < positions.length && applies(record.lookupIndex, glyphs, positions[record.sequenceIndex], depth + 1));
989
+ });
990
+ };
991
+ return pairLookupIndices(gpos, script).filter((index) => subtablesOf(lookupAt(index), 9).some(([type, t]) => type !== 2 && changes(type, t, 0))).map((index) => (glyphs) => glyphs.some((_, at) => applies(index, glyphs, at, 0)));
992
+ }
993
+ // src/fonts/binary.ts
994
+ /** A big-endian reader over a part of a font file. The offsets are relative to the start of the part. */
995
+ var Reader = class Reader {
996
+ bytes;
997
+ view;
998
+ constructor(bytes) {
999
+ this.bytes = bytes;
1000
+ this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
1001
+ }
1002
+ get length() {
1003
+ return this.bytes.byteLength;
1004
+ }
1005
+ u8(offset) {
1006
+ return this.view.getUint8(offset);
1007
+ }
1008
+ u16(offset) {
1009
+ return this.view.getUint16(offset);
1010
+ }
1011
+ i16(offset) {
1012
+ return this.view.getInt16(offset);
1013
+ }
1014
+ u32(offset) {
1015
+ return this.view.getUint32(offset);
1016
+ }
1017
+ tag(offset) {
1018
+ return String.fromCharCode(this.u8(offset), this.u8(offset + 1), this.u8(offset + 2), this.u8(offset + 3));
1019
+ }
1020
+ /** This method gives a reader over the bytes from `offset`, with the length `length` or to the end. */
1021
+ part(offset, length) {
1022
+ const end = length === void 0 ? this.bytes.byteLength : offset + length;
1023
+ if (offset < 0 || end > this.bytes.byteLength || offset > end) throw new RangeError(`the part ${offset}..${end} is outside ${this.bytes.byteLength} bytes`);
1024
+ return new Reader(this.bytes.subarray(offset, end));
1025
+ }
1026
+ };
1027
+ // src/fonts/sfnt.ts
1028
+ /** The tags of the WOFF2 table directory, in the order of the WOFF2 specification. */
1029
+ const knownTags = [
1030
+ "cmap",
1031
+ "head",
1032
+ "hhea",
1033
+ "hmtx",
1034
+ "maxp",
1035
+ "name",
1036
+ "OS/2",
1037
+ "post",
1038
+ "cvt ",
1039
+ "fpgm",
1040
+ "glyf",
1041
+ "loca",
1042
+ "prep",
1043
+ "CFF ",
1044
+ "VORG",
1045
+ "EBDT",
1046
+ "EBLC",
1047
+ "gasp",
1048
+ "hdmx",
1049
+ "kern",
1050
+ "LTSH",
1051
+ "PCLT",
1052
+ "VDMX",
1053
+ "vhea",
1054
+ "vmtx",
1055
+ "BASE",
1056
+ "GDEF",
1057
+ "GPOS",
1058
+ "GSUB",
1059
+ "EBSC",
1060
+ "JSTF",
1061
+ "MATH",
1062
+ "CBDT",
1063
+ "CBLC",
1064
+ "COLR",
1065
+ "CPAL",
1066
+ "SVG ",
1067
+ "sbix",
1068
+ "acnt",
1069
+ "avar",
1070
+ "bdat",
1071
+ "bloc",
1072
+ "bsln",
1073
+ "cvar",
1074
+ "fdsc",
1075
+ "feat",
1076
+ "fmtx",
1077
+ "fvar",
1078
+ "gvar",
1079
+ "hsty",
1080
+ "just",
1081
+ "lcar",
1082
+ "mort",
1083
+ "morx",
1084
+ "opbd",
1085
+ "prop",
1086
+ "trak",
1087
+ "Zapf",
1088
+ "Silf",
1089
+ "Glat",
1090
+ "Gloc",
1091
+ "Feat",
1092
+ "Sill"
1093
+ ];
1094
+ /** This function gives the tables of a TrueType, OpenType or WOFF2 file. */
1095
+ function readTables(bytes, decompress) {
1096
+ const file = new Reader(bytes);
1097
+ if (file.length < 12) throw new RangeError("the file is too short for a font");
1098
+ const signature = file.tag(0);
1099
+ if (signature === "wOF2") {
1100
+ if (decompress === void 0) throw new Error("a WOFF2 file needs a Brotli decompress function");
1101
+ return readWoff2(file, decompress);
1102
+ }
1103
+ if (signature === "wOFF") throw new Error("WOFF 1 files are not supported");
1104
+ if (signature === "ttcf") throw new Error("font collections are not supported");
1105
+ if (file.u32(0) !== 65536 && signature !== "OTTO" && signature !== "true") throw new Error(`the signature ${JSON.stringify(signature)} is not a font`);
1106
+ const tables = /* @__PURE__ */ new Map();
1107
+ const count = file.u16(4);
1108
+ for (let i = 0; i < count; i++) {
1109
+ const record = 12 + i * 16;
1110
+ tables.set(file.tag(record), file.part(file.u32(record + 8), file.u32(record + 12)));
1111
+ }
1112
+ return tables;
1113
+ }
1114
+ /** This function reads a UIntBase128 number of WOFF2 and gives the number and the next offset. */
1115
+ function base128(file, offset) {
1116
+ let value = 0;
1117
+ for (let i = 0; i < 5; i++) {
1118
+ const byte = file.u8(offset + i);
1119
+ if (i === 0 && byte === 128) throw new RangeError("a UIntBase128 has a leading zero");
1120
+ value = value * 128 + (byte & 127);
1121
+ if ((byte & 128) === 0) return [value, offset + i + 1];
1122
+ }
1123
+ throw new RangeError("a UIntBase128 is longer than 5 bytes");
1124
+ }
1125
+ /**
1126
+ * This function gives the tables of a WOFF2 file. The `glyf` and `loca` tables stay in their transformed form,
1127
+ * because the width does not use them. A transformed `hmtx` table becomes a plain table again.
1128
+ */
1129
+ function readWoff2(file, decompress) {
1130
+ if (file.u32(4) === 1953784678) throw new Error("WOFF2 font collections are not supported");
1131
+ const count = file.u16(12);
1132
+ const compressedLength = file.u32(20);
1133
+ let offset = 48;
1134
+ const entries = [];
1135
+ for (let i = 0; i < count; i++) {
1136
+ const flags = file.u8(offset++);
1137
+ let tag;
1138
+ if ((flags & 63) === 63) {
1139
+ tag = file.tag(offset);
1140
+ offset += 4;
1141
+ } else {
1142
+ const known = knownTags[flags & 63];
1143
+ if (known === void 0) throw new RangeError(`the WOFF2 tag index ${flags & 63} is unknown`);
1144
+ tag = known;
1145
+ }
1146
+ const version = flags >> 6 & 3;
1147
+ let length;
1148
+ [length, offset] = base128(file, offset);
1149
+ const transformed = tag === "glyf" || tag === "loca" ? version === 0 : version !== 0;
1150
+ if (transformed) [length, offset] = base128(file, offset);
1151
+ entries.push({
1152
+ tag,
1153
+ length,
1154
+ transformed
1155
+ });
1156
+ }
1157
+ const data = new Reader(decompress(file.bytes.subarray(offset, offset + compressedLength)));
1158
+ const tables = /* @__PURE__ */ new Map();
1159
+ let at = 0;
1160
+ for (const entry of entries) {
1161
+ tables.set(entry.tag, data.part(at, entry.length));
1162
+ at += entry.length;
1163
+ }
1164
+ if (entries.find((entry) => entry.tag === "hmtx")?.transformed === true) tables.set("hmtx", untransformHmtx(tables));
1165
+ return tables;
1166
+ }
1167
+ /** This function gives a plain `hmtx` table from the transform of WOFF2. Only the advances matter, so each side bearing is 0. */
1168
+ function untransformHmtx(tables) {
1169
+ const source = tables.get("hmtx");
1170
+ const hhea = tables.get("hhea");
1171
+ if (hhea === void 0) throw new Error("a transformed hmtx needs hhea");
1172
+ const metrics = hhea.u16(34);
1173
+ const plain = new Uint8Array(metrics * 4);
1174
+ const view = new DataView(plain.buffer);
1175
+ for (let i = 0; i < metrics; i++) view.setUint16(i * 4, source.u16(1 + i * 2));
1176
+ return new Reader(plain);
1177
+ }
1163
1178
  // src/fonts/variations.ts
1164
1179
  /** This function reads a 16.16 fixed number. */
1165
1180
  function fixed(table, offset) {
@@ -1521,12 +1536,114 @@ function gvarAdvanceDeltas(gvar, pointCounts, coordinates, deltas) {
1521
1536
  }
1522
1537
  return result;
1523
1538
  }
1539
+ // src/fonts/machine.ts
1540
+ /** This function gives a pattern that matches one of the letters. */
1541
+ const set = (letters) => ({
1542
+ kind: "set",
1543
+ letters
1544
+ });
1545
+ /** The pattern that matches any one letter. */
1546
+ const any = { kind: "any" };
1547
+ /** This function gives a pattern that matches the parts one after the other. */
1548
+ const seq = (...parts) => ({
1549
+ kind: "seq",
1550
+ parts
1551
+ });
1552
+ /** This function gives a pattern that matches one of the parts. */
1553
+ const alt = (...parts) => ({
1554
+ kind: "alt",
1555
+ parts
1556
+ });
1557
+ /** This function gives a pattern that matches the part 0 or more times. */
1558
+ const star = (part) => ({
1559
+ kind: "star",
1560
+ part
1561
+ });
1562
+ /** This function gives a pattern that matches the part 0 or 1 times. */
1563
+ const opt = (part) => ({
1564
+ kind: "opt",
1565
+ part
1566
+ });
1567
+ /** This function builds the automaton of a pattern, by the construction of Thompson. */
1568
+ function compile(pattern) {
1569
+ const states = [{}];
1570
+ const build = (p, next) => {
1571
+ if (typeof p === "string") return [...p].reduceRight((to, letter) => states.push({
1572
+ letters: letter,
1573
+ next: to
1574
+ }) - 1, next);
1575
+ switch (p.kind) {
1576
+ case "set": return states.push({
1577
+ letters: p.letters,
1578
+ next
1579
+ }) - 1;
1580
+ case "any": return states.push({
1581
+ letters: null,
1582
+ next
1583
+ }) - 1;
1584
+ case "seq": return p.parts.reduceRight((to, part) => build(part, to), next);
1585
+ case "alt": return states.push({ split: p.parts.map((part) => build(part, next)) }) - 1;
1586
+ case "opt": return states.push({ split: [build(p.part, next), next] }) - 1;
1587
+ case "star": {
1588
+ const loop = states.push({ split: [] }) - 1;
1589
+ states[loop].split = [build(p.part, loop), next];
1590
+ return loop;
1591
+ }
1592
+ }
1593
+ };
1594
+ return {
1595
+ states,
1596
+ start: build(pattern, 0)
1597
+ };
1598
+ }
1599
+ /** This function adds a state and each state that it reaches with no letter to a set. */
1600
+ function close(machine, state, into) {
1601
+ const stack = [state];
1602
+ while (stack.length > 0) {
1603
+ const s = stack.pop();
1604
+ if (into.has(s)) continue;
1605
+ into.add(s);
1606
+ for (const to of machine.states[s].split ?? []) stack.push(to);
1607
+ }
1608
+ }
1609
+ /**
1610
+ * This function gives the longest match of the rules in `text` from `at`, which ends at `end` or before it. A tie of two
1611
+ * rules goes to the first rule. A match has at least one letter. It gives `undefined` when no rule matches.
1612
+ */
1613
+ function longestMatch(machines, text, at, end) {
1614
+ let best;
1615
+ let current = machines.map((machine) => {
1616
+ const states = /* @__PURE__ */ new Set();
1617
+ close(machine, machine.start, states);
1618
+ return states;
1619
+ });
1620
+ for (let position = at; position < end && current.some((states) => states.size > 0); position++) {
1621
+ const letter = text[position];
1622
+ current = current.map((states, rule) => {
1623
+ const machine = machines[rule];
1624
+ const next = /* @__PURE__ */ new Set();
1625
+ for (const s of states) {
1626
+ const state = machine.states[s];
1627
+ if (state.letters !== void 0 && (state.letters === null || state.letters.includes(letter))) close(machine, state.next, next);
1628
+ }
1629
+ return next;
1630
+ });
1631
+ const length = position + 1 - at;
1632
+ const rule = current.findIndex((states) => states.has(0));
1633
+ if (rule >= 0) best = {
1634
+ length,
1635
+ rule
1636
+ };
1637
+ }
1638
+ return best;
1639
+ }
1524
1640
  // src/fonts/normalize.ts
1525
1641
  /**
1526
1642
  * The Unicode normalization of HarfBuzz, from `hb-ot-shape-normalize.cc`. HarfBuzz decomposes each cluster of a base and its marks.
1527
1643
  * It then sorts the marks, and composes each mark with its base when the font has the glyph of the composite.
1528
1644
  */
1529
1645
  const unicodeMark = /\p{M}/u;
1646
+ const nonspacingMark = /\p{Mn}/u;
1530
1647
  /**
1531
1648
  * The classes of HarfBuzz for the marks of Hebrew and Arabic, from `hb-unicode.hh`. Their order is not the canonical order:
1532
1649
  * for example the shadda comes first. Each other pair of marks uses the canonical order.
@@ -1567,6 +1684,16 @@ const sortClasses = /* @__PURE__ */ new Map([
1567
1684
  [2289, 29],
1568
1685
  [2290, 30]
1569
1686
  ]);
1687
+ /** The default ignorables of `hb-unicode.hh`. This set is not the Unicode property: it does not have the Hangul fillers. */
1688
+ const defaultIgnorable = /[\u00ad\u034f\u061c\u17b4\u17b5\u180b-\u180f\u200b-\u200f\u202a-\u202e\u2060-\u206f\ufe00-\ufe0f\ufeff\ufff0-\ufff8\u{1d173}-\u{1d17a}\u{e0000}-\u{e0fff}]/u;
1689
+ /** This function tells if a text has a default ignorable. */
1690
+ function hasDefaultIgnorable(text) {
1691
+ return defaultIgnorable.test(text);
1692
+ }
1693
+ /** This function tells if the model hides a code point. A hidden code point takes no advance, and the kerning skips it. */
1694
+ function isHidden(codePoint) {
1695
+ return hasDefaultIgnorable(String.fromCodePoint(codePoint));
1696
+ }
1570
1697
  /** This function tells if a code point is a mark of Unicode. The first mark of Unicode is U+0300. */
1571
1698
  function isMark(codePoint) {
1572
1699
  return codePoint >= 768 && unicodeMark.test(String.fromCodePoint(codePoint));
@@ -1575,6 +1702,18 @@ function isMark(codePoint) {
1575
1702
  function hasMark(text) {
1576
1703
  return unicodeMark.test(text);
1577
1704
  }
1705
+ /** This function tells which code points take no advance: nonspacing marks with `classes`, marks with a class after a base with `fallback`. */
1706
+ function zeroedMarks(codePoints, classes, fallback) {
1707
+ let base = false;
1708
+ return codePoints.map((codePoint) => {
1709
+ const character = String.fromCodePoint(codePoint);
1710
+ if (!isMark(codePoint)) {
1711
+ base = true;
1712
+ return false;
1713
+ }
1714
+ return classes && nonspacingMark.test(character) && !hasDefaultIgnorable(character) || fallback && base && !isStarter(codePoint);
1715
+ });
1716
+ }
1578
1717
  function decomposed(codePoint) {
1579
1718
  return String.fromCodePoint(codePoint).normalize("NFD");
1580
1719
  }
@@ -1991,48 +2130,54 @@ const letters = /* @__PURE__ */ new Map([
1991
2130
  [CS, "S"],
1992
2131
  [SMPST, "t"]
1993
2132
  ]);
1994
- const c = "[cR]";
1995
- const n = "(?:w?r)?(?:nn?)?";
1996
- const z = "[wj]";
1997
- const reph = "(?:Rh|f)";
1998
- const sm = "[st]";
1999
- const cn = `${c}j?${n}`;
2000
- const matraGroup = `${z}*(?:m|${sm}?q)n?h?`;
2001
- const syllableTail = `(?:${z}?${sm}${sm}?w?)?a*`;
2002
- const halantGroup = `(?:${z}?h(?:jn?)?)`;
2003
- const complexSyllableTail = `(?:${halantGroup}${cn})*k?(?:${`(?:${halantGroup}|hw)`}|(?:${matraGroup})*)${syllableTail}`;
2004
- /** The rules of the machine in the order of HarfBuzz. A tie of two lengths goes to the first rule. */
2005
- const machine = [
2006
- [`[fS]?${cn}${complexSyllableTail}`, CONSONANT_SYLLABLE],
2007
- [`${reph}?v${n}(?:j|${complexSyllableTail})`, VOWEL_SYLLABLE],
2008
- [`(?:[fS]?p|${reph}?d)${n}${complexSyllableTail}`, STANDALONE_CLUSTER],
2009
- [`yn?${syllableTail}`, SYMBOL_CLUSTER],
2133
+ const c = set("cR");
2134
+ const n = seq(opt(seq(opt("w"), "r")), opt(seq("n", opt("n"))));
2135
+ const z = set("wj");
2136
+ const reph = alt("Rh", "f");
2137
+ const sm = set("st");
2138
+ const cn = seq(c, opt("j"), n);
2139
+ const matraGroup = seq(star(z), alt("m", seq(opt(sm), "q")), opt("n"), opt("h"));
2140
+ const syllableTail = seq(opt(seq(opt(z), sm, opt(sm), opt("w"))), star("a"));
2141
+ const halantGroup = seq(opt(z), "h", opt(seq("j", opt("n"))));
2142
+ const finalHalantGroup = alt(halantGroup, "hw");
2143
+ const complexSyllableTail = seq(star(seq(halantGroup, cn)), opt("k"), alt(finalHalantGroup, star(matraGroup)), syllableTail);
2144
+ /** The rules of the machine in the order of HarfBuzz, with the type of each. A tie of two lengths goes to the first rule. */
2145
+ const indicRules = [
2146
+ [seq(opt(set("fS")), cn, complexSyllableTail), CONSONANT_SYLLABLE],
2147
+ [seq(opt(reph), "v", n, alt("j", complexSyllableTail)), VOWEL_SYLLABLE],
2148
+ [seq(alt(seq(opt(set("fS")), "p"), seq(opt(reph), "d")), n, complexSyllableTail), STANDALONE_CLUSTER],
2149
+ [seq("y", opt("n"), syllableTail), SYMBOL_CLUSTER],
2010
2150
  ["t", NON_INDIC_CLUSTER],
2011
- [`${reph}?${n}${complexSyllableTail}`, BROKEN_CLUSTER],
2012
- ["[^]", NON_INDIC_CLUSTER]
2013
- ].map(([rule, type]) => [new RegExp(`^(?:${rule})$`), type]);
2151
+ [seq(opt(reph), n, complexSyllableTail), BROKEN_CLUSTER],
2152
+ [any, NON_INDIC_CLUSTER]
2153
+ ];
2154
+ const machines = indicRules.map(([rule]) => compile(rule));
2155
+ /**
2156
+ * This function divides a text of category letters into syllables, as the machine of HarfBuzz does. At each position,
2157
+ * it takes the longest match of the rules, in one pass. It gives the length and the type of each syllable.
2158
+ */
2159
+ function letterSyllables(text) {
2160
+ const out = [];
2161
+ let at = 0;
2162
+ while (at < text.length) {
2163
+ const next = text.indexOf("x", at + 1);
2164
+ const match = longestMatch(machines, text, at, next < 0 ? text.length : next);
2165
+ const length = match?.length ?? 1;
2166
+ out.push([length, match === void 0 ? NON_INDIC_CLUSTER : indicRules[match.rule][1]]);
2167
+ at += length;
2168
+ }
2169
+ return out;
2170
+ }
2014
2171
  /**
2015
- * This function divides the glyphs into syllables, as the machine of HarfBuzz does. At each position, it takes the
2016
- * longest match of the rules. Each glyph gets the syllable byte. Its high 4 bits are a serial number from 1 to 15, and
2017
- * its low 4 bits are the type. The function tells if a syllable is a broken cluster.
2172
+ * This function divides the glyphs into syllables. Each glyph gets the syllable byte. Its high 4 bits are a serial
2173
+ * number from 1 to 15, and its low 4 bits are the type. The function tells if a syllable is a broken cluster.
2018
2174
  */
2019
2175
  function findSyllables(infos) {
2020
2176
  const text = infos.map((info) => letters.get(info.category) ?? "x").join("");
2021
2177
  let serial = 1;
2022
2178
  let broken = false;
2023
2179
  let at = 0;
2024
- while (at < text.length) {
2025
- const next = text.indexOf("x", at + 1);
2026
- let length = 1;
2027
- let type = NON_INDIC_CLUSTER;
2028
- search: for (let end = next < 0 ? text.length : next; end > at; end--) {
2029
- const piece = text.slice(at, end);
2030
- for (const [rule, kind] of machine) if (rule.test(piece)) {
2031
- length = end - at;
2032
- type = kind;
2033
- break search;
2034
- }
2035
- }
2180
+ for (const [length, type] of letterSyllables(text)) {
2036
2181
  if (type === BROKEN_CLUSTER) broken = true;
2037
2182
  for (let i = at; i < at + length; i++) infos[i].syllable = serial << 4 | type;
2038
2183
  serial = serial === 15 ? 1 : serial + 1;
@@ -2040,6 +2185,38 @@ function findSyllables(infos) {
2040
2185
  }
2041
2186
  return broken;
2042
2187
  }
2188
+ const syllableTypes = [
2189
+ "consonant",
2190
+ "vowel",
2191
+ "standalone",
2192
+ "symbol",
2193
+ "broken",
2194
+ "non-indic"
2195
+ ];
2196
+ /**
2197
+ * This function divides a text into the syllables of the Indic machine of HarfBuzz. It does not normalize the text and
2198
+ * does not insert dotted circles.
2199
+ */
2200
+ function indicSyllables(text) {
2201
+ const characters = [...text];
2202
+ const infos = characters.map((character) => {
2203
+ const codePoint = character.codePointAt(0);
2204
+ const [category, position] = indicProperties(codePoint);
2205
+ return {
2206
+ mask: GLOBAL,
2207
+ syllable: 0,
2208
+ props: 0,
2209
+ category,
2210
+ position,
2211
+ codePoint
2212
+ };
2213
+ });
2214
+ findSyllables(infos);
2215
+ return syllables(infos).map(([start, end]) => ({
2216
+ text: characters.slice(start, end).join(""),
2217
+ type: syllableTypes[infos[start].syllable & 15]
2218
+ }));
2219
+ }
2043
2220
  /** It is true when the glyph made no ligature and its category is one of `categories`. A ligature has no category. */
2044
2221
  function isOneOf(info, categories) {
2045
2222
  return (info.props & 32) === 0 && categories.includes(info.category);
@@ -2406,7 +2583,7 @@ function indicShaper(font, script, features) {
2406
2583
  for (const { lookups, mask } of basic) for (const lookup of lookups) lookup.applyInfos(run.glyphs, run.infos, mask, true);
2407
2584
  for (const [start, end] of syllables(run.infos)) finalReordering(run, start, end);
2408
2585
  for (const { lookup, mask, perSyllable } of last) lookup.applyInfos(run.glyphs, run.infos, mask, perSyllable);
2409
- return run.glyphs;
2586
+ return run.glyphs.map((glyph, i) => (run.infos[i].props & 16) === 0 && isHidden(run.infos[i].codePoint) ? ~glyph : glyph);
2410
2587
  };
2411
2588
  }
2412
2589
  // src/fonts/joining.ts
@@ -2738,12 +2915,18 @@ const joiningTable = [
2738
2915
  2274,
2739
2916
  2274,
2740
2917
  U,
2918
+ 6158,
2919
+ 6158,
2920
+ U,
2741
2921
  8204,
2742
2922
  8204,
2743
2923
  U,
2744
2924
  8205,
2745
2925
  8205,
2746
- D
2926
+ D,
2927
+ 8294,
2928
+ 8297,
2929
+ U
2747
2930
  ];
2748
2931
  const transparent = /[\p{Mn}\p{Me}\p{Cf}]/u;
2749
2932
  /** This function gives the joining type of a code point: a column of the state machine, or `T`. */
@@ -3534,6 +3717,7 @@ function build(parsed, requested, modes) {
3534
3717
  const kern = tables.get("kern");
3535
3718
  const gsub = tables.get("GSUB");
3536
3719
  const lookups = /* @__PURE__ */ new Map();
3720
+ const contexts = /* @__PURE__ */ new Map();
3537
3721
  const substitutions = /* @__PURE__ */ new Map();
3538
3722
  const hashed = [
3539
3723
  tables.get("cmap"),
@@ -3549,6 +3733,8 @@ function build(parsed, requested, modes) {
3549
3733
  epoch: `${header.revision.toString(16).padStart(8, "0")}-${widthHash(header.unitsPerEm, coordinates, advances, hashed)}${deltas === "exact" ? "-exact" : ""}${advanceSource === "gvar" && hvar !== void 0 ? "-gvar" : ""}${metrics !== void 0 ? "-metrics" : ""}`,
3550
3734
  unitsPerEm: header.unitsPerEm,
3551
3735
  numGlyphs: header.numGlyphs,
3736
+ hasPositioning: gpos !== void 0,
3737
+ hasGlyphClasses: gdef !== void 0 && gdef.u16(4) !== 0,
3552
3738
  axes,
3553
3739
  instance,
3554
3740
  coordinates,
@@ -3564,11 +3750,19 @@ function build(parsed, requested, modes) {
3564
3750
  }
3565
3751
  return found;
3566
3752
  },
3753
+ contextKerning(script) {
3754
+ if (!contexts.has(script)) contexts.set(script, gpos === void 0 ? [] : readContextKerning(gpos, script, parsed.filter, !noMarkZero.includes(script)));
3755
+ return contexts.get(script);
3756
+ },
3567
3757
  substitutions(script, features) {
3568
3758
  const key = `${script} ${features.join(",")}`;
3569
3759
  let found = substitutions.get(key);
3570
3760
  if (found === void 0) {
3571
- found = gsub === void 0 ? [] : readSubstitutions(gsub, script, features, parsed.filter);
3761
+ const joiners = script === "deva" ? [] : [characters.get(8204) ?? 0, characters.get(8205) ?? 0].filter((glyph) => glyph !== 0);
3762
+ found = gsub === void 0 ? [] : readSubstitutions(gsub, script, features, {
3763
+ ...parsed.filter,
3764
+ joiners
3765
+ });
3572
3766
  substitutions.set(key, found);
3573
3767
  }
3574
3768
  return found;
@@ -3648,6 +3842,25 @@ function shapingOf(font, options) {
3648
3842
  kerning
3649
3843
  };
3650
3844
  }
3845
+ /** The bits of a mask: all the glyphs, a default ignorable, a mark with no advance. */
3846
+ const GLOBAL_MASK = 128;
3847
+ const IGNORABLE_MASK = 256;
3848
+ const ZEROED_MASK = 512;
3849
+ /** The scripts whose shaper in HarfBuzz has no fallback mark position. */
3850
+ const noFallbackPosition = [
3851
+ "thai",
3852
+ "lao ",
3853
+ "khmr",
3854
+ "mymr",
3855
+ "mym2",
3856
+ ...indicScripts
3857
+ ];
3858
+ /** The scripts whose shaper in HarfBuzz does not zero the marks. */
3859
+ const noMarkZero = [
3860
+ "hang",
3861
+ "khmr",
3862
+ ...indicScripts
3863
+ ];
3651
3864
  /** The GSUB pass of one set of options: a function that gives the glyphs of one word, and the kerning lookups. */
3652
3865
  function shaperOf(font, options) {
3653
3866
  const { script, features, shaper, kerning } = shapingOf(font, options);
@@ -3655,6 +3868,8 @@ function shaperOf(font, options) {
3655
3868
  substitute: indicShaper(font, script, features),
3656
3869
  kerning
3657
3870
  };
3871
+ const classes = font.hasGlyphClasses === false && !noMarkZero.includes(script);
3872
+ const fallback = font.hasPositioning === false && !noFallbackPosition.includes(script);
3658
3873
  const joins = shaper === "joining";
3659
3874
  const lookups = joins ? [] : font.substitutions(script, features);
3660
3875
  const stages = joins ? joiningStages(features, features.includes("rclt") && font.substitutions(script, ["rclt"]).length > 0).map((stage) => ({
@@ -3673,15 +3888,20 @@ function shaperOf(font, options) {
3673
3888
  codePoints = normalizeCodePoints(codePoints, rules);
3674
3889
  wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
3675
3890
  }
3676
- for (const lookup of lookups) lookup.apply(wordGlyphs);
3677
- if (joins) {
3678
- const masks = joiningForms(codePoints).map((form) => 128 | (form === void 0 ? 0 : 1 << joiningFeatures.indexOf(form)));
3679
- for (const { stage, lookups: stageLookups } of stages) {
3680
- const mask = stage.form === void 0 ? 128 : 1 << joiningFeatures.indexOf(stage.form);
3681
- for (const lookup of stageLookups) lookup.apply(wordGlyphs, masks, mask);
3682
- }
3683
- }
3684
- return wordGlyphs;
3891
+ const hides = hasDefaultIgnorable(word);
3892
+ const zeroed = (classes || fallback) && hasMark(word) ? zeroedMarks(codePoints, classes, fallback) : [];
3893
+ const forms = joins ? joiningForms(codePoints) : [];
3894
+ const masks = joins || hides || zeroed.length > 0 ? codePoints.map((codePoint, i) => GLOBAL_MASK | (forms[i] === void 0 ? 0 : 1 << joiningFeatures.indexOf(forms[i])) | (hides && isHidden(codePoint) ? IGNORABLE_MASK : 0) | (zeroed[i] === true ? ZEROED_MASK : 0)) : void 0;
3895
+ const before = hides ? [...wordGlyphs] : [];
3896
+ for (const lookup of lookups) lookup.apply(wordGlyphs, masks, GLOBAL_MASK);
3897
+ for (const { stage, lookups: stageLookups } of stages) {
3898
+ const mask = stage.form === void 0 ? GLOBAL_MASK : 1 << joiningFeatures.indexOf(stage.form);
3899
+ for (const lookup of stageLookups) lookup.apply(wordGlyphs, masks, mask);
3900
+ }
3901
+ return masks === void 0 ? wordGlyphs : wordGlyphs.map((glyph, i) => {
3902
+ const mask = masks[i] ?? 0;
3903
+ return (mask & ZEROED_MASK) !== 0 || (mask & IGNORABLE_MASK) !== 0 && before.includes(glyph) ? ~glyph : glyph;
3904
+ });
3685
3905
  };
3686
3906
  return {
3687
3907
  substitute,
@@ -3695,6 +3915,14 @@ function shaperOf(font, options) {
3695
3915
  * the joining forms of each word, in the stages of `joiningStages`. A script of `indicScripts` uses `indicShaper`.
3696
3916
  */
3697
3917
  function glyphAdvances(font, text, options = {}) {
3918
+ const { glyphs, advances } = shapedAdvances(font, text, options);
3919
+ return {
3920
+ glyphs: glyphs.map((glyph) => glyph < 0 ? ~glyph : glyph),
3921
+ advances
3922
+ };
3923
+ }
3924
+ /** This function is `glyphAdvances`, with `~glyph` for a hidden glyph. It has no advance, and the kerning skips it. */
3925
+ function shapedAdvances(font, text, options) {
3698
3926
  const { substitute, kerning } = shaperOf(font, options);
3699
3927
  const canvas = options.shaping === "canvas";
3700
3928
  const glyphs = [];
@@ -3732,7 +3960,7 @@ function kerningPosition(value, delta, scale) {
3732
3960
  function kern(font, lookups, glyphs, advances, positions, scale) {
3733
3961
  for (const lookup of lookups) {
3734
3962
  const next = (from) => {
3735
- for (let j = from; j < glyphs.length; j++) if (!(lookup.skip?.(glyphs[j]) ?? skips(lookup.flags, font.glyphClass(glyphs[j])))) return j;
3963
+ for (let j = from; j < glyphs.length; j++) if (glyphs[j] >= 0 && !(lookup.skip?.(glyphs[j]) ?? skips(lookup.flags, font.glyphClass(glyphs[j])))) return j;
3736
3964
  return -1;
3737
3965
  };
3738
3966
  let i = next(0);
@@ -3785,7 +4013,7 @@ function float32Width(font, text, size, options) {
3785
4013
  */
3786
4014
  function measureWidth(font, text, size, options = {}) {
3787
4015
  if (options.sum === "float32" && options.rounding !== "pixel") return float32Width(font, text, size, options);
3788
- const { glyphs, advances } = glyphAdvances(font, text, options);
4016
+ const { glyphs, advances } = shapedAdvances(font, text, options);
3789
4017
  if (options.rounding === "pixel") {
3790
4018
  const scale = size / font.unitsPerEm;
3791
4019
  let px = 0;
@@ -3799,6 +4027,6 @@ function measureWidth(font, text, size, options = {}) {
3799
4027
  for (const advance of advances) units += advance;
3800
4028
  return units * size / font.unitsPerEm;
3801
4029
  }
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 };
4030
+ export { shapingWords as a, joiningStages as c, indicSyllables as d, hasDefaultIgnorable as f, defaultFeatures as g, readTables as h, shapingOf as i, indicFeatures as l, normalizeCodePoints as m, measureWidth as n, canvasSegments as o, normalForms as p, readFont as r, canvasWords as s, glyphAdvances as t, indicForms as u };
3803
4031
 
3804
4032
  //# sourceMappingURL=font.js.map