@mark1russell7/typeset 0.1.4 → 0.1.5

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.
@@ -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) {
@@ -1527,6 +1542,7 @@ function gvarAdvanceDeltas(gvar, pointCounts, coordinates, deltas) {
1527
1542
  * It then sorts the marks, and composes each mark with its base when the font has the glyph of the composite.
1528
1543
  */
1529
1544
  const unicodeMark = /\p{M}/u;
1545
+ const nonspacingMark = /\p{Mn}/u;
1530
1546
  /**
1531
1547
  * The classes of HarfBuzz for the marks of Hebrew and Arabic, from `hb-unicode.hh`. Their order is not the canonical order:
1532
1548
  * for example the shadda comes first. Each other pair of marks uses the canonical order.
@@ -1567,6 +1583,16 @@ const sortClasses = /* @__PURE__ */ new Map([
1567
1583
  [2289, 29],
1568
1584
  [2290, 30]
1569
1585
  ]);
1586
+ /** The default ignorables of `hb-unicode.hh`. This set is not the Unicode property: it does not have the Hangul fillers. */
1587
+ 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;
1588
+ /** This function tells if a text has a default ignorable. */
1589
+ function hasDefaultIgnorable(text) {
1590
+ return defaultIgnorable.test(text);
1591
+ }
1592
+ /** This function tells if the model hides a code point. A hidden code point takes no advance, and the kerning skips it. */
1593
+ function isHidden(codePoint) {
1594
+ return hasDefaultIgnorable(String.fromCodePoint(codePoint));
1595
+ }
1570
1596
  /** This function tells if a code point is a mark of Unicode. The first mark of Unicode is U+0300. */
1571
1597
  function isMark(codePoint) {
1572
1598
  return codePoint >= 768 && unicodeMark.test(String.fromCodePoint(codePoint));
@@ -1575,6 +1601,18 @@ function isMark(codePoint) {
1575
1601
  function hasMark(text) {
1576
1602
  return unicodeMark.test(text);
1577
1603
  }
1604
+ /** This function tells which code points take no advance: nonspacing marks with `classes`, marks with a class after a base with `fallback`. */
1605
+ function zeroedMarks(codePoints, classes, fallback) {
1606
+ let base = false;
1607
+ return codePoints.map((codePoint) => {
1608
+ const character = String.fromCodePoint(codePoint);
1609
+ if (!isMark(codePoint)) {
1610
+ base = true;
1611
+ return false;
1612
+ }
1613
+ return classes && nonspacingMark.test(character) && !hasDefaultIgnorable(character) || fallback && base && !isStarter(codePoint);
1614
+ });
1615
+ }
1578
1616
  function decomposed(codePoint) {
1579
1617
  return String.fromCodePoint(codePoint).normalize("NFD");
1580
1618
  }
@@ -2406,7 +2444,7 @@ function indicShaper(font, script, features) {
2406
2444
  for (const { lookups, mask } of basic) for (const lookup of lookups) lookup.applyInfos(run.glyphs, run.infos, mask, true);
2407
2445
  for (const [start, end] of syllables(run.infos)) finalReordering(run, start, end);
2408
2446
  for (const { lookup, mask, perSyllable } of last) lookup.applyInfos(run.glyphs, run.infos, mask, perSyllable);
2409
- return run.glyphs;
2447
+ return run.glyphs.map((glyph, i) => (run.infos[i].props & 16) === 0 && isHidden(run.infos[i].codePoint) ? ~glyph : glyph);
2410
2448
  };
2411
2449
  }
2412
2450
  // src/fonts/joining.ts
@@ -2738,12 +2776,18 @@ const joiningTable = [
2738
2776
  2274,
2739
2777
  2274,
2740
2778
  U,
2779
+ 6158,
2780
+ 6158,
2781
+ U,
2741
2782
  8204,
2742
2783
  8204,
2743
2784
  U,
2744
2785
  8205,
2745
2786
  8205,
2746
- D
2787
+ D,
2788
+ 8294,
2789
+ 8297,
2790
+ U
2747
2791
  ];
2748
2792
  const transparent = /[\p{Mn}\p{Me}\p{Cf}]/u;
2749
2793
  /** This function gives the joining type of a code point: a column of the state machine, or `T`. */
@@ -3534,6 +3578,7 @@ function build(parsed, requested, modes) {
3534
3578
  const kern = tables.get("kern");
3535
3579
  const gsub = tables.get("GSUB");
3536
3580
  const lookups = /* @__PURE__ */ new Map();
3581
+ const contexts = /* @__PURE__ */ new Map();
3537
3582
  const substitutions = /* @__PURE__ */ new Map();
3538
3583
  const hashed = [
3539
3584
  tables.get("cmap"),
@@ -3549,6 +3594,8 @@ function build(parsed, requested, modes) {
3549
3594
  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
3595
  unitsPerEm: header.unitsPerEm,
3551
3596
  numGlyphs: header.numGlyphs,
3597
+ hasPositioning: gpos !== void 0,
3598
+ hasGlyphClasses: gdef !== void 0 && gdef.u16(4) !== 0,
3552
3599
  axes,
3553
3600
  instance,
3554
3601
  coordinates,
@@ -3564,11 +3611,19 @@ function build(parsed, requested, modes) {
3564
3611
  }
3565
3612
  return found;
3566
3613
  },
3614
+ contextKerning(script) {
3615
+ if (!contexts.has(script)) contexts.set(script, gpos === void 0 ? [] : readContextKerning(gpos, script, parsed.filter, !noMarkZero.includes(script)));
3616
+ return contexts.get(script);
3617
+ },
3567
3618
  substitutions(script, features) {
3568
3619
  const key = `${script} ${features.join(",")}`;
3569
3620
  let found = substitutions.get(key);
3570
3621
  if (found === void 0) {
3571
- found = gsub === void 0 ? [] : readSubstitutions(gsub, script, features, parsed.filter);
3622
+ const joiners = script === "deva" ? [] : [characters.get(8204) ?? 0, characters.get(8205) ?? 0].filter((glyph) => glyph !== 0);
3623
+ found = gsub === void 0 ? [] : readSubstitutions(gsub, script, features, {
3624
+ ...parsed.filter,
3625
+ joiners
3626
+ });
3572
3627
  substitutions.set(key, found);
3573
3628
  }
3574
3629
  return found;
@@ -3648,6 +3703,25 @@ function shapingOf(font, options) {
3648
3703
  kerning
3649
3704
  };
3650
3705
  }
3706
+ /** The bits of a mask: all the glyphs, a default ignorable, a mark with no advance. */
3707
+ const GLOBAL_MASK = 128;
3708
+ const IGNORABLE_MASK = 256;
3709
+ const ZEROED_MASK = 512;
3710
+ /** The scripts whose shaper in HarfBuzz has no fallback mark position. */
3711
+ const noFallbackPosition = [
3712
+ "thai",
3713
+ "lao ",
3714
+ "khmr",
3715
+ "mymr",
3716
+ "mym2",
3717
+ ...indicScripts
3718
+ ];
3719
+ /** The scripts whose shaper in HarfBuzz does not zero the marks. */
3720
+ const noMarkZero = [
3721
+ "hang",
3722
+ "khmr",
3723
+ ...indicScripts
3724
+ ];
3651
3725
  /** The GSUB pass of one set of options: a function that gives the glyphs of one word, and the kerning lookups. */
3652
3726
  function shaperOf(font, options) {
3653
3727
  const { script, features, shaper, kerning } = shapingOf(font, options);
@@ -3655,6 +3729,8 @@ function shaperOf(font, options) {
3655
3729
  substitute: indicShaper(font, script, features),
3656
3730
  kerning
3657
3731
  };
3732
+ const classes = font.hasGlyphClasses === false && !noMarkZero.includes(script);
3733
+ const fallback = font.hasPositioning === false && !noFallbackPosition.includes(script);
3658
3734
  const joins = shaper === "joining";
3659
3735
  const lookups = joins ? [] : font.substitutions(script, features);
3660
3736
  const stages = joins ? joiningStages(features, features.includes("rclt") && font.substitutions(script, ["rclt"]).length > 0).map((stage) => ({
@@ -3673,15 +3749,20 @@ function shaperOf(font, options) {
3673
3749
  codePoints = normalizeCodePoints(codePoints, rules);
3674
3750
  wordGlyphs = codePoints.map((codePoint) => font.glyph(codePoint));
3675
3751
  }
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;
3752
+ const hides = hasDefaultIgnorable(word);
3753
+ const zeroed = (classes || fallback) && hasMark(word) ? zeroedMarks(codePoints, classes, fallback) : [];
3754
+ const forms = joins ? joiningForms(codePoints) : [];
3755
+ 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;
3756
+ const before = hides ? [...wordGlyphs] : [];
3757
+ for (const lookup of lookups) lookup.apply(wordGlyphs, masks, GLOBAL_MASK);
3758
+ for (const { stage, lookups: stageLookups } of stages) {
3759
+ const mask = stage.form === void 0 ? GLOBAL_MASK : 1 << joiningFeatures.indexOf(stage.form);
3760
+ for (const lookup of stageLookups) lookup.apply(wordGlyphs, masks, mask);
3761
+ }
3762
+ return masks === void 0 ? wordGlyphs : wordGlyphs.map((glyph, i) => {
3763
+ const mask = masks[i] ?? 0;
3764
+ return (mask & ZEROED_MASK) !== 0 || (mask & IGNORABLE_MASK) !== 0 && before.includes(glyph) ? ~glyph : glyph;
3765
+ });
3685
3766
  };
3686
3767
  return {
3687
3768
  substitute,
@@ -3695,6 +3776,14 @@ function shaperOf(font, options) {
3695
3776
  * the joining forms of each word, in the stages of `joiningStages`. A script of `indicScripts` uses `indicShaper`.
3696
3777
  */
3697
3778
  function glyphAdvances(font, text, options = {}) {
3779
+ const { glyphs, advances } = shapedAdvances(font, text, options);
3780
+ return {
3781
+ glyphs: glyphs.map((glyph) => glyph < 0 ? ~glyph : glyph),
3782
+ advances
3783
+ };
3784
+ }
3785
+ /** This function is `glyphAdvances`, with `~glyph` for a hidden glyph. It has no advance, and the kerning skips it. */
3786
+ function shapedAdvances(font, text, options) {
3698
3787
  const { substitute, kerning } = shaperOf(font, options);
3699
3788
  const canvas = options.shaping === "canvas";
3700
3789
  const glyphs = [];
@@ -3732,7 +3821,7 @@ function kerningPosition(value, delta, scale) {
3732
3821
  function kern(font, lookups, glyphs, advances, positions, scale) {
3733
3822
  for (const lookup of lookups) {
3734
3823
  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;
3824
+ 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
3825
  return -1;
3737
3826
  };
3738
3827
  let i = next(0);
@@ -3785,7 +3874,7 @@ function float32Width(font, text, size, options) {
3785
3874
  */
3786
3875
  function measureWidth(font, text, size, options = {}) {
3787
3876
  if (options.sum === "float32" && options.rounding !== "pixel") return float32Width(font, text, size, options);
3788
- const { glyphs, advances } = glyphAdvances(font, text, options);
3877
+ const { glyphs, advances } = shapedAdvances(font, text, options);
3789
3878
  if (options.rounding === "pixel") {
3790
3879
  const scale = size / font.unitsPerEm;
3791
3880
  let px = 0;
@@ -3799,6 +3888,6 @@ function measureWidth(font, text, size, options = {}) {
3799
3888
  for (const advance of advances) units += advance;
3800
3889
  return units * size / font.unitsPerEm;
3801
3890
  }
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 };
3891
+ export { shapingWords as a, joiningStages as c, hasDefaultIgnorable as d, normalForms as f, defaultFeatures as h, shapingOf as i, indicFeatures as l, readTables as m, measureWidth as n, canvasSegments as o, normalizeCodePoints as p, readFont as r, canvasWords as s, glyphAdvances as t, indicForms as u };
3803
3892
 
3804
3893
  //# sourceMappingURL=font.js.map