Skip to content

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

fix(canvas): derive rich decoration geometry

Danila Poyarkovd3eed6f4a9fc
diff --git a/packages/core/src/canvas/text-derived.ts b/packages/core/src/canvas/text-derived.ts
index 61bdb7947..7a8ec4f73 100644
--- a/packages/core/src/canvas/text-derived.ts
+++ b/packages/core/src/canvas/text-derived.ts
@@ -34,6 +34,9 @@ export function derivedUnderlineRect(node: Pick<SceneNode, 'width'>, baselineY:
}
function styleRunX(node: SceneNode, index: number): number {
+ const glyph = node.figmaDerivedTextGlyphs?.[index]
+ if (glyph) return glyph.x
+ if (index >= node.text.length) return node.width
if (node.text.length === 0) return 0
return (node.width * index) / node.text.length
}
@@ -122,11 +125,19 @@ function drawSolidDecoration(
canvas.drawRect(r.ltrb(span.x1, y, span.x2, y + span.thickness), paint)
}
-function drawDottedDecoration(canvas: Canvas, paint: Paint, span: DecorationSpan, y: number): void {
- const radius = Math.max(0.5, span.thickness / 2)
- const step = Math.max(radius * 3, 3)
- for (let x = span.x1 + radius; x <= span.x2 - radius; x += step) {
- canvas.drawCircle(x, y + radius, radius, paint)
+function drawDottedDecoration(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ paint: Paint,
+ span: DecorationSpan,
+ y: number
+): void {
+ paint.setStyle(r.ck.PaintStyle.Fill)
+ const dotSize = 1
+ const step = dotSize * 2
+ const dotY = y - span.thickness / 3
+ for (let x = span.x1; x <= span.x2; x += step) {
+ canvas.drawRect(r.ck.LTRBRect(x, dotY, x + dotSize, dotY + span.thickness), paint)
}
}
@@ -137,7 +148,7 @@ function drawWavyDecoration(
span: DecorationSpan,
y: number
): void {
- const amplitude = Math.max(0.75, span.thickness * 0.75)
+ const amplitude = Math.max(0.5, span.thickness * 0.5)
const wavelength = Math.max(6, span.thickness * 5)
const path = new r.ck.Path()
path.moveTo(span.x1, y)
@@ -149,6 +160,12 @@ function drawWavyDecoration(
path.delete()
}
+function derivedDecorationY(node: SceneNode, span: DecorationSpan, baselineY: number): number {
+ const hasRichDecoration = span.style !== 'SOLID' || span.fills.length > 0
+ if (!hasRichDecoration) return derivedUnderlineRect(node, baselineY).y1 + span.offset
+ return baselineY + node.fontSize / 2 - span.thickness / 4 + span.offset
+}
+
function drawDerivedDecorations(
r: SkiaRenderer,
canvas: Canvas,
@@ -160,10 +177,9 @@ function drawDerivedDecorations(
const paint = new r.ck.Paint()
try {
for (const span of spans) {
- const hasRichDecoration = span.style !== 'SOLID' || span.fills.length > 0
- const y = baselineY + (hasRichDecoration ? 13.5 : 2.75) + span.offset
+ const y = derivedDecorationY(node, span, baselineY)
configureDecorationPaint(r, span, paint)
- if (span.style === 'DOTTED') drawDottedDecoration(canvas, paint, span, y)
+ if (span.style === 'DOTTED') drawDottedDecoration(r, canvas, paint, span, y)
else if (span.style === 'WAVY') drawWavyDecoration(r, canvas, paint, span, y)
else drawSolidDecoration(r, canvas, paint, span, y)
}
diff --git a/tests/fixtures/figma-oracles/rich-text-decoration.json b/tests/fixtures/figma-oracles/rich-text-decoration.json
index 47463761d..c7849cf6b 100644
--- a/tests/fixtures/figma-oracles/rich-text-decoration.json
+++ b/tests/fixtures/figma-oracles/rich-text-decoration.json
@@ -2,13 +2,17 @@
"source": {
"tool": "figma-use eval",
"fileKey": "jSxlQDCrjsvqEQq17IWEcC",
- "page": "OpenPencil rich text oracle 2026-05-22",
- "nodeId": "296488:2238",
- "captured": "2026-05-22"
+ "page": "OpenPencil rich text oracle 2026-05-27",
+ "nodeId": "297274:2265",
+ "captured": "2026-05-27",
+ "note": "Original node 296488:2238 was later overwritten in the live Figma file; this fixture was recreated from the same styling recipe."
},
"characters": "wavy red | dotted blue | cap trim",
"leadingTrim": "CAP_HEIGHT",
- "textDecorationOffset": { "unit": "PIXELS", "value": 5 },
+ "textDecorationOffset": {
+ "unit": "PIXELS",
+ "value": 5
+ },
"textDecorationSkipInk": false,
"textDecorationOffsetOracleNodeId": "296619:2252",
"ranges": [
@@ -18,14 +22,21 @@
"text": "wavy red ",
"textDecoration": "UNDERLINE",
"textDecorationStyle": "WAVY",
- "textDecorationThickness": { "unit": "PIXELS", "value": 2 },
+ "textDecorationThickness": {
+ "unit": "PIXELS",
+ "value": 2
+ },
"textDecorationColor": {
"value": {
"type": "SOLID",
"visible": true,
"opacity": 1,
"blendMode": "NORMAL",
- "color": { "r": 1, "g": 0, "b": 0 },
+ "color": {
+ "r": 1,
+ "g": 0,
+ "b": 0
+ },
"boundVariables": {}
}
}
@@ -36,17 +47,27 @@
"text": "dotted blue",
"textDecoration": "UNDERLINE",
"textDecorationStyle": "DOTTED",
- "textDecorationThickness": { "unit": "PIXELS", "value": 3 },
+ "textDecorationThickness": {
+ "unit": "PIXELS",
+ "value": 3
+ },
"textDecorationColor": {
"value": {
"type": "SOLID",
"visible": true,
"opacity": 1,
"blendMode": "NORMAL",
- "color": { "r": 0, "g": 0.25, "b": 1 },
+ "color": {
+ "r": 0,
+ "g": 0.25,
+ "b": 1
+ },
"boundVariables": {}
}
}
}
- ]
+ ],
+ "mainTextDecorationOffset": {
+ "unit": "AUTO"
+ }
}
diff --git a/tests/fixtures/figma-oracles/visual-comparison-report.json b/tests/fixtures/figma-oracles/visual-comparison-report.json
index 6bbac4292..82232aa07 100644
--- a/tests/fixtures/figma-oracles/visual-comparison-report.json
+++ b/tests/fixtures/figma-oracles/visual-comparison-report.json
@@ -24,16 +24,19 @@
},
{
"name": "rich-text-decorations",
- "nodeId": "296488:2238",
- "output": "/tmp/open-pencil-oracles/rich-text/decorations-bounds-derived",
+ "nodeId": "297274:2265",
+ "output": "/tmp/open-pencil-oracles/rich-text/recreated-decoration-metric-derived",
"figmaSize": "493x41",
"openPencilSize": "493x42",
"normalization": "Padded/cropped without scaling before diff",
- "differentPixels": 7568,
- "differentPercent": 37.44,
- "rmseNormalized": 0.22616,
- "previousOpenPencilSize": "493x23",
- "finding": "Text export bounds now include decoration overflow so underlines are not clipped, but derived glyph rendering still approximates rich decoration style/color poorly."
+ "differentPixels": 7734,
+ "differentPercent": 38.26,
+ "rmseNormalized": 0.217758,
+ "finding": "Recreated rich text decoration oracle. Derived-glyph decorations now use glyph positions and font metrics instead of inline pixel offsets; colored decoration row bounds are close, but glyph/text rendering differences still dominate the raster diff.",
+ "fuzz": "2%",
+ "fuzzDifferentPixels": 7361,
+ "fuzzDifferentPercent": 36.42,
+ "rmse": "14270.7 (0.217758)"
},
{
"name": "nested-luminance-mask",