-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont.tsx
165 lines (147 loc) · 4.24 KB
/
font.tsx
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
import { TitleResp } from "./typings/TitleResp";
import { jsxSVG as jsx } from "./jsx";
import { GroupResp } from "./typings/GroupResp";
export type RenderDimensions = {
totalWidth: number;
opticalWidth: number;
lineCnt: number;
unitsPerEm: number;
};
export type WidthLine = {
optWidth: number;
fullWidth: number;
line: GroupResp[];
};
// Render flow: render gives Line[] and RenderDimensions
// Compare Line[] with rendered lines to see if they are the same
// First render call materialize
// Second render call relayout, with a WeakMap for cached lines and glyphs
// TODO: cache with WeakMap
export function materialize(
lines: WidthLine[],
dim: RenderDimensions,
additionalClasses: string[] = [],
): SVGElement {
let grpCnt = 0;
let globalXdiff = 0;
const linesEl = lines.map((line, lineIdx) => {
let lineAccum = 0;
const grps = line.line.map((grp, localGrpIdx) => {
let grpAccum = 0;
const chrs = grp.chars.map((chr) => {
const glyph = (
<g
class="glyph"
style={{
"--in-grp-xdiff": (grpAccum / dim.unitsPerEm).toString(),
}}
>
{chr.components.map((comp) => (
<path d={comp}></path>
))}
</g>
);
grpAccum += chr.hadv;
return glyph;
});
const grpEl = (
<g
class="var-group"
style={{
"--local-grp-idx": (localGrpIdx / dim.unitsPerEm).toString(),
"--grp-idx": grpCnt.toString(),
"--grp-line-xdiff": (lineAccum / dim.unitsPerEm).toString(),
"--grp-width": (grp.hadv / dim.unitsPerEm).toString(),
"--grp-xdiff": (globalXdiff / dim.unitsPerEm).toString(),
}}
data-text={grp.text}
>
{chrs}
</g>
);
lineAccum += grp.hadv;
globalXdiff += grp.hadv;
++grpCnt;
return grpEl;
});
return (
<g
class="line"
style={{
"--line-idx": lineIdx.toString(),
"--line-optical-width": (line.optWidth / dim.unitsPerEm).toString(),
}}
>
{grps}
</g>
);
});
return (
<svg
class={["title", ...additionalClasses]}
style={{
"--em": dim.unitsPerEm.toString(),
"--line-cnt": dim.lineCnt.toString(),
"--grp-cnt": grpCnt.toString(),
"--optical-width": (dim.opticalWidth / dim.unitsPerEm).toString(),
}}
>
{linesEl}
</svg>
);
}
function nextLine(grps: GroupResp[], maxWidth: number): WidthLine {
let lineWidth = 0;
let lineOpticalWidth = 0;
let lastBreakAfter: null | number = null;
let idx = 0;
for (const grp of grps) {
if (lineWidth + grp.hadv > maxWidth && lastBreakAfter !== null) break;
lineWidth += grp.hadv;
if (!grp.text.match(/^ +$/)) lineOpticalWidth = lineWidth;
if (grp.breakAfter) lastBreakAfter = idx;
++idx;
}
if (lastBreakAfter === null) throw new Error("Cannot break line");
return {
optWidth: lineOpticalWidth,
fullWidth: lineWidth,
line: grps.slice(0, lastBreakAfter + 1),
};
}
export function render(
line: TitleResp,
maxWidth: number = Infinity,
): [WidthLine[], RenderDimensions] {
const widthLines: WidthLine[] = [];
const maxWidthInUnit = maxWidth * line.em;
let remaining = line.groups;
while (remaining.length > 0) {
const line = nextLine(remaining, maxWidthInUnit);
widthLines.push(line);
remaining = remaining.slice(line.line.length);
}
const dimensions = {
totalWidth: widthLines.reduce((acc, line) => acc + line.fullWidth, 0),
opticalWidth: widthLines.reduce(
(acc, line) => Math.max(acc, line.optWidth),
0,
),
lineCnt: widthLines.length,
unitsPerEm: line.em,
};
return [widthLines, dimensions];
}
export function getStrokeDist(stroke: SVGPathElement, size: number): number {
const bbox = stroke.getBoundingClientRect();
const parentBbox = stroke.parentElement!.getBoundingClientRect();
const inGrpXdiff =
stroke.parentElement!.style.getPropertyValue("--in-grp-xdiff");
const grpXdiff =
stroke.parentElement!.parentElement!.style.getPropertyValue("--grp-xdiff");
return (
bbox.x -
parentBbox.x +
(parseFloat(inGrpXdiff) + parseFloat(grpXdiff)) * size
);
}