AI-native design editor. Open-source Figma alternative.
feat(text): outline loaded fallback glyphs
Danila Poyarkov75c2975fdcc9
diff --git a/packages/core/src/text/opentype.ts b/packages/core/src/text/opentype.tsindex 856ddbc33..ebe97e369 100644--- a/packages/core/src/text/opentype.ts+++ b/packages/core/src/text/opentype.ts@@ -27,6 +27,7 @@ interface OutlineFont {ascender: numberdescender: numbertables: { os2?: { sTypoLineGap?: number } }+ charToGlyphIndex(char: string): numberstringToGlyphs(text: string): OutlineGlyph[]getAdvanceWidth(text: string, fontSize: number): number}@@ -86,6 +87,12 @@ export interface GlyphOutlineMetrics {advance: number}+export function fontHasGlyphSync(family: string, style: string, char: string): boolean {+ const font = getParsedFont(family, style)+ if (!font) return false+ return font.charToGlyphIndex(char) !== 0+}+export function getGlyphOutlineMetricsSync(family: string,style: string,diff --git a/packages/core/src/text/outlines.ts b/packages/core/src/text/outlines.tsindex d3556f17c..f50ef87d6 100644--- a/packages/core/src/text/outlines.ts+++ b/packages/core/src/text/outlines.ts@@ -2,12 +2,13 @@ import { prepareWithSegments, layoutWithLines } from '@chenglou/pretext'import type { CharacterStyleOverride, SceneNode } from '#core/scene-graph'import { fontManager, weightToStyle } from '#core/text/fonts'-import { getGlyphOutlineMetricsSync, type OutlineCommand } from '#core/text/opentype'+import { fontHasGlyphSync, getGlyphOutlineMetricsSync, type OutlineCommand } from '#core/text/opentype'export type TextOutlineUnsupportedReason =| 'not-text'| 'empty-text'| 'missing-font'+ | 'missing-glyph'| 'complex-script'export type TextOutlineSupport =@@ -61,6 +62,23 @@ function textStyleAt(node: SceneNode, index: number): TextStyle {}}+function resolvedGlyphStyle(style: TextStyle, char: string): TextStyle | null {+ if (fontHasGlyphSync(style.fontFamily, styleName(style), char)) return style+ const family = fallbackFamilies().find((candidate) => {+ const next = fallbackStyle(style, candidate)+ return fontManager.loadedData(next.fontFamily, styleName(next)) && fontHasGlyphSync(next.fontFamily, styleName(next), char)+ })+ return family ? fallbackStyle(style, family) : null+}++function fallbackFamilies(): string[] {+ return [...fontManager.getCJKFallbackFamilies(), ...fontManager.getArabicFallbackFamilies()]+}++function fallbackStyle(style: TextStyle, family: string): TextStyle {+ return { ...style, fontFamily: family }+}+function textStyles(node: SceneNode): TextStyle[] {if (node.styleRuns.length === 0) return [baseTextStyle(node)]const styles = new Map<string, TextStyle>()@@ -80,6 +98,12 @@ export function getTextOutlineSupport(node: SceneNode): TextOutlineSupport {return { supported: false, reason: 'missing-font' }}}+ for (let index = 0; index < node.text.length; index++) {+ const char = node.text[index]+ if (char === '\n') continue+ const style = textStyleAt(node, index)+ if (!resolvedGlyphStyle(style, char)) return { supported: false, reason: 'missing-glyph' }+ }return { supported: true }}@@ -156,10 +180,15 @@ function lineGlyphs(node: SceneNode, line: TextLine, baseline: number, xOffset:while (index < line.text.length) {const absoluteIndex = line.start + index- const style = textStyleAt(node, absoluteIndex)+ const style = resolvedGlyphStyle(textStyleAt(node, absoluteIndex), line.text[index])+ if (!style) return nullconst key = styleKey(style)let end = index + 1- while (end < line.text.length && styleKey(textStyleAt(node, line.start + end)) === key) end+++ while (end < line.text.length) {+ const nextStyle = resolvedGlyphStyle(textStyleAt(node, line.start + end), line.text[end])+ if (!nextStyle || styleKey(nextStyle) !== key) break+ end+++ }const segment = line.text.slice(index, end)const metrics = getGlyphOutlineMetricsSync(style.fontFamily, styleName(style), segment, style.fontSize)diff --git a/tests/engine/editor/structure/flatten.test.ts b/tests/engine/editor/structure/flatten.test.tsindex 13b301f8a..6882e44f1 100644--- a/tests/engine/editor/structure/flatten.test.ts+++ b/tests/engine/editor/structure/flatten.test.ts@@ -26,6 +26,12 @@ async function loadInterBold() {fontManager.markLoaded('Inter', 'Bold', data)}+async function loadNotoSansSC() {+ const data = await Bun.file('tests/fixtures/fonts/NotoSansSC-Regular.ttf').arrayBuffer()+ fontManager.markLoaded('Noto Sans SC', 'Regular', data)+ fontManager.setCJKFallbackFamily('Noto Sans SC')+}+describe('flattenSelected', () => {test('converts selected shapes into a vector path', async () => {const { editor, surface } = await createEditorWithRenderer()@@ -123,6 +129,30 @@ describe('flattenSelected', () => {surface.delete()})+ test('flattens mixed Latin and CJK text with loaded fallback outlines', async () => {+ await loadInterRegular()+ await loadNotoSansSC()+ const { editor, surface } = await createEditorWithRenderer()+ const pageId = editor.state.currentPageId+ const text = editor.graph.createNode('TEXT', pageId, {+ text: 'Hi你',+ fontFamily: 'Inter',+ fontWeight: 400,+ fontSize: 32,+ width: 120,+ height: 40+ })++ editor.select([text.id])+ editor.flattenSelected()++ const [vectorId] = [...editor.state.selectedIds]+ const vector = editor.graph.getNode(vectorId)+ expect(vector?.type).toBe('VECTOR')+ expect(vector?.vectorNetwork?.vertices.length).toBeGreaterThan(0)+ surface.delete()+ })+test('does not flatten unsupported text nodes', async () => {const { editor, surface } = await createEditorWithRenderer()const pageId = editor.state.currentPageId