Skip to content

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

fix(core): preserve text around SVG style runs (#1395)

Danny Wardf20572aba40f
diff --git a/changes/1395/fixed.md b/changes/1395/fixed.md
new file mode 100644
index 000000000..c4501763b
--- /dev/null
+++ b/changes/1395/fixed.md
@@ -0,0 +1 @@
+- SVG export preserves text outside styled character ranges.
diff --git a/packages/core/src/io/formats/svg/text/export.ts b/packages/core/src/io/formats/svg/text/export.ts
index b91349724..3a7adef81 100644
--- a/packages/core/src/io/formats/svg/text/export.ts
+++ b/packages/core/src/io/formats/svg/text/export.ts
@@ -4,8 +4,8 @@ import { resolveNodeTextDirection } from '@open-pencil/scene-graph/text/directio
import { formatColor, type SVGExportContext, type SVGFontAlias } from '#core/io/formats/svg/defs'
import { svg, type SVGNode } from '#core/io/formats/svg/node'
import { round } from '#core/io/formats/svg/paths'
-import { laidOutTextSpans } from '#core/io/formats/svg/text/lines'
-import { shownText, shownTextSlice } from '#core/text/case'
+import { laidOutTextSpans, textStyleSpans } from '#core/io/formats/svg/text/lines'
+import { shownText } from '#core/text/case'
import type { FontFaceRef } from '#core/text/face'
import { textFontFace } from '#core/text/requirements'
@@ -114,15 +114,11 @@ export function renderTextNode(
const y = node.fontSize || 14
if (node.styleRuns.length > 0) {
- const spans: SVGNode[] = []
- let pos = 0
- for (const run of node.styleRuns) {
- const text = shownTextSlice(node, pos, pos + run.length)
- pos += run.length
- spans.push(svg('tspan', spanAttrs(run.style), text))
- }
-
- return svg('text', { x, y, ...attrs }, ...spans)
+ return svg(
+ 'text',
+ { x, y, ...attrs, 'xml:space': 'preserve' },
+ ...textStyleSpans(node, spanAttrs)
+ )
}
return svg('text', { x, y, ...attrs }, shownText(node))
diff --git a/packages/core/src/io/formats/svg/text/lines.ts b/packages/core/src/io/formats/svg/text/lines.ts
index dda7c1448..5190d13e5 100644
--- a/packages/core/src/io/formats/svg/text/lines.ts
+++ b/packages/core/src/io/formats/svg/text/lines.ts
@@ -54,6 +54,17 @@ function textPieces(node: SceneNode): TextPiece[] {
return pieces
}
+/** One unpositioned span per style run, including uncovered text in the node's own style. */
+export function textStyleSpans(node: SceneNode, spanAttrs: SpanAttrs): SVGNode[] {
+ return textPieces(node).map((piece) =>
+ svg(
+ 'tspan',
+ piece.style ? spanAttrs(piece.style) : {},
+ shownTextSlice(node, piece.from, piece.to)
+ )
+ )
+}
+
/** The left and right edges of the characters from `from` to `to` that have glyphs. */
function spanEdges(line: SVGTextLine, from: number, to: number): SVGCharacterEdges | null {
let left = Infinity
diff --git a/packages/core/tests/io/formats/svg/text/lines.test.ts b/packages/core/tests/io/formats/svg/text/lines.test.ts
index 9dfbbc9d8..e82038f1a 100644
--- a/packages/core/tests/io/formats/svg/text/lines.test.ts
+++ b/packages/core/tests/io/formats/svg/text/lines.test.ts
@@ -139,6 +139,16 @@ describe('SVG text with laid-out lines', () => {
})
describe('SVG text case', () => {
+ test('keeps text before and after a style run on one line', () => {
+ const svg = exportText({
+ text: 'Hello bold world',
+ styleRuns: [{ start: 6, length: 4, style: { fontWeight: 700 } }]
+ })
+
+ expect(svg).toContain('xml:space="preserve"')
+ expect(svg).toContain('<tspan>Hello </tspan><tspan font-weight="700">bold</tspan><tspan> world</tspan>')
+ })
+
test('text on one line is written in the case it is shown in', () => {
expect(exportText({ text: 'Mixed Case', textCase: 'UPPER' })).toContain('>MIXED CASE</text>')
const runs = exportText({
diff --git a/tests/e2e/canvas/svg-paste-visual.spec.ts b/tests/e2e/canvas/svg-paste-visual.spec.ts
index d0a329331..5a2a29f32 100644
--- a/tests/e2e/canvas/svg-paste-visual.spec.ts
+++ b/tests/e2e/canvas/svg-paste-visual.spec.ts
@@ -1,3 +1,6 @@
+import { renderNodesToSVG } from '@open-pencil/core/io/formats/svg'
+import { SceneGraph } from '@open-pencil/scene-graph'
+
import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
@@ -14,14 +17,7 @@ const CLIPPED_SVG = `<svg viewBox="0 0 200 120" width="200" height="120">
</svg>`
test('pasted SVG clip paths draw through their mask groups', async () => {
- const pasted = await editor.page.evaluate(async (markup) => {
- const store = window.openPencil?.getStore?.()
- if (!store) throw new Error('OpenPencil store not initialized')
- const done = await store.pasteSVG(markup, 220, 160)
- store.clearSelection()
- store.requestRender()
- return done
- }, CLIPPED_SVG)
+ const pasted = await editor.canvas.pasteSVG(CLIPPED_SVG, 220, 160)
expect(pasted).toBe(true)
await editor.canvas.waitForRender()
editor.canvas.assertNoErrors()
@@ -36,16 +32,14 @@ const TEXT_SVG = `<svg viewBox="0 0 200 130" width="200" height="130">
</svg>`
test('pasted SVG text measures and draws as Figma lays it out', async () => {
- const layers = await editor.page.evaluate(async (markup) => {
+ await editor.canvas.pasteSVG(TEXT_SVG, 220, 160)
+ const layers = await editor.page.evaluate(() => {
const store = window.openPencil?.getStore?.()
if (!store) throw new Error('OpenPencil store not initialized')
- await store.pasteSVG(markup, 220, 160)
- store.clearSelection()
- store.requestRender()
return [...store.graph.getAllNodes()]
.filter((node) => node.type === 'TEXT')
.map(({ text, x, width, height }) => ({ text, x, width, height }))
- }, TEXT_SVG)
+ })
const expected = [
{ text: 'Hello', width: 48, height: 24 },
{ text: 'Bold', width: 44, height: 24 },
@@ -62,3 +56,27 @@ test('pasted SVG text measures and draws as Figma lays it out', async () => {
editor.canvas.assertNoErrors()
expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('svg-text-layers.png')
})
+
+function exportedStyledText(): string {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ const node = graph.createNode('TEXT', pageId, {
+ width: 200,
+ height: 60,
+ text: 'Hello bold world',
+ fontFamily: 'Inter',
+ fontSize: 24,
+ styleRuns: [{ start: 6, length: 4, style: { fontWeight: 700 } }]
+ })
+ const svg = renderNodesToSVG(graph, pageId, [node.id], { xmlDeclaration: false })
+ if (!svg) throw new Error('nothing exported')
+ return svg
+}
+
+test('exported SVG text keeps characters outside the style run', async () => {
+ const pasted = await editor.canvas.pasteSVG(exportedStyledText(), 220, 160)
+ expect(pasted).toBe(true)
+ await editor.canvas.waitForRender()
+ editor.canvas.assertNoErrors()
+ expect(await editor.canvas.screenshotCanvasRegion()).toMatchSnapshot('svg-export-style-runs.png')
+})
diff --git a/tests/e2e/canvas/svg-paste-visual.spec.ts-snapshots/svg-export-style-runs-openpencil-darwin.png b/tests/e2e/canvas/svg-paste-visual.spec.ts-snapshots/svg-export-style-runs-openpencil-darwin.png
new file mode 100644
index 000000000..b6367a129
Binary files /dev/null and b/tests/e2e/canvas/svg-paste-visual.spec.ts-snapshots/svg-export-style-runs-openpencil-darwin.png differ
diff --git a/tests/helpers/canvas.ts b/tests/helpers/canvas.ts
index f1082dbe3..3ccfb948d 100644
--- a/tests/helpers/canvas.ts
+++ b/tests/helpers/canvas.ts
@@ -79,6 +79,20 @@ export class CanvasHelper {
await this.waitForRender()
}
+ async pasteSVG(markup: string, x: number, y: number) {
+ return this.page.evaluate(
+ async ({ markup, x, y }) => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ const pasted = await store.pasteSVG(markup, x, y)
+ store.clearSelection()
+ store.requestRender()
+ return pasted
+ },
+ { markup, x, y }
+ )
+ }
+
async screenshotCanvas() {
return this.canvas.screenshot()
}