Skip to content

AI-native design editor. Open-source Figma alternative.

build(text): upgrade opentype.js to v2

Danila Poyarkov35af00433fd0
diff --git a/bun.lock b/bun.lock
index 0f42a9e48..be1e55a26 100644
--- a/bun.lock
+++ b/bun.lock
@@ -44,7 +44,7 @@
"jspdf": "^4.2.1",
"lib0": "^0.2.117",
"motion-v": "^2.0.0",
- "opentype.js": "^1.3.4",
+ "opentype.js": "^2.0.0",
"prismjs": "^1.30.0",
"reka-ui": "^2.9.0",
"svg2pdf.js": "^2.7.0",
@@ -152,7 +152,7 @@
"jspdf": "^4.2.1",
"nanoevents": "^9.1.0",
"ofetch": "^1.5.1",
- "opentype.js": "^1.3.4",
+ "opentype.js": "^2.0.0",
"sucrase": "^3.35.1",
"svg2pdf.js": "^2.7.0",
"svgpath": "^2.6.0",
@@ -2460,7 +2460,7 @@
"open": ["[email protected]", "", { "dependencies": { "default-browser": "^5.2.1", "define-lazy-prop": "^3.0.0", "is-inside-container": "^1.0.0", "wsl-utils": "^0.1.0" } }, "sha512-YgBpdJHPyQ2UE5x+hlSXcnejzAvD0b22U2OuAP+8OnlJT+PjWPxtgmGqKKc+RgTM63U9gN0YzrYc71R2WT/hTA=="],
- "opentype.js": ["[email protected]", "", { "dependencies": { "string.prototype.codepointat": "^0.2.1", "tiny-inflate": "^1.0.3" }, "bin": { "ot": "bin/ot" } }, "sha512-d2JE9RP/6uagpQAVtJoF0pJJA/fgai89Cc50Yp0EJHk+eLp6QQ7gBoblsnubRULNY132I0J1QKMJ+JTbMqz4sw=="],
+ "opentype.js": ["[email protected]", "", { "bin": { "ot": "bin/ot" } }, "sha512-kCyjv6xdDY1W/jLWZ/L3QhhTlKUqDZMQ5+Jdlw12b3dXkKNpYBqqlMMj0YDQPShWFTMwgZI1hG14kN3XUDSg/A=="],
"own-keys": ["[email protected]", "", { "dependencies": { "get-intrinsic": "^1.2.6", "object-keys": "^1.1.1", "safe-push-apply": "^1.0.0" } }, "sha512-qFOyK5PjiWZd+QQIh+1jhdb9LpxTF0qs7Pm8o5QHYZ0M3vKqSqzsZaEB6oWlxZ+q2sJBMI/Ktgd2N5ZwQoRHfg=="],
@@ -2822,8 +2822,6 @@
"string-width": ["[email protected]", "", { "dependencies": { "emoji-regex": "^8.0.0", "is-fullwidth-code-point": "^3.0.0", "strip-ansi": "^6.0.1" } }, "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g=="],
- "string.prototype.codepointat": ["[email protected]", "", {}, "sha512-2cBVCj6I4IOvEnjgO/hWqXjqBGsY+zwPmHl12Srk9IXSZ56Jwwmy+66XO5Iut/oQVR7t5ihYdLB0GMa4alEUcg=="],
-
"string.prototype.matchall": ["[email protected]", "", { "dependencies": { "call-bind": "^1.0.8", "call-bound": "^1.0.3", "define-properties": "^1.2.1", "es-abstract": "^1.23.6", "es-errors": "^1.3.0", "es-object-atoms": "^1.0.0", "get-intrinsic": "^1.2.6", "gopd": "^1.2.0", "has-symbols": "^1.1.0", "internal-slot": "^1.1.0", "regexp.prototype.flags": "^1.5.3", "set-function-name": "^2.0.2", "side-channel": "^1.1.0" } }, "sha512-6CC9uyBL+/48dYizRf7H7VAYCMCNTBeM78x/VTUe9bFEaxBepPJDa1Ow99LqI/1yF7kuy7Q3cQsYMrcjGUcskA=="],
"string.prototype.trim": ["[email protected]", "", { "dependencies": { "call-bind": "^1.0.8", "call-bound": "^1.0.2", "define-data-property": "^1.1.4", "define-properties": "^1.2.1", "es-abstract": "^1.23.5", "es-object-atoms": "^1.0.0", "has-property-descriptors": "^1.0.2" } }, "sha512-Rs66F0P/1kedk5lyYyH9uBzuiI/kNRmwJAR9quK6VOtIpZ2G+hMZd+HQbbv25MgCA6gEffoMZYxlTod4WcdrKA=="],
@@ -2890,8 +2888,6 @@
"thenify-all": ["[email protected]", "", { "dependencies": { "thenify": ">= 3.1.0 < 4" } }, "sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA=="],
- "tiny-inflate": ["[email protected]", "", {}, "sha512-pkY1fj1cKHb2seWDy0B16HeWyczlJA9/WW3u3c4z/NiWDsO3DOU5D7nhTLE9CF0yXv/QZFY7sEJmj24dK+Rrqw=="],
-
"tiny-invariant": ["[email protected]", "", {}, "sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg=="],
"tinyexec": ["[email protected]", "", {}, "sha512-SHf/r48b7vOrjve9PxJo3MN5v5yuyjHvdUcrQffT3WXMUfnGmHDVbC4k3sHJaJTgZCwpUplIaAo5ANtMyp3YHg=="],
diff --git a/package.json b/package.json
index 9bd56184d..38abddb32 100644
--- a/package.json
+++ b/package.json
@@ -97,7 +97,7 @@
"jspdf": "^4.2.1",
"lib0": "^0.2.117",
"motion-v": "^2.0.0",
- "opentype.js": "^1.3.4",
+ "opentype.js": "^2.0.0",
"prismjs": "^1.30.0",
"reka-ui": "^2.9.0",
"svg2pdf.js": "^2.7.0",
diff --git a/packages/core/package.json b/packages/core/package.json
index 71f486676..b1709056f 100644
--- a/packages/core/package.json
+++ b/packages/core/package.json
@@ -184,7 +184,7 @@
"jspdf": "^4.2.1",
"nanoevents": "^9.1.0",
"ofetch": "^1.5.1",
- "opentype.js": "^1.3.4",
+ "opentype.js": "^2.0.0",
"sucrase": "^3.35.1",
"svg2pdf.js": "^2.7.0",
"svgpath": "^2.6.0",
diff --git a/packages/core/src/text/opentype.ts b/packages/core/src/text/opentype.ts
index ba24204c8..4b3255f8c 100644
--- a/packages/core/src/text/opentype.ts
+++ b/packages/core/src/text/opentype.ts
@@ -28,8 +28,7 @@ interface OutlineFont {
descender: number
tables: { os2?: { sTypoLineGap?: number } }
charToGlyphIndex(char: string): number
- stringToGlyphs(text: string): OutlineGlyph[]
- getAdvanceWidth(text: string, fontSize: number): number
+ charToGlyph(char: string): OutlineGlyph
}
export interface GlyphOutlineProbe {
@@ -44,23 +43,37 @@ interface OpenTypeModule {
parse(buffer: ArrayBuffer): OutlineFont
}
-const parsedFontCache = new Map<string, OutlineFont | null>()
+interface ParsedFontCacheEntry {
+ bytes: ArrayBuffer
+ font: OutlineFont | null
+}
+
+const parsedFontCache = new Map<string, ParsedFontCacheEntry>()
function getParsedFont(family: string, style: string): OutlineFont | null {
const key = `${family}|${style}`
- if (parsedFontCache.has(key)) return parsedFontCache.get(key) ?? null
const bytes = fontManager.loadedData(family, style)
if (!bytes) return null
+ const cached = parsedFontCache.get(key)
+ if (cached?.bytes === bytes) return cached.font
try {
const font = (OpenTypeSync as OpenTypeModule).parse(bytes.slice(0))
- parsedFontCache.set(key, font)
+ parsedFontCache.set(key, { bytes, font })
return font
} catch {
- parsedFontCache.set(key, null)
+ parsedFontCache.set(key, { bytes, font: null })
return null
}
}
+function glyphsForCodePoints(font: OutlineFont, text: string): OutlineGlyph[] {
+ return [...text].map((character) => font.charToGlyph(character))
+}
+
+function glyphAdvanceWidth(font: OutlineFont, glyph: OutlineGlyph, fontSize: number): number {
+ return ((glyph.advanceWidth ?? 0) * fontSize) / font.unitsPerEm
+}
+
export function measureTextWithOpenType(
text: string,
fontSize: number,
@@ -76,7 +89,10 @@ export function measureTextWithOpenType(
const lineGap = font.tables.os2?.sTypoLineGap ?? 0
const lineH = lineHeight ?? Math.ceil((font.ascender - font.descender + lineGap) * scale)
- const singleLineWidth = font.getAdvanceWidth(text, fontSize)
+ const singleLineWidth = glyphsForCodePoints(font, text).reduce(
+ (width, glyph) => width + glyphAdvanceWidth(font, glyph, fontSize),
+ 0
+ )
if (maxWidth && maxWidth > 0 && singleLineWidth > maxWidth) {
const lines = Math.ceil(singleLineWidth / maxWidth)
@@ -118,12 +134,11 @@ export function getGlyphOutlineMetricsSync(
const font = getParsedFont(family, style)
if (!font) return null
- const glyphs = font.stringToGlyphs(text)
+ const glyphs = glyphsForCodePoints(font, text)
let x = 0
- const scale = fontSize / font.unitsPerEm
return glyphs.map((glyph) => {
const commands = glyph.getPath(0, 0, fontSize).commands
- const advance = (glyph.advanceWidth ?? 0) * scale
+ const advance = glyphAdvanceWidth(font, glyph, fontSize)
const metrics = { commands, x, advance }
x += advance
return metrics
@@ -140,7 +155,7 @@ export async function probeGlyphOutlineCommands(
if (!bytes) return null
const font = (OpenTypeSync as OpenTypeModule).parse(bytes.slice(0))
- const glyphs = font.stringToGlyphs(text)
+ const glyphs = glyphsForCodePoints(font, text)
const firstGlyph = glyphs.find((glyph: OutlineGlyph) => glyph.path.commands.length > 0)
const firstGlyphCommandSample = (firstGlyph?.getPath(0, 0, fontSize).commands ?? []).slice(0, 12)