AI-native design editor. Open-source Figma alternative.
System font loading via Local Font Access API
Danila Poyarkove3c80fc790e1
diff --git a/src/app.css b/src/app.cssindex 59bad9ff5..d353b7385 100644--- a/src/app.css+++ b/src/app.css@@ -1,12 +1,5 @@@import "tailwindcss";-@font-face {- font-family: Inter;- src: url("/Inter-Regular.ttf") format("truetype");- font-weight: 400;- font-style: normal;- font-display: swap;-}@theme {--color-panel: #2a2a2a;diff --git a/src/engine/fonts.ts b/src/engine/fonts.tsnew file mode 100644index 000000000..6e4749a24--- /dev/null+++ b/src/engine/fonts.ts@@ -0,0 +1,140 @@+import type { CanvasKit, TypefaceFontProvider } from 'canvaskit-wasm'++export interface FontInfo {+ family: string+ fullName: string+ style: string+ postscriptName: string+}++const loadedFamilies = new Map<string, ArrayBuffer>()+let fontProvider: TypefaceFontProvider | null = null+let ck: CanvasKit | null = null++export function initFontService(canvasKit: CanvasKit, provider: TypefaceFontProvider) {+ ck = canvasKit+ fontProvider = provider+}++export function getFontProvider(): TypefaceFontProvider | null {+ return fontProvider+}++export async function queryFonts(): Promise<FontInfo[]> {+ if (!('queryLocalFonts' in window)) return []+ try {+ const fonts = await (window as any).queryLocalFonts()+ const seen = new Set<string>()+ const result: FontInfo[] = []+ for (const f of fonts) {+ const key = `${f.family}|${f.style}`+ if (seen.has(key)) continue+ seen.add(key)+ result.push({+ family: f.family,+ fullName: f.fullName,+ style: f.style,+ postscriptName: f.postscriptName+ })+ }+ return result+ } catch {+ return []+ }+}++export async function listFamilies(): Promise<string[]> {+ const fonts = await queryFonts()+ return [...new Set(fonts.map((f) => f.family))].sort()+}++const BUNDLED_FONTS: Record<string, string> = {+ 'Inter|Regular': '/Inter-Regular.ttf'+}++export async function loadFont(family: string, style = 'Regular'): Promise<ArrayBuffer | null> {+ const cacheKey = `${family}|${style}`+ if (loadedFamilies.has(cacheKey)) return loadedFamilies.get(cacheKey)!++ // Try local font access API first+ if ('queryLocalFonts' in window) {+ try {+ const fonts = await (window as any).queryLocalFonts()+ const match = fonts.find(+ (f: FontInfo) => f.family === family && f.style === style+ ) ?? fonts.find(+ (f: FontInfo) => f.family === family+ )+ if (match) {+ const blob: Blob = await match.blob()+ const buffer = await blob.arrayBuffer()++ loadedFamilies.set(cacheKey, buffer)+ registerFontInCanvasKit(family, buffer)+ registerFontInBrowser(family, style, buffer)+ return buffer+ }+ } catch { /* fall through to bundled */ }+ }++ // Fall back to bundled font+ const bundledUrl = BUNDLED_FONTS[cacheKey]+ if (bundledUrl) {+ try {+ const response = await fetch(bundledUrl)+ const buffer = await response.arrayBuffer()++ loadedFamilies.set(cacheKey, buffer)+ registerFontInCanvasKit(family, buffer)+ registerFontInBrowser(family, style, buffer)+ return buffer+ } catch { /* no bundled font available */ }+ }++ return null+}++function registerFontInCanvasKit(family: string, data: ArrayBuffer) {+ if (!fontProvider) return+ fontProvider.registerFont(data, family)+}++function registerFontInBrowser(family: string, style: string, data: ArrayBuffer) {+ const weight = styleToWeight(style)+ const italic = style.toLowerCase().includes('italic') ? 'italic' : 'normal'+ const face = new FontFace(family, data, {+ weight: String(weight),+ style: italic+ })+ face.load().then(() => document.fonts.add(face))+}++function styleToWeight(style: string): number {+ const s = style.toLowerCase()+ if (s.includes('thin') || s.includes('hairline')) return 100+ if (s.includes('extralight') || s.includes('ultralight')) return 200+ if (s.includes('light')) return 300+ if (s.includes('medium')) return 500+ if (s.includes('semibold') || s.includes('demibold')) return 600+ if (s.includes('extrabold') || s.includes('ultrabold')) return 800+ if (s.includes('black') || s.includes('heavy')) return 900+ if (s.includes('bold')) return 700+ return 400+}++export async function ensureNodeFont(family: string, weight: number): Promise<void> {+ const style = weightToStyle(weight)+ await loadFont(family, style)+}++function weightToStyle(weight: number): string {+ if (weight <= 100) return 'Thin'+ if (weight <= 200) return 'ExtraLight'+ if (weight <= 300) return 'Light'+ if (weight <= 400) return 'Regular'+ if (weight <= 500) return 'Medium'+ if (weight <= 600) return 'SemiBold'+ if (weight <= 700) return 'Bold'+ if (weight <= 800) return 'ExtraBold'+ return 'Black'+}diff --git a/src/engine/renderer.ts b/src/engine/renderer.tsindex 97ed1ce66..039319a46 100644--- a/src/engine/renderer.ts+++ b/src/engine/renderer.ts@@ -93,23 +93,25 @@ export class SkiaRenderer {}async loadFonts(): Promise<void> {- const response = await fetch('/Inter-Regular.ttf')- const fontData = await response.arrayBuffer()-this.fontProvider = this.ck.TypefaceFontProvider.Make()- this.fontProvider.registerFont(fontData, 'Inter')-- const typeface = this.ck.Typeface.MakeFreeTypeFaceFromData(fontData)- if (typeface) {- this.textFont?.delete()- this.labelFont?.delete()- this.sizeFont?.delete()- this.textFont = new this.ck.Font(typeface, 14)- this.labelFont = new this.ck.Font(typeface, 11)- this.sizeFont = new this.ck.Font(typeface, 10)++ const { initFontService, loadFont } = await import('./fonts')+ initFontService(this.ck, this.fontProvider)++ const fontData = await loadFont('Inter', 'Regular')+ if (fontData) {+ const typeface = this.ck.Typeface.MakeFreeTypeFaceFromData(fontData)+ if (typeface) {+ this.textFont?.delete()+ this.labelFont?.delete()+ this.sizeFont?.delete()+ this.textFont = new this.ck.Font(typeface, 14)+ this.labelFont = new this.ck.Font(typeface, 11)+ this.sizeFont = new this.ck.Font(typeface, 10)+ }+ this.fontMgr = this.ck.FontMgr.FromData(fontData) ?? null}- this.fontMgr = this.ck.FontMgr.FromData(fontData) ?? nullthis.fontsLoaded = true}