@pairdraw/dom-css 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/behaviours/states/layers.js +58 -57
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +573 -38
- package/dist/chunks/browser.js.map +1 -1
- package/dist/export.d.ts +1 -1
- package/package.json +3 -3
- package/src/export/css.ts +36 -20
- package/src/export/projection.ts +34 -18
- package/src/export/text.ts +32 -16
- package/src/import/background-pictures.ts +430 -0
- package/src/import/scene-graph.ts +53 -51
- package/src/import/stacking.ts +109 -0
- package/src/runtime/browser/runtime.ts +9 -3
- package/src/runtime/browser/sandbox.ts +70 -0
- package/src/tokens/names.ts +3 -3
- package/src/tokens/stylesheet.ts +13 -4
package/dist/export.d.ts
CHANGED
|
@@ -412,7 +412,7 @@ type TokenStylesheet = {
|
|
|
412
412
|
css: string;
|
|
413
413
|
issues: TokenStylesheetIssue[];
|
|
414
414
|
};
|
|
415
|
-
type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections'>;
|
|
415
|
+
type TokenSource = Pick<SceneGraph, 'variables' | 'variableCollections' | 'documentColorSpace'>;
|
|
416
416
|
/** The attribute a collection's manual modes are switched by, unless it names its own. */
|
|
417
417
|
declare function defaultModeAttributeName(collection: VariableCollection): string;
|
|
418
418
|
/** The attribute that puts an element in a mode with no condition of its own: `data-theme`. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pairdraw/dom-css",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"files": [
|
|
6
6
|
"dist",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts",
|
|
12
12
|
"dependencies": {
|
|
13
13
|
"@acemir/cssom": "^0.9.31",
|
|
14
|
-
"@pairdraw/emit": "^0.
|
|
14
|
+
"@pairdraw/emit": "^0.21.0",
|
|
15
15
|
"css-tree": "^3.2.1",
|
|
16
16
|
"cssesc": "^3.0.0",
|
|
17
17
|
"dedent": "^1.7.2",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"twirlwind": "^0.4.1"
|
|
25
25
|
},
|
|
26
26
|
"peerDependencies": {
|
|
27
|
-
"@pairdraw/scene-graph": "^0.
|
|
27
|
+
"@pairdraw/scene-graph": "^0.21.0"
|
|
28
28
|
},
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
package/src/export/css.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
ColorProfile,
|
|
2
3
|
Effect,
|
|
3
4
|
Fill,
|
|
4
5
|
GradientStop,
|
|
@@ -7,21 +8,21 @@ import type {
|
|
|
7
8
|
Stroke,
|
|
8
9
|
TextLength
|
|
9
10
|
} from '@pairdraw/scene-graph'
|
|
10
|
-
import {
|
|
11
|
+
import { colorToDocumentCSS } from '@pairdraw/scene-graph/color'
|
|
11
12
|
import { blurSigma } from '@pairdraw/scene-graph/geometry'
|
|
12
13
|
import { gradientPoint } from '@pairdraw/scene-graph/gradient'
|
|
13
14
|
import type { Color, Vector } from '@pairdraw/scene-graph/primitives'
|
|
14
15
|
|
|
15
16
|
import type { DesignStyleDeclaration } from '../types'
|
|
16
17
|
|
|
17
|
-
/**
|
|
18
|
-
export function cssColor(color: Color): string {
|
|
19
|
-
return
|
|
18
|
+
/** A color as CSS in the document's profile, so a Display P3 color keeps its gamut. */
|
|
19
|
+
export function cssColor(color: Color, profile: ColorProfile = 'srgb'): string {
|
|
20
|
+
return colorToDocumentCSS(color, profile)
|
|
20
21
|
}
|
|
21
22
|
|
|
22
|
-
export function fillToCSS(fill: Fill | undefined
|
|
23
|
+
export function fillToCSS(fill: Fill | undefined, profile: ColorProfile = 'srgb') {
|
|
23
24
|
if (fill?.type !== 'SOLID' || !fill.visible) return undefined
|
|
24
|
-
return cssColor({ ...fill.color, a: fill.opacity })
|
|
25
|
+
return cssColor({ ...fill.color, a: fill.opacity }, profile)
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/** A number CSS reads the same back: six decimals, no trailing zeros or negative zero. */
|
|
@@ -30,8 +31,8 @@ function cssNumber(value: number): string {
|
|
|
30
31
|
return String(rounded === 0 ? 0 : rounded)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
function stopToCSS(stop: GradientStop, opacity: number): string {
|
|
34
|
-
const color = cssColor({ ...stop.color, a: stop.color.a * opacity })
|
|
34
|
+
function stopToCSS(stop: GradientStop, opacity: number, profile: ColorProfile): string {
|
|
35
|
+
const color = cssColor({ ...stop.color, a: stop.color.a * opacity }, profile)
|
|
35
36
|
return `${color} ${cssNumber(stop.position * 100)}%`
|
|
36
37
|
}
|
|
37
38
|
|
|
@@ -45,13 +46,18 @@ function positionToCSS(point: Vector): string {
|
|
|
45
46
|
* maps the box's unit square onto gradient space; its inverse gives the line's ends, the
|
|
46
47
|
* ellipse's centre and radii, or the sweep's start ray. Diamond gradients have no CSS spelling.
|
|
47
48
|
*/
|
|
48
|
-
export function gradientFillToCSS(
|
|
49
|
+
export function gradientFillToCSS(
|
|
50
|
+
fill: Fill,
|
|
51
|
+
width: number,
|
|
52
|
+
height: number,
|
|
53
|
+
profile: ColorProfile = 'srgb'
|
|
54
|
+
): string | undefined {
|
|
49
55
|
const transform = fill.gradientTransform
|
|
50
56
|
const stops = fill.gradientStops
|
|
51
57
|
if (!fill.visible || !transform || !stops || stops.length === 0 || width <= 0 || height <= 0) {
|
|
52
58
|
return undefined
|
|
53
59
|
}
|
|
54
|
-
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity)).join(', ')
|
|
60
|
+
const painted = stops.map((stop) => stopToCSS(stop, fill.opacity, profile)).join(', ')
|
|
55
61
|
if (fill.type === 'GRADIENT_LINEAR') {
|
|
56
62
|
const start = gradientPoint(transform, { x: 0, y: 0.5 })
|
|
57
63
|
const end = gradientPoint(transform, { x: 1, y: 0.5 })
|
|
@@ -90,11 +96,12 @@ export function gradientFillToCSS(fill: Fill, width: number, height: number): st
|
|
|
90
96
|
export function gradientFillsToCSS(
|
|
91
97
|
fills: Fill[],
|
|
92
98
|
width: number,
|
|
93
|
-
height: number
|
|
99
|
+
height: number,
|
|
100
|
+
profile: ColorProfile = 'srgb'
|
|
94
101
|
): string | undefined {
|
|
95
102
|
const layers: string[] = []
|
|
96
103
|
for (let index = fills.length - 1; index >= 0; index--) {
|
|
97
|
-
const css = gradientFillToCSS(fills[index], width, height)
|
|
104
|
+
const css = gradientFillToCSS(fills[index], width, height, profile)
|
|
98
105
|
if (css) layers.push(css)
|
|
99
106
|
}
|
|
100
107
|
return layers.length > 0 ? layers.join(', ') : undefined
|
|
@@ -121,9 +128,12 @@ export function borderWidths(node: SceneNode): Record<BorderSide, number> {
|
|
|
121
128
|
}
|
|
122
129
|
}
|
|
123
130
|
|
|
124
|
-
export function strokeColorToCSS(
|
|
131
|
+
export function strokeColorToCSS(
|
|
132
|
+
stroke: Stroke | undefined,
|
|
133
|
+
profile: ColorProfile = 'srgb'
|
|
134
|
+
): string | undefined {
|
|
125
135
|
if (!stroke?.visible) return undefined
|
|
126
|
-
return cssColor({ ...stroke.color, a: stroke.opacity })
|
|
136
|
+
return cssColor({ ...stroke.color, a: stroke.opacity }, profile)
|
|
127
137
|
}
|
|
128
138
|
|
|
129
139
|
function px(value: number): string {
|
|
@@ -134,7 +144,7 @@ function px(value: number): string {
|
|
|
134
144
|
* A shadow as `box-shadow` writes it, or as `text-shadow` does without its spread. CSS blurs a
|
|
135
145
|
* shadow by half its blur length, so the length is twice the sigma the canvas blurs it by.
|
|
136
146
|
*/
|
|
137
|
-
function shadowToCSS(effect: ShadowEffect, spread = true): string {
|
|
147
|
+
function shadowToCSS(effect: ShadowEffect, spread = true, profile: ColorProfile = 'srgb'): string {
|
|
138
148
|
const inset = effect.type === 'INNER_SHADOW' ? 'inset ' : ''
|
|
139
149
|
const parts = [
|
|
140
150
|
`${inset}${px(effect.offset.x)}`,
|
|
@@ -142,7 +152,7 @@ function shadowToCSS(effect: ShadowEffect, spread = true): string {
|
|
|
142
152
|
px(2 * blurSigma(effect.radius))
|
|
143
153
|
]
|
|
144
154
|
if (spread) parts.push(px(effect.spread))
|
|
145
|
-
parts.push(cssColor(effect.color))
|
|
155
|
+
parts.push(cssColor(effect.color, profile))
|
|
146
156
|
return parts.join(' ')
|
|
147
157
|
}
|
|
148
158
|
|
|
@@ -174,12 +184,15 @@ function addBlurs(style: DesignStyleDeclaration, visible: Effect[]): void {
|
|
|
174
184
|
* Every visible shadow, in order, plus layer and background blur, blurred as the canvas draws
|
|
175
185
|
* them: CSS's `blur()` takes the Gaussian sigma itself.
|
|
176
186
|
*/
|
|
177
|
-
export function effectsToCSS(
|
|
187
|
+
export function effectsToCSS(
|
|
188
|
+
effects: Effect[],
|
|
189
|
+
profile: ColorProfile = 'srgb'
|
|
190
|
+
): DesignStyleDeclaration {
|
|
178
191
|
const style: DesignStyleDeclaration = {}
|
|
179
192
|
const visible = effects.filter((effect) => effect.visible)
|
|
180
193
|
const shadows = visible.filter(isShadow)
|
|
181
194
|
if (shadows.length > 0)
|
|
182
|
-
style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect)).join(', ')
|
|
195
|
+
style['box-shadow'] = shadows.map((effect) => shadowToCSS(effect, true, profile)).join(', ')
|
|
183
196
|
addBlurs(style, visible)
|
|
184
197
|
return style
|
|
185
198
|
}
|
|
@@ -188,12 +201,15 @@ export function effectsToCSS(effects: Effect[]): DesignStyleDeclaration {
|
|
|
188
201
|
* Text's effects: each visible drop shadow as `text-shadow`, which has no spread and shadows the
|
|
189
202
|
* glyphs, and its layer and background blurs. An inner shadow has no form CSS gives text.
|
|
190
203
|
*/
|
|
191
|
-
export function textEffectsToCSS(
|
|
204
|
+
export function textEffectsToCSS(
|
|
205
|
+
effects: Effect[],
|
|
206
|
+
profile: ColorProfile = 'srgb'
|
|
207
|
+
): DesignStyleDeclaration {
|
|
192
208
|
const style: DesignStyleDeclaration = {}
|
|
193
209
|
const visible = effects.filter((effect) => effect.visible)
|
|
194
210
|
const shadows = visible.filter(isDropShadow)
|
|
195
211
|
if (shadows.length > 0) {
|
|
196
|
-
style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false)).join(', ')
|
|
212
|
+
style['text-shadow'] = shadows.map((effect) => shadowToCSS(effect, false, profile)).join(', ')
|
|
197
213
|
}
|
|
198
214
|
addBlurs(style, visible)
|
|
199
215
|
return style
|
package/src/export/projection.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isEqual } from 'es-toolkit/predicate'
|
|
2
2
|
import { fromUint8Array } from 'js-base64'
|
|
3
3
|
|
|
4
|
-
import type { SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
4
|
+
import type { ColorProfile, SceneGraph, SceneNode, Stroke } from '@pairdraw/scene-graph'
|
|
5
5
|
import { BLACK } from '@pairdraw/scene-graph/constants'
|
|
6
6
|
import { readIcon } from '@pairdraw/scene-graph/icons'
|
|
7
7
|
import { imageMimeType } from '@pairdraw/scene-graph/images'
|
|
@@ -166,9 +166,14 @@ function addCornerRadii(style: DesignStyleDeclaration, node: SceneNode, css: Fie
|
|
|
166
166
|
if (node.cornerRadius > 0) style['border-radius'] = css('cornerRadius', px(node.cornerRadius))
|
|
167
167
|
}
|
|
168
168
|
|
|
169
|
-
function addStroke(
|
|
169
|
+
function addStroke(
|
|
170
|
+
style: DesignStyleDeclaration,
|
|
171
|
+
node: SceneNode,
|
|
172
|
+
css: FieldCSS,
|
|
173
|
+
profile: ColorProfile
|
|
174
|
+
): void {
|
|
170
175
|
const stroke = node.strokes.find((item) => item.visible)
|
|
171
|
-
const literalColor = strokeColorToCSS(stroke)
|
|
176
|
+
const literalColor = strokeColorToCSS(stroke, profile)
|
|
172
177
|
if (!literalColor || !stroke) return
|
|
173
178
|
const color = css('strokes/0/color', literalColor)
|
|
174
179
|
const borderStyle = node.dashPattern.length > 0 ? 'dashed' : 'solid'
|
|
@@ -193,14 +198,14 @@ function addStroke(style: DesignStyleDeclaration, node: SceneNode, css: FieldCSS
|
|
|
193
198
|
* dash or take per-side weights, and outer and inner rings paint in separate CSS layers, so a
|
|
194
199
|
* stroke order that alternates them is approximated.
|
|
195
200
|
*/
|
|
196
|
-
function extraStrokeShadows(strokes: Stroke[], first: Stroke): string[] {
|
|
201
|
+
function extraStrokeShadows(strokes: Stroke[], first: Stroke, profile: ColorProfile): string[] {
|
|
197
202
|
const outer: string[] = []
|
|
198
203
|
const inner: string[] = []
|
|
199
204
|
let outside = 0
|
|
200
205
|
let inside = first.weight
|
|
201
206
|
for (const stroke of strokes) {
|
|
202
207
|
if (!stroke.visible) continue
|
|
203
|
-
const color = strokeColorToCSS(stroke)
|
|
208
|
+
const color = strokeColorToCSS(stroke, profile)
|
|
204
209
|
if (!color) continue
|
|
205
210
|
if (stroke.align !== 'OUTSIDE') {
|
|
206
211
|
inside += stroke.align === 'CENTER' ? stroke.weight / 2 : stroke.weight
|
|
@@ -352,22 +357,23 @@ const CSS_BLEND_MODES: Partial<Record<SceneNode['blendMode'], string>> = {
|
|
|
352
357
|
function styleFromSceneNode(
|
|
353
358
|
node: SceneNode,
|
|
354
359
|
parent: SceneNode | undefined,
|
|
355
|
-
css: FieldCSS
|
|
360
|
+
css: FieldCSS,
|
|
361
|
+
profile: ColorProfile
|
|
356
362
|
): DesignStyleDeclaration {
|
|
357
363
|
const style: DesignStyleDeclaration = {}
|
|
358
364
|
addSize(style, node, parent, css)
|
|
359
365
|
addPositioning(style, node, parent)
|
|
360
366
|
addSizeConstraints(style, node, css)
|
|
361
|
-
const fill = fillToCSS(node.fills.at(0))
|
|
367
|
+
const fill = fillToCSS(node.fills.at(0), profile)
|
|
362
368
|
if (fill) style['background-color'] = css('fills/0/color', fill)
|
|
363
|
-
const gradients = gradientFillsToCSS(node.fills, node.width, node.height)
|
|
369
|
+
const gradients = gradientFillsToCSS(node.fills, node.width, node.height, profile)
|
|
364
370
|
if (gradients) style['background-image'] = gradients
|
|
365
371
|
addImageStyle(style, node)
|
|
366
|
-
addStroke(style, node, css)
|
|
367
|
-
Object.assign(style, effectsToCSS(node.effects))
|
|
372
|
+
addStroke(style, node, css, profile)
|
|
373
|
+
Object.assign(style, effectsToCSS(node.effects, profile))
|
|
368
374
|
const visibleStrokes = node.strokes.filter((stroke) => stroke.visible)
|
|
369
375
|
if (visibleStrokes.length > 1) {
|
|
370
|
-
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0])
|
|
376
|
+
const rings = extraStrokeShadows(visibleStrokes.slice(1), visibleStrokes[0], profile)
|
|
371
377
|
if (rings.length > 0) {
|
|
372
378
|
style['box-shadow'] = [rings.join(', '), style['box-shadow']]
|
|
373
379
|
.filter((shadow): shadow is string => Boolean(shadow))
|
|
@@ -408,15 +414,16 @@ function styleFromSceneNode(
|
|
|
408
414
|
function styleFromTextNode(
|
|
409
415
|
node: SceneNode,
|
|
410
416
|
parent: SceneNode | undefined,
|
|
411
|
-
css: FieldCSS
|
|
417
|
+
css: FieldCSS,
|
|
418
|
+
profile: ColorProfile
|
|
412
419
|
): DesignStyleDeclaration {
|
|
413
420
|
const style: DesignStyleDeclaration = {}
|
|
414
421
|
addSize(style, node, parent, css)
|
|
415
422
|
addPositioning(style, node, parent)
|
|
416
423
|
addLayoutChild(style, node, parent)
|
|
417
424
|
if (resolveNodeTextDirection(node) === 'RTL') style.direction = 'rtl'
|
|
418
|
-
const color = fillToCSS(node.fills.at(0))
|
|
419
|
-
style.color = color ? css('fills/0/color', color) : cssColor(BLACK)
|
|
425
|
+
const color = fillToCSS(node.fills.at(0), profile)
|
|
426
|
+
style.color = color ? css('fills/0/color', color) : cssColor(BLACK, profile)
|
|
420
427
|
style['font-family'] = node.fontFamily
|
|
421
428
|
style['font-size'] = css('fontSize', px(node.fontSize))
|
|
422
429
|
style['font-weight'] = String(node.fontWeight)
|
|
@@ -435,7 +442,7 @@ function styleFromTextNode(
|
|
|
435
442
|
if (node.textAlignHorizontal !== 'LEFT')
|
|
436
443
|
style['text-align'] = node.textAlignHorizontal.toLowerCase()
|
|
437
444
|
if (node.opacity < 1) style.opacity = css('opacity', String(node.opacity))
|
|
438
|
-
Object.assign(style, textEffectsToCSS(node.effects))
|
|
445
|
+
Object.assign(style, textEffectsToCSS(node.effects, profile))
|
|
439
446
|
if (node.textDecoration !== 'NONE') {
|
|
440
447
|
style['text-decoration-line'] =
|
|
441
448
|
node.textDecoration === 'UNDERLINE' ? 'underline' : 'line-through'
|
|
@@ -587,7 +594,11 @@ function sceneNodeToDesignNode(
|
|
|
587
594
|
const css: FieldCSS = (field, literal) => tokens?.reference(node, field, modes) ?? literal
|
|
588
595
|
|
|
589
596
|
if (node.type === 'TEXT') {
|
|
590
|
-
const content = textElementContent(
|
|
597
|
+
const content = textElementContent(
|
|
598
|
+
node,
|
|
599
|
+
styleFromTextNode(node, parent, css, graph.documentColorSpace),
|
|
600
|
+
graph.documentColorSpace
|
|
601
|
+
)
|
|
591
602
|
return {
|
|
592
603
|
type: 'element',
|
|
593
604
|
tagName: content.tagName,
|
|
@@ -601,7 +612,12 @@ function sceneNodeToDesignNode(
|
|
|
601
612
|
|
|
602
613
|
// An icon is one drawing, however many paths it has.
|
|
603
614
|
if (VECTOR_SHAPES.has(node.type) || readIcon(node)) {
|
|
604
|
-
const vector = vectorDesignNode(
|
|
615
|
+
const vector = vectorDesignNode(
|
|
616
|
+
context,
|
|
617
|
+
node,
|
|
618
|
+
attrs,
|
|
619
|
+
styleFromSceneNode(node, parent, css, graph.documentColorSpace)
|
|
620
|
+
)
|
|
605
621
|
if (vector) return vector
|
|
606
622
|
}
|
|
607
623
|
|
|
@@ -618,7 +634,7 @@ function sceneNodeToDesignNode(
|
|
|
618
634
|
}
|
|
619
635
|
}
|
|
620
636
|
|
|
621
|
-
const style = styleFromSceneNode(node, parent, css)
|
|
637
|
+
const style = styleFromSceneNode(node, parent, css, graph.documentColorSpace)
|
|
622
638
|
const shader = layerShader(graph, node)
|
|
623
639
|
if (shader) addShaderLayerStyle(style)
|
|
624
640
|
return {
|
package/src/export/text.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { isEqual } from 'es-toolkit/predicate'
|
|
|
3
3
|
|
|
4
4
|
import type {
|
|
5
5
|
CharacterStyleOverride,
|
|
6
|
+
ColorProfile,
|
|
6
7
|
HyperlinkTarget,
|
|
7
8
|
SceneNode,
|
|
8
9
|
TextLength
|
|
@@ -157,7 +158,7 @@ function styledRanges(node: SceneNode): StyledRange[] {
|
|
|
157
158
|
}
|
|
158
159
|
|
|
159
160
|
/** Underline or strikethrough as CSS writes it: Figma's offset is CSS's, from the baseline. */
|
|
160
|
-
function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
161
|
+
function decorationCSS(style: CharacterStyle, profile: ColorProfile): DesignStyleDeclaration {
|
|
161
162
|
if (style.textDecoration === 'NONE') return {}
|
|
162
163
|
const underline = style.textDecoration === 'UNDERLINE'
|
|
163
164
|
const css: DesignStyleDeclaration = {
|
|
@@ -166,7 +167,10 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
|
166
167
|
if (style.textDecorationStyle !== 'SOLID') {
|
|
167
168
|
css['text-decoration-style'] = style.textDecorationStyle.toLowerCase()
|
|
168
169
|
}
|
|
169
|
-
const color = fillToCSS(
|
|
170
|
+
const color = fillToCSS(
|
|
171
|
+
style.textDecorationFills.find((fill) => fill.visible),
|
|
172
|
+
profile
|
|
173
|
+
)
|
|
170
174
|
if (color) css['text-decoration-color'] = color
|
|
171
175
|
if (style.textDecorationThickness !== null) {
|
|
172
176
|
css['text-decoration-thickness'] = px(style.textDecorationThickness)
|
|
@@ -178,9 +182,13 @@ function decorationCSS(style: CharacterStyle): DesignStyleDeclaration {
|
|
|
178
182
|
return css
|
|
179
183
|
}
|
|
180
184
|
|
|
181
|
-
function characterCSS(
|
|
185
|
+
function characterCSS(
|
|
186
|
+
style: CharacterStyle,
|
|
187
|
+
decorated: boolean,
|
|
188
|
+
profile: ColorProfile
|
|
189
|
+
): DesignStyleDeclaration {
|
|
182
190
|
return {
|
|
183
|
-
color: fillToCSS(style.fills.at(0)) ?? '',
|
|
191
|
+
color: fillToCSS(style.fills.at(0), profile) ?? '',
|
|
184
192
|
'font-family': style.fontFamily,
|
|
185
193
|
'font-size': px(style.fontSize),
|
|
186
194
|
'font-weight': String(style.fontWeight),
|
|
@@ -188,7 +196,7 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
|
|
|
188
196
|
'letter-spacing': letterSpacingToCSS(style.letterSpacing),
|
|
189
197
|
'line-height': style.lineHeight === null ? 'normal' : lineHeightToCSS(style.lineHeight),
|
|
190
198
|
'text-transform': textCaseToCSS(style.textCase) ?? 'none',
|
|
191
|
-
...(decorated ? decorationCSS(style) : {})
|
|
199
|
+
...(decorated ? decorationCSS(style, profile) : {})
|
|
192
200
|
}
|
|
193
201
|
}
|
|
194
202
|
|
|
@@ -200,9 +208,10 @@ function characterCSS(style: CharacterStyle, decorated: boolean): DesignStyleDec
|
|
|
200
208
|
function spanCSS(
|
|
201
209
|
style: CharacterStyle,
|
|
202
210
|
decorated: boolean,
|
|
203
|
-
base: DesignStyleDeclaration
|
|
211
|
+
base: DesignStyleDeclaration,
|
|
212
|
+
profile: ColorProfile
|
|
204
213
|
): DesignStyleDeclaration {
|
|
205
|
-
const own = characterCSS(style, decorated)
|
|
214
|
+
const own = characterCSS(style, decorated, profile)
|
|
206
215
|
const css = differing(own, base)
|
|
207
216
|
const { unit, value } = style.letterSpacing
|
|
208
217
|
if (unit === 'PERCENT' && value !== 0 && 'font-size' in css) {
|
|
@@ -225,10 +234,14 @@ function differing(
|
|
|
225
234
|
* Whether the text decorates every character alike. CSS draws an element's decoration through
|
|
226
235
|
* its descendants, which cannot take it off, so mixed decorations go on the spans instead.
|
|
227
236
|
*/
|
|
228
|
-
function uniformDecoration(
|
|
237
|
+
function uniformDecoration(
|
|
238
|
+
node: SceneNode,
|
|
239
|
+
ranges: readonly StyledRange[],
|
|
240
|
+
profile: ColorProfile
|
|
241
|
+
): boolean {
|
|
229
242
|
const decorations = ranges
|
|
230
243
|
.filter((range) => node.text.slice(range.start, range.end).replaceAll('\n', '') !== '')
|
|
231
|
-
.map((range) => decorationCSS(range.style))
|
|
244
|
+
.map((range) => decorationCSS(range.style, profile))
|
|
232
245
|
return decorations.every((decoration) => isEqual(decoration, decorations[0]))
|
|
233
246
|
}
|
|
234
247
|
|
|
@@ -237,6 +250,7 @@ interface TextProjection {
|
|
|
237
250
|
ranges: StyledRange[]
|
|
238
251
|
base: DesignStyleDeclaration
|
|
239
252
|
decorated: boolean
|
|
253
|
+
profile: ColorProfile
|
|
240
254
|
}
|
|
241
255
|
|
|
242
256
|
function textNode(text: string): DesignNode {
|
|
@@ -265,13 +279,13 @@ function linkElement(href: string, css: DesignStyleDeclaration, text: string): D
|
|
|
265
279
|
* linked characters are a link instead.
|
|
266
280
|
*/
|
|
267
281
|
function inlineContent(projection: TextProjection, start: number, end: number): DesignNode[] {
|
|
268
|
-
const { node, ranges, base, decorated } = projection
|
|
282
|
+
const { node, ranges, base, decorated, profile } = projection
|
|
269
283
|
const pieces: { css: DesignStyleDeclaration; text: string; href: string | null }[] = []
|
|
270
284
|
for (const range of ranges) {
|
|
271
285
|
const from = Math.max(start, range.start)
|
|
272
286
|
const to = Math.min(end, range.end)
|
|
273
287
|
if (to <= from) continue
|
|
274
|
-
const css = spanCSS(range.style, !decorated, base)
|
|
288
|
+
const css = spanCSS(range.style, !decorated, base, profile)
|
|
275
289
|
const text = node.text.slice(from, to)
|
|
276
290
|
const previous = pieces.at(-1)
|
|
277
291
|
if (previous && previous.href === range.href && isEqual(previous.css, css)) {
|
|
@@ -443,19 +457,21 @@ function groupEm(
|
|
|
443
457
|
*/
|
|
444
458
|
export function textElementContent(
|
|
445
459
|
node: SceneNode,
|
|
446
|
-
layerStyle: DesignStyleDeclaration
|
|
460
|
+
layerStyle: DesignStyleDeclaration,
|
|
461
|
+
profile: ColorProfile = 'srgb'
|
|
447
462
|
): { tagName: string; style: DesignStyleDeclaration; children: DesignNode[] } {
|
|
448
463
|
const ranges = styledRanges(node)
|
|
449
|
-
const decorated = uniformDecoration(node, ranges)
|
|
464
|
+
const decorated = uniformDecoration(node, ranges, profile)
|
|
450
465
|
const style = Object.fromEntries(
|
|
451
466
|
Object.entries(layerStyle).filter(([property]) => !DECORATION_PROPERTIES.has(property))
|
|
452
467
|
)
|
|
453
|
-
if (decorated) Object.assign(style, decorationCSS(characterStyle(node)))
|
|
468
|
+
if (decorated) Object.assign(style, decorationCSS(characterStyle(node), profile))
|
|
454
469
|
const projection: TextProjection = {
|
|
455
470
|
node,
|
|
456
471
|
ranges,
|
|
457
|
-
base: characterCSS(characterStyle(node), decorated),
|
|
458
|
-
decorated
|
|
472
|
+
base: characterCSS(characterStyle(node), decorated, profile),
|
|
473
|
+
decorated,
|
|
474
|
+
profile
|
|
459
475
|
}
|
|
460
476
|
if (!needsBlocks(node)) {
|
|
461
477
|
return { tagName: 'span', style, children: inlineContent(projection, 0, node.text.length) }
|