Code-first video & image, rendered natively on WebGPU.
fix(text): measure with the opsz instance text layers render
okanasl75580b1933fd
diff --git a/packages/framefields/src/font-manager.test.ts b/packages/framefields/src/font-manager.test.tsindex 7939e4ff..123efb50 100644--- a/packages/framefields/src/font-manager.test.ts+++ b/packages/framefields/src/font-manager.test.ts@@ -3,6 +3,7 @@ import fs from "node:fs/promises";import os from "node:os";import path from "node:path";import { afterAll, beforeAll, beforeEach, describe, expect, it } from "vitest";+import { SlugFontCache, SlugGeometry } from "@framefields/webgpu-renderers";import { Composition, FontManager, Layer } from "./index.js";// Fonts are kept on disk, not in git (.gitignore: *.ttf). Every test that@@ -204,6 +205,23 @@ describe.skipIf(!hasInter)("FontManager.measure", () => {expect(wrapped.glyphs.find((g) => g.char === "w")?.line).toBe(1);});+ it("measures with the opsz instance text layers render at that size", () => {+ const text = "¿Por qué la inteligencia";+ const fontSize = 90;+ const m = FontManager.measure(text, { fontFamily: "Inter", fontSize });+ // The font Layer.text draws with: opsz follows the font size.+ const font = SlugFontCache.getFont(+ "Inter",+ undefined,+ null,+ undefined,+ fontSize,+ );+ if (!font) throw new Error("Inter not registered");+ const rendered = SlugGeometry.measure(text, font, fontSize, 0, 108);+ expect(m.width).toBeCloseTo(rendered.width, 3);+ });+it("throws a clear error for unregistered fonts", () => {expect(() => FontManager.measure("x", { fontFamily: "Nope" })).toThrow(/not registered/,diff --git a/packages/framefields/src/fonts/manager.ts b/packages/framefields/src/fonts/manager.tsindex 30667877..0d1cc4f8 100644--- a/packages/framefields/src/fonts/manager.ts+++ b/packages/framefields/src/fonts/manager.ts@@ -271,6 +271,8 @@ export class FontManager {options.fontFamily,options.fontWeight,null,+ undefined,+ fontSize,);if (!font) {throw new Error(diff --git a/packages/renderers/src/text-measurer.ts b/packages/renderers/src/text-measurer.tsindex cf3b2e02..82750a6e 100644--- a/packages/renderers/src/text-measurer.ts+++ b/packages/renderers/src/text-measurer.ts@@ -56,7 +56,13 @@ export function measureText(? style.lineHeight * fontSize: (style.lineHeight ?? fontSize * 1.2);- const slugFont = SlugFontCache.getFont(fontFamily, style.fontWeight, null);+ const slugFont = SlugFontCache.getFont(+ fontFamily,+ style.fontWeight,+ null,+ undefined,+ fontSize,+ );const padding = style.padding ?? 0;diff --git a/packages/webgpu-renderers/src/nodes/caption.ts b/packages/webgpu-renderers/src/nodes/caption.tsindex 10eb695c..eb72274b 100644--- a/packages/webgpu-renderers/src/nodes/caption.ts+++ b/packages/webgpu-renderers/src/nodes/caption.ts@@ -119,7 +119,13 @@ export function measureMaxCaptionHeight(if (SlugFontCache.isFailed(fontFamily)) {fontFamily = "Inter";}- const slugFont = SlugFontCache.getFont(fontFamily, style.fontWeight, null);+ const slugFont = SlugFontCache.getFont(+ fontFamily,+ style.fontWeight,+ null,+ undefined,+ fontSize,+ );let maxH = 0;for (const cue of captions) {@@ -287,6 +293,8 @@ export async function drawCaptionNode(fontFamily,props.fontWeight,ctx.device,+ undefined,+ fontSize,);if (!slugFont || !slugFont.curvesTex) {diff --git a/packages/webgpu-renderers/src/slug/slug-geometry.ts b/packages/webgpu-renderers/src/slug/slug-geometry.tsindex 7e5f9554..04c5477c 100644--- a/packages/webgpu-renderers/src/slug/slug-geometry.ts+++ b/packages/webgpu-renderers/src/slug/slug-geometry.ts@@ -412,12 +412,6 @@ export class SlugGeometry {const s = spans[sIdx];const rawText = String(resolveValue(s.text) ?? "");- let spanFont = defaultFont;- if (s.fontFamily || s.fontWeight !== undefined) {- const family = s.fontFamily ?? defaultFont.fontFamily ?? "Inter";- const resolved = SlugFontCache.getFont(family, s.fontWeight, null);- if (resolved) spanFont = resolved;- }const sizeValue = resolveValue(s.fontSize);const spanFontSize =typeof sizeValue === "number"@@ -425,6 +419,18 @@ export class SlugGeometry {: sizeValue !== undefined? Number(sizeValue): defaultFontSize;+ let spanFont = defaultFont;+ if (s.fontFamily || s.fontWeight !== undefined) {+ const family = s.fontFamily ?? defaultFont.fontFamily ?? "Inter";+ const resolved = SlugFontCache.getFont(+ family,+ s.fontWeight,+ null,+ undefined,+ spanFontSize,+ );+ if (resolved) spanFont = resolved;+ }const fillValue = resolveValue(s.fill);const opacityValue = resolveValue(s.opacity);const style: SpanStyle = {