Skip to content

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

fix(svg): export solid fill and stroke opacity in sRGB (#1440)

Mark David Prado05cfe40acb44
diff --git a/changes/1440/fixed.md b/changes/1440/fixed.md
new file mode 100644
index 000000000..38511c464
--- /dev/null
+++ b/changes/1440/fixed.md
@@ -0,0 +1,2 @@
+- SVG export keeps solid fill and stroke opacity in sRGB, including the alpha of a `.pen` paint and of a text fill.
+- A tinted solid fill keeps its opacity in a Display P3 SVG, written beside `currentColor`.
diff --git a/packages/core/src/io/formats/svg/defs.ts b/packages/core/src/io/formats/svg/defs.ts
index fcacb67b7..8b2c99466 100644
--- a/packages/core/src/io/formats/svg/defs.ts
+++ b/packages/core/src/io/formats/svg/defs.ts
@@ -49,9 +49,41 @@ export function formatColor(
if (colorSpace === 'display-p3') {
return colorToDisplayCSS(alphaColor, { colorSpace })
}
+ // Hex has no alpha. Stops and shadows already emit theirs; solid fills use `solidFillOpacity`.
return colorToHex(alphaColor)
}
+/**
+ * A solid paint's SVG color. The canvas paints `opacity` and drops color alpha: `setAlphaf`
+ * replaces it, and `.pen` stores a hex alpha in both fields. Display P3 carries that painted
+ * alpha in the color. sRGB hex cannot, so `solidFillOpacity` emits it beside the color.
+ */
+export function formatSolidColor(
+ color: Color,
+ opacity: number,
+ colorSpace: RenderColorSpace
+): string {
+ if (colorSpace === 'display-p3') {
+ return formatColor({ ...color, a: 1 }, round(opacity), colorSpace)
+ }
+ return formatColor(color, 1, colorSpace)
+}
+
+/**
+ * `fill-opacity` for a solid paint. Omitted at full opacity so an opaque export stays unchanged.
+ * Display P3 already carries the same alpha in the color `formatSolidColor` returns, unless
+ * `separate` is set because that color was replaced and can no longer carry it.
+ */
+export function solidFillOpacity(
+ fill: Fill,
+ colorSpace: RenderColorSpace,
+ separate = false
+): number | undefined {
+ if (fill.type !== 'SOLID' || (colorSpace === 'display-p3' && !separate)) return undefined
+ const alpha = round(fill.opacity)
+ return alpha < 1 ? alpha : undefined
+}
+
function createGradientDef(
fill: Fill,
node: SceneNode,
@@ -230,7 +262,7 @@ export function resolveFill(fill: Fill, node: SceneNode, ctx: SVGExportContext):
if (!fill.visible) return null
if (fill.type === 'SOLID') {
- return formatColor(fill.color, fill.opacity, ctx.colorSpace)
+ return formatSolidColor(fill.color, fill.opacity, ctx.colorSpace)
}
if (fill.type.startsWith('GRADIENT')) {
diff --git a/packages/core/src/io/formats/svg/export.ts b/packages/core/src/io/formats/svg/export.ts
index c0e44cad9..c807058b9 100644
--- a/packages/core/src/io/formats/svg/export.ts
+++ b/packages/core/src/io/formats/svg/export.ts
@@ -7,9 +7,10 @@ import { computeContentBounds } from '#core/io/formats/raster'
import {
nextDefId,
- formatColor,
+ formatSolidColor,
createFilterDef,
resolveFill,
+ solidFillOpacity,
SVG_STROKE_CAP,
SVG_STROKE_JOIN,
SVG_BLEND_MODE
@@ -76,6 +77,7 @@ function vectorShapeElements(
svg('path', {
...attrs,
fill: fillAttr,
+ 'fill-opacity': solidFillOpacity(fill, ctx.colorSpace),
stroke: index === visibleFills.length - 1 ? common.stroke : 'none'
})
)
@@ -120,11 +122,13 @@ function lineDashAttrs(stroke: Stroke | undefined, length: number) {
function nodeShapeElements(
node: SceneNode,
fillAttr: string | null,
+ fillOpacity: number | undefined,
strokeAttrs: Record<string, string | number | undefined>,
ctx: SVGExportContext
): SVGNode[] {
const common: Record<string, string | number | undefined> = {
fill: fillAttr ?? 'none',
+ 'fill-opacity': fillOpacity,
...strokeAttrs
}
@@ -261,7 +265,9 @@ function buildSVGStrokeAttrs(
const stroke = visibleStrokes[0]
const cap = stroke.cap ?? nodeCap
const attrs: Record<string, string | number | undefined> = {
- stroke: formatColor(stroke.color, 1, colorSpace),
+ // Opacity 1 keeps the color opaque. Display P3 must not also keep color alpha here, or a
+ // viewer multiplies it by stroke-opacity; the canvas paints stroke.opacity alone.
+ stroke: formatSolidColor(stroke.color, 1, colorSpace),
'stroke-width': round(stroke.weight)
}
if (stroke.opacity < 1) attrs['stroke-opacity'] = round(stroke.opacity)
@@ -283,6 +289,7 @@ function buildShapeChildren(
node: SceneNode,
visibleFills: Fill[],
fillAttr: string | null,
+ fillOpacity: number | undefined,
strokeAttrs: Record<string, string | number | undefined>,
visibleStrokeCount: number,
ctx: SVGExportContext
@@ -290,7 +297,7 @@ function buildShapeChildren(
if (hasPathLevelFills(node)) {
return vectorShapeElements(
node,
- { fill: fillAttr ?? 'none', ...strokeAttrs },
+ { fill: fillAttr ?? 'none', 'fill-opacity': fillOpacity, ...strokeAttrs },
strokeAttrs,
ctx,
visibleFills
@@ -306,6 +313,7 @@ function buildShapeChildren(
...nodeShapeElements(
node,
ref,
+ solidFillOpacity(fill, ctx.colorSpace),
fill === visibleFills[visibleFills.length - 1] ? strokeAttrs : {},
ctx
)
@@ -317,7 +325,7 @@ function buildShapeChildren(
const hasFillOrStroke = fillAttr || visibleStrokeCount > 0
if (hasFillOrStroke && !isGroupLike(node)) {
- return nodeShapeElements(node, fillAttr, strokeAttrs, ctx)
+ return nodeShapeElements(node, fillAttr, fillOpacity, strokeAttrs, ctx)
}
return []
@@ -326,26 +334,43 @@ function buildShapeChildren(
/** The paint CSS `color` sets, for a tinted icon path. */
const CURRENT_COLOR = 'currentColor'
+/** A paint's SVG color and, for an sRGB solid, the `fill-opacity` beside it. */
+function exportedFill(
+ fill: Fill | undefined,
+ node: SceneNode,
+ ctx: SVGExportContext
+): { color: string | null; opacity: number | undefined } {
+ if (!fill) return { color: null, opacity: undefined }
+ return {
+ color: resolveFill(fill, node, ctx),
+ opacity: solidFillOpacity(fill, ctx.colorSpace)
+ }
+}
+
function renderNode(node: SceneNode, ctx: SVGExportContext): SVGNode | null {
if (!node.visible) return null
const { attrs: groupAttrs, clipId } = buildGroupAttrs(node, ctx)
if (node.type === 'TEXT') {
- const firstFill = node.fills.find((f) => f.visible)
- const fillAttr = firstFill ? resolveFill(firstFill, node, ctx) : null
- const textEl = renderTextNode(node, fillAttr, ctx)
- return svg('g', groupAttrs, textEl)
+ const fill = exportedFill(
+ node.fills.find((f) => f.visible),
+ node,
+ ctx
+ )
+ return svg('g', groupAttrs, renderTextNode(node, fill.color, ctx, fill.opacity))
}
const visibleFills = node.fills.filter((f) => f.visible)
const visibleStrokes = node.strokes.filter((s) => s.visible)
const tint = ctx.tint?.(node) ?? []
- const ownFill =
- visibleFills.length > 0 && !hasPathLevelFills(node)
- ? resolveFill(visibleFills[0], node, ctx)
- : null
- const fillAttr = ownFill && tint.includes('fill') ? CURRENT_COLOR : ownFill
+ const ownFill = visibleFills.length > 0 && !hasPathLevelFills(node) ? visibleFills[0] : undefined
+ const fill = exportedFill(ownFill, node, ctx)
+ const tintedFill = fill.color !== null && tint.includes('fill')
+ const fillAttr = tintedFill ? CURRENT_COLOR : fill.color
+ // `currentColor` has no alpha. Display P3 normally bakes solid opacity into the color, which a
+ // tint throws away, so a tinted solid keeps `fill-opacity` in every color space.
+ const fillOpacity = ownFill ? solidFillOpacity(ownFill, ctx.colorSpace, tintedFill) : fill.opacity
const ownStrokes = buildSVGStrokeAttrs(visibleStrokes, ctx.colorSpace, node.strokeCap)
const strokeAttrs =
ownStrokes.stroke && tint.includes('stroke')
@@ -356,6 +381,7 @@ function renderNode(node: SceneNode, ctx: SVGExportContext): SVGNode | null {
node,
visibleFills,
fillAttr,
+ fillOpacity,
strokeAttrs,
visibleStrokes.length,
ctx
diff --git a/packages/core/src/io/formats/svg/text/export.ts b/packages/core/src/io/formats/svg/text/export.ts
index b1aa3749a..0c2929062 100644
--- a/packages/core/src/io/formats/svg/text/export.ts
+++ b/packages/core/src/io/formats/svg/text/export.ts
@@ -16,7 +16,8 @@ import { textFontFace } from '#core/text/requirements'
function styleOverrideToTspanAttrs(
style: CharacterStyleOverride,
- colorSpace: 'srgb' | 'display-p3'
+ colorSpace: 'srgb' | 'display-p3',
+ parentFillOpacity: number | undefined
): Record<string, string | number | undefined> {
const attrs: Record<string, string | number | undefined> = {}
if (style.fontFamily) attrs['font-family'] = style.fontFamily
@@ -29,7 +30,16 @@ function styleOverrideToTspanAttrs(
if (style.fills) {
const visibleFill = style.fills.find((f) => f.visible && f.type === 'SOLID')
if (visibleFill) {
- attrs.fill = formatColor(visibleFill.color, visibleFill.opacity, colorSpace)
+ // Paragraphs and saved glyphs draw a run at color.a * opacity. Shapes paint opacity alone.
+ const alpha = round(visibleFill.color.a * visibleFill.opacity)
+ if (colorSpace === 'display-p3') {
+ attrs.fill = formatColor({ ...visibleFill.color, a: 1 }, alpha, colorSpace)
+ } else {
+ attrs.fill = formatColor(visibleFill.color, 1, colorSpace)
+ if (alpha < 1) attrs['fill-opacity'] = alpha
+ }
+ // `fill-opacity` inherits, so an opaque run has to cover a translucent parent.
+ if (alpha >= 1 && parentFillOpacity !== undefined) attrs['fill-opacity'] = 1
}
}
return attrs
@@ -107,7 +117,8 @@ function renderUnmeasuredText(
export function renderTextNode(
node: SceneNode,
fillAttr: string | null,
- ctx: SVGExportContext
+ ctx: SVGExportContext,
+ fillOpacity?: number
): SVGNode {
const { colorSpace, textLayout, fontAlias } = ctx
const direction = resolveNodeTextDirection(node)
@@ -124,6 +135,7 @@ export function renderTextNode(
'font-weight': node.fontWeight !== 400 ? node.fontWeight : undefined,
'font-style': node.italic ? 'italic' : undefined,
fill: fillAttr ?? undefined,
+ 'fill-opacity': fillOpacity,
// Laid-out spans each start at their own left edge, so they take no direction or anchor.
direction: direction === 'RTL' && !laidOut ? 'rtl' : undefined,
'text-anchor': laidOut ? undefined : textAnchorForNode(node, direction),
@@ -132,7 +144,7 @@ export function renderTextNode(
...(fontAlias ? aliasedFontAttrs(textFontFace(node), fontAlias) : {})
}
const spanAttrs = (style: CharacterStyleOverride) => ({
- ...styleOverrideToTspanAttrs(style, colorSpace),
+ ...styleOverrideToTspanAttrs(style, colorSpace, fillOpacity),
...(fontAlias ? aliasedFontAttrs(textFontFace(node, style), fontAlias) : {})
})
diff --git a/packages/core/tests/io/formats/svg/export.test.ts b/packages/core/tests/io/formats/svg/export.test.ts
index 16ad3c151..fe769e573 100644
--- a/packages/core/tests/io/formats/svg/export.test.ts
+++ b/packages/core/tests/io/formats/svg/export.test.ts
@@ -1,12 +1,22 @@
import { describe, expect, test } from 'bun:test'
-import { renderNodesToSVG } from '@open-pencil/core/io/formats/svg'
-import { SceneGraph, type SceneNode, type Stroke } from '@open-pencil/scene-graph'
-
import { expectDefined } from '#core-tests/helpers/assert'
+import {
+ renderNodesToSVG,
+ renderSVGNode,
+ type SVGExportOptions
+} from '@open-pencil/core/io/formats/svg'
+import { parsePenFile } from '@open-pencil/pen'
+import { SceneGraph, type Fill, type SceneNode, type Stroke } from '@open-pencil/scene-graph'
+
+import { renderNodeSVGElement } from '#core/io/formats/svg/export'
+
/** The attributes of the `<line>` an SVG export writes for a line with a 6 px black stroke. */
-function exportedLine(props: Partial<SceneNode>, stroke: Partial<Stroke> = {}): Map<string, string> {
+function exportedLine(
+ props: Partial<SceneNode>,
+ stroke: Partial<Stroke> = {}
+): Map<string, string> {
const graph = new SceneGraph()
const page = graph.getPages()[0].id
const line = graph.createNode('LINE', page, {
@@ -28,7 +38,9 @@ function exportedLine(props: Partial<SceneNode>, stroke: Partial<Stroke> = {}):
})
const svg = expectDefined(renderNodesToSVG(graph, page, [line.id]), 'SVG output')
const element = expectDefined(/<line\b([^>]*)>/.exec(svg), 'line element')[1]
- return new Map(Array.from(element.matchAll(/([\w-]+)="([^"]*)"/g), ([, name, value]) => [name, value]))
+ return new Map(
+ Array.from(element.matchAll(/([\w-]+)="([^"]*)"/g), ([, name, value]) => [name, value])
+ )
}
// The canvas strokes a line as Figma 126 does; its export draws the same.
@@ -51,3 +63,350 @@ describe('a line exported to SVG', () => {
expect(Number(short.get('stroke-dashoffset'))).toBeCloseTo(4.7619, 1)
})
})
+
+function svgOf(
+ build: (graph: SceneGraph, pageId: string) => string[],
+ options: SVGExportOptions = {}
+): string {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ return expectDefined(
+ renderNodesToSVG(graph, pageId, build(graph, pageId), { xmlDeclaration: false, ...options }),
+ 'SVG output'
+ )
+}
+
+function rects(svg: string): string[] {
+ return Array.from(svg.matchAll(/<rect\b[^>]*>/g), ([element]) => element)
+}
+
+function paths(svg: string): string[] {
+ return Array.from(svg.matchAll(/<path\b[^>]*>/g), ([element]) => element)
+}
+
+/** A move command, enough for a vector geometry to export as a path. */
+function moveTo(x: number, y: number): Uint8Array {
+ const blob = new Uint8Array(9)
+ blob[0] = 1
+ const view = new DataView(blob.buffer)
+ view.setFloat32(1, x, true)
+ view.setFloat32(5, y, true)
+ return blob
+}
+
+function solid(r: number, g: number, b: number, a: number, opacity: number): Fill {
+ return { type: 'SOLID', color: { r, g, b, a }, opacity, visible: true }
+}
+
+// Solid fill opacity is a paint attribute. Baking it into the layer would also fade strokes.
+describe('solid paint opacity exported to SVG', () => {
+ test('keeps sRGB fill opacities and leaves a fully opaque fill unchanged', () => {
+ const build = (graph: SceneGraph, pageId: string) => {
+ const frame = graph.createNode('FRAME', pageId, { width: 120, height: 40 })
+ for (const [index, opacity] of [0, 0.4, 1].entries()) {
+ graph.createNode('RECTANGLE', frame.id, {
+ x: index * 40,
+ width: 40,
+ height: 40,
+ fills: [{ type: 'SOLID', color: { r: 0, g: 0, b: 0, a: 1 }, opacity, visible: true }]
+ })
+ }
+ return [frame.id]
+ }
+
+ expect(rects(svgOf(build, { colorSpace: 'srgb' }))).toEqual([
+ '<rect width="40" height="40" fill="#000000" fill-opacity="0"/>',
+ '<rect width="40" height="40" fill="#000000" fill-opacity="0.4"/>',
+ '<rect width="40" height="40" fill="#000000"/>'
+ ])
+ expect(rects(svgOf(build, { colorSpace: 'display-p3' }))).toEqual([
+ '<rect width="40" height="40" fill="color(display-p3 0 0 0 / 0)"/>',
+ '<rect width="40" height="40" fill="color(display-p3 0 0 0 / 0.4)"/>',
+ '<rect width="40" height="40" fill="color(display-p3 0 0 0)"/>'
+ ])
+ })
+
+ test('paints a solid fill at its opacity, which replaces color alpha', () => {
+ const build = (graph: SceneGraph, pageId: string) => [
+ graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [solid(0, 0, 0, 0.5, 0.5)]
+ }).id
+ ]
+ // `.pen` stores one hex alpha in both fields. The product would export 0.25.
+ expect(rects(svgOf(build))).toEqual([
+ '<rect width="40" height="40" fill="#000000" fill-opacity="0.5"/>'
+ ])
+ expect(rects(svgOf(build, { colorSpace: 'display-p3' }))).toEqual([
+ '<rect width="40" height="40" fill="color(display-p3 0 0 0 / 0.5)"/>'
+ ])
+
+ const colorAlphaOnly = svgOf((graph, pageId) => [
+ graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [solid(0, 0, 0, 0.5, 1)]
+ }).id
+ ])
+ expect(rects(colorAlphaOnly)).toEqual(['<rect width="40" height="40" fill="#000000"/>'])
+ })
+
+ test('fades the fill without fading an opaque stroke', () => {
+ const svg = svgOf((graph, pageId) => [
+ graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [{ type: 'SOLID', color: { r: 0, g: 0, b: 0, a: 1 }, opacity: 0.4, visible: true }],
+ strokes: [
+ {
+ type: 'SOLID',
+ color: { r: 1, g: 0, b: 0, a: 1 },
+ opacity: 1,
+ visible: true,
+ weight: 1,
+ align: 'CENTER'
+ }
+ ]
+ }).id
+ ])
+ expect(svg).toContain(
+ '<rect width="40" height="40" fill="#000000" fill-opacity="0.4" stroke="#FF0000" stroke-width="1"/>'
+ )
+ expect(svg).not.toMatch(/(^|\s)opacity="/)
+ })
+
+ test('keeps each fill opacity when a shape has more than one', () => {
+ const svg = svgOf((graph, pageId) => [
+ graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [
+ { type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 0.4, visible: true },
+ { type: 'SOLID', color: { r: 0, g: 0, b: 1, a: 1 }, opacity: 1, visible: true }
+ ],
+ strokes: [
+ {
+ type: 'SOLID',
+ color: { r: 0, g: 0, b: 0, a: 1 },
+ opacity: 0.5,
+ visible: true,
+ weight: 2,
+ align: 'CENTER'
+ }
+ ]
+ }).id
+ ])
+ expect(rects(svg)).toEqual([
+ '<rect width="40" height="40" fill="#FF0000" fill-opacity="0.4"/>',
+ '<rect width="40" height="40" fill="#0000FF" stroke="#000000" stroke-width="2" stroke-opacity="0.5"/>'
+ ])
+ })
+
+ test('keeps stroke opacity and drops color alpha the canvas replaces', () => {
+ const stroke = (graph: SceneGraph, pageId: string, color: Stroke['color'], opacity: number) =>
+ graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ strokes: [{ type: 'SOLID', color, opacity, visible: true, weight: 2, align: 'CENTER' }]
+ }).id
+
+ const partial = svgOf((graph, pageId) => [
+ stroke(graph, pageId, { r: 0, g: 0, b: 0, a: 1 }, 0.4)
+ ])
+ expect(partial).toContain(
+ '<rect width="40" height="40" fill="none" stroke="#000000" stroke-width="2" stroke-opacity="0.4"/>'
+ )
+
+ const colorAlpha = svgOf((graph, pageId) => [
+ stroke(graph, pageId, { r: 0, g: 0, b: 0, a: 0.5 }, 1)
+ ])
+ expect(colorAlpha).toContain(
+ '<rect width="40" height="40" fill="none" stroke="#000000" stroke-width="2"/>'
+ )
+
+ const both = svgOf((graph, pageId) => [
+ stroke(graph, pageId, { r: 0, g: 0, b: 0, a: 0.5 }, 0.4)
+ ])
+ expect(both).toContain(
+ '<rect width="40" height="40" fill="none" stroke="#000000" stroke-width="2" stroke-opacity="0.4"/>'
+ )
+
+ // Display P3 leaves the color opaque so a viewer does not multiply it by stroke-opacity.
+ const p3 = svgOf(
+ (graph, pageId) => [stroke(graph, pageId, { r: 0, g: 0, b: 0, a: 0.5 }, 0.4)],
+ { colorSpace: 'display-p3' }
+ )
+ expect(p3).toContain(
+ '<rect width="40" height="40" fill="none" stroke="color(display-p3 0 0 0)" stroke-width="2" stroke-opacity="0.4"/>'
+ )
+ })
+
+ test('keeps text fill opacity, and an opaque run does not inherit it', () => {
+ const svg = svgOf((graph, pageId) => [
+ graph.createNode('TEXT', pageId, {
+ text: 'Ab',
+ width: 40,
+ height: 20,
+ fills: [{ type: 'SOLID', color: { r: 0, g: 0, b: 0, a: 1 }, opacity: 0.4, visible: true }],
+ styleRuns: [
+ { start: 0, length: 1, style: {} },
+ {
+ start: 1,
+ length: 1,
+ style: {
+ fills: [
+ { type: 'SOLID', color: { r: 1, g: 0, b: 0, a: 1 }, opacity: 1, visible: true }
+ ]
+ }
+ }
+ ]
+ }).id
+ ])
+ expect(svg).toContain(
+ '<text x="0" y="14" font-family="Inter" font-size="14" fill="#000000" fill-opacity="0.4" xml:space="preserve"><tspan>A</tspan><tspan fill="#FF0000" fill-opacity="1">b</tspan></text>'
+ )
+ })
+
+ test('multiplies a character fill color alpha by its opacity', () => {
+ const build = (graph: SceneGraph, pageId: string) => [
+ graph.createNode('TEXT', pageId, {
+ text: 'Ab',
+ width: 40,
+ height: 20,
+ fills: [solid(0, 0, 0, 1, 1)],
+ styleRuns: [
+ { start: 0, length: 1, style: {} },
+ {
+ start: 1,
+ length: 1,
+ style: { fills: [solid(0, 0, 0, 0.5, 0.4)] }
+ }
+ ]
+ }).id
+ ]
+
+ expect(svgOf(build)).toContain('<tspan fill="#000000" fill-opacity="0.2">b</tspan>')
+ expect(svgOf(build, { colorSpace: 'display-p3' })).toContain(
+ '<tspan fill="color(display-p3 0 0 0 / 0.2)">b</tspan>'
+ )
+ })
+
+ test('exports a .pen #00000080 fill and stroke at that alpha once', () => {
+ const graph = parsePenFile(
+ JSON.stringify({
+ version: '2.21',
+ children: [
+ {
+ id: 'mark',
+ type: 'rectangle',
+ width: 40,
+ height: 40,
+ fill: '#00000080',
+ stroke: '#00000080',
+ strokeWidth: 2
+ }
+ ]
+ })
+ )
+ const node = expectDefined(graph.getNode('mark'), 'imported rectangle')
+ const alpha = 128 / 255
+ expect(node.fills[0]).toMatchObject({ opacity: alpha, color: { a: alpha } })
+ expect(node.strokes[0]).toMatchObject({ opacity: alpha, color: { a: alpha } })
+
+ const pageId = graph.getPages()[0].id
+ const svg = expectDefined(
+ renderNodesToSVG(graph, pageId, [node.id], { xmlDeclaration: false }),
+ 'SVG output'
+ )
+ expect(svg).toContain(
+ '<rect width="40" height="40" fill="#000000" fill-opacity="0.5" stroke="#000000" stroke-width="2" stroke-opacity="0.5"/>'
+ )
+ expect(svg).not.toContain('0.25')
+
+ const p3 = expectDefined(
+ renderNodesToSVG(graph, pageId, [node.id], {
+ xmlDeclaration: false,
+ colorSpace: 'display-p3'
+ }),
+ 'Display P3 SVG output'
+ )
+ expect(p3).toContain(
+ '<rect width="40" height="40" fill="color(display-p3 0 0 0 / 0.5)" stroke="color(display-p3 0 0 0)" stroke-width="2" stroke-opacity="0.5"/>'
+ )
+ expect(p3).not.toContain('0.25')
+ })
+
+ test('keeps a tinted solid fill opacity beside currentColor in Display P3', () => {
+ const graph = new SceneGraph()
+ const pageId = graph.getPages()[0].id
+ const node = graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [solid(0, 0, 0, 1, 0.4)]
+ })
+ const element = (colorSpace: 'srgb' | 'display-p3', tinted: boolean) => {
+ const drawn = expectDefined(
+ renderNodeSVGElement(graph, node, {
+ idPrefix: 'n-',
+ colorSpace,
+ tint: tinted ? () => ['fill'] : undefined
+ }),
+ 'SVG element'
+ )
+ return renderSVGNode(drawn)
+ }
+
+ expect(element('display-p3', true)).toContain('fill="currentColor" fill-opacity="0.4"')
+ // Untinted Display P3 still carries the alpha in the color and omits fill-opacity.
+ const plain = element('display-p3', false)
+ expect(plain).toContain('fill="color(display-p3 0 0 0 / 0.4)"')
+ expect(plain).not.toContain('fill-opacity')
+ expect(element('srgb', true)).toContain('fill="currentColor" fill-opacity="0.4"')
+
+ // An opaque tint, such as an icon path, stays currentColor without an extra fill-opacity.
+ const opaque = graph.createNode('RECTANGLE', pageId, {
+ width: 40,
+ height: 40,
+ fills: [solid(0, 0, 0, 1, 1)]
+ })
+ const opaqueSVG = renderSVGNode(
+ expectDefined(
+ renderNodeSVGElement(graph, opaque, {
+ idPrefix: 'n-',
+ colorSpace: 'display-p3',
+ tint: () => ['fill']
+ }),
+ 'opaque SVG element'
+ )
+ )
+ expect(opaqueSVG).toContain('fill="currentColor"')
+ expect(opaqueSVG).not.toContain('fill-opacity')
+ })
+
+ test('keeps each geometry fill of a vector, translucent and opaque', () => {
+ const svg = svgOf((graph, pageId) => [
+ graph.createNode('VECTOR', pageId, {
+ width: 40,
+ height: 40,
+ fillGeometry: [
+ {
+ windingRule: 'NONZERO',
+ commandsBlob: moveTo(0, 0),
+ fills: [solid(0, 0, 0, 0.5, 0.5), solid(0, 0, 1, 1, 1)]
+ },
+ {
+ windingRule: 'NONZERO',
+ commandsBlob: moveTo(10, 0),
+ fills: [solid(1, 0, 0, 1, 0.4)]
+ }
+ ]
+ }).id
+ ])
+ expect(paths(svg)).toEqual([
+ '<path d="M0 0" fill="#000000" fill-opacity="0.5" stroke="none"/>',
+ '<path d="M0 0" fill="#0000FF"/>',
+ '<path d="M10 0" fill="#FF0000" fill-opacity="0.4"/>'
+ ])
+ })
+})