AI-native design editor. Open-source Figma alternative.
perf(canvas): cache paragraph font families
Danila Poyarkova091ac7284c4
diff --git a/packages/core/src/canvas/text.ts b/packages/core/src/canvas/text.tsindex e72d6705b..825c457e6 100644--- a/packages/core/src/canvas/text.ts+++ b/packages/core/src/canvas/text.ts@@ -32,6 +32,8 @@ export interface ClipboardShapedText {const CJK_RE = /[\u3040-\u30ff\u3400-\u9fff\uf900-\ufaff\uac00-\ud7af]/uconst ARABIC_RE = /[\u0600-\u06ff\u0750-\u077f\u08a0-\u08ff\ufb50-\ufdff\ufe70-\ufeff]/u+const FONT_FAMILY_CACHE_LIMIT = 256+const fontFamilyCache = new Map<string, string[]>()function hasRequiredFallbackFonts(text: string): boolean {if (CJK_RE.test(text) && fontManager.getCJKFallbackFamilies().length === 0) return false@@ -114,6 +116,28 @@ function buildTruncateOpts(return opts}+function resolveParagraphFontFamilies(+ primary: string,+ arabicFallbacks: readonly string[],+ cjkFallbacks: readonly string[]+): string[] {+ const key = `${primary}\0${arabicFallbacks.join('\0')}\0${cjkFallbacks.join('\0')}`+ const cached = fontFamilyCache.get(key)+ if (cached) return cached++ const families = [primary]+ if (primary !== DEFAULT_FONT_FAMILY) families.push(DEFAULT_FONT_FAMILY)+ families.push(...arabicFallbacks, ...cjkFallbacks)++ const resolved = [...new Set(families)]+ fontFamilyCache.set(key, resolved)+ if (fontFamilyCache.size > FONT_FAMILY_CACHE_LIMIT) {+ const oldestKey = fontFamilyCache.keys().next().value+ if (oldestKey) fontFamilyCache.delete(oldestKey)+ }+ return resolved+}+function getParagraphTextAlign(ck: CanvasKit,node: Pick<SceneNode, 'textAlignHorizontal' | 'textDirection' | 'text'>@@ -212,13 +236,8 @@ export function buildParagraph(const truncateOpts = buildTruncateOpts(node, baseFontSize)- const fontFamilies = (primary: string) => {- const families = [primary]- if (primary !== DEFAULT_FONT_FAMILY) families.push(DEFAULT_FONT_FAMILY)- families.push(...arabicFallbacks)- families.push(...cjkFallbacks)- return [...new Set(families)]- }+ const fontFamilies = (primary: string) =>+ resolveParagraphFontFamilies(primary, arabicFallbacks, cjkFallbacks)const paraStyle = new ck.ParagraphStyle({textAlign: getParagraphTextAlign(ck, node),