forked from observablehq/plot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.js
282 lines (265 loc) · 10.7 KB
/
text.js
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
import {create, namespaces} from "d3";
import {nonempty} from "../defined.js";
import {formatDefault} from "../format.js";
import {indexOf, identity, string, maybeNumberChannel, maybeTuple, numberChannel, isNumeric, isTemporal, keyword, maybeFrameAnchor, isTextual, isIterable} from "../options.js";
import {Mark} from "../plot.js";
import {applyChannelStyles, applyDirectStyles, applyIndirectStyles, applyAttr, applyTransform, impliedString, applyFrameAnchor} from "../style.js";
import {maybeIntervalMidX, maybeIntervalMidY} from "../transforms/interval.js";
const defaults = {
ariaLabel: "text",
strokeLinejoin: "round",
strokeWidth: 3,
paintOrder: "stroke"
};
export class Text extends Mark {
constructor(data, options = {}) {
const {
x,
y,
text = isIterable(data) && isTextual(data) ? identity : indexOf,
frameAnchor,
textAnchor = /right$/i.test(frameAnchor) ? "end" : /left$/i.test(frameAnchor) ? "start" : "middle",
lineAnchor = /^top/i.test(frameAnchor) ? "top" : /^bottom/i.test(frameAnchor) ? "bottom" : "middle",
lineHeight = 1,
lineWidth = Infinity,
monospace,
fontFamily = monospace ? "ui-monospace, monospace" : undefined,
fontSize,
fontStyle,
fontVariant,
fontWeight,
rotate
} = options;
const [vrotate, crotate] = maybeNumberChannel(rotate, 0);
const [vfontSize, cfontSize] = maybeFontSizeChannel(fontSize);
super(
data,
[
{name: "x", value: x, scale: "x", optional: true},
{name: "y", value: y, scale: "y", optional: true},
{name: "fontSize", value: vfontSize, optional: true},
{name: "rotate", value: numberChannel(vrotate), optional: true},
{name: "text", value: text, filter: nonempty}
],
options,
defaults
);
this.rotate = crotate;
this.textAnchor = impliedString(textAnchor, "middle");
this.lineAnchor = keyword(lineAnchor, "lineAnchor", ["top", "middle", "bottom"]);
this.lineHeight = +lineHeight;
this.lineWidth = +lineWidth;
this.monospace = !!monospace;
this.fontFamily = string(fontFamily);
this.fontSize = cfontSize;
this.fontStyle = string(fontStyle);
this.fontVariant = string(fontVariant);
this.fontWeight = string(fontWeight);
this.frameAnchor = maybeFrameAnchor(frameAnchor);
}
render(index, scales, channels, dimensions) {
const {x: X, y: Y, rotate: R, text: T, fontSize: FS} = channels;
const {rotate} = this;
const [cx, cy] = applyFrameAnchor(this, dimensions);
return create("svg:g")
.call(applyIndirectStyles, this, scales, dimensions)
.call(applyIndirectTextStyles, this, T, dimensions)
.call(applyTransform, this, scales)
.call(g => g.selectAll()
.data(index)
.enter()
.append("text")
.call(applyDirectStyles, this)
.call(applyMultilineText, this, T)
.attr("transform", R ? (X && Y ? i => `translate(${X[i]},${Y[i]}) rotate(${R[i]})`
: X ? i => `translate(${X[i]},${cy}) rotate(${R[i]})`
: Y ? i => `translate(${cx},${Y[i]}) rotate(${R[i]})`
: i => `translate(${cx},${cy}) rotate(${R[i]})`)
: rotate ? (X && Y ? i => `translate(${X[i]},${Y[i]}) rotate(${rotate})`
: X ? i => `translate(${X[i]},${cy}) rotate(${rotate})`
: Y ? i => `translate(${cx},${Y[i]}) rotate(${rotate})`
: `translate(${cx},${cy}) rotate(${rotate})`)
: (X && Y ? i => `translate(${X[i]},${Y[i]})`
: X ? i => `translate(${X[i]},${cy})`
: Y ? i => `translate(${cx},${Y[i]})`
: `translate(${cx},${cy})`))
.call(applyAttr, "font-size", FS && (i => FS[i]))
.call(applyChannelStyles, this, channels))
.node();
}
}
function applyMultilineText(selection, {monospace, lineAnchor, lineHeight, lineWidth}, T) {
if (!T) return;
const linesof = isFinite(lineWidth) ? (monospace
? t => lineWrap(t, lineWidth, monospaceWidth)
: t => lineWrap(t, lineWidth * 100, defaultWidth))
: t => t.split(/\r\n?|\n/g);
selection.each(function(i) {
const lines = linesof(formatDefault(T[i]));
const n = lines.length;
const y = lineAnchor === "top" ? 0.71 : lineAnchor === "bottom" ? 1 - n : (164 - n * 100) / 200;
if (n > 1) {
for (let i = 0; i < n; ++i) {
if (!lines[i]) continue;
const tspan = document.createElementNS(namespaces.svg, "tspan");
tspan.setAttribute("x", 0);
tspan.setAttribute("y", `${(y + i) * lineHeight}em`);
tspan.textContent = lines[i];
this.appendChild(tspan);
}
} else {
if (y) this.setAttribute("y", `${y * lineHeight}em`);
this.textContent = lines[0];
}
});
}
export function text(data, {x, y, ...options} = {}) {
if (options.frameAnchor === undefined) ([x, y] = maybeTuple(x, y));
return new Text(data, {...options, x, y});
}
export function textX(data, {x = identity, ...options} = {}) {
return new Text(data, maybeIntervalMidY({...options, x}));
}
export function textY(data, {y = identity, ...options} = {}) {
return new Text(data, maybeIntervalMidX({...options, y}));
}
function applyIndirectTextStyles(selection, mark, T) {
applyAttr(selection, "text-anchor", mark.textAnchor);
applyAttr(selection, "font-family", mark.fontFamily);
applyAttr(selection, "font-size", mark.fontSize);
applyAttr(selection, "font-style", mark.fontStyle);
applyAttr(selection, "font-variant", mark.fontVariant === undefined && (isNumeric(T) || isTemporal(T)) ? "tabular-nums" : mark.fontVariant);
applyAttr(selection, "font-weight", mark.fontWeight);
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/font-size
const fontSizes = new Set([
// global keywords
"inherit",
"initial",
"revert",
"unset",
// absolute keywords
"xx-small",
"x-small",
"small",
"medium",
"large",
"x-large",
"xx-large",
"xxx-large",
// relative keywords
"larger",
"smaller"
]);
// The font size may be expressed as a constant in the following forms:
// - number in pixels
// - string keyword: see above
// - string <length>: e.g., "12px"
// - string <percentage>: e.g., "80%"
// Anything else is assumed to be a channel definition.
function maybeFontSizeChannel(fontSize) {
if (fontSize == null || typeof fontSize === "number") return [undefined, fontSize];
if (typeof fontSize !== "string") return [fontSize, undefined];
fontSize = fontSize.trim().toLowerCase();
return fontSizes.has(fontSize) || /^[+-]?\d*\.?\d+(e[+-]?\d+)?(\w*|%)$/.test(fontSize)
? [undefined, fontSize]
: [fontSize, undefined];
}
// This is a greedy algorithm for line wrapping. It would be better to use the
// Knuth–Plass line breaking algorithm (but that would be much more complex).
// https://en.wikipedia.org/wiki/Line_wrap_and_word_wrap
function lineWrap(input, maxWidth, widthof = (_, i, j) => j - i) {
const lines = [];
let lineStart, lineEnd = 0;
for (const [wordStart, wordEnd, required] of lineBreaks(input)) {
// Record the start of a line. This isn’t the same as the previous line’s
// end because we often skip spaces between lines.
if (lineStart === undefined) lineStart = wordStart;
// If the current line is not empty, and if adding the current word would
// make the line longer than the allowed width, then break the line at the
// previous word end.
if (lineEnd > lineStart && widthof(input, lineStart, wordEnd) > maxWidth) {
lines.push(input.slice(lineStart, lineEnd));
lineStart = wordStart;
}
// If this is a required break (a newline), emit the line and reset.
if (required) {
lines.push(input.slice(lineStart, wordEnd));
lineStart = undefined;
continue;
}
// Extend the current line to include the new word.
lineEnd = wordEnd;
}
return lines;
}
// This is a rudimentary (and U.S.-centric) algorithm for finding opportunities
// to break lines between words. A better and far more comprehensive approach
// would be to use the official Unicode Line Breaking Algorithm.
// https://unicode.org/reports/tr14/
function* lineBreaks(input) {
let i = 0, j = 0;
const n = input.length;
while (j < n) {
let k = 1;
switch (input[j]) {
case "-": // hyphen
++j;
yield [i, j, false];
i = j;
break;
case " ":
yield [i, j, false];
while (input[++j] === " "); // skip multiple spaces
i = j;
break;
case "\r": if (input[j + 1] === "\n") ++k; // falls through
case "\n":
yield [i, j, true];
j += k;
i = j;
break;
default:
++j;
break;
}
}
yield [i, j, true];
}
// Computed as round(measureText(text).width * 10) at 10px system-ui. For
// characters that are not represented in this map, we’d ideally want to use a
// weighted average of what we expect to see. But since we don’t really know
// what that is, using “e” seems reasonable.
const defaultWidthMap = {
a: 56, b: 63, c: 57, d: 63, e: 58, f: 37, g: 62, h: 60, i: 26, j: 26, k: 55, l: 26, m: 88, n: 60, o: 60, p: 62, q: 62, r: 39, s: 54, t: 38, u: 60, v: 55, w: 79, x: 54, y: 55, z: 55,
A: 69, B: 67, C: 73, D: 74, E: 61, F: 58, G: 76, H: 75, I: 28, J: 55, K: 67, L: 58, M: 89, N: 75, O: 78, P: 65, Q: 78, R: 67, S: 65, T: 65, U: 75, V: 69, W: 98, X: 69, Y: 67, Z: 67,
0: 64, 1: 48, 2: 62, 3: 64, 4: 66, 5: 63, 6: 65, 7: 58, 8: 65, 9: 65,
" ": 29, "!": 32, '"': 49, "'": 31, "(": 39, ")": 39, ",": 31, "-": 48, ".": 31, "/": 32, ":": 31, ";": 31, "?": 52, "‘": 31, "’": 31, "“": 47, "”": 47
};
// This is a rudimentary (and U.S.-centric) algorithm for measuring the width of
// a string based on a technique of Gregor Aisch; it assumes that individual
// characters are laid out independently and does not implement the Unicode
// grapheme cluster breaking algorithm. It does understand code points, though,
// and so treats things like emoji as having the width of a lowercase e (and
// should be equivalent to using for-of to iterate over code points, while also
// being fast). TODO Optimize this by noting that we often re-measure characters
// that were previously measured?
// http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries
// https://exploringjs.com/impatient-js/ch_strings.html#atoms-of-text
function defaultWidth(text, start, end) {
let sum = 0;
for (let i = start; i < end; ++i) {
sum += defaultWidthMap[text[i]] || defaultWidthMap.e;
const first = text.charCodeAt(i);
if (first >= 0xd800 && first <= 0xdbff) { // high surrogate
const second = text.charCodeAt(i + 1);
if (second >= 0xdc00 && second <= 0xdfff) { // low surrogate
++i; // surrogate pair
}
}
}
return sum;
}
function monospaceWidth(text, start, end) {
return end - start;
}