AI-native design editor. Open-source Figma alternative.
fix(CLI): measure a script's text in the document's fonts (#1504)
Marcos Velásquez4356a3113d69
diff --git a/changes/1504/fixed.md b/changes/1504/fixed.mdnew file mode 100644index 000000000..709aafb01--- /dev/null+++ b/changes/1504/fixed.md@@ -0,0 +1 @@+- `openpencil eval` and tool calls measure the text a script edits in the fonts the document uses.diff --git a/packages/cli/src/commands/eval.ts b/packages/cli/src/commands/eval.tsindex 771e460c3..44c551bce 100644--- a/packages/cli/src/commands/eval.ts+++ b/packages/cli/src/commands/eval.ts@@ -9,7 +9,7 @@ import { printError } from '#cli/format'import {documentWriteOptions,loadDocument,- measureTextHeadlessly,+ measureDocumentText,populateWholeDocument,writeFigDocument} from '#cli/headless'@@ -71,7 +71,7 @@ export default defineCommand({const file = requireFile(args.file)const graph = await loadDocument(file)populateWholeDocument(graph)- await measureTextHeadlessly()+ await measureDocumentText(graph)const figma = new FigmaAPI(graph)let result: unknowndiff --git a/packages/cli/src/headless.ts b/packages/cli/src/headless.tsindex 5f32a6ac5..9f9d79b0a 100644--- a/packages/cli/src/headless.ts+++ b/packages/cli/src/headless.ts@@ -7,6 +7,7 @@ import { populateAllFigPages, populateFigPage } from '@open-pencil/core/io/formaimport { headlessHTMLReader } from '@open-pencil/core/io/formats/html/import'import { initCanvasKit } from '@open-pencil/core/io/formats/raster'import { computeAllLayouts, installTextMeasurer } from '@open-pencil/core/layout'+import { prepareGraphFonts } from '@open-pencil/core/text'import type { SceneGraph } from '@open-pencil/scene-graph'import { printError } from '#cli/format'@@ -64,6 +65,19 @@ export function measureTextHeadlessly(): Promise<void> {return textMeasurer}+/**+ * Measures the text an edit to `graph` lays out as `measureTextHeadlessly` does, in the fonts the+ * document uses: a weight or family the measurer does not start with is otherwise measured in a+ * stand-in, and the edited layer comes out taller or wider than the app draws it.+ */+export async function measureDocumentText(graph: SceneGraph): Promise<void> {+ await measureTextHeadlessly()+ await prepareGraphFonts(+ graph,+ graph.getPages(true).map((page) => page.id)+ )+}+/** `--write` and `--output` for commands that change a headless document. */export const documentWriteOptions = {write: { type: 'boolean', alias: 'w', description: 'Write changes back to the input file' },diff --git a/packages/cli/src/tool-data.ts b/packages/cli/src/tool-data.tsindex b11966a78..141f5a98a 100644--- a/packages/cli/src/tool-data.ts+++ b/packages/cli/src/tool-data.ts@@ -9,7 +9,7 @@ import type { SceneGraph } from '@open-pencil/scene-graph'import { isAppMode, requireFile, rpc } from '#cli/app/client'import { appTargetRPCArgs, type AppTargetCLIArgs } from '#cli/app/target'-import { loadDocument, measureTextHeadlessly, populateWholeDocument } from '#cli/headless'+import { loadDocument, measureDocumentText, populateWholeDocument } from '#cli/headless'/** A Figma API over a headless document, with CanvasKit raster export and pixel decoding. */export async function createHeadlessFigma(graph: SceneGraph): Promise<FigmaAPI> {@@ -38,7 +38,7 @@ export async function runToolData(if (!tool) throw new Error(`Unknown tool: ${name}`)const graph = await loadDocument(requireFile(file))populateWholeDocument(graph)- await measureTextHeadlessly()+ await measureDocumentText(graph)const figma = await createHeadlessFigma(graph)return { result: await tool.execute(figma, args), graph }}diff --git a/packages/cli/tests/commands/eval.test.ts b/packages/cli/tests/commands/eval.test.tsindex c255f63b0..c56edfef3 100644--- a/packages/cli/tests/commands/eval.test.ts+++ b/packages/cli/tests/commands/eval.test.ts@@ -6,6 +6,10 @@ import { join } from 'node:path'import { CLI_ENTRY, FIXTURES } from '#cli-tests/helpers/paths'import * as v from 'valibot'+import { getTextMeasurer } from '@open-pencil/core/layout'++import { loadDocument, measureDocumentText } from '#cli/headless'+setDefaultTimeout(60_000)async function cli(args: string[], stdin?: string) {@@ -22,6 +26,49 @@ async function cli(args: string[], stdin?: string) {}describe('eval CLI', () => {+ test('measures the text a script edits in the font the document sets for it', async () => {+ const dir = await mkdtemp(join(tmpdir(), 'open-pencil-eval-fonts-'))+ const source = join(dir, 'tag.pen')+ const output = join(dir, 'tag.fig')+ // Inter at 600 is not among the faces the measurer starts with.+ await Bun.write(+ source,+ JSON.stringify({+ version: '2.21',+ children: [+ {+ id: 'tag',+ type: 'frame',+ layout: 'horizontal',+ padding: [4, 10],+ children: [+ {+ id: 'label',+ type: 'text',+ content: '1 lote',+ fontFamily: 'Inter',+ fontSize: 12,+ fontWeight: '600',+ fill: '#111111'+ }+ ]+ }+ ]+ })+ )+ const script = `figma.root.findOne((node) => node.type === 'TEXT').characters = '1 lote propuesto'`+ expect((await cli(['eval', source, '--stdin', '-o', output], script)).exitCode).toBe(0)++ const graph = await loadDocument(output)+ const label = [...graph.getAllNodes()].find((node) => node.type === 'TEXT')+ if (!label) throw new Error('Expected the label')+ await measureDocumentText(graph)+ const measured = getTextMeasurer()?.(label)+ if (!measured) throw new Error('Expected the label measured in its font')+ expect(label.width).toBeCloseTo(measured.width, 0)+ expect(label.height).toBeCloseTo(measured.height, 0)+ })+test('measures the text a script writes, its line breaks and wrapping included', async () => {const script = `await figma.loadFontAsync({ family: 'Inter', style: 'Regular' })