-
Notifications
You must be signed in to change notification settings - Fork 1
/
render.ts
55 lines (46 loc) · 1.17 KB
/
render.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
import isNonVisual from './isNonVisual';
import { SKRSContext2D } from '@napi-rs/canvas';
import { LayoutNode } from './layout';
type RenderStyles = {
fillStyle?: string;
strokeStyle?: string;
font?: string;
};
const userAgentRenderStyles: { [tag: string]: RenderStyles } = {
A: {
fillStyle: 'blue',
},
H1: {
font: 'bold 24px sans-serif',
}
};
export default function render(node: LayoutNode, context: SKRSContext2D, styles: RenderStyles = {}) {
if (isNonVisual(node)) {
return;
}
if (node.type === 'text') {
context.save();
if (styles.fillStyle) {
context.fillStyle = styles.fillStyle;
}
if (styles.strokeStyle) {
context.strokeStyle = styles.strokeStyle;
}
if (styles.font) {
context.font = styles.font;
}
context.fillText(node.text, node.x, node.y);
context.restore();
return;
}
if (node.nodes) {
for (const subNode of node.nodes) {
if (subNode.type === 'element') {
render(subNode, context, { ...styles, ...userAgentRenderStyles[subNode.tag] });
}
else {
render(subNode, context, { ...styles, ...userAgentRenderStyles[node.tag] });
}
}
}
}