Skip to content

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

fix(core): turn path text glyphs along their chord, as Figma does (#1152)

rcoenen3bbacdc0b507
diff --git a/changes/1152/fixed.md b/changes/1152/fixed.md
new file mode 100644
index 000000000..da44941ac
--- /dev/null
+++ b/changes/1152/fixed.md
@@ -0,0 +1 @@
+- After editing text on a path, each character turns along the stretch of path it covers, as in Figma, so wide characters no longer point off the curve on tight bends.
diff --git a/packages/core/src/text/path/layout.ts b/packages/core/src/text/path/layout.ts
index d076f64d0..77cbbab01 100644
--- a/packages/core/src/text/path/layout.ts
+++ b/packages/core/src/text/path/layout.ts
@@ -12,7 +12,7 @@ export interface PathTextLayout {
deltas: number[]
/** Per-glyph signed offset along the left normal (-ty, tx), px. */
offsets: number[]
- /** Per-glyph rotation minus the tangent-derived base rotation, radians. */
+ /** Per-glyph rotation minus its chord's rotation, radians. */
phases: number[]
}
@@ -32,6 +32,28 @@ function baseRotation(p: PathPoint, forward: boolean): number {
return -Math.atan2(ty, tx)
}
+/**
+ * A glyph's rotation as Figma sets it: along the line from where the glyph starts on the path to
+ * where it ends, so a wide glyph on a bend sits across the curve instead of along the tangent at
+ * its left edge. A glyph with no width takes the tangent.
+ */
+function chordRotation(start: PathPoint, end: PathPoint, forward: boolean): number {
+ const dx = end.x - start.x
+ const dy = end.y - start.y
+ if (Math.hypot(dx, dy) < 1e-6) return baseRotation(start, forward)
+ return -Math.atan2(dy, dx)
+}
+
+/**
+ * How far along the path, signed, glyph `index` spans: to where the next glyph starts. The last
+ * glyph is taken as wide as the one before it, and a lone glyph has no width, so it takes the
+ * tangent.
+ */
+function glyphSpan(deltas: number[], index: number): number {
+ if (index + 1 < deltas.length) return deltas[index + 1] - deltas[index]
+ return index > 0 ? deltas[index] - deltas[index - 1] : 0
+}
+
/**
* Measure layout parameters from the node's current baked glyph baselines.
* Reflowing with these on the same box reproduces the input exactly.
@@ -54,7 +76,6 @@ export function calibratePathTextLayout(
const off = (g.x - p.x) * -p.ty + (g.y - p.y) * p.tx
positions.push(p)
offsets.push(off)
- phases.push(normalizeAngle((g.rotation ?? 0) - baseRotation(p, data.forward)))
}
const s0 = positions[0].s
@@ -67,6 +88,13 @@ export function calibratePathTextLayout(
}
return d
})
+ // Rotations are kept relative to each glyph's chord, so a resize that bends the path more or
+ // less turns the glyphs with it, as re-laying out the text does.
+ for (const [index, g] of glyphs.entries()) {
+ const p = positions[index]
+ const end = pointAtArc(path, p.s + glyphSpan(deltas, index))
+ phases.push(normalizeAngle((g.rotation ?? 0) - chordRotation(p, end, data.forward)))
+ }
return { anchor: s0 / path.length, deltas, offsets, phases }
}
@@ -101,7 +129,7 @@ export function layoutPathTextFromAdvances(
x: p.x + -p.ty * offset,
y: p.y + p.tx * offset,
fontSize: src.fontSize,
- rotation: normalizeAngle(baseRotation(p, data.forward))
+ rotation: normalizeAngle(chordRotation(p, pointAtArc(path, s), data.forward))
}
})
}
@@ -118,13 +146,14 @@ export function reflowPathTextGlyphs(
const s0 = layout.anchor * path.length
return glyphs.map((g, i) => {
const p = pointAtArc(path, s0 + layout.deltas[i])
+ const end = pointAtArc(path, p.s + glyphSpan(layout.deltas, i))
const off = layout.offsets[i]
return {
...g,
commandsBlob: new Uint8Array(g.commandsBlob),
x: p.x + -p.ty * off,
y: p.y + p.tx * off,
- rotation: normalizeAngle(baseRotation(p, data.forward) + layout.phases[i]),
+ rotation: normalizeAngle(chordRotation(p, end, data.forward) + layout.phases[i]),
// Reflow replaces geometric scaling entirely.
scaleX: undefined,
scaleY: undefined
diff --git a/tests/engine/core/text/path-layout.test.ts b/tests/engine/core/text/path-layout.test.ts
index 0801e795d..2ae1bbf23 100644
--- a/tests/engine/core/text/path-layout.test.ts
+++ b/tests/engine/core/text/path-layout.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, test } from 'bun:test'
import { SceneGraph } from '@open-pencil/scene-graph'
import type { SceneNode, VectorNetwork } from '@open-pencil/scene-graph'
+import type { Vector } from '@open-pencil/scene-graph/primitives'
import {
calibratePathTextLayout,
@@ -370,3 +371,96 @@ describe('selection overlay — path helpers', () => {
}
})
})
+
+describe('pen-walked glyph rotation', () => {
+ const zero = { x: 0, y: 0 }
+ /** Right 100, then down 100: 200 long, turning a quarter at (100, 50). */
+ function corner(forward = true) {
+ const network: VectorNetwork = {
+ vertices: [
+ { x: 0, y: 50 },
+ { x: 100, y: 50 },
+ { x: 100, y: 150 }
+ ],
+ segments: [
+ { start: 0, end: 1, tangentStart: zero, tangentEnd: zero },
+ { start: 1, end: 2, tangentStart: zero, tangentEnd: zero }
+ ],
+ regions: []
+ }
+ const data = { network, normalizedSize: { x: 400, y: 200 }, tValue: 0, forward }
+ const box = { x: 0, y: 0, width: 400, height: 200 }
+ return { data, box, path: expectDefined(sampleTextPath(data, box)) }
+ }
+ /** A glyph 20 wide at font size 40. */
+ const source = { commandsBlob: new Uint8Array(4), fontSize: 40, advance: 0.5 }
+ const chordBetween = (a: Vector, b: Vector) => -Math.atan2(b.y - a.y, b.x - a.x)
+
+ test('a glyph on text running backwards turns along its chord the other way', () => {
+ const { data, box, path } = corner(false)
+ // Starts 10 past the corner and runs back to 10 before it.
+ const [glyph] = expectDefined(
+ layoutPathTextFromAdvances(data, box, 110 / path.length, 0, [source])
+ )
+ const chord = chordBetween(pointAtArc(path, 110), pointAtArc(path, 90))
+ expect(Math.abs(angleDiff(expectDefined(glyph.rotation), chord))).toBeLessThan(1e-9)
+ })
+
+ test('a glyph past the end of an open path turns along what is left of it', () => {
+ const { data, box, path } = corner()
+ // Starts 5 before the end; the rest of it hangs past the end, which stays put.
+ const [glyph] = expectDefined(
+ layoutPathTextFromAdvances(data, box, 195 / path.length, 0, [source])
+ )
+ expect(Math.abs(angleDiff(expectDefined(glyph.rotation), -Math.PI / 2))).toBeLessThan(1e-9)
+ })
+
+ test('a resize turns the glyphs along their new chords, as re-laying them out does', () => {
+ const { data, box, path } = corner()
+ const glyphs = expectDefined(
+ layoutPathTextFromAdvances(data, box, 80 / path.length, 0, [source, source, source])
+ )
+ const layout = expectDefined(calibratePathTextLayout(glyphs, data, box))
+ // Half as wide: the first leg shortens and the corner bends at a different angle.
+ const narrow = { ...box, width: box.width / 2 }
+ const reflowed = expectDefined(reflowPathTextGlyphs(glyphs, data, layout, narrow))
+ const fresh = expectDefined(
+ layoutPathTextFromAdvances(data, narrow, layout.anchor, 0, [source, source, source])
+ )
+ for (const [index, glyph] of reflowed.entries()) {
+ const rotation = expectDefined(glyph.rotation)
+ expect(Math.abs(angleDiff(rotation, expectDefined(fresh[index].rotation)))).toBeLessThan(1e-6)
+ }
+ })
+
+ test('a glyph turns along the line from where it starts on the path to where it ends', () => {
+ const zero = { x: 0, y: 0 }
+ // Right 100, then down 100: 200 long, turning a quarter at (100, 50).
+ const network: VectorNetwork = {
+ vertices: [
+ { x: 0, y: 50 },
+ { x: 100, y: 50 },
+ { x: 100, y: 150 }
+ ],
+ segments: [
+ { start: 0, end: 1, tangentStart: zero, tangentEnd: zero },
+ { start: 1, end: 2, tangentStart: zero, tangentEnd: zero }
+ ],
+ regions: []
+ }
+ const data = { network, normalizedSize: { x: 400, y: 200 }, tValue: 0, forward: true }
+ const box = { x: 0, y: 0, width: 400, height: 200 }
+ const source = { commandsBlob: new Uint8Array(4), fontSize: 40, advance: 0.5 }
+ const path = expectDefined(sampleTextPath(data, box))
+ // The glyph starts 10 before the corner and, 20 wide, ends 10 past it.
+ const [straddling] = expectDefined(
+ layoutPathTextFromAdvances(data, box, 90 / path.length, 0, [source])
+ )
+ const start = pointAtArc(path, 90)
+ const end = pointAtArc(path, 110)
+ const chord = -Math.atan2(end.y - start.y, end.x - start.x)
+ expect(Math.abs(angleDiff(expectDefined(straddling.rotation), chord))).toBeLessThan(1e-9)
+ // Close to a half turn of the corner, not level with the first leg as its left edge is.
+ expect(Math.abs(angleDiff(expectDefined(straddling.rotation), -Math.PI / 4))).toBeLessThan(0.05)
+ })
+})