@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
|
@@ -0,0 +1,430 @@
|
|
|
1
|
+
import { toUint8Array } from 'js-base64'
|
|
2
|
+
|
|
3
|
+
import type { Fill, SceneGraph } from '@pairdraw/scene-graph'
|
|
4
|
+
import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
|
|
5
|
+
import { parseCSSBackgroundImages, splitCSSValue } from '@pairdraw/scene-graph/css'
|
|
6
|
+
import { computeImageHash, imageSize } from '@pairdraw/scene-graph/images'
|
|
7
|
+
import type { Matrix, Rect, Size, Vector } from '@pairdraw/scene-graph/primitives'
|
|
8
|
+
|
|
9
|
+
import type { DesignStyleDeclaration } from '../types'
|
|
10
|
+
import { colorToFillFromCSS, pickStyle } from './css-values'
|
|
11
|
+
|
|
12
|
+
const REPEAT_KEYWORDS = new Set(['repeat', 'no-repeat', 'repeat-x', 'repeat-y', 'space', 'round'])
|
|
13
|
+
const BOX_KEYWORDS = new Set(['border-box', 'padding-box', 'content-box'])
|
|
14
|
+
const ATTACHMENT_KEYWORDS = new Set(['scroll', 'fixed', 'local'])
|
|
15
|
+
const GRADIENT_PREFIXES = [
|
|
16
|
+
'linear-gradient(',
|
|
17
|
+
'radial-gradient(',
|
|
18
|
+
'conic-gradient(',
|
|
19
|
+
'repeating-linear-gradient(',
|
|
20
|
+
'repeating-radial-gradient(',
|
|
21
|
+
'repeating-conic-gradient('
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
/** Splits a CSS value list at its top-level commas, keeping functions whole. */
|
|
25
|
+
function splitLayers(value: string): string[] {
|
|
26
|
+
const layers: string[] = []
|
|
27
|
+
let depth = 0
|
|
28
|
+
let start = 0
|
|
29
|
+
for (let index = 0; index < value.length; index++) {
|
|
30
|
+
const character = value[index]
|
|
31
|
+
if (character === '(') depth++
|
|
32
|
+
else if (character === ')') depth = Math.max(0, depth - 1)
|
|
33
|
+
else if (character === ',' && depth === 0) {
|
|
34
|
+
layers.push(value.slice(start, index).trim())
|
|
35
|
+
start = index + 1
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const last = value.slice(start).trim()
|
|
39
|
+
if (last) layers.push(last)
|
|
40
|
+
return layers
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The value a property list gives layer `index`, repeating its last entry as CSS does. */
|
|
44
|
+
function at(list: readonly string[], index: number): string | undefined {
|
|
45
|
+
return list[Math.min(index, list.length - 1)]
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** One CSS background layer as the properties that place its picture. */
|
|
49
|
+
export interface BackgroundLayer {
|
|
50
|
+
image: string
|
|
51
|
+
size: string
|
|
52
|
+
position: string
|
|
53
|
+
repeat: string
|
|
54
|
+
origin: string
|
|
55
|
+
clip: string
|
|
56
|
+
/** A gradient parsed from the longhand, which has no image URL. */
|
|
57
|
+
gradient?: Fill
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Splits a value into its `url(...)` and gradient functions and the words around them. */
|
|
61
|
+
function splitBackgroundFunctions(value: string): { image: string; rest: string } {
|
|
62
|
+
let image = ''
|
|
63
|
+
let depth = 0
|
|
64
|
+
let start = -1
|
|
65
|
+
let end = -1
|
|
66
|
+
for (let index = 0; index < value.length; index++) {
|
|
67
|
+
const character = value[index]
|
|
68
|
+
if (character === '(') {
|
|
69
|
+
if (depth === 0) start = index
|
|
70
|
+
depth++
|
|
71
|
+
} else if (character === ')') {
|
|
72
|
+
depth--
|
|
73
|
+
if (depth === 0) end = index
|
|
74
|
+
} else if (depth === 0 && character === ' ') {
|
|
75
|
+
continue
|
|
76
|
+
}
|
|
77
|
+
if (depth === 0 && end !== -1) {
|
|
78
|
+
const name = value.slice(0, start).trim().split(/[\s,]/).pop() ?? ''
|
|
79
|
+
const candidate = `${name}${value.slice(start, end + 1)}`
|
|
80
|
+
if (name === 'url' || GRADIENT_PREFIXES.some((prefix) => candidate.startsWith(prefix))) {
|
|
81
|
+
image = candidate
|
|
82
|
+
const before = value.slice(0, start).trim().split(/\s+/)
|
|
83
|
+
const after = value.slice(end + 1).trim()
|
|
84
|
+
return { image, rest: [...before.slice(0, -1), after].join(' ').trim() }
|
|
85
|
+
}
|
|
86
|
+
start = -1
|
|
87
|
+
end = -1
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return { image, rest: value }
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function parseShorthand(layer: string): BackgroundLayer {
|
|
94
|
+
const { image, rest } = splitBackgroundFunctions(layer)
|
|
95
|
+
const [positionPart = '', sizePart = ''] = rest.split('/')
|
|
96
|
+
const words = splitCSSValue(positionPart.toLowerCase())
|
|
97
|
+
const sizes = splitCSSValue(sizePart.toLowerCase())
|
|
98
|
+
const result: BackgroundLayer = {
|
|
99
|
+
image,
|
|
100
|
+
size: 'auto',
|
|
101
|
+
position: '0% 0%',
|
|
102
|
+
repeat: 'repeat',
|
|
103
|
+
origin: 'padding-box',
|
|
104
|
+
clip: 'border-box'
|
|
105
|
+
}
|
|
106
|
+
const places: string[] = []
|
|
107
|
+
let sawOrigin = false
|
|
108
|
+
let sawClip = false
|
|
109
|
+
for (const word of words) {
|
|
110
|
+
if (REPEAT_KEYWORDS.has(word)) result.repeat = word
|
|
111
|
+
else if (BOX_KEYWORDS.has(word)) {
|
|
112
|
+
// The first of the two box words is the origin, the second the clip.
|
|
113
|
+
if (!sawOrigin) {
|
|
114
|
+
result.origin = word
|
|
115
|
+
sawOrigin = true
|
|
116
|
+
} else {
|
|
117
|
+
result.clip = word
|
|
118
|
+
sawClip = true
|
|
119
|
+
}
|
|
120
|
+
} else if (ATTACHMENT_KEYWORDS.has(word)) continue
|
|
121
|
+
else if (/^[\d.+-]/.test(word) || ['left', 'right', 'center', 'top', 'bottom'].includes(word)) {
|
|
122
|
+
places.push(word)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
const sizing = sizes.filter((word) => !REPEAT_KEYWORDS.has(word) && !BOX_KEYWORDS.has(word))
|
|
126
|
+
if (sizing.length > 0) result.size = sizing.slice(0, 2).join(' ')
|
|
127
|
+
for (const word of sizes) {
|
|
128
|
+
if (REPEAT_KEYWORDS.has(word)) {
|
|
129
|
+
result.repeat = word
|
|
130
|
+
} else if (BOX_KEYWORDS.has(word)) {
|
|
131
|
+
if (!sawOrigin) {
|
|
132
|
+
result.origin = word
|
|
133
|
+
sawOrigin = true
|
|
134
|
+
} else {
|
|
135
|
+
result.clip = word
|
|
136
|
+
sawClip = true
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (!sawClip && sawOrigin) result.clip = result.origin
|
|
141
|
+
if (places.length === 1) result.position = places[0]
|
|
142
|
+
else if (places.length >= 2) result.position = places.slice(-2).join(' ')
|
|
143
|
+
return result
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The background layers of an element, top layer first, from the shorthand or the longhands. */
|
|
147
|
+
export function backgroundLayers(
|
|
148
|
+
style: DesignStyleDeclaration,
|
|
149
|
+
width = 1,
|
|
150
|
+
height = 1
|
|
151
|
+
): BackgroundLayer[] {
|
|
152
|
+
const shorthand = splitLayers(pickStyle(style, 'background') ?? '')
|
|
153
|
+
if (shorthand.length > 0) {
|
|
154
|
+
return shorthand.map((layer) => parseShorthand(layer))
|
|
155
|
+
}
|
|
156
|
+
const parsed = parseCSSBackgroundImages(pickStyle(style, 'background-image'), width, height)
|
|
157
|
+
const sizes = splitLayers(pickStyle(style, 'background-size') ?? '')
|
|
158
|
+
const positions = splitLayers(pickStyle(style, 'background-position') ?? '')
|
|
159
|
+
const repeats = splitLayers(pickStyle(style, 'background-repeat') ?? '')
|
|
160
|
+
const origins = splitLayers(pickStyle(style, 'background-origin') ?? '')
|
|
161
|
+
const clips = splitLayers(pickStyle(style, 'background-clip') ?? '')
|
|
162
|
+
return parsed.map((layer, index) => ({
|
|
163
|
+
image: layer.kind === 'image' ? `url(${layer.url})` : '',
|
|
164
|
+
gradient: layer.kind === 'gradient' ? layer.fill : undefined,
|
|
165
|
+
size: at(sizes, index) ?? 'auto',
|
|
166
|
+
position: at(positions, index) ?? '0% 0%',
|
|
167
|
+
repeat: at(repeats, index) ?? 'repeat',
|
|
168
|
+
origin: at(origins, index) ?? 'padding-box',
|
|
169
|
+
clip: at(clips, index) ?? 'border-box'
|
|
170
|
+
}))
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** A length or percentage of `total`, as one value of `background-size` or `-position` gives. */
|
|
174
|
+
function lengthOf(value: string | undefined, total: number): number | null {
|
|
175
|
+
if (!value) return null
|
|
176
|
+
const trimmed = value.trim()
|
|
177
|
+
const percent = /^(-?[\d.]+)%$/.exec(trimmed)
|
|
178
|
+
if (percent) {
|
|
179
|
+
const share = Number.parseFloat(percent[1])
|
|
180
|
+
return Number.isFinite(share) ? (total * share) / 100 : null
|
|
181
|
+
}
|
|
182
|
+
const pixels = /^(-?[\d.]+)(?:px)?$/.exec(trimmed)
|
|
183
|
+
if (!pixels) return null
|
|
184
|
+
const number = Number.parseFloat(pixels[1])
|
|
185
|
+
return Number.isFinite(number) ? number : null
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** The size `background-size` draws a picture of `natural` size at in `box`. */
|
|
189
|
+
function pictureSize(size: string, box: Size, natural: Size): Size {
|
|
190
|
+
const sizing = size.trim().toLowerCase()
|
|
191
|
+
if (sizing === 'cover' || sizing === 'contain') {
|
|
192
|
+
const scale = (sizing === 'cover' ? Math.max : Math.min)(
|
|
193
|
+
box.width / natural.width,
|
|
194
|
+
box.height / natural.height
|
|
195
|
+
)
|
|
196
|
+
return { width: natural.width * scale, height: natural.height * scale }
|
|
197
|
+
}
|
|
198
|
+
const [across, down = 'auto'] = splitCSSValue(sizing)
|
|
199
|
+
const width = across === 'auto' ? null : lengthOf(across, box.width)
|
|
200
|
+
const height = down === 'auto' ? null : lengthOf(down, box.height)
|
|
201
|
+
if (width === null && height === null) return natural
|
|
202
|
+
if (width === null && height !== null) {
|
|
203
|
+
return { width: (height * natural.width) / natural.height, height }
|
|
204
|
+
}
|
|
205
|
+
if (width !== null && height === null) {
|
|
206
|
+
return { width, height: (width * natural.height) / natural.width }
|
|
207
|
+
}
|
|
208
|
+
return { width: width ?? natural.width, height: height ?? natural.height }
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
const POSITION_KEYWORDS: Partial<Record<string, string>> = {
|
|
212
|
+
left: '0%',
|
|
213
|
+
top: '0%',
|
|
214
|
+
center: '50%',
|
|
215
|
+
right: '100%',
|
|
216
|
+
bottom: '100%'
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** Where `background-position` puts a picture of `size` in `box`: a percentage of the room left. */
|
|
220
|
+
function pictureOffset(position: string, box: Size, size: Size): Vector {
|
|
221
|
+
const values = splitCSSValue(position.trim().toLowerCase()).map(
|
|
222
|
+
(value) => POSITION_KEYWORDS[value] ?? value
|
|
223
|
+
)
|
|
224
|
+
const [across = '50%', down = '50%'] = values
|
|
225
|
+
const along = (value: string, room: number, total: number) => {
|
|
226
|
+
if (value.endsWith('%')) {
|
|
227
|
+
const number = Number.parseFloat(value)
|
|
228
|
+
return Number.isFinite(number) ? (room * number) / 100 : room / 2
|
|
229
|
+
}
|
|
230
|
+
return lengthOf(value, total) ?? room / 2
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
x: along(across, box.width - size.width, box.width),
|
|
234
|
+
y: along(down, box.height - size.height, box.height)
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function cropTransform(area: Rect, picture: Rect): Matrix {
|
|
239
|
+
return {
|
|
240
|
+
m00: area.width / picture.width,
|
|
241
|
+
m01: 0,
|
|
242
|
+
m02: (area.x - picture.x) / picture.width,
|
|
243
|
+
m10: 0,
|
|
244
|
+
m11: area.height / picture.height,
|
|
245
|
+
m12: (area.y - picture.y) / picture.height
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function cropFill(imageHash: string, imageTransform: Matrix): Fill {
|
|
250
|
+
return {
|
|
251
|
+
type: 'IMAGE',
|
|
252
|
+
imageHash,
|
|
253
|
+
imageScaleMode: 'CROP',
|
|
254
|
+
imageTransform,
|
|
255
|
+
color: TRANSPARENT,
|
|
256
|
+
opacity: 1,
|
|
257
|
+
visible: true
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function borderWidth(style: DesignStyleDeclaration, side: string): number {
|
|
262
|
+
const raw = pickStyle(style, `border-${side}-width`) ?? pickStyle(style, 'border-width')
|
|
263
|
+
const parsed = lengthOf(raw, 0)
|
|
264
|
+
return parsed === null || parsed < 0 ? 0 : parsed
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function paddingWidth(style: DesignStyleDeclaration, side: string): number {
|
|
268
|
+
const parsed = lengthOf(pickStyle(style, `padding-${side}`), 0)
|
|
269
|
+
return parsed === null || parsed < 0 ? 0 : parsed
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
interface Insets {
|
|
273
|
+
top: number
|
|
274
|
+
right: number
|
|
275
|
+
bottom: number
|
|
276
|
+
left: number
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function boxInsets(box: string, style: DesignStyleDeclaration): Insets {
|
|
280
|
+
const border = {
|
|
281
|
+
top: borderWidth(style, 'top'),
|
|
282
|
+
right: borderWidth(style, 'right'),
|
|
283
|
+
bottom: borderWidth(style, 'bottom'),
|
|
284
|
+
left: borderWidth(style, 'left')
|
|
285
|
+
}
|
|
286
|
+
if (box === 'padding-box') return border
|
|
287
|
+
if (box !== 'content-box') return { top: 0, right: 0, bottom: 0, left: 0 }
|
|
288
|
+
return {
|
|
289
|
+
top: border.top + paddingWidth(style, 'top'),
|
|
290
|
+
right: border.right + paddingWidth(style, 'right'),
|
|
291
|
+
bottom: border.bottom + paddingWidth(style, 'bottom'),
|
|
292
|
+
left: border.left + paddingWidth(style, 'left')
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function insetArea(node: Size, insets: Insets): Rect {
|
|
297
|
+
return {
|
|
298
|
+
x: insets.left,
|
|
299
|
+
y: insets.top,
|
|
300
|
+
width: Math.max(0, node.width - insets.left - insets.right),
|
|
301
|
+
height: Math.max(0, node.height - insets.top - insets.bottom)
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/** One background layer, in the order CSS paints it: the bottom layer first. */
|
|
306
|
+
export interface BackgroundLayerPaint {
|
|
307
|
+
fill: Fill
|
|
308
|
+
/** The area a picture covers, when it covers only part of its layer. */
|
|
309
|
+
area?: Rect
|
|
310
|
+
/** Whether the layer is a picture, which a layer of its own behind the children draws. */
|
|
311
|
+
picture: boolean
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export interface BackgroundPaint {
|
|
315
|
+
/** Fills in CSS paint order bottom-first, the color first, when every layer covers its box. */
|
|
316
|
+
fills: Fill[]
|
|
317
|
+
/**
|
|
318
|
+
* Every background layer in CSS paint order when one picture covers part of its box, so layers
|
|
319
|
+
* that stay fills and layers that need their own still paint in the order CSS gives them.
|
|
320
|
+
*/
|
|
321
|
+
layers: BackgroundLayerPaint[]
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/** Adds a layer's picture to the stack, at the area it shows when it covers part of its box. */
|
|
325
|
+
function placePicture(
|
|
326
|
+
graph: SceneGraph,
|
|
327
|
+
node: Size,
|
|
328
|
+
style: DesignStyleDeclaration,
|
|
329
|
+
layer: BackgroundLayer,
|
|
330
|
+
stack: BackgroundLayerPaint[]
|
|
331
|
+
): void {
|
|
332
|
+
const images = parseCSSBackgroundImages(layer.image, node.width, node.height)
|
|
333
|
+
for (const image of images) {
|
|
334
|
+
if (image.kind === 'gradient') {
|
|
335
|
+
stack.push({ fill: image.fill, picture: false })
|
|
336
|
+
continue
|
|
337
|
+
}
|
|
338
|
+
const bytes = bytesFromDataURL(image.url)
|
|
339
|
+
if (!bytes) continue
|
|
340
|
+
const natural = imageSize(bytes)
|
|
341
|
+
if (!natural || natural.width <= 0 || natural.height <= 0) continue
|
|
342
|
+
const imageHash = computeImageHash(bytes)
|
|
343
|
+
graph.images.set(imageHash, bytes)
|
|
344
|
+
const origin = insetArea(node, boxInsets(layer.origin, style))
|
|
345
|
+
const size = pictureSize(layer.size, origin, natural)
|
|
346
|
+
if (size.width <= 0 || size.height <= 0) continue
|
|
347
|
+
const clip = insetArea(node, boxInsets(layer.clip, style))
|
|
348
|
+
const tiles =
|
|
349
|
+
layer.repeat !== 'no-repeat' &&
|
|
350
|
+
layer.repeat !== 'space' &&
|
|
351
|
+
layer.repeat !== 'round' &&
|
|
352
|
+
(layer.size.trim() === 'auto' || layer.size.trim() === 'auto auto')
|
|
353
|
+
if (tiles) {
|
|
354
|
+
stack.push({
|
|
355
|
+
fill: {
|
|
356
|
+
type: 'IMAGE',
|
|
357
|
+
imageHash,
|
|
358
|
+
imageScaleMode: 'TILE',
|
|
359
|
+
color: TRANSPARENT,
|
|
360
|
+
opacity: 1,
|
|
361
|
+
visible: true
|
|
362
|
+
},
|
|
363
|
+
picture: true
|
|
364
|
+
})
|
|
365
|
+
continue
|
|
366
|
+
}
|
|
367
|
+
const offset = pictureOffset(layer.position, origin, size)
|
|
368
|
+
const picture = { x: origin.x + offset.x, y: origin.y + offset.y, ...size }
|
|
369
|
+
const left = Math.max(clip.x, picture.x)
|
|
370
|
+
const top = Math.max(clip.y, picture.y)
|
|
371
|
+
const right = Math.min(clip.x + clip.width, picture.x + picture.width)
|
|
372
|
+
const bottom = Math.min(clip.y + clip.height, picture.y + picture.height)
|
|
373
|
+
if (right <= left || bottom <= top) continue
|
|
374
|
+
const area = { x: left, y: top, width: right - left, height: bottom - top }
|
|
375
|
+
const fill = cropFill(imageHash, cropTransform(area, picture))
|
|
376
|
+
if (
|
|
377
|
+
area.x <= 0.5 &&
|
|
378
|
+
area.y <= 0.5 &&
|
|
379
|
+
area.width >= node.width - 1 &&
|
|
380
|
+
area.height >= node.height - 1
|
|
381
|
+
) {
|
|
382
|
+
stack.push({ fill, picture: true })
|
|
383
|
+
} else {
|
|
384
|
+
stack.push({ area, fill, picture: true })
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Reads an element's background layers: the color, gradients, tiles and pictures placed where
|
|
391
|
+
* `background-size`, `-position`, `-repeat`, `-origin`, and `-clip` put them, bottom layer first.
|
|
392
|
+
* Every layer stays a fill while all of them cover the box; as soon as one covers part of it, the
|
|
393
|
+
* whole stack becomes layers of their own, so CSS paint order survives across the two kinds.
|
|
394
|
+
*/
|
|
395
|
+
export function backgroundPaint(
|
|
396
|
+
graph: SceneGraph,
|
|
397
|
+
style: DesignStyleDeclaration,
|
|
398
|
+
node: Size
|
|
399
|
+
): BackgroundPaint {
|
|
400
|
+
const color = fillFromColor(style)
|
|
401
|
+
const layers = backgroundLayers(style, node.width, node.height).reverse()
|
|
402
|
+
const stack: BackgroundLayerPaint[] = []
|
|
403
|
+
for (const layer of layers) {
|
|
404
|
+
if (layer.gradient) {
|
|
405
|
+
stack.push({ fill: layer.gradient, picture: false })
|
|
406
|
+
continue
|
|
407
|
+
}
|
|
408
|
+
placePicture(graph, node, style, layer, stack)
|
|
409
|
+
}
|
|
410
|
+
if (stack.every((layer) => layer.area === undefined)) {
|
|
411
|
+
const fills = stack.map((layer) => layer.fill)
|
|
412
|
+
return { fills: color ? [color, ...fills] : fills, layers: [] }
|
|
413
|
+
}
|
|
414
|
+
return { fills: color ? [color] : [], layers: stack }
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function fillFromColor(style: DesignStyleDeclaration): Fill | null {
|
|
418
|
+
const fills = colorToFillFromCSS(pickStyle(style, 'background-color'))
|
|
419
|
+
return fills[0] ?? null
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function bytesFromDataURL(value: string | undefined): Uint8Array | null {
|
|
423
|
+
const match = value ? /^data:[^,]*;base64,([\s\S]*)$/.exec(value.trim()) : null
|
|
424
|
+
if (!match) return null
|
|
425
|
+
try {
|
|
426
|
+
return toUint8Array(match[1])
|
|
427
|
+
} catch {
|
|
428
|
+
return null
|
|
429
|
+
}
|
|
430
|
+
}
|
|
@@ -4,25 +4,21 @@ import { isValid, toUint8Array } from 'js-base64'
|
|
|
4
4
|
import {
|
|
5
5
|
SceneGraph,
|
|
6
6
|
type Effect,
|
|
7
|
-
type Fill,
|
|
8
7
|
type ImageScaleMode,
|
|
9
8
|
type SceneNode,
|
|
10
9
|
type Stroke
|
|
11
10
|
} from '@pairdraw/scene-graph'
|
|
12
11
|
import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
|
|
13
|
-
import {
|
|
14
|
-
parseCSSAlignSelf,
|
|
15
|
-
parseCSSBackgroundImages,
|
|
16
|
-
parseCSSNumber,
|
|
17
|
-
parseCSSShadows
|
|
18
|
-
} from '@pairdraw/scene-graph/css'
|
|
12
|
+
import { parseCSSAlignSelf, parseCSSNumber, parseCSSShadows } from '@pairdraw/scene-graph/css'
|
|
19
13
|
import { normalizeEffects } from '@pairdraw/scene-graph/effects'
|
|
20
14
|
import { blurRadiusForSigma } from '@pairdraw/scene-graph/geometry'
|
|
21
15
|
import { computeImageHash } from '@pairdraw/scene-graph/images'
|
|
22
16
|
import { textLayerName } from '@pairdraw/scene-graph/node-defaults'
|
|
23
17
|
|
|
24
18
|
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
|
|
25
|
-
import {
|
|
19
|
+
import type { BackgroundLayerPaint } from './background-pictures'
|
|
20
|
+
import { backgroundPaint } from './background-pictures'
|
|
21
|
+
import { colorToStrokeFromCSS, mergedStyle, pickStyle } from './css-values'
|
|
26
22
|
import {
|
|
27
23
|
applyContainerLayout,
|
|
28
24
|
applyFlowSizing,
|
|
@@ -33,6 +29,8 @@ import {
|
|
|
33
29
|
type ParentFlow
|
|
34
30
|
} from './flow'
|
|
35
31
|
import { resolveFontFamilyStack } from './fonts'
|
|
32
|
+
import type { PaintedChild } from './stacking'
|
|
33
|
+
import { applyStacking } from './stacking'
|
|
36
34
|
import {
|
|
37
35
|
blockRichText,
|
|
38
36
|
characterStyleFromCSS,
|
|
@@ -126,45 +124,35 @@ function firstCSSNumber(style: DesignStyleDeclaration, ...properties: string[]):
|
|
|
126
124
|
return null
|
|
127
125
|
}
|
|
128
126
|
|
|
129
|
-
function fillsFromStyle(style: DesignStyleDeclaration, property: string): Fill[] {
|
|
130
|
-
return colorToFillFromCSS(pickStyle(style, property))
|
|
131
|
-
}
|
|
132
|
-
|
|
133
127
|
/**
|
|
134
|
-
*
|
|
135
|
-
*
|
|
136
|
-
* bytes are inline; a remote address is skipped, as an unfetchable background paints nothing.
|
|
128
|
+
* Puts one background layer behind the children, at the area a picture shows or over the whole
|
|
129
|
+
* box. Layers are added bottom-first, so each new one paints above the last.
|
|
137
130
|
*/
|
|
138
|
-
function
|
|
131
|
+
function addBackgroundLayer(
|
|
139
132
|
graph: SceneGraph,
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
):
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
fills.
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
opacity: 1,
|
|
164
|
-
visible: true
|
|
165
|
-
})
|
|
166
|
-
}
|
|
167
|
-
return fills.reverse()
|
|
133
|
+
parent: SceneNode,
|
|
134
|
+
layer: BackgroundLayerPaint
|
|
135
|
+
): void {
|
|
136
|
+
// A layer that covers the box follows its corners; one at a picture's own area does not.
|
|
137
|
+
const corners =
|
|
138
|
+
layer.area === undefined
|
|
139
|
+
? {
|
|
140
|
+
cornerRadius: parent.cornerRadius,
|
|
141
|
+
topLeftRadius: parent.topLeftRadius,
|
|
142
|
+
topRightRadius: parent.topRightRadius,
|
|
143
|
+
bottomRightRadius: parent.bottomRightRadius,
|
|
144
|
+
bottomLeftRadius: parent.bottomLeftRadius,
|
|
145
|
+
independentCorners: parent.independentCorners
|
|
146
|
+
}
|
|
147
|
+
: {}
|
|
148
|
+
const background = graph.createNode('RECTANGLE', parent.id, {
|
|
149
|
+
name: layer.picture ? 'background image' : 'background',
|
|
150
|
+
...(layer.area ?? { x: 0, y: 0, width: parent.width, height: parent.height }),
|
|
151
|
+
fills: [layer.fill],
|
|
152
|
+
...corners,
|
|
153
|
+
layoutPositioning: parent.layoutMode === 'NONE' ? 'AUTO' : 'ABSOLUTE'
|
|
154
|
+
})
|
|
155
|
+
graph.insertChildAt(background.id, parent.id, parent.childIds.length)
|
|
168
156
|
}
|
|
169
157
|
|
|
170
158
|
function aspectRatioFromCSS(value: string | undefined): number | null {
|
|
@@ -400,9 +388,9 @@ function applyElementStyle(
|
|
|
400
388
|
applyPositioning(node, style)
|
|
401
389
|
applyPadding(node, style)
|
|
402
390
|
|
|
403
|
-
const
|
|
404
|
-
|
|
405
|
-
|
|
391
|
+
const background = backgroundPaint(graph, style, node)
|
|
392
|
+
if (background.fills.length > 0) node.fills = background.fills
|
|
393
|
+
for (const layer of background.layers) addBackgroundLayer(graph, node, layer)
|
|
406
394
|
applyImageFill(graph, node, element, style)
|
|
407
395
|
|
|
408
396
|
const strokes = colorToStrokeFromCSS(
|
|
@@ -647,6 +635,23 @@ function containerFlow(element: DesignElement, display: string) {
|
|
|
647
635
|
return { flow, richText, lines }
|
|
648
636
|
}
|
|
649
637
|
|
|
638
|
+
/** Builds a container's children and orders them the way CSS paints them. */
|
|
639
|
+
function paintChildren(
|
|
640
|
+
graph: SceneGraph,
|
|
641
|
+
node: SceneNode,
|
|
642
|
+
element: DesignElement,
|
|
643
|
+
style: DesignStyleDeclaration,
|
|
644
|
+
flow: ParentFlow
|
|
645
|
+
): void {
|
|
646
|
+
const children = flow === 'flex' ? element.children.toSorted(byFlexOrder) : element.children
|
|
647
|
+
const painted: PaintedChild[] = []
|
|
648
|
+
for (const child of children) {
|
|
649
|
+
const layer = createDesignNode(graph, node.id, child, flow, inheritedTextStyle(element, style))
|
|
650
|
+
if (layer) painted.push({ layer, style: child.type === 'element' ? mergedStyle(child) : {} })
|
|
651
|
+
}
|
|
652
|
+
applyStacking(graph, node, style, painted)
|
|
653
|
+
}
|
|
654
|
+
|
|
650
655
|
function createElementNode(
|
|
651
656
|
graph: SceneGraph,
|
|
652
657
|
parentId: string,
|
|
@@ -691,10 +696,7 @@ function createElementNode(
|
|
|
691
696
|
createRichTextNode(graph, node.id, element, textStyle, textDisplay, flow ?? 'none')
|
|
692
697
|
return node
|
|
693
698
|
}
|
|
694
|
-
|
|
695
|
-
for (const child of children) {
|
|
696
|
-
createDesignNode(graph, node.id, child, flow ?? 'none', inheritedTextStyle(element, style))
|
|
697
|
-
}
|
|
699
|
+
paintChildren(graph, node, element, style, flow ?? 'none')
|
|
698
700
|
|
|
699
701
|
return node
|
|
700
702
|
}
|