Skip to content

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

fix(canvas): render derived text decoration styles

Danila Poyarkov4224179501ce
diff --git a/packages/core/src/canvas/text-derived.ts b/packages/core/src/canvas/text-derived.ts
index 71ce05739..a18ef2391 100644
--- a/packages/core/src/canvas/text-derived.ts
+++ b/packages/core/src/canvas/text-derived.ts
@@ -1,10 +1,19 @@
-import type { Canvas } from 'canvaskit-wasm'
+import type { Canvas, Paint } from 'canvaskit-wasm'
-import type { SceneNode } from '#core/scene-graph'
+import type { Fill, SceneNode, StyleRun, TextDecorationStyle } from '#core/scene-graph'
import { geometryBlobToPath } from '#core/vector'
import type { SkiaRenderer } from './renderer'
+interface DecorationSpan {
+ x1: number
+ x2: number
+ style: TextDecorationStyle
+ thickness: number
+ offset: number
+ fills: Fill[]
+}
+
export function snapFigmaDerivedGlyphBaseline(y: number): number {
return Math.round(y)
}
@@ -24,6 +33,144 @@ export function derivedUnderlineRect(node: Pick<SceneNode, 'width'>, baselineY:
}
}
+function styleRunX(node: SceneNode, index: number): number {
+ if (node.text.length === 0) return 0
+ return (node.width * index) / node.text.length
+}
+
+function styleRunDecorationSpan(node: SceneNode, run: StyleRun): DecorationSpan | null {
+ const decoration = run.style.textDecoration ?? node.textDecoration
+ const hasDecorationOverride =
+ run.style.textDecoration !== undefined ||
+ run.style.textDecorationStyle !== undefined ||
+ run.style.textDecorationThickness !== undefined ||
+ run.style.textDecorationFills !== undefined ||
+ run.style.textUnderlineOffset !== undefined
+ if (decoration !== 'UNDERLINE' || !hasDecorationOverride) return null
+ return {
+ x1: styleRunX(node, run.start),
+ x2: styleRunX(node, run.start + run.length),
+ style: run.style.textDecorationStyle ?? node.textDecorationStyle,
+ thickness: run.style.textDecorationThickness ?? node.textDecorationThickness ?? 1,
+ offset: run.style.textUnderlineOffset ?? node.textUnderlineOffset ?? 0,
+ fills: run.style.textDecorationFills ?? node.textDecorationFills
+ }
+}
+
+function isDecorationSpan(span: DecorationSpan | null): span is DecorationSpan {
+ return span !== null
+}
+
+function baseDecorationSpan(node: SceneNode): DecorationSpan | null {
+ if (node.textDecoration !== 'UNDERLINE') return null
+ const rect = derivedUnderlineRect(node, 0)
+ return {
+ x1: rect.x1,
+ x2: rect.x2,
+ style: node.textDecorationStyle,
+ thickness: node.textDecorationThickness ?? rect.y2 - rect.y1,
+ offset: node.textUnderlineOffset ?? 0,
+ fills: node.textDecorationFills
+ }
+}
+
+function splitBaseDecorationSpan(
+ base: DecorationSpan,
+ overrides: DecorationSpan[]
+): DecorationSpan[] {
+ const spans: DecorationSpan[] = []
+ let cursor = base.x1
+ for (const override of overrides.toSorted((a, b) => a.x1 - b.x1)) {
+ if (override.x1 > cursor) spans.push({ ...base, x1: cursor, x2: override.x1 })
+ cursor = Math.max(cursor, override.x2)
+ }
+ if (cursor < base.x2) spans.push({ ...base, x1: cursor, x2: base.x2 })
+ return spans
+}
+
+function derivedDecorationSpans(node: SceneNode): DecorationSpan[] {
+ const overrides = node.styleRuns
+ .map((run) => styleRunDecorationSpan(node, run))
+ .filter(isDecorationSpan)
+ const base = baseDecorationSpan(node)
+ return base ? [...splitBaseDecorationSpan(base, overrides), ...overrides] : overrides
+}
+
+function firstVisibleFillColor(fills: Fill[]) {
+ const fill = fills.find((item) => item.visible && item.type === 'SOLID')
+ return fill?.color ?? null
+}
+
+function configureDecorationPaint(r: SkiaRenderer, span: DecorationSpan, paint: Paint): void {
+ const color = firstVisibleFillColor(span.fills)
+ if (color)
+ paint.setColor(r.ck.Color4f(color.r, color.g, color.b, color.a * (span.fills[0]?.opacity ?? 1)))
+ else paint.setColor(r.fillPaint.getColor())
+ paint.setAntiAlias(true)
+ paint.setStyle(r.ck.PaintStyle.Stroke)
+ paint.setStrokeWidth(span.thickness)
+}
+
+function drawSolidDecoration(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ paint: Paint,
+ span: DecorationSpan,
+ y: number
+): void {
+ paint.setStyle(r.ck.PaintStyle.Fill)
+ 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 drawWavyDecoration(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ paint: Paint,
+ span: DecorationSpan,
+ y: number
+): void {
+ const amplitude = Math.max(1, span.thickness * 1.5)
+ const wavelength = Math.max(6, span.thickness * 6)
+ const path = new r.ck.Path()
+ path.moveTo(span.x1, y)
+ for (let x = span.x1; x <= span.x2; x += 2) {
+ path.lineTo(x, y + Math.sin(((x - span.x1) / wavelength) * Math.PI * 2) * amplitude)
+ }
+ path.lineTo(span.x2, y)
+ canvas.drawPath(path, paint)
+ path.delete()
+}
+
+function drawDerivedDecorations(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ node: SceneNode,
+ baselineY: number
+): void {
+ const spans = derivedDecorationSpans(node)
+ if (spans.length === 0) return
+ const paint = new r.ck.Paint()
+ try {
+ for (const span of spans) {
+ const y = baselineY + 2.75 + span.offset
+ configureDecorationPaint(r, span, paint)
+ if (span.style === 'DOTTED') drawDottedDecoration(canvas, paint, span, y)
+ else if (span.style === 'WAVY') drawWavyDecoration(r, canvas, paint, span, y)
+ else drawSolidDecoration(r, canvas, paint, span, y)
+ }
+ } finally {
+ paint.delete()
+ }
+}
+
export function drawFigmaDerivedText(r: SkiaRenderer, canvas: Canvas, node: SceneNode): boolean {
if (!node.figmaDerivedTextGlyphs?.length) return false
@@ -42,9 +189,6 @@ export function drawFigmaDerivedText(r: SkiaRenderer, canvas: Canvas, node: Scen
path.delete()
}
- if (node.textDecoration === 'UNDERLINE') {
- const rect = derivedUnderlineRect(node, underlineBaselineY)
- canvas.drawRect(r.ltrb(rect.x1, rect.y1, rect.x2, rect.y2), r.fillPaint)
- }
+ drawDerivedDecorations(r, canvas, node, underlineBaselineY)
return true
}
diff --git a/tests/engine/render/canvas/derived-text.test.ts b/tests/engine/render/canvas/derived-text.test.ts
index f83cf0834..85ac6b11d 100644
--- a/tests/engine/render/canvas/derived-text.test.ts
+++ b/tests/engine/render/canvas/derived-text.test.ts
@@ -172,4 +172,101 @@ describe('derived text rendering', () => {
surface.delete()
}
})
+
+ test('draws styled decoration runs for Figma-derived text', async () => {
+ const graph = new SceneGraph()
+ const page = graph.getPages()[0]
+ const text = graph.createNode('TEXT', page.id, {
+ width: 60,
+ height: 28,
+ text: 'red blue',
+ fontFamily: '__MissingFont__',
+ textDecoration: 'UNDERLINE',
+ textDecorationStyle: 'WAVY',
+ textDecorationThickness: 2,
+ textDecorationFills: [
+ {
+ type: 'SOLID',
+ color: { r: 1, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true
+ }
+ ],
+ styleRuns: [
+ {
+ start: 4,
+ length: 4,
+ style: {
+ textDecorationStyle: 'DOTTED',
+ textDecorationThickness: 3,
+ textDecorationFills: [
+ {
+ type: 'SOLID',
+ color: { r: 0, g: 0, b: 1, a: 1 },
+ opacity: 1,
+ visible: true
+ }
+ ]
+ }
+ }
+ ],
+ fills: [
+ {
+ type: 'SOLID',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true
+ }
+ ],
+ figmaDerivedTextGlyphs: [
+ {
+ commandsBlob: squareCommandsBlob(),
+ x: 2,
+ y: 12,
+ fontSize: 10
+ }
+ ]
+ })
+
+ const surface = expectDefined(ck.MakeSurface(1, 1), 'surface')
+ const renderer = new SkiaRenderer(ck, surface)
+
+ try {
+ const png = expectDefined(
+ renderNodesToImage(ck, renderer, graph, page.id, [text.id], {
+ scale: 1,
+ format: 'PNG'
+ }),
+ 'png'
+ )
+ const image = expectDefined(ck.MakeImageFromEncoded(png), 'image')
+ const pixels = expectDefined(
+ image.readPixels(0, 0, {
+ alphaType: ck.AlphaType.Unpremul,
+ colorType: ck.ColorType.RGBA_8888,
+ colorSpace: ck.ColorSpace.SRGB,
+ width: image.width(),
+ height: image.height()
+ }),
+ 'pixels'
+ )
+
+ let redPixels = 0
+ let bluePixels = 0
+ for (let index = 0; index < pixels.length; index += 4) {
+ const red = pixels[index] ?? 0
+ const green = pixels[index + 1] ?? 0
+ const blue = pixels[index + 2] ?? 0
+ const alpha = pixels[index + 3] ?? 0
+ if (alpha > 0 && red > 150 && green < 80 && blue < 80) redPixels++
+ if (alpha > 0 && blue > 150 && red < 80 && green < 80) bluePixels++
+ }
+
+ expect(redPixels).toBeGreaterThan(0)
+ expect(bluePixels).toBeGreaterThan(0)
+ image.delete()
+ } finally {
+ surface.delete()
+ }
+ })
})