-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap.ts
151 lines (141 loc) · 5.03 KB
/
wrap.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
import {chain} from "@softwareventures/chain";
import {
concatMapFn,
filterFn,
foldFn,
mapFn,
pairwise,
pushFn,
scanFn
} from "@softwareventures/iterable";
interface MeasureLineOptions {
readonly context: CanvasRenderingContext2D;
readonly text: string;
readonly fontSize: number;
readonly lineHeight: number;
}
interface LineMetrics {
readonly left: number;
readonly right: number;
readonly top: number;
readonly bottom: number;
readonly width: number;
}
function measureLine(options: MeasureLineOptions): LineMetrics {
const metrics = options.context.measureText(options.text);
if (metrics.actualBoundingBoxLeft == null) {
return {
left: metrics.width + options.fontSize * 2,
right: metrics.width + options.fontSize * 2,
top: options.lineHeight + options.fontSize * 2,
bottom: options.lineHeight + options.fontSize * 2,
width: metrics.width
};
} else {
return {
left: Math.trunc(metrics.actualBoundingBoxLeft) + 1,
right: Math.trunc(metrics.actualBoundingBoxRight) + 1,
top: Math.trunc(metrics.actualBoundingBoxAscent) + 1,
bottom: Math.trunc(metrics.actualBoundingBoxDescent) + 1,
width: metrics.width
};
}
}
interface WrapTextOptions extends MeasureLineOptions {
readonly wrapWidth: number;
}
interface WrappedText {
readonly lines: string[];
readonly left: number;
readonly right: number;
readonly top: number;
readonly bottom: number;
}
interface AppendWrappedTextLineOptions {
readonly wrappedText: WrappedText;
readonly line: {
readonly text: string;
readonly metrics: LineMetrics;
};
readonly lineHeight: number;
}
function appendWrappedTextLine(options: AppendWrappedTextLineOptions): WrappedText {
return {
lines: [...options.wrappedText.lines, options.line.text],
left: Math.max(options.wrappedText.left, options.line.metrics.left),
right: Math.max(options.wrappedText.right, options.line.metrics.right),
top: Math.max(
options.wrappedText.top,
options.line.metrics.top - options.lineHeight * options.wrappedText.lines.length
),
bottom: Math.max(
options.wrappedText.bottom,
options.line.metrics.bottom + options.lineHeight * options.wrappedText.lines.length
)
};
}
interface WrapTextAccumulator {
readonly line: string;
readonly metrics: LineMetrics;
readonly newLine: boolean;
}
export function wrapText(options: WrapTextOptions): WrappedText {
const first: WrapTextAccumulator = {
line: "",
metrics: {left: 0, right: 0, top: 0, bottom: 0, width: 0},
newLine: false
};
const last: WrapTextAccumulator = {
line: "",
metrics: {left: 0, right: 0, top: 0, bottom: 0, width: 0},
newLine: true
};
return chain(options.text.split(/\n/u))
.map(mapFn(line => ({line, metrics: measureLine({...options, text: line})})))
.map(
concatMapFn(({line, metrics}) =>
metrics.width <= options.wrapWidth
? [{line, metrics}]
: chain(line.split(/\s/u))
.map(
scanFn((accumulator: WrapTextAccumulator, word) => {
const line =
accumulator.line === ""
? word
: `${accumulator.line} ${word}`;
const metrics = measureLine({...options, text: line});
if (metrics.width > options.wrapWidth && line !== word) {
return {
line: word,
metrics: measureLine({...options, text: word}),
newLine: true
};
} else {
return {line, metrics, newLine: false};
}
}, first)
)
.map(pushFn(last))
.map(pairwise)
.map(filterFn(([_, b]) => b.newLine))
.map(mapFn(([{line, metrics}]) => ({line, metrics}))).value
)
)
.map(
foldFn(
(wrappedText: WrappedText, {line, metrics}) =>
appendWrappedTextLine({
wrappedText,
line: {text: line, metrics},
lineHeight: options.lineHeight
}),
{
lines: [],
left: 0,
right: 0,
top: 0,
bottom: 0
}
)
).value;
}