Skip to content

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

fix: draw the interface in Inter's own weights everywhere (#1691)

Danila Poyarkovd6a1d60127f9
diff --git a/changes/1691/fixed.md b/changes/1691/fixed.md
new file mode 100644
index 000000000..ede4ff39a
--- /dev/null
+++ b/changes/1691/fixed.md
@@ -0,0 +1 @@
+- The interface draws in Inter's own medium and semibold weights, and in Inter on Home, also on systems without Inter installed, such as most Linux desktops; once a document opened, every weight used to come from the regular face.
diff --git a/packages/core/src/text/font/faces.ts b/packages/core/src/text/font/faces.ts
index 352fd36f1..bcf25a5a9 100644
--- a/packages/core/src/text/font/faces.ts
+++ b/packages/core/src/text/font/faces.ts
@@ -6,6 +6,8 @@ import {
type ParsedFontStyle
} from '@open-pencil/scene-graph/font-style'
+import { fontAxes } from './variation'
+
/** A weight, upright or italic. */
export interface FaceWeight {
weight: number
@@ -88,3 +90,23 @@ export function fontFaceFromFigmaFontName(fontName: {
export function fontFaceRenderFamily(family: string, style: string): string {
return `__op_font__${family.replace(/[^a-z0-9_-]+/giu, '_')}__${style.replace(/[^a-z0-9_-]+/giu, '_')}`
}
+
+/** What a font file is registered with in the page's `document.fonts`. */
+export interface BrowserFaceDescriptors {
+ weight: string
+ style: 'normal' | 'italic'
+}
+
+/**
+ * The page face for a font file loaded as `style`. A variable font covers its whole `wght` range,
+ * so text at any weight draws from the font's own instance; registered at the one weight `style`
+ * names, every other weight would fall back to that instance, synthesized bold or not at all.
+ */
+export function browserFaceDescriptors(style: string, data: ArrayBuffer): BrowserFaceDescriptors {
+ const { weight, italic } = parseFontStyle(style)
+ const wght = fontAxes(data)?.find((axis) => axis.tag === 'wght')
+ return {
+ weight: wght ? `${wght.min} ${wght.max}` : String(weight),
+ style: italic ? 'italic' : 'normal'
+ }
+}
diff --git a/packages/core/src/text/font/manager.ts b/packages/core/src/text/font/manager.ts
index ad8e024a2..5bcb82244 100644
--- a/packages/core/src/text/font/manager.ts
+++ b/packages/core/src/text/font/manager.ts
@@ -7,15 +7,18 @@ import {
faceLoadKey,
namesWeight,
normalizeFontFamily,
- parseFontStyle,
- styleToWeight
+ parseFontStyle
} from '@open-pencil/scene-graph/font-style'
import { DEFAULT_FONT_FAMILY, IS_BROWSER } from '#core/constants'
import { fontFallbackEntry, type FontFallbackScript } from '#core/text/fallbacks'
import { BundledFonts, type BundledFontLocator } from '#core/text/font/bundled'
import { FamilyFaceCatalog, type HostFontFamily } from '#core/text/font/catalog'
-import { fontFaceRenderFamily, type FamilyFace } from '#core/text/font/faces'
+import {
+ browserFaceDescriptors,
+ fontFaceRenderFamily,
+ type FamilyFace
+} from '#core/text/font/faces'
import { loadHarfBuzz } from '#core/text/font/harfbuzz'
import { LocalFonts } from '#core/text/font/local'
import { sameFontFile, SharedReads, withHarfBuzz } from '#core/text/font/reads'
@@ -871,12 +874,7 @@ export class FontManager {
/** Makes a face available to the page's own text, such as fields over canvas labels. */
registerFontInBrowser(family: string, style: string, data: ArrayBuffer) {
if (!IS_BROWSER) return
- const weight = styleToWeight(style)
- const italic = style.toLowerCase().includes('italic') ? 'italic' : 'normal'
- const face = new FontFace(family, data, {
- weight: String(weight),
- style: italic
- })
+ const face = new FontFace(family, data, browserFaceDescriptors(style, data))
face
.load()
.then(() => document.fonts.add(face))
diff --git a/packages/core/tests/text/font/faces.test.ts b/packages/core/tests/text/font/faces.test.ts
index 46f0582a6..49fc4f391 100644
--- a/packages/core/tests/text/font/faces.test.ts
+++ b/packages/core/tests/text/font/faces.test.ts
@@ -1,6 +1,8 @@
import { expect, test } from 'bun:test'
-import { facesFromStyles, nearestFamilyFace } from '@open-pencil/core/text'
+import { corePackagePath } from '#core-tests/helpers/paths'
+
+import { browserFaceDescriptors, facesFromStyles, nearestFamilyFace } from '@open-pencil/core/text'
// Lora's faces as Figma desktop 126 lists them.
const lora = facesFromStyles([
@@ -35,3 +37,24 @@ test('a family without the slant gives the nearest weight, and a tie takes the l
expect(nearestFamilyFace(upright, 500, false)).toMatchObject({ weight: 400, italic: false })
expect(nearestFamilyFace([], 400, false)).toBeNull()
})
+
+// The page draws its own text, such as the interface and fields over canvas labels, from these
+// faces: a variable font registered at one weight would serve every weight from that instance.
+test('registers a variable font for the page across its whole weight axis', async () => {
+ const upright = await Bun.file(corePackagePath('assets', 'InterVariable.ttf')).arrayBuffer()
+ const italic = await Bun.file(corePackagePath('assets', 'InterVariable-Italic.ttf')).arrayBuffer()
+
+ expect(browserFaceDescriptors('Regular', upright)).toEqual({ weight: '100 900', style: 'normal' })
+ expect(browserFaceDescriptors('Bold', upright)).toEqual({ weight: '100 900', style: 'normal' })
+ expect(browserFaceDescriptors('Italic', italic)).toEqual({ weight: '100 900', style: 'italic' })
+})
+
+test('registers a static font for the page at the weight of its style', async () => {
+ const semiBold = await Bun.file(corePackagePath('assets', 'Inter-SemiBold.ttf')).arrayBuffer()
+
+ expect(browserFaceDescriptors('SemiBold', semiBold)).toEqual({ weight: '600', style: 'normal' })
+ expect(browserFaceDescriptors('SemiBold Italic', semiBold)).toEqual({
+ weight: '600',
+ style: 'italic'
+ })
+})
diff --git a/src/app/editor/fonts/bundled.ts b/src/app/editor/fonts/bundled.ts
index 125983318..1effc5a4e 100644
--- a/src/app/editor/fonts/bundled.ts
+++ b/src/app/editor/fonts/bundled.ts
@@ -30,4 +30,26 @@ const BUNDLED_FONT_URLS: Record<string, string> = {
export function installBundledFonts(): void {
fontManager.setBundledFontLocator((file) => BUNDLED_FONT_URLS[file] ?? `/${file}`)
setHarfBuzzSubsetLocator(() => harfBuzzSubset)
+ registerInterfaceFont()
+}
+
+/** The interface's font, as `app.css` names it, and the weights its variable files cover. */
+const INTERFACE_FONT_FAMILY = 'Inter'
+const INTERFACE_FONT_WEIGHTS = '100 900'
+
+/**
+ * The interface is set in Inter. Its variable files are registered for the page at start, for
+ * every weight, so Home and every panel draw in Inter's own weights on any system, whether Inter
+ * is installed or not and before a canvas loads the font for its own text.
+ */
+function registerInterfaceFont(): void {
+ if (typeof document === 'undefined') return
+ for (const [url, style] of [
+ [interVariable, 'normal'],
+ [interVariableItalic, 'italic']
+ ] as const) {
+ document.fonts.add(
+ new FontFace(INTERFACE_FONT_FAMILY, `url(${url})`, { weight: INTERFACE_FONT_WEIGHTS, style })
+ )
+ }
}