-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathWrapper.ts
388 lines (349 loc) · 12.4 KB
/
Wrapper.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*************************************************************
*
* Copyright (c) 2018 The MathJax Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Implements the SVGWrapper class
*
* @author [email protected] (Davide Cervone)
*/
import {OptionList} from '../../util/Options.js';
import {CommonWrapper, AnyWrapperClass, Constructor} from '../common/Wrapper.js';
import {SVG, XLINKNS} from '../svg.js';
import {SVGWrapperFactory} from './WrapperFactory.js';
import {SVGFontData, SVGDelimiterData, SVGCharOptions} from './FontData.js';
export {Constructor, StringMap} from '../common/Wrapper.js';
/*****************************************************************/
/**
* Shorthand for makeing a SVGWrapper constructor
*/
export type SVGConstructor<N, T, D> = Constructor<SVGWrapper<N, T, D>>;
/*****************************************************************/
/**
* The type of the SVGWrapper class (used when creating the wrapper factory for this class)
*/
export interface SVGWrapperClass extends AnyWrapperClass {
kind: string;
}
/*****************************************************************/
/**
* The base SVGWrapper class
*
* @template N The HTMLElement node class
* @template T The Text node class
* @template D The Document class
*/
export class SVGWrapper<N, T, D> extends
CommonWrapper<
SVG<N, T, D>,
SVGWrapper<N, T, D>,
SVGWrapperClass,
SVGCharOptions,
SVGDelimiterData,
SVGFontData
> {
/**
* The kind of wrapper
*/
public static kind: string = 'unknown';
/**
* The factory used to create more SVGWrappers
*/
protected factory: SVGWrapperFactory<N, T, D>;
/**
* @override
*/
public parent: SVGWrapper<N, T, D>;
/**
* @override
*/
public childNodes: SVGWrapper<N, T, D>[];
/**
* The SVG element generated for this wrapped node
*/
public element: N = null;
/**
* @override
*/
public font: SVGFontData;
/*******************************************************************/
/**
* Create the HTML for the wrapped node.
*
* @param {N} parent The HTML node where the output is added
*/
public toSVG(parent: N) {
this.addChildren(this.standardSVGnode(parent));
}
/**
* @param {N} parent The element in which to add the children
*/
public addChildren(parent: N) {
let x = 0;
for (const child of this.childNodes) {
child.toSVG(parent);
if (child.element) {
child.place(x + child.bbox.L * child.bbox.rscale, 0);
}
x += (child.bbox.L + child.bbox.w + child.bbox.R) * child.bbox.rscale;
}
}
/*******************************************************************/
/**
* Create the standard SVG element for the given wrapped node.
*
* @param {N} parent The HTML element in which the node is to be created
* @returns {N} The root of the HTML tree for the wrapped node's output
*/
protected standardSVGnode(parent: N): N {
const svg = this.createSVGnode(parent);
this.handleStyles();
this.handleScale();
this.handleColor();
this.handleAttributes();
return svg;
}
/**
* @param {N} parent The HTML element in which the node is to be created
* @returns {N} The root of the HTML tree for the wrapped node's output
*/
protected createSVGnode(parent: N): N {
const href = this.node.attributes.get('href');
if (href) {
parent = this.adaptor.append(parent, this.svg('a', {href: href})) as N;
const {h, d, w} = this.getBBox();
this.adaptor.append(parent, this.svg('rect', {
'data-hitbox': true, fill: 'none', stroke: 'none', 'pointer-events': 'all',
width: this.fixed(w), height: this.fixed(h + d), y: this.fixed(-d)
}));
}
this.element = this.adaptor.append(parent, this.svg('g', {'data-mml-node': this.node.kind})) as N;
return this.element;
}
/**
* Set the CSS styles for the svg element
*/
protected handleStyles() {
if (!this.styles) return;
const styles = this.styles.cssText;
if (styles) {
this.adaptor.setAttribute(this.element, 'style', styles);
}
}
/**
* Set the (relative) scaling factor for the node
*/
protected handleScale() {
if (this.bbox.rscale !== 1) {
const scale = 'scale(' + this.fixed(this.bbox.rscale / 1000, 3) + ')';
this.adaptor.setAttribute(this.element, 'transform', scale);
}
}
/**
* Add the foreground and background colors
* (Only look at explicit attributes, since inherited ones will
* be applied to a parent element, and we will inherit from that)
*/
protected handleColor() {
const adaptor = this.adaptor;
const attributes = this.node.attributes;
const mathcolor = attributes.getExplicit('mathcolor') as string;
const color = attributes.getExplicit('color') as string;
const mathbackground = attributes.getExplicit('mathbackground') as string;
const background = attributes.getExplicit('background') as string;
if (mathcolor || color) {
adaptor.setAttribute(this.element, 'fill', mathcolor || color);
adaptor.setAttribute(this.element, 'stroke', mathcolor || color);
}
if (mathbackground || background) {
let {h, d, w} = this.getBBox();
let rect = this.svg('rect', {
fill: mathbackground || background,
x: 0, y: this.fixed(-d),
width: this.fixed(w),
height: this.fixed(h + d),
'data-bgcolor': true
});
let child = adaptor.firstChild(this.element);
if (child) {
adaptor.insert(rect, child);
} else {
adaptor.append(this.element, rect);
}
}
}
/**
* Copy RDFa, aria, and other tags from the MathML to the SVG output nodes.
* Don't copy those in the skipAttributes list, or anything that already exists
* as a property of the node (e.g., no "onlick", etc.). If a name in the
* skipAttributes object is set to false, then the attribute WILL be copied.
* Add the class to any other classes already in use.
*/
protected handleAttributes() {
const attributes = this.node.attributes;
const defaults = attributes.getAllDefaults();
const skip = SVGWrapper.skipAttributes;
for (const name of attributes.getExplicitNames()) {
if (skip[name] === false || (!(name in defaults) && !skip[name] &&
!this.adaptor.hasAttribute(this.element, name))) {
this.adaptor.setAttribute(this.element, name, attributes.getExplicit(name) as string);
}
}
if (attributes.get('class')) {
const names = (attributes.get('class') as string).trim().split(/ +/);
for (const name of names) {
this.adaptor.addClass(this.element, name);
}
}
}
/*******************************************************************/
/**
* @param {number} x The x-offset for the element
* @param {number} y The y-offset for the element
* @param {N} element The element to be placed
*/
public place(x: number, y: number, element: N = null) {
if (!(x || y)) return;
if (!element) {
element = this.element;
}
let transform = this.adaptor.getAttribute(element, 'transform') || '';
transform = 'translate(' + this.fixed(x) + ', ' + this.fixed(y) + ')' + (transform ? ' ' + transform : '');
this.adaptor.setAttribute(element, 'transform', transform);
}
/**
* @param {number} n The character number
* @param {number} x The x-position of the character
* @param {number} y The y-position of the character
* @param {N} parent The container for the character
* @param {string} variant The variant to use for the character
* @return {number} The width of the character
*/
public placeChar(n: number, x: number, y: number, parent: N, variant: string = null): number {
if (variant === null) {
variant = this.variant;
}
const C = n.toString(16).toUpperCase();
const [ , , w, data] = this.getVariantChar(variant, n);
if ('p' in data) {
const path = (data.p ? 'M' + data.p + 'Z' : '');
this.place(x, y, this.adaptor.append(parent, this.charNode(variant, C, path)));
} else if ('c' in data) {
const g = this.adaptor.append(parent, this.svg('g', {'data-c': C}));
this.place(x, y, g);
x = 0;
for (const n of this.unicodeChars(data.c, variant)) {
x += this.placeChar(n, x, y, g, variant);
}
} else if (data.unknown) {
const char = String.fromCodePoint(n);
const text = this.adaptor.append(parent, this.jax.unknownText(char, variant));
this.place(x, y, text);
return this.jax.measureTextNodeWithCache(text, char, variant).w;
}
return w;
}
/**
* @param {string} variant The name of the variant being used
* @param {string} C The hex string for the character code
* @param {string} path The data from the character
* @return {N} The <path> or <use> node for the glyph
*/
protected charNode(variant: string, C: string, path: string): N {
const cache = this.jax.options.fontCache;
return (cache !== 'none' ? this.useNode(variant, C, path) : this.pathNode(C, path));
}
/**
* @param {string} C The hex string for the character code
* @param {string} path The data from the character
* @return {N} The <path> for the glyph
*/
protected pathNode(C: string, path: string): N {
return this.svg('path', {'data-c': C, d: path});
}
/**
* @param {string} variant The name of the variant being used
* @param {string} C The hex string for the character code
* @param {string} path The data from the character
* @return {N} The <use> node for the glyph
*/
protected useNode(variant: string, C: string, path: string): N {
const use = this.svg('use');
const id = '#' + this.jax.fontCache.cachePath(variant, C, path);
this.adaptor.setAttribute(use, 'href', id, XLINKNS);
return use;
}
/*******************************************************************/
/**
* For debugging
*/
public drawBBox() {
let {w, h, d} = this.getBBox();
const box = this.svg('g', {style: {
opacity: .25
}}, [
this.svg('rect', {
fill: 'red',
height: this.fixed(h),
width: this.fixed(w)
}),
this.svg('rect', {
fill: 'green',
height: this.fixed(d),
width: this.fixed(w),
y: this.fixed(-d)
})
] as N[]);
const node = this.element || this.parent.element;
this.adaptor.append(node, box);
}
/*******************************************************************/
/*
* Easy access to some utility routines
*/
/**
* @param {string} type The tag name of the HTML node to be created
* @param {OptionList} def The properties to set for the created node
* @param {(N|T)[]} content The child nodes for the created HTML node
* @return {N} The generated HTML tree
*/
public html(type: string, def: OptionList = {}, content: (N | T)[] = []): N {
return this.jax.html(type, def, content);
}
/**
* @param {string} type The tag name of the svg node to be created
* @param {OptionList} def The properties to set for the created node
* @param {(N|T)[]} content The child nodes for the created SVG node
* @return {N} The generated SVG tree
*/
public svg(type: string, def: OptionList = {}, content: (N | T)[] = []): N {
return this.jax.svg(type, def, content);
}
/**
* @param {string} text The text from which to create an HTML text node
* @return {T} The generated text node with the given text
*/
public text(text: string): T {
return this.jax.text(text);
}
/**
* @param {number} x The dimension to display
* @param {number=} n The number of digits to display
* @return {string} The dimension with the given number of digits (minus trailing zeros)
*/
public fixed(x: number, n: number = 1): string {
return this.jax.fixed(x * 1000, n);
}
}