vexflow 5.0.0-alpha.2 → 5.0.0-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (112) hide show
  1. package/build/cjs/vexflow-bravura.js +2 -2
  2. package/build/cjs/vexflow-bravura.js.map +1 -1
  3. package/build/cjs/vexflow-core.js +2 -2
  4. package/build/cjs/vexflow-core.js.map +1 -1
  5. package/build/cjs/vexflow-debug-with-tests.js +4 -4
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +4 -4
  8. package/build/cjs/vexflow-debug.js.map +1 -1
  9. package/build/cjs/vexflow.js +2 -2
  10. package/build/cjs/vexflow.js.map +1 -1
  11. package/build/esm/src/version.js +3 -3
  12. package/package.json +1 -3
  13. package/entry/vexflow-bravura.ts +0 -25
  14. package/entry/vexflow-core.ts +0 -16
  15. package/entry/vexflow-debug-with-tests.ts +0 -64
  16. package/entry/vexflow-debug.ts +0 -54
  17. package/entry/vexflow.ts +0 -47
  18. package/src/accidental.ts +0 -570
  19. package/src/annotation.ts +0 -294
  20. package/src/articulation.ts +0 -383
  21. package/src/barnote.ts +0 -95
  22. package/src/beam.ts +0 -999
  23. package/src/bend.ts +0 -268
  24. package/src/boundingbox.ts +0 -108
  25. package/src/canvascontext.ts +0 -348
  26. package/src/chordsymbol.ts +0 -473
  27. package/src/clef.ts +0 -164
  28. package/src/clefnote.ts +0 -50
  29. package/src/crescendo.ts +0 -132
  30. package/src/curve.ts +0 -195
  31. package/src/dot.ts +0 -171
  32. package/src/easyscore.ts +0 -538
  33. package/src/element.ts +0 -692
  34. package/src/factory.ts +0 -694
  35. package/src/font.ts +0 -343
  36. package/src/fonts/academico.ts +0 -2
  37. package/src/fonts/academicobold.ts +0 -2
  38. package/src/fonts/bravura.ts +0 -2
  39. package/src/fonts/gonville.ts +0 -2
  40. package/src/fonts/petaluma.ts +0 -2
  41. package/src/fonts/petalumascript.ts +0 -2
  42. package/src/formatter.ts +0 -1107
  43. package/src/fraction.ts +0 -245
  44. package/src/frethandfinger.ts +0 -180
  45. package/src/ghostnote.ts +0 -75
  46. package/src/glyphnote.ts +0 -68
  47. package/src/glyphs.ts +0 -5880
  48. package/src/gracenote.ts +0 -146
  49. package/src/gracenotegroup.ts +0 -194
  50. package/src/gracetabnote.ts +0 -30
  51. package/src/index.ts +0 -90
  52. package/src/keymanager.ts +0 -153
  53. package/src/keysignature.ts +0 -272
  54. package/src/keysignote.ts +0 -46
  55. package/src/metrics.ts +0 -222
  56. package/src/modifier.ts +0 -216
  57. package/src/modifiercontext.ts +0 -177
  58. package/src/multimeasurerest.ts +0 -229
  59. package/src/music.ts +0 -443
  60. package/src/note.ts +0 -690
  61. package/src/notehead.ts +0 -158
  62. package/src/notesubgroup.ts +0 -88
  63. package/src/ornament.ts +0 -301
  64. package/src/parenthesis.ts +0 -89
  65. package/src/parser.ts +0 -263
  66. package/src/pedalmarking.ts +0 -273
  67. package/src/registry.ts +0 -146
  68. package/src/rendercontext.ts +0 -98
  69. package/src/renderer.ts +0 -166
  70. package/src/repeatnote.ts +0 -25
  71. package/src/stave.ts +0 -885
  72. package/src/stavebarline.ts +0 -231
  73. package/src/staveconnector.ts +0 -281
  74. package/src/stavehairpin.ts +0 -209
  75. package/src/staveline.ts +0 -343
  76. package/src/stavemodifier.ts +0 -93
  77. package/src/stavenote.ts +0 -1230
  78. package/src/staverepetition.ts +0 -148
  79. package/src/stavesection.ts +0 -54
  80. package/src/stavetempo.ts +0 -162
  81. package/src/stavetext.ts +0 -69
  82. package/src/stavetie.ts +0 -235
  83. package/src/stavevolta.ts +0 -66
  84. package/src/stem.ts +0 -225
  85. package/src/stemmablenote.ts +0 -248
  86. package/src/stringnumber.ts +0 -286
  87. package/src/strokes.ts +0 -214
  88. package/src/svgcontext.ts +0 -639
  89. package/src/system.ts +0 -327
  90. package/src/tables.ts +0 -796
  91. package/src/tabnote.ts +0 -449
  92. package/src/tabslide.ts +0 -115
  93. package/src/tabstave.ts +0 -31
  94. package/src/tabtie.ts +0 -44
  95. package/src/textbracket.ts +0 -231
  96. package/src/textdynamics.ts +0 -101
  97. package/src/textnote.ts +0 -175
  98. package/src/tickable.ts +0 -374
  99. package/src/tickcontext.ts +0 -280
  100. package/src/timesignature.ts +0 -229
  101. package/src/timesignote.ts +0 -45
  102. package/src/tremolo.ts +0 -51
  103. package/src/tuning.ts +0 -90
  104. package/src/tuplet.ts +0 -349
  105. package/src/typeguard.ts +0 -136
  106. package/src/util.ts +0 -169
  107. package/src/version.ts +0 -4
  108. package/src/vexflow.ts +0 -378
  109. package/src/vibrato.ts +0 -99
  110. package/src/vibratobracket.ts +0 -81
  111. package/src/voice.ts +0 -302
  112. package/src/web.ts +0 -35
package/src/svgcontext.ts DELETED
@@ -1,639 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
- // @author Gregory Ristow (2015)
4
-
5
- import { Element } from './element';
6
- import { Font, FontInfo, FontStyle, FontWeight } from './font';
7
- import { Metrics } from './metrics';
8
- import { RenderContext, TextMeasure } from './rendercontext';
9
- import { Tables } from './tables';
10
- import { normalizeAngle, prefix, RuntimeError } from './util';
11
-
12
- export type Attributes = {
13
- [name: string]: string | number | undefined;
14
- 'font-family'?: string;
15
- 'font-size'?: string | number;
16
- 'font-style'?: string;
17
- 'font-weight'?: string | number;
18
- scaleX?: number;
19
- scaleY?: number;
20
- };
21
-
22
- /** For a particular element type (e.g., rect), we will not apply certain presentation attributes. */
23
- const ATTRIBUTES_TO_IGNORE: Record<string /* element type */, Record<string, boolean> /* ignored attributes */> = {
24
- path: {
25
- x: true,
26
- y: true,
27
- width: true,
28
- height: true,
29
- 'font-family': true,
30
- 'font-weight': true,
31
- 'font-style': true,
32
- 'font-size': true,
33
- },
34
- rect: {
35
- 'font-family': true,
36
- 'font-weight': true,
37
- 'font-style': true,
38
- 'font-size': true,
39
- },
40
- text: {
41
- width: true,
42
- height: true,
43
- },
44
- };
45
-
46
- /** Create the SVG in the SVG namespace. */
47
- const SVG_NS = 'http://www.w3.org/2000/svg';
48
-
49
- const TWO_PI = 2 * Math.PI;
50
-
51
- export interface State {
52
- state: Attributes;
53
- attributes: Attributes;
54
- }
55
-
56
- /**
57
- * SVG rendering context with an API similar to CanvasRenderingContext2D.
58
- */
59
- export class SVGContext extends RenderContext {
60
- protected static measureTextElement = new Element();
61
-
62
- element: HTMLElement; // the parent DOM object
63
- svg: SVGSVGElement;
64
- width: number = 0;
65
- height: number = 0;
66
- path: string;
67
- pen: { x: number; y: number };
68
- attributes: Attributes;
69
- state: Attributes;
70
- stateStack: State[];
71
-
72
- // Always points to the current group.
73
- // Calls to add() or openGroup() will append the new element to `this.parent`.
74
- parent: SVGGElement;
75
- // The stack of groups.
76
- groups: SVGGElement[];
77
- // The stack of attributes associated with each group.
78
- protected groupAttributes: Attributes[];
79
-
80
- protected precision = 1;
81
-
82
- backgroundFillStyle: string = 'white';
83
-
84
- /** Formatted as CSS font shorthand (e.g., 'italic bold 12pt Arial') */
85
- protected fontCSSString: string = '';
86
-
87
- constructor(element: HTMLElement) {
88
- super();
89
- this.element = element;
90
-
91
- this.precision = Math.pow(10, Tables.RENDER_PRECISION_PLACES);
92
-
93
- // Create a SVG element and add it to the container element.
94
- const svg = this.create('svg');
95
- svg.setAttribute('pointer-events', 'none');
96
- this.element.appendChild(svg);
97
- this.svg = svg;
98
-
99
- this.parent = this.svg;
100
- this.groups = [this.svg];
101
-
102
- this.path = '';
103
- this.pen = { x: NaN, y: NaN };
104
-
105
- const defaultFontAttributes = {
106
- 'font-family': Metrics.get('fontFamily') as string,
107
- 'font-size': '10pt',
108
- 'font-weight': FontWeight.NORMAL,
109
- 'font-style': FontStyle.NORMAL,
110
- };
111
-
112
- this.state = {
113
- scaleX: 1,
114
- scaleY: 1,
115
- ...defaultFontAttributes,
116
- };
117
-
118
- this.attributes = {
119
- 'stroke-width': 1.0,
120
- 'stroke-dasharray': 'none',
121
- fill: 'black',
122
- stroke: 'black',
123
- shadowBlur: 0,
124
- shadowColor: 'black',
125
- ...defaultFontAttributes,
126
- };
127
-
128
- this.groupAttributes = [];
129
- this.applyAttributes(svg, this.attributes);
130
- this.groupAttributes.push({ ...this.attributes });
131
-
132
- this.stateStack = [];
133
- }
134
-
135
- protected round(n: number): number {
136
- return Math.round(n * this.precision) / this.precision;
137
- }
138
-
139
- /**
140
- * Use one of the overload signatures to create an SVG element of a specific type.
141
- * The last overload accepts an arbitrary string, and is identical to the
142
- * implementation signature.
143
- * Feel free to add new overloads for other SVG element types as required.
144
- */
145
- create(svgElementType: 'g'): SVGGElement;
146
- create(svgElementType: 'path'): SVGPathElement;
147
- create(svgElementType: 'rect'): SVGRectElement;
148
- create(svgElementType: 'svg'): SVGSVGElement;
149
- create(svgElementType: 'text'): SVGTextElement;
150
- create(svgElementType: string): SVGElement;
151
- create(svgElementType: string): SVGElement {
152
- return document.createElementNS(SVG_NS, svgElementType);
153
- }
154
-
155
- // Allow grouping elements in containers for interactivity.
156
- openGroup(cls?: string, id?: string): SVGGElement {
157
- const group = this.create('g');
158
- this.groups.push(group);
159
- this.parent.appendChild(group);
160
- this.parent = group;
161
- if (cls) group.setAttribute('class', prefix(cls));
162
- if (id) group.setAttribute('id', prefix(id));
163
-
164
- this.applyAttributes(group, this.attributes);
165
- this.groupAttributes.push({ ...this.groupAttributes[this.groupAttributes.length - 1], ...this.attributes });
166
- return group;
167
- }
168
-
169
- closeGroup(): void {
170
- this.groups.pop();
171
- this.groupAttributes.pop();
172
- this.parent = this.groups[this.groups.length - 1];
173
- }
174
-
175
- openRotation(angleDegrees: number, x: number, y: number) {
176
- this.openGroup().setAttribute('transform', `translate(${x},${y}) rotate(${angleDegrees}) translate(-${x},-${y})`);
177
- }
178
-
179
- closeRotation() {
180
- this.closeGroup();
181
- }
182
-
183
- add(elem: SVGElement): void {
184
- this.parent.appendChild(elem);
185
- }
186
-
187
- setFillStyle(style: string): this {
188
- this.attributes.fill = style;
189
- return this;
190
- }
191
-
192
- /**
193
- * Used to set the fill color for `clearRect()`. This allows us to simulate
194
- * cutting a "hole" into the SVG drawing.
195
- */
196
- setBackgroundFillStyle(style: string): this {
197
- this.backgroundFillStyle = style;
198
- return this;
199
- }
200
-
201
- setStrokeStyle(style: string): this {
202
- this.attributes.stroke = style;
203
- return this;
204
- }
205
-
206
- setShadowColor(color: string): this {
207
- this.attributes.shadowColor = color;
208
- return this;
209
- }
210
-
211
- /**
212
- * @param blur A non-negative float specifying the level of shadow blur, where 0
213
- * represents no blur and larger numbers represent increasingly more blur.
214
- * @returns this
215
- */
216
- setShadowBlur(blur: number): this {
217
- this.attributes.shadowBlur = blur;
218
- return this;
219
- }
220
-
221
- /**
222
- * @param width
223
- * @returns this
224
- */
225
- setLineWidth(width: number): this {
226
- this.attributes['stroke-width'] = width;
227
- return this;
228
- }
229
-
230
- /**
231
- * @param lineDash an array of integers in the form of [dash, space, dash, space, etc...]
232
- * @returns this
233
- *
234
- * See: [SVG `stroke-dasharray` attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray)
235
- */
236
- setLineDash(lineDash: number[]): this {
237
- if (Object.prototype.toString.call(lineDash) === '[object Array]') {
238
- this.attributes['stroke-dasharray'] = lineDash.join(',');
239
- return this;
240
- } else {
241
- throw new RuntimeError('ArgumentError', 'lineDash must be an array of integers.');
242
- }
243
- }
244
-
245
- /**
246
- * @param capType
247
- * @returns this
248
- */
249
- setLineCap(capType: CanvasLineCap): this {
250
- this.attributes['stroke-linecap'] = capType;
251
- return this;
252
- }
253
-
254
- // ### Sizing & Scaling Methods:
255
-
256
- // TODO (GCR): See note at scale() -- separate our internal
257
- // conception of pixel-based width/height from the style.width
258
- // and style.height properties eventually to allow users to
259
- // apply responsive sizing attributes to the SVG.
260
- resize(width: number, height: number): this {
261
- this.width = width;
262
- this.height = height;
263
- this.element.style.width = width.toString();
264
-
265
- this.svg.style.width = width.toString();
266
- this.svg.style.height = height.toString();
267
-
268
- const attributes = {
269
- width,
270
- height,
271
- };
272
-
273
- this.applyAttributes(this.svg, attributes);
274
- this.scale(this.state.scaleX as number, this.state.scaleY as number);
275
- return this;
276
- }
277
-
278
- scale(x: number, y: number): this {
279
- // uses viewBox to scale
280
- // TODO (GCR): we may at some point want to distinguish the
281
- // style.width / style.height properties that are applied to
282
- // the SVG object from our internal conception of the SVG
283
- // width/height. This would allow us to create automatically
284
- // scaling SVG's that filled their containers, for instance.
285
- //
286
- // As this isn't implemented in Canvas contexts,
287
- // I've left as is for now, but in using the viewBox to
288
- // handle internal scaling, am trying to make it possible
289
- // for us to eventually move in that direction.
290
-
291
- this.state.scaleX = this.state.scaleX ? this.state.scaleX * x : x;
292
- this.state.scaleY = this.state.scaleY ? this.state.scaleY * y : y;
293
- const visibleWidth = this.width / this.state.scaleX;
294
- const visibleHeight = this.height / this.state.scaleY;
295
- this.setViewBox(0, 0, visibleWidth, visibleHeight);
296
-
297
- return this;
298
- }
299
-
300
- /**
301
- * 1 arg: string in the "x y w h" format
302
- * 4 args: x:number, y:number, w:number, h:number
303
- */
304
- setViewBox(viewBoxOrMinX: string | number, minY?: number, width?: number, height?: number): void {
305
- if (typeof viewBoxOrMinX === 'string') {
306
- this.svg.setAttribute('viewBox', viewBoxOrMinX);
307
- } else {
308
- const viewBoxString = viewBoxOrMinX + ' ' + minY + ' ' + width + ' ' + height;
309
- this.svg.setAttribute('viewBox', viewBoxString);
310
- }
311
- }
312
-
313
- // ### Drawing helper methods:
314
-
315
- applyAttributes(element: SVGElement, attributes: Attributes): SVGElement {
316
- const attrNamesToIgnore = ATTRIBUTES_TO_IGNORE[element.nodeName];
317
- for (const attrName in attributes) {
318
- if (attrNamesToIgnore && attrNamesToIgnore[attrName]) {
319
- continue;
320
- }
321
- if (
322
- attributes[attrName] &&
323
- (this.groupAttributes.length === 0 ||
324
- attributes[attrName] != this.groupAttributes[this.groupAttributes.length - 1][attrName])
325
- )
326
- element.setAttributeNS(null, attrName, attributes[attrName] as string);
327
- }
328
-
329
- return element;
330
- }
331
-
332
- // ### Shape & Path Methods:
333
-
334
- clear(): void {
335
- // Clear the SVG by removing all inner children.
336
-
337
- // (This approach is usually slightly more efficient
338
- // than removing the old SVG & adding a new one to
339
- // the container element, since it does not cause the
340
- // container to resize twice. Also, the resize
341
- // triggered by removing the entire SVG can trigger
342
- // a touchcancel event when the element resizes away
343
- // from a touch point.)
344
-
345
- while (this.svg.lastChild) {
346
- this.svg.removeChild(this.svg.lastChild);
347
- }
348
-
349
- // Replace the viewbox attribute we just removed.
350
- this.scale(this.state.scaleX as number, this.state.scaleY as number);
351
- }
352
-
353
- // ## Rectangles:
354
- rect(x: number, y: number, width: number, height: number, attributes?: Attributes): this {
355
- // Avoid invalid negative height attributes by flipping the rectangle on its head:
356
- if (height < 0) {
357
- y += height;
358
- height *= -1;
359
- }
360
-
361
- const rectangle = this.create('rect');
362
- attributes = attributes ?? { fill: 'none', 'stroke-width': this.attributes['stroke-width'], stroke: 'black' };
363
- x = this.round(x);
364
- y = this.round(y);
365
- width = this.round(width);
366
- height = this.round(height);
367
- this.applyAttributes(rectangle, { x, y, width, height, ...attributes });
368
- this.add(rectangle);
369
- return this;
370
- }
371
-
372
- fillRect(x: number, y: number, width: number, height: number): this {
373
- const attributes = { fill: this.attributes.fill, stroke: 'none' };
374
- this.rect(x, y, width, height, attributes);
375
- return this;
376
- }
377
-
378
- pointerRect(x: number, y: number, width: number, height: number): this {
379
- const attributes = { opacity: '0', 'pointer-events': 'auto' };
380
- this.rect(x, y, width, height, attributes);
381
- return this;
382
- }
383
-
384
- clearRect(x: number, y: number, width: number, height: number): this {
385
- // Currently this fills a rect with the backgroundFillStyle, rather
386
- // than "cut a hole" into the existing shapes.
387
- //
388
- // Since tabNote seems to be the only module that makes use of this
389
- // it may be worth creating a separate tabStave that would
390
- // draw lines around locations of tablature fingering.
391
- this.rect(x, y, width, height, { fill: this.backgroundFillStyle, stroke: 'none' });
392
- return this;
393
- }
394
-
395
- // ## Paths:
396
-
397
- beginPath(): this {
398
- this.path = '';
399
- this.pen.x = NaN;
400
- this.pen.y = NaN;
401
- return this;
402
- }
403
-
404
- moveTo(x: number, y: number): this {
405
- x = this.round(x);
406
- y = this.round(y);
407
- this.path += 'M' + x + ' ' + y;
408
- this.pen.x = x;
409
- this.pen.y = y;
410
- return this;
411
- }
412
-
413
- lineTo(x: number, y: number): this {
414
- x = this.round(x);
415
- y = this.round(y);
416
- this.path += 'L' + x + ' ' + y;
417
- this.pen.x = x;
418
- this.pen.y = y;
419
- return this;
420
- }
421
-
422
- bezierCurveTo(x1: number, y1: number, x2: number, y2: number, x: number, y: number): this {
423
- x = this.round(x);
424
- y = this.round(y);
425
- x1 = this.round(x1);
426
- y1 = this.round(y1);
427
- x2 = this.round(x2);
428
- y2 = this.round(y2);
429
- this.path += 'C' + x1 + ' ' + y1 + ',' + x2 + ' ' + y2 + ',' + x + ' ' + y;
430
- this.pen.x = x;
431
- this.pen.y = y;
432
- return this;
433
- }
434
-
435
- quadraticCurveTo(x1: number, y1: number, x: number, y: number): this {
436
- x = this.round(x);
437
- y = this.round(y);
438
- x1 = this.round(x1);
439
- y1 = this.round(y1);
440
- this.path += 'Q' + x1 + ' ' + y1 + ',' + x + ' ' + y;
441
- this.pen.x = x;
442
- this.pen.y = y;
443
- return this;
444
- }
445
-
446
- arc(x: number, y: number, radius: number, startAngle: number, endAngle: number, counterclockwise: boolean): this {
447
- let x0 = x + radius * Math.cos(startAngle);
448
- let y0 = y + radius * Math.sin(startAngle);
449
- x0 = this.round(x0);
450
- y0 = this.round(y0);
451
-
452
- // svg behavior different from canvas. Don't normalize angles if
453
- // we are drawing a circle because they both normalize to 0
454
- const tmpStartTest = normalizeAngle(startAngle);
455
- const tmpEndTest = normalizeAngle(endAngle);
456
- if (
457
- (!counterclockwise && endAngle - startAngle >= TWO_PI) ||
458
- (counterclockwise && startAngle - endAngle >= TWO_PI) ||
459
- tmpStartTest === tmpEndTest
460
- ) {
461
- let x1 = x + radius * Math.cos(startAngle + Math.PI);
462
- let y1 = y + radius * Math.sin(startAngle + Math.PI);
463
- // There's no way to specify a completely circular arc in SVG so we have to
464
- // use two semi-circular arcs.
465
- x1 = this.round(x1);
466
- y1 = this.round(y1);
467
- radius = this.round(radius);
468
- this.path += `M${x0} ${y0} A${radius} ${radius} 0 0 0 ${x1} ${y1} `;
469
- this.path += `A${radius} ${radius} 0 0 0 ${x0} ${y0}`;
470
- this.pen.x = x0;
471
- this.pen.y = y0;
472
- } else {
473
- let x1 = x + radius * Math.cos(endAngle);
474
- let y1 = y + radius * Math.sin(endAngle);
475
-
476
- startAngle = tmpStartTest;
477
- endAngle = tmpEndTest;
478
- let large: boolean;
479
- if (Math.abs(endAngle - startAngle) < Math.PI) {
480
- large = counterclockwise;
481
- } else {
482
- large = !counterclockwise;
483
- }
484
- if (startAngle > endAngle) {
485
- large = !large;
486
- }
487
-
488
- const sweep = !counterclockwise;
489
-
490
- x1 = this.round(x1);
491
- y1 = this.round(y1);
492
- radius = this.round(radius);
493
- this.path += `M${x0} ${y0} A${radius} ${radius} 0 ${+large} ${+sweep} ${x1} ${y1}`;
494
- this.pen.x = x1;
495
- this.pen.y = y1;
496
- }
497
- return this;
498
- }
499
-
500
- closePath(): this {
501
- this.path += 'Z';
502
- return this;
503
- }
504
-
505
- #getShadowStyle(): string {
506
- // A CSS drop-shadow filter blur looks different than a canvas shadowBlur
507
- // of the same radius, so we scale the drop-shadow radius here to make it
508
- // look close to the canvas shadow.
509
- return `filter: drop-shadow(0 0 ${(this.attributes.shadowBlur as number) / 1.5}px ${this.attributes.shadowColor})`;
510
- }
511
-
512
- fill(attributes?: Attributes): this {
513
- const path = this.create('path');
514
- if (typeof attributes === 'undefined') {
515
- attributes = { ...this.attributes, stroke: 'none' };
516
- }
517
-
518
- attributes.d = this.path;
519
- if ((this.attributes.shadowBlur as number) > 0) {
520
- attributes.style = this.#getShadowStyle();
521
- }
522
-
523
- this.applyAttributes(path, attributes);
524
- this.add(path);
525
- return this;
526
- }
527
-
528
- stroke(): this {
529
- const path = this.create('path');
530
- const attributes: Attributes = {
531
- ...this.attributes,
532
- fill: 'none',
533
- d: this.path,
534
- };
535
- if ((this.attributes.shadowBlur as number) > 0) {
536
- attributes.style = this.#getShadowStyle();
537
- }
538
-
539
- this.applyAttributes(path, attributes);
540
- this.add(path);
541
- return this;
542
- }
543
-
544
- // ## Text Methods:
545
- measureText(text: string): TextMeasure {
546
- SVGContext.measureTextElement.setText(text);
547
- SVGContext.measureTextElement.setFont(
548
- this.attributes['font-family'],
549
- this.attributes['font-size'],
550
- this.attributes['font-weight'],
551
- this.attributes['font-style']
552
- );
553
- const bb = SVGContext.measureTextElement.getBoundingBox();
554
- return { x: bb.x, y: bb.y, width: bb.w, height: bb.h };
555
- }
556
-
557
- fillText(text: string, x: number, y: number): this {
558
- if (!text || text.length <= 0) {
559
- return this;
560
- }
561
- x = this.round(x);
562
- y = this.round(y);
563
- const attributes: Attributes = {
564
- ...this.attributes,
565
- stroke: 'none',
566
- x,
567
- y,
568
- };
569
-
570
- const txt = this.create('text');
571
- txt.textContent = text;
572
- this.applyAttributes(txt, attributes);
573
- this.add(txt);
574
- return this;
575
- }
576
-
577
- save(): this {
578
- this.stateStack.push({
579
- state: structuredClone(this.state),
580
- attributes: structuredClone(this.attributes),
581
- });
582
- return this;
583
- }
584
-
585
- restore(): this {
586
- const savedState = this.stateStack.pop();
587
- if (savedState) {
588
- const state = savedState;
589
- this.state = structuredClone(state.state);
590
- this.attributes = structuredClone(state.attributes);
591
- }
592
- return this;
593
- }
594
-
595
- set fillStyle(style: string | CanvasGradient | CanvasPattern) {
596
- this.setFillStyle(style as string);
597
- }
598
-
599
- get fillStyle(): string | CanvasGradient | CanvasPattern {
600
- return this.attributes.fill as string;
601
- }
602
-
603
- set strokeStyle(style: string | CanvasGradient | CanvasPattern) {
604
- this.setStrokeStyle(style as string);
605
- }
606
-
607
- get strokeStyle(): string | CanvasGradient | CanvasPattern {
608
- return this.attributes.stroke as string;
609
- }
610
-
611
- /**
612
- * @param f is 1) a `FontInfo` object or
613
- * 2) a string formatted as CSS font shorthand (e.g., 'bold 10pt Arial') or
614
- * 3) a string representing the font family (one of `size`, `weight`, or `style` must also be provided).
615
- * @param size a string specifying the font size and unit (e.g., '16pt'), or a number (the unit is assumed to be 'pt').
616
- * @param weight is a string (e.g., 'bold', 'normal') or a number (100, 200, ... 900). It is inserted
617
- * into the font-weight attribute (e.g., font-weight="bold")
618
- * @param style is a string (e.g., 'italic', 'normal') that is inserted into the
619
- * font-style attribute (e.g., font-style="italic")
620
- */
621
- setFont(f?: string | FontInfo, size?: string | number, weight?: string | number, style?: string): this {
622
- const fontInfo = Font.validate(f, size, weight, style);
623
- this.fontCSSString = Font.toCSSString(fontInfo);
624
- const fontAttributes = {
625
- 'font-family': fontInfo.family,
626
- 'font-size': fontInfo.size,
627
- 'font-weight': fontInfo.weight,
628
- 'font-style': fontInfo.style,
629
- };
630
- this.attributes = { ...this.attributes, ...fontAttributes };
631
- this.state = { ...this.state, ...fontAttributes };
632
- return this;
633
- }
634
-
635
- /** Return a string of the form `'italic bold 15pt Arial'` */
636
- getFont(): string {
637
- return this.fontCSSString;
638
- }
639
- }