@pairdraw/dom-css 0.19.2 → 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 +140 -59
- package/dist/chunks/behaviours/states/layers.js.map +1 -1
- package/dist/chunks/browser.js +585 -74
- 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 +39 -22
- package/src/export/projection.ts +125 -24
- package/src/export/text.ts +32 -16
- package/src/import/background-pictures.ts +430 -0
- package/src/import/fonts.ts +10 -29
- package/src/import/scene-graph.ts +62 -59
- 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
|
+
}
|
package/src/import/fonts.ts
CHANGED
|
@@ -1,25 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
'ui-sans-serif': 'sans-serif',
|
|
8
|
-
'system-ui': 'sans-serif',
|
|
9
|
-
'-apple-system': 'sans-serif',
|
|
10
|
-
blinkmacsystemfont: 'sans-serif',
|
|
11
|
-
serif: 'serif',
|
|
12
|
-
'ui-serif': 'serif',
|
|
13
|
-
monospace: 'monospace',
|
|
14
|
-
'ui-monospace': 'monospace'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Known system faces of each class, best first. The app bundles Inter for sans-serif. */
|
|
18
|
-
const DEFAULT_FAMILIES = {
|
|
19
|
-
'sans-serif': [DEFAULT_FONT_FAMILY],
|
|
20
|
-
serif: ['Georgia', 'Times New Roman', 'Times'],
|
|
21
|
-
monospace: ['Menlo', 'SF Mono', 'Consolas', 'Courier New']
|
|
22
|
-
} as const satisfies Record<'sans-serif' | 'serif' | 'monospace', readonly string[]>
|
|
1
|
+
import {
|
|
2
|
+
GENERIC_FONT_FAMILIES_BY_CLASS,
|
|
3
|
+
genericFontFamilyClass,
|
|
4
|
+
parseCSSFontFamilies,
|
|
5
|
+
type GenericFontClass
|
|
6
|
+
} from '@pairdraw/scene-graph/css'
|
|
23
7
|
|
|
24
8
|
export interface FontStackOptions {
|
|
25
9
|
/**
|
|
@@ -29,11 +13,8 @@ export interface FontStackOptions {
|
|
|
29
13
|
isAvailable?: (family: string) => boolean
|
|
30
14
|
}
|
|
31
15
|
|
|
32
|
-
function defaultFamily(
|
|
33
|
-
|
|
34
|
-
options: FontStackOptions
|
|
35
|
-
): string {
|
|
36
|
-
const candidates = DEFAULT_FAMILIES[kind]
|
|
16
|
+
function defaultFamily(kind: GenericFontClass, options: FontStackOptions): string {
|
|
17
|
+
const candidates = GENERIC_FONT_FAMILIES_BY_CLASS[kind]
|
|
37
18
|
return (
|
|
38
19
|
(options.isAvailable ? candidates.find((family) => options.isAvailable?.(family)) : null) ??
|
|
39
20
|
candidates[0]
|
|
@@ -50,8 +31,8 @@ export function resolveFontFamilyStack(
|
|
|
50
31
|
options: FontStackOptions = {}
|
|
51
32
|
): string | null {
|
|
52
33
|
for (const family of parseCSSFontFamilies(value)) {
|
|
53
|
-
const
|
|
54
|
-
if (
|
|
34
|
+
const kind = genericFontFamilyClass(family)
|
|
35
|
+
if (kind) return defaultFamily(kind, options)
|
|
55
36
|
if (options.isAvailable && !options.isAvailable(family)) continue
|
|
56
37
|
return family
|
|
57
38
|
}
|
|
@@ -3,25 +3,22 @@ import { isValid, toUint8Array } from 'js-base64'
|
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
5
|
SceneGraph,
|
|
6
|
-
type
|
|
6
|
+
type Effect,
|
|
7
7
|
type ImageScaleMode,
|
|
8
8
|
type SceneNode,
|
|
9
9
|
type Stroke
|
|
10
10
|
} from '@pairdraw/scene-graph'
|
|
11
11
|
import { TRANSPARENT } from '@pairdraw/scene-graph/constants'
|
|
12
|
-
import {
|
|
13
|
-
parseCSSAlignSelf,
|
|
14
|
-
parseCSSBackgroundImages,
|
|
15
|
-
parseCSSNumber,
|
|
16
|
-
parseCSSShadows
|
|
17
|
-
} from '@pairdraw/scene-graph/css'
|
|
12
|
+
import { parseCSSAlignSelf, parseCSSNumber, parseCSSShadows } from '@pairdraw/scene-graph/css'
|
|
18
13
|
import { normalizeEffects } from '@pairdraw/scene-graph/effects'
|
|
19
14
|
import { blurRadiusForSigma } from '@pairdraw/scene-graph/geometry'
|
|
20
15
|
import { computeImageHash } from '@pairdraw/scene-graph/images'
|
|
21
16
|
import { textLayerName } from '@pairdraw/scene-graph/node-defaults'
|
|
22
17
|
|
|
23
18
|
import type { DesignDocument, DesignElement, DesignNode, DesignStyleDeclaration } from '../types'
|
|
24
|
-
import {
|
|
19
|
+
import type { BackgroundLayerPaint } from './background-pictures'
|
|
20
|
+
import { backgroundPaint } from './background-pictures'
|
|
21
|
+
import { colorToStrokeFromCSS, mergedStyle, pickStyle } from './css-values'
|
|
25
22
|
import {
|
|
26
23
|
applyContainerLayout,
|
|
27
24
|
applyFlowSizing,
|
|
@@ -32,6 +29,8 @@ import {
|
|
|
32
29
|
type ParentFlow
|
|
33
30
|
} from './flow'
|
|
34
31
|
import { resolveFontFamilyStack } from './fonts'
|
|
32
|
+
import type { PaintedChild } from './stacking'
|
|
33
|
+
import { applyStacking } from './stacking'
|
|
35
34
|
import {
|
|
36
35
|
blockRichText,
|
|
37
36
|
characterStyleFromCSS,
|
|
@@ -51,9 +50,10 @@ const IMAGE_SOURCE_URL_KEY = 'image-source-url'
|
|
|
51
50
|
* by half its blur length, which the canvas draws from the radius that gives that sigma.
|
|
52
51
|
*/
|
|
53
52
|
function cssShadows(value: string | undefined) {
|
|
54
|
-
return parseCSSShadows(value).map((effect) =>
|
|
55
|
-
effect.type
|
|
56
|
-
|
|
53
|
+
return parseCSSShadows(value).map((effect): Effect => {
|
|
54
|
+
if (effect.type !== 'DROP_SHADOW' && effect.type !== 'INNER_SHADOW') return effect
|
|
55
|
+
return { ...effect, radius: blurRadiusForSigma(effect.radius / 2) }
|
|
56
|
+
})
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
export interface DesignDocumentToSceneGraphOptions {
|
|
@@ -124,45 +124,35 @@ function firstCSSNumber(style: DesignStyleDeclaration, ...properties: string[]):
|
|
|
124
124
|
return null
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
function fillsFromStyle(style: DesignStyleDeclaration, property: string): Fill[] {
|
|
128
|
-
return colorToFillFromCSS(pickStyle(style, property))
|
|
129
|
-
}
|
|
130
|
-
|
|
131
127
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* 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.
|
|
135
130
|
*/
|
|
136
|
-
function
|
|
131
|
+
function addBackgroundLayer(
|
|
137
132
|
graph: SceneGraph,
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
):
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
fills.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
opacity: 1,
|
|
162
|
-
visible: true
|
|
163
|
-
})
|
|
164
|
-
}
|
|
165
|
-
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)
|
|
166
156
|
}
|
|
167
157
|
|
|
168
158
|
function aspectRatioFromCSS(value: string | undefined): number | null {
|
|
@@ -365,11 +355,10 @@ function applyImageFill(
|
|
|
365
355
|
const bytes = bytesFromDataURL(source)
|
|
366
356
|
if (!bytes) {
|
|
367
357
|
if (source) {
|
|
368
|
-
node.pluginData
|
|
369
|
-
|
|
370
|
-
key: IMAGE_SOURCE_URL_KEY,
|
|
371
|
-
|
|
372
|
-
})
|
|
358
|
+
node.pluginData = [
|
|
359
|
+
...node.pluginData,
|
|
360
|
+
{ pluginId: DOM_CSS_PLUGIN_ID, key: IMAGE_SOURCE_URL_KEY, value: source }
|
|
361
|
+
]
|
|
373
362
|
}
|
|
374
363
|
return
|
|
375
364
|
}
|
|
@@ -399,9 +388,9 @@ function applyElementStyle(
|
|
|
399
388
|
applyPositioning(node, style)
|
|
400
389
|
applyPadding(node, style)
|
|
401
390
|
|
|
402
|
-
const
|
|
403
|
-
|
|
404
|
-
|
|
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)
|
|
405
394
|
applyImageFill(graph, node, element, style)
|
|
406
395
|
|
|
407
396
|
const strokes = colorToStrokeFromCSS(
|
|
@@ -646,6 +635,23 @@ function containerFlow(element: DesignElement, display: string) {
|
|
|
646
635
|
return { flow, richText, lines }
|
|
647
636
|
}
|
|
648
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
|
+
|
|
649
655
|
function createElementNode(
|
|
650
656
|
graph: SceneGraph,
|
|
651
657
|
parentId: string,
|
|
@@ -690,10 +696,7 @@ function createElementNode(
|
|
|
690
696
|
createRichTextNode(graph, node.id, element, textStyle, textDisplay, flow ?? 'none')
|
|
691
697
|
return node
|
|
692
698
|
}
|
|
693
|
-
|
|
694
|
-
for (const child of children) {
|
|
695
|
-
createDesignNode(graph, node.id, child, flow ?? 'none', inheritedTextStyle(element, style))
|
|
696
|
-
}
|
|
699
|
+
paintChildren(graph, node, element, style, flow ?? 'none')
|
|
697
700
|
|
|
698
701
|
return node
|
|
699
702
|
}
|