-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathcompute.ts
368 lines (334 loc) · 10.4 KB
/
compute.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/**
* Handler to update the Yoga node properties with the given element type and
* style. Each supported element has its own preset styles, so this function
* also returns the inherited style for children of the element.
*/
import type { Node as YogaNode } from 'yoga-wasm-web'
import getYoga from '../yoga/index.js'
import presets from './presets.js'
import inheritable from './inheritable.js'
import expand, { SerializedStyle } from './expand.js'
import { lengthToNumber, parseViewBox, v } from '../utils.js'
import { resolveImageData } from './image.js'
type SatoriElement = keyof typeof presets
export default async function compute(
node: YogaNode,
type: SatoriElement | string,
inheritedStyle: SerializedStyle,
definedStyle: Record<string, string | number>,
props: Record<string, any>
): Promise<[SerializedStyle, SerializedStyle]> {
const Yoga = await getYoga()
// Extend the default style with defined and inherited styles.
const style: SerializedStyle = {
...inheritedStyle,
...expand(presets[type], inheritedStyle),
...expand(definedStyle, inheritedStyle),
}
if (type === 'img') {
let [resolvedSrc, imageWidth, imageHeight] = await resolveImageData(
props.src
)
// Cannot parse the image size (e.g. base64 data URI).
if (imageWidth === undefined && imageHeight === undefined) {
if (props.width === undefined || props.height === undefined) {
throw new Error(
'Image size cannot be determined. Please provide the width and height of the image.'
)
}
imageWidth = parseInt(props.width)
imageHeight = parseInt(props.height)
}
const r = imageHeight / imageWidth
// Before calculating the missing width or height based on the image ratio,
// we must subtract the padding and border due to how box model works.
// TODO: Ensure these are absolute length values, not relative values.
let extraHorizontal =
(style.borderLeftWidth || 0) +
(style.borderRightWidth || 0) +
(style.paddingLeft || 0) +
(style.paddingRight || 0)
let extraVertical =
(style.borderTopWidth || 0) +
(style.borderBottomWidth || 0) +
(style.paddingTop || 0) +
(style.paddingBottom || 0)
let contentBoxWidth = style.width || props.width
let contentBoxHeight = style.height || props.height
const isAbsoluteContentSize =
typeof contentBoxWidth === 'number' &&
typeof contentBoxHeight === 'number'
if (isAbsoluteContentSize) {
contentBoxWidth -= extraHorizontal
contentBoxHeight -= extraVertical
}
// When no content size is defined, we use the image size as the content size.
if (contentBoxWidth === undefined && contentBoxHeight === undefined) {
contentBoxWidth = '100%'
node.setAspectRatio(1 / r)
} else {
// If only one sisde is not defined, we can calculate the other one.
if (contentBoxWidth === undefined) {
if (typeof contentBoxHeight === 'number') {
contentBoxWidth = contentBoxHeight / r
} else {
// If it uses a relative value (e.g. 50%), we can rely on aspect ratio.
// Note: this doesn't work well if there are paddings or borders.
node.setAspectRatio(1 / r)
}
} else if (contentBoxHeight === undefined) {
if (typeof contentBoxWidth === 'number') {
contentBoxHeight = contentBoxWidth * r
} else {
// If it uses a relative value (e.g. 50%), we can rely on aspect ratio.
// Note: this doesn't work well if there are paddings or borders.
node.setAspectRatio(1 / r)
}
}
}
style.width = isAbsoluteContentSize
? (contentBoxWidth as number) + extraHorizontal
: contentBoxWidth
style.height = isAbsoluteContentSize
? (contentBoxHeight as number) + extraVertical
: contentBoxHeight
style.__src = resolvedSrc
}
if (type === 'svg') {
const viewBox = props.viewBox || props.viewbox
const viewBoxSize = parseViewBox(viewBox)
const ratio = viewBoxSize ? viewBoxSize[3] / viewBoxSize[2] : null
let { width, height } = props
if (typeof width === 'undefined' && height) {
if (ratio == null) {
width = 0
} else if (typeof height === 'string' && height.endsWith('%')) {
width = parseInt(height) / ratio + '%'
} else {
height = lengthToNumber(
height,
inheritedStyle.fontSize,
1,
inheritedStyle
)
width = height / ratio
}
} else if (typeof height === 'undefined' && width) {
if (ratio == null) {
width = 0
} else if (typeof width === 'string' && width.endsWith('%')) {
height = parseInt(width) * ratio + '%'
} else {
width = lengthToNumber(
width,
inheritedStyle.fontSize,
1,
inheritedStyle
)
height = width * ratio
}
} else {
if (typeof width !== 'undefined') {
width =
lengthToNumber(width, inheritedStyle.fontSize, 1, inheritedStyle) ||
width
}
if (typeof height !== 'undefined') {
height =
lengthToNumber(height, inheritedStyle.fontSize, 1, inheritedStyle) ||
height
}
width ||= viewBoxSize?.[2]
height ||= viewBoxSize?.[3]
}
if (!style.width && width) style.width = width
if (!style.height && height) style.height = height
}
// Set properties for Yoga.
node.setDisplay(
v(
style.display,
{
flex: Yoga.DISPLAY_FLEX,
block: Yoga.DISPLAY_FLEX,
none: Yoga.DISPLAY_NONE,
'-webkit-box': Yoga.DISPLAY_FLEX,
},
Yoga.DISPLAY_FLEX,
'display'
)
)
node.setAlignContent(
v(
style.alignContent,
{
stretch: Yoga.ALIGN_STRETCH,
center: Yoga.ALIGN_CENTER,
'flex-start': Yoga.ALIGN_FLEX_START,
'flex-end': Yoga.ALIGN_FLEX_END,
'space-between': Yoga.ALIGN_SPACE_BETWEEN,
'space-around': Yoga.ALIGN_SPACE_AROUND,
baseline: Yoga.ALIGN_BASELINE,
normal: Yoga.ALIGN_AUTO,
},
Yoga.ALIGN_AUTO,
'alignContent'
)
)
node.setAlignItems(
v(
style.alignItems,
{
stretch: Yoga.ALIGN_STRETCH,
center: Yoga.ALIGN_CENTER,
'flex-start': Yoga.ALIGN_FLEX_START,
'flex-end': Yoga.ALIGN_FLEX_END,
baseline: Yoga.ALIGN_BASELINE,
normal: Yoga.ALIGN_AUTO,
},
Yoga.ALIGN_STRETCH,
'alignItems'
)
)
node.setAlignSelf(
v(
style.alignSelf,
{
stretch: Yoga.ALIGN_STRETCH,
center: Yoga.ALIGN_CENTER,
'flex-start': Yoga.ALIGN_FLEX_START,
'flex-end': Yoga.ALIGN_FLEX_END,
baseline: Yoga.ALIGN_BASELINE,
normal: Yoga.ALIGN_AUTO,
},
Yoga.ALIGN_AUTO,
'alignSelf'
)
)
node.setJustifyContent(
v(
style.justifyContent,
{
center: Yoga.JUSTIFY_CENTER,
'flex-start': Yoga.JUSTIFY_FLEX_START,
'flex-end': Yoga.JUSTIFY_FLEX_END,
'space-between': Yoga.JUSTIFY_SPACE_BETWEEN,
'space-around': Yoga.JUSTIFY_SPACE_AROUND,
},
Yoga.JUSTIFY_FLEX_START,
'justifyContent'
)
)
// @TODO: node.setAspectRatio
node.setFlexDirection(
v(
style.flexDirection,
{
row: Yoga.FLEX_DIRECTION_ROW,
column: Yoga.FLEX_DIRECTION_COLUMN,
'row-reverse': Yoga.FLEX_DIRECTION_ROW_REVERSE,
'column-reverse': Yoga.FLEX_DIRECTION_COLUMN_REVERSE,
},
Yoga.FLEX_DIRECTION_ROW,
'flexDirection'
)
)
node.setFlexWrap(
v(
style.flexWrap,
{
wrap: Yoga.WRAP_WRAP,
nowrap: Yoga.WRAP_NO_WRAP,
'wrap-reverse': Yoga.WRAP_WRAP_REVERSE,
},
Yoga.WRAP_NO_WRAP,
'flexWrap'
)
)
if (typeof style.gap !== 'undefined') {
node.setGap(Yoga.GUTTER_ALL, style.gap)
}
if (typeof style.rowGap !== 'undefined') {
node.setGap(Yoga.GUTTER_ROW, style.rowGap)
}
if (typeof style.columnGap !== 'undefined') {
node.setGap(Yoga.GUTTER_COLUMN, style.columnGap)
}
// @TODO: node.setFlex
if (typeof style.flexBasis !== 'undefined') {
node.setFlexBasis(style.flexBasis)
}
node.setFlexGrow(typeof style.flexGrow === 'undefined' ? 0 : style.flexGrow)
node.setFlexShrink(
typeof style.flexShrink === 'undefined' ? 0 : style.flexShrink
)
if (typeof style.maxHeight !== 'undefined') {
node.setMaxHeight(style.maxHeight)
}
if (typeof style.maxWidth !== 'undefined') {
node.setMaxWidth(style.maxWidth)
}
if (typeof style.minHeight !== 'undefined') {
node.setMinHeight(style.minHeight)
}
if (typeof style.minWidth !== 'undefined') {
node.setMinWidth(style.minWidth)
}
node.setOverflow(
v(
style.overflow,
{
visible: Yoga.OVERFLOW_VISIBLE,
hidden: Yoga.OVERFLOW_HIDDEN,
},
Yoga.OVERFLOW_VISIBLE,
'overflow'
)
)
node.setMargin(Yoga.EDGE_TOP, style.marginTop || 0)
node.setMargin(Yoga.EDGE_BOTTOM, style.marginBottom || 0)
node.setMargin(Yoga.EDGE_LEFT, style.marginLeft || 0)
node.setMargin(Yoga.EDGE_RIGHT, style.marginRight || 0)
node.setBorder(Yoga.EDGE_TOP, style.borderTopWidth || 0)
node.setBorder(Yoga.EDGE_BOTTOM, style.borderBottomWidth || 0)
node.setBorder(Yoga.EDGE_LEFT, style.borderLeftWidth || 0)
node.setBorder(Yoga.EDGE_RIGHT, style.borderRightWidth || 0)
node.setPadding(Yoga.EDGE_TOP, style.paddingTop || 0)
node.setPadding(Yoga.EDGE_BOTTOM, style.paddingBottom || 0)
node.setPadding(Yoga.EDGE_LEFT, style.paddingLeft || 0)
node.setPadding(Yoga.EDGE_RIGHT, style.paddingRight || 0)
node.setPositionType(
v(
style.position,
{
absolute: Yoga.POSITION_TYPE_ABSOLUTE,
relative: Yoga.POSITION_TYPE_RELATIVE,
},
Yoga.POSITION_TYPE_RELATIVE,
'position'
)
)
if (typeof style.top !== 'undefined') {
node.setPosition(Yoga.EDGE_TOP, style.top)
}
if (typeof style.bottom !== 'undefined') {
node.setPosition(Yoga.EDGE_BOTTOM, style.bottom)
}
if (typeof style.left !== 'undefined') {
node.setPosition(Yoga.EDGE_LEFT, style.left)
}
if (typeof style.right !== 'undefined') {
node.setPosition(Yoga.EDGE_RIGHT, style.right)
}
if (typeof style.height !== 'undefined') {
node.setHeight(style.height)
} else {
node.setHeightAuto()
}
if (typeof style.width !== 'undefined') {
node.setWidth(style.width)
} else {
node.setWidthAuto()
}
return [style, inheritable(style)]
}