three-text 0.4.5 → 0.4.7

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.
package/dist/index.umd.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * three-text v0.4.5
2
+ * three-text v0.4.7
3
3
  * Copyright (C) 2025 Countertype LLC
4
4
  *
5
5
  * This program is free software: you can redistribute it and/or modify
@@ -1471,7 +1471,7 @@
1471
1471
  FONT_SIGNATURE_OPEN_TYPE_CFF
1472
1472
  ];
1473
1473
  if (!validSignatures.includes(sfntVersion)) {
1474
- throw new Error(`Invalid font format. Expected TTF/OTF (or WOFF), got signature: 0x${sfntVersion.toString(16)}`);
1474
+ throw new Error(`Invalid font format. Expected TTF/OTF/WOFF/WOFF2, got signature: 0x${sfntVersion.toString(16)}`);
1475
1475
  }
1476
1476
  const tableDirectory = parseTableDirectory(view);
1477
1477
  const isCFF = tableDirectory.has(TABLE_TAG_CFF) || tableDirectory.has(TABLE_TAG_CFF2);
@@ -1660,7 +1660,7 @@
1660
1660
  FONT_SIGNATURE_OPEN_TYPE_CFF
1661
1661
  ];
1662
1662
  if (!validSignatures.includes(sfntVersion)) {
1663
- throw new Error(`Invalid font format. Expected TTF/OTF (or WOFF), got signature: 0x${sfntVersion.toString(16)}`);
1663
+ throw new Error(`Invalid font format. Expected TTF/OTF/WOFF/WOFF2, got signature: 0x${sfntVersion.toString(16)}`);
1664
1664
  }
1665
1665
  const tableDirectory = parseTableDirectory(view);
1666
1666
  const nameTableOffset = tableDirectory.get(TABLE_TAG_NAME)?.offset ?? 0;
@@ -1894,6 +1894,10 @@
1894
1894
  }
1895
1895
  }
1896
1896
 
1897
+ let woff2Decoder = null;
1898
+ function setWoff2Decoder(decoder) {
1899
+ woff2Decoder = decoder;
1900
+ }
1897
1901
  // Uses DecompressionStream to decompress WOFF (WOFF is just zlib compressed TTF/OTF so we can use deflate)
1898
1902
  class WoffConverter {
1899
1903
  static detectFormat(buffer) {
@@ -1995,6 +1999,21 @@
1995
1999
  logger.log('WOFF font decompressed successfully');
1996
2000
  return sfntData.buffer.slice(0, sfntOffset);
1997
2001
  }
2002
+ static async decompressWoff2(woff2Buffer) {
2003
+ const view = new DataView(woff2Buffer);
2004
+ const signature = view.getUint32(0);
2005
+ if (signature !== FONT_SIGNATURE_WOFF2) {
2006
+ throw new Error('Not a valid WOFF2 font');
2007
+ }
2008
+ if (!woff2Decoder) {
2009
+ throw new Error('WOFF2 fonts require enabling the decoder. Add to your code:\n' +
2010
+ " import { decode } from 'woff2-decode';\n" +
2011
+ ' Text.enableWoff2(decode);');
2012
+ }
2013
+ const decoded = await woff2Decoder(woff2Buffer);
2014
+ logger.log('WOFF2 font decompressed successfully');
2015
+ return decoded.buffer;
2016
+ }
1998
2017
  static async decompressZlib(compressedData) {
1999
2018
  const stream = new ReadableStream({
2000
2019
  start(controller) {
@@ -2025,7 +2044,8 @@
2025
2044
  fontBuffer = await WoffConverter.decompressWoff(fontBuffer);
2026
2045
  }
2027
2046
  else if (format === 'woff2') {
2028
- throw new Error('WOFF2 fonts are not yet supported. Please use WOFF or TTF/OTF format.');
2047
+ logger.log('WOFF2 font detected, decompressing...');
2048
+ fontBuffer = await WoffConverter.decompressWoff2(fontBuffer);
2029
2049
  }
2030
2050
  const view = new DataView(fontBuffer);
2031
2051
  const sfntVersion = view.getUint32(0);
@@ -2034,7 +2054,7 @@
2034
2054
  FONT_SIGNATURE_OPEN_TYPE_CFF
2035
2055
  ];
2036
2056
  if (!validSignatures.includes(sfntVersion)) {
2037
- throw new Error(`Invalid font format. Expected TTF/OTF (or WOFF), got signature: 0x${sfntVersion.toString(16)}`);
2057
+ throw new Error(`Invalid font format. Expected TTF/OTF/WOFF/WOFF2, got signature: 0x${sfntVersion.toString(16)}`);
2038
2058
  }
2039
2059
  const { hb, module } = await this.getHarfBuzzInstance();
2040
2060
  try {
@@ -5586,6 +5606,11 @@
5586
5606
  static { this.fontCacheMemoryBytes = 0; }
5587
5607
  static { this.maxFontCacheMemoryBytes = Infinity; }
5588
5608
  static { this.fontIdCounter = 0; }
5609
+ // Enable WOFF2 font support (adds ~45KB to bundle if imported)
5610
+ // Usage: import { decode } from 'woff2-decode'; Text.enableWoff2(decode);
5611
+ static enableWoff2(decoder) {
5612
+ setWoff2Decoder(decoder);
5613
+ }
5589
5614
  // Stringify with sorted keys for cache stability
5590
5615
  static stableStringify(obj) {
5591
5616
  const keys = Object.keys(obj).sort();