Skip to content

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

feat(fonts): load multiple CJK Google Fonts for full Han/Japanese/Korean coverage

myhency8599750f1d89
diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts
index 57c438173..591126303 100644
--- a/packages/core/src/constants.ts
+++ b/packages/core/src/constants.ts
@@ -281,7 +281,7 @@ export const CJK_FALLBACK_FAMILIES_LINUX = [
'Droid Sans Fallback'
]
-export const CJK_GOOGLE_FONT = 'Noto Sans SC'
+export const CJK_GOOGLE_FONTS = ['Noto Sans SC', 'Noto Sans JP', 'Noto Sans KR']
export const DEFAULT_SHAPE_FILL: Fill = {
type: 'SOLID',
diff --git a/packages/core/src/fonts.ts b/packages/core/src/fonts.ts
index 7120426d7..0a1e861a8 100644
--- a/packages/core/src/fonts.ts
+++ b/packages/core/src/fonts.ts
@@ -4,7 +4,7 @@ import {
CJK_FALLBACK_FAMILIES_MACOS,
CJK_FALLBACK_FAMILIES_WINDOWS,
CJK_FALLBACK_FAMILIES_LINUX,
- CJK_GOOGLE_FONT,
+ CJK_GOOGLE_FONTS,
GOOGLE_FONTS_API_KEY
} from './constants'
@@ -301,8 +301,8 @@ export function collectFontKeys(graph: SceneGraph, nodeIds: string[]): Array<[st
return [...fontKeys].map((k) => k.split('\0') as [string, string])
}
-let cjkFallbackFamily: string | null = null
-let cjkFallbackPromise: Promise<string | null> | null = null
+let cjkFallbackFamilies: string[] = []
+let cjkFallbackPromise: Promise<string[]> | null = null
function getCJKCandidates(): string[] {
if (typeof navigator === 'undefined') return [...CJK_FALLBACK_FAMILIES_LINUX]
@@ -312,37 +312,53 @@ function getCJKCandidates(): string[] {
return CJK_FALLBACK_FAMILIES_LINUX
}
-export async function ensureCJKFallback(): Promise<string | null> {
- if (cjkFallbackFamily) return cjkFallbackFamily
+export async function ensureCJKFallback(): Promise<string[]> {
+ if (cjkFallbackFamilies.length > 0) return cjkFallbackFamilies
if (cjkFallbackPromise) return cjkFallbackPromise
cjkFallbackPromise = (async () => {
+ // Try local system fonts first
for (const family of getCJKCandidates()) {
const buffer = await findLocalFont(family)
if (buffer && registerAndCache(family, 'Regular', buffer)) {
- cjkFallbackFamily = family
- return family
+ cjkFallbackFamilies.push(family)
}
}
- const data = await loadFont(CJK_GOOGLE_FONT, 'Regular')
- if (data) {
- cjkFallbackFamily = CJK_GOOGLE_FONT
- return CJK_GOOGLE_FONT
+ // Load all CJK Google Fonts in parallel for full coverage
+ if (cjkFallbackFamilies.length === 0) {
+ const results = await Promise.allSettled(
+ CJK_GOOGLE_FONTS.map(async (family) => {
+ const data = await loadFont(family, 'Regular')
+ return data ? family : null
+ })
+ )
+ for (const result of results) {
+ if (result.status === 'fulfilled' && result.value) {
+ cjkFallbackFamilies.push(result.value)
+ }
+ }
}
- return null
+ return cjkFallbackFamilies
})()
return cjkFallbackPromise
}
+/** @deprecated Use getCJKFallbackFamilies() instead */
export function getCJKFallbackFamily(): string | null {
- return cjkFallbackFamily
+ return cjkFallbackFamilies[0] ?? null
+}
+
+export function getCJKFallbackFamilies(): string[] {
+ return cjkFallbackFamilies
}
export function setCJKFallbackFamily(family: string): void {
- cjkFallbackFamily = family
+ if (!cjkFallbackFamilies.includes(family)) {
+ cjkFallbackFamilies.push(family)
+ }
}
export const FONT_WEIGHT_NAMES: Record<number, string> = {
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 37abf5dd9..836f9d2fc 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -146,6 +146,7 @@ export {
ensureNodeFont,
ensureCJKFallback,
getCJKFallbackFamily,
+ getCJKFallbackFamilies,
setCJKFallbackFamily,
styleToWeight,
weightToStyle,
diff --git a/packages/core/src/renderer/renderer.ts b/packages/core/src/renderer/renderer.ts
index 9f4936fe4..f43c32656 100644
--- a/packages/core/src/renderer/renderer.ts
+++ b/packages/core/src/renderer/renderer.ts
@@ -417,8 +417,8 @@ export class SkiaRenderer {
this.fontsLoaded = true
this.invalidateAllPictures()
- void ensureCJKFallback().then((family) => {
- if (family) this.invalidateAllPictures()
+ void ensureCJKFallback().then((families) => {
+ if (families.length > 0) this.invalidateAllPictures()
})
}
diff --git a/packages/core/src/renderer/text.ts b/packages/core/src/renderer/text.ts
index 19ffa20ee..361dc786c 100644
--- a/packages/core/src/renderer/text.ts
+++ b/packages/core/src/renderer/text.ts
@@ -1,5 +1,5 @@
import { DEFAULT_FONT_SIZE, DEFAULT_FONT_FAMILY } from '../constants'
-import { isFontLoaded, getCJKFallbackFamily } from '../fonts'
+import { isFontLoaded, getCJKFallbackFamilies } from '../fonts'
import type { SceneNode } from '../scene-graph'
import type { CanvasKit, FontWeight, Paragraph, TypefaceFontProvider } from 'canvaskit-wasm'
@@ -166,14 +166,14 @@ export function buildParagraph(
const ck = r.ck
const baseColor = color ?? ck.BLACK
const baseFontSize = node.fontSize || DEFAULT_FONT_SIZE
- const cjkFallback = getCJKFallbackFamily()
+ const cjkFallbacks = getCJKFallbackFamilies()
const truncateOpts = buildTruncateOpts(node, baseFontSize)
const fontFamilies = (primary: string) => {
const families = [primary]
if (primary !== DEFAULT_FONT_FAMILY) families.push(DEFAULT_FONT_FAMILY)
- if (cjkFallback) families.push(cjkFallback)
+ families.push(...cjkFallbacks)
return families
}