vexflow 5.0.0-alpha.2 → 5.0.0-alpha.4

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 (157) 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 +528 -492
  6. package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
  7. package/build/cjs/vexflow-debug.js +460 -452
  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/accidental.js +22 -28
  12. package/build/esm/src/beam.js +24 -42
  13. package/build/esm/src/clefnote.js +6 -1
  14. package/build/esm/src/element.js +83 -101
  15. package/build/esm/src/fonts/gonville.js +1 -1
  16. package/build/esm/src/formatter.js +9 -17
  17. package/build/esm/src/fraction.js +15 -22
  18. package/build/esm/src/glyphnote.js +3 -2
  19. package/build/esm/src/metrics.js +3 -0
  20. package/build/esm/src/modifier.js +6 -21
  21. package/build/esm/src/multimeasurerest.js +16 -25
  22. package/build/esm/src/note.js +7 -0
  23. package/build/esm/src/registry.js +5 -19
  24. package/build/esm/src/stave.js +4 -6
  25. package/build/esm/src/stavenote.js +26 -40
  26. package/build/esm/src/stavetempo.js +4 -11
  27. package/build/esm/src/stavetie.js +82 -36
  28. package/build/esm/src/svgcontext.js +5 -12
  29. package/build/esm/src/tickable.js +13 -26
  30. package/build/esm/src/tickcontext.js +20 -2
  31. package/build/esm/src/version.js +3 -3
  32. package/build/esm/src/vibrato.js +6 -1
  33. package/build/esm/tests/clef_tests.js +5 -1
  34. package/build/esm/tests/glyphnote_tests.js +6 -0
  35. package/build/esm/tests/stavetie_tests.js +29 -0
  36. package/build/esm/tests/tabstave_tests.js +1 -0
  37. package/build/esm/tests/vexflow_test_helpers.js +25 -39
  38. package/build/types/src/accidental.d.ts +19 -1
  39. package/build/types/src/annotation.d.ts +1 -1
  40. package/build/types/src/beam.d.ts +12 -1
  41. package/build/types/src/clefnote.d.ts +2 -0
  42. package/build/types/src/element.d.ts +13 -3
  43. package/build/types/src/fonts/gonville.d.ts +1 -1
  44. package/build/types/src/formatter.d.ts +5 -3
  45. package/build/types/src/fraction.d.ts +12 -7
  46. package/build/types/src/modifier.d.ts +2 -1
  47. package/build/types/src/multimeasurerest.d.ts +4 -1
  48. package/build/types/src/note.d.ts +2 -0
  49. package/build/types/src/registry.d.ts +1 -1
  50. package/build/types/src/stavenote.d.ts +2 -1
  51. package/build/types/src/stavetempo.d.ts +3 -3
  52. package/build/types/src/stavetie.d.ts +39 -2
  53. package/build/types/src/svgcontext.d.ts +1 -1
  54. package/build/types/src/tickable.d.ts +2 -1
  55. package/build/types/src/tickcontext.d.ts +30 -0
  56. package/package.json +2 -4
  57. package/tests/flow.html +2 -2
  58. package/entry/vexflow-bravura.ts +0 -25
  59. package/entry/vexflow-core.ts +0 -16
  60. package/entry/vexflow-debug-with-tests.ts +0 -64
  61. package/entry/vexflow-debug.ts +0 -54
  62. package/entry/vexflow.ts +0 -47
  63. package/src/accidental.ts +0 -570
  64. package/src/annotation.ts +0 -294
  65. package/src/articulation.ts +0 -383
  66. package/src/barnote.ts +0 -95
  67. package/src/beam.ts +0 -999
  68. package/src/bend.ts +0 -268
  69. package/src/boundingbox.ts +0 -108
  70. package/src/canvascontext.ts +0 -348
  71. package/src/chordsymbol.ts +0 -473
  72. package/src/clef.ts +0 -164
  73. package/src/clefnote.ts +0 -50
  74. package/src/crescendo.ts +0 -132
  75. package/src/curve.ts +0 -195
  76. package/src/dot.ts +0 -171
  77. package/src/easyscore.ts +0 -538
  78. package/src/element.ts +0 -692
  79. package/src/factory.ts +0 -694
  80. package/src/font.ts +0 -343
  81. package/src/fonts/academico.ts +0 -2
  82. package/src/fonts/academicobold.ts +0 -2
  83. package/src/fonts/bravura.ts +0 -2
  84. package/src/fonts/gonville.ts +0 -2
  85. package/src/fonts/petaluma.ts +0 -2
  86. package/src/fonts/petalumascript.ts +0 -2
  87. package/src/formatter.ts +0 -1107
  88. package/src/fraction.ts +0 -245
  89. package/src/frethandfinger.ts +0 -180
  90. package/src/ghostnote.ts +0 -75
  91. package/src/glyphnote.ts +0 -68
  92. package/src/glyphs.ts +0 -5880
  93. package/src/gracenote.ts +0 -146
  94. package/src/gracenotegroup.ts +0 -194
  95. package/src/gracetabnote.ts +0 -30
  96. package/src/index.ts +0 -90
  97. package/src/keymanager.ts +0 -153
  98. package/src/keysignature.ts +0 -272
  99. package/src/keysignote.ts +0 -46
  100. package/src/metrics.ts +0 -222
  101. package/src/modifier.ts +0 -216
  102. package/src/modifiercontext.ts +0 -177
  103. package/src/multimeasurerest.ts +0 -229
  104. package/src/music.ts +0 -443
  105. package/src/note.ts +0 -690
  106. package/src/notehead.ts +0 -158
  107. package/src/notesubgroup.ts +0 -88
  108. package/src/ornament.ts +0 -301
  109. package/src/parenthesis.ts +0 -89
  110. package/src/parser.ts +0 -263
  111. package/src/pedalmarking.ts +0 -273
  112. package/src/registry.ts +0 -146
  113. package/src/rendercontext.ts +0 -98
  114. package/src/renderer.ts +0 -166
  115. package/src/repeatnote.ts +0 -25
  116. package/src/stave.ts +0 -885
  117. package/src/stavebarline.ts +0 -231
  118. package/src/staveconnector.ts +0 -281
  119. package/src/stavehairpin.ts +0 -209
  120. package/src/staveline.ts +0 -343
  121. package/src/stavemodifier.ts +0 -93
  122. package/src/stavenote.ts +0 -1230
  123. package/src/staverepetition.ts +0 -148
  124. package/src/stavesection.ts +0 -54
  125. package/src/stavetempo.ts +0 -162
  126. package/src/stavetext.ts +0 -69
  127. package/src/stavetie.ts +0 -235
  128. package/src/stavevolta.ts +0 -66
  129. package/src/stem.ts +0 -225
  130. package/src/stemmablenote.ts +0 -248
  131. package/src/stringnumber.ts +0 -286
  132. package/src/strokes.ts +0 -214
  133. package/src/svgcontext.ts +0 -639
  134. package/src/system.ts +0 -327
  135. package/src/tables.ts +0 -796
  136. package/src/tabnote.ts +0 -449
  137. package/src/tabslide.ts +0 -115
  138. package/src/tabstave.ts +0 -31
  139. package/src/tabtie.ts +0 -44
  140. package/src/textbracket.ts +0 -231
  141. package/src/textdynamics.ts +0 -101
  142. package/src/textnote.ts +0 -175
  143. package/src/tickable.ts +0 -374
  144. package/src/tickcontext.ts +0 -280
  145. package/src/timesignature.ts +0 -229
  146. package/src/timesignote.ts +0 -45
  147. package/src/tremolo.ts +0 -51
  148. package/src/tuning.ts +0 -90
  149. package/src/tuplet.ts +0 -349
  150. package/src/typeguard.ts +0 -136
  151. package/src/util.ts +0 -169
  152. package/src/version.ts +0 -4
  153. package/src/vexflow.ts +0 -378
  154. package/src/vibrato.ts +0 -99
  155. package/src/vibratobracket.ts +0 -81
  156. package/src/voice.ts +0 -302
  157. package/src/web.ts +0 -35
package/src/element.ts DELETED
@@ -1,692 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
- // @author Mohit Cheppudira
4
-
5
- import { BoundingBox } from './boundingbox';
6
- import { Font, FontInfo } from './font';
7
- import { Metrics } from './metrics';
8
- import { Registry } from './registry';
9
- import { RenderContext } from './rendercontext';
10
- import { Category } from './typeguard';
11
- import { defined, prefix, RuntimeError } from './util';
12
-
13
- /** Element attributes. */
14
- export interface ElementAttributes {
15
- [name: string]: string | undefined;
16
- id: string;
17
- type: string;
18
- class: string;
19
- }
20
-
21
- /** Element style. */
22
- export interface ElementStyle {
23
- /**
24
- * CSS color used for the shadow.
25
- *
26
- * Examples: 'red', '#ff0000', '#ff000010', 'rgb(255,0,0)'
27
- *
28
- * See [CSS Legal Color Values](https://www.w3schools.com/cssref/css_colors_legal.asp)
29
- */
30
- shadowColor?: string;
31
- /**
32
- * Level of blur applied to shadows.
33
- *
34
- * Values that are not finite numbers greater than or equal to zero are ignored.
35
- */
36
- shadowBlur?: number;
37
- /**
38
- * CSS color used with context fill command.
39
- *
40
- * Examples: 'red', '#ff0000', '#ff000010', 'rgb(255,0,0)'
41
- *
42
- * See [CSS Legal Color Values](https://www.w3schools.com/cssref/css_colors_legal.asp)
43
- */
44
- fillStyle?: string;
45
- /**
46
- * CSS color used with context stroke command.
47
- *
48
- * Examples: 'red', '#ff0000', '#ff000010', 'rgb(255,0,0)'
49
- *
50
- * See [CSS Legal Color Values](https://www.w3schools.com/cssref/css_colors_legal.asp)
51
- */
52
- strokeStyle?: string;
53
- /**
54
- * Line width, 1.0 by default.
55
- */
56
- lineWidth?: number;
57
- /**
58
- * See: [SVG `stroke-dasharray` attribute](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/stroke-dasharray)
59
- */
60
- lineDash?: string;
61
- }
62
-
63
- /**
64
- * Element implements a generic base class for VexFlow, with implementations
65
- * of general functions and properties that can be inherited by all VexFlow elements.
66
- *
67
- * The Element handles style and font properties for the Element and any child
68
- * elements, along with working with the Registry to create unique ids.
69
- *
70
- * The `text` is a series of unicode characters (including SMuFL codes).
71
- * The `textFont` property contains information required to style the text (i.e., font family, size, weight, and style).
72
- * This font family is a comma separated list of fonts.
73
- * The method `measureText` calculates the `textMetrics`, `boundingBox`, `height` and `width` of the `text`.
74
- * The method `renderText(...)` will render the text using the provided context and coordinates,
75
- * taking `xShift` and `yShift` into account.
76
- */
77
- export class Element {
78
- static get CATEGORY(): string {
79
- return Category.Element;
80
- }
81
-
82
- // Element objects keep a list of children that they are responsible for.
83
- // Children inherit the style from their parents (see: setGroupStyle(s)).
84
- protected children: Element[] = [];
85
- protected static ID: number = 1000;
86
- protected static newID(): string {
87
- return `auto${Element.ID++}`;
88
- }
89
-
90
- /** Canvas used to measure text. See measureText(): TextMetrics. */
91
- static #txtCanvas?: HTMLCanvasElement | OffscreenCanvas;
92
-
93
- // Note: Canvas is node-canvas.
94
- // https://www.npmjs.com/package/canvas
95
- static setTextMeasurementCanvas(canvas: HTMLCanvasElement | OffscreenCanvas /* | Canvas */): void {
96
- Element.#txtCanvas = canvas;
97
- }
98
-
99
- static getTextMeasurementCanvas(): HTMLCanvasElement | OffscreenCanvas | undefined {
100
- let txtCanvas: HTMLCanvasElement | OffscreenCanvas | undefined = Element.#txtCanvas;
101
- // Create the canvas element that will be used to measure text.
102
- if (!txtCanvas) {
103
- if (typeof document !== 'undefined') {
104
- txtCanvas = document.createElement('canvas'); // Defaults to 300 x 150. See: https://www.w3.org/TR/2012/WD-html5-author-20120329/the-canvas-element.html#the-canvas-element
105
- } else if (typeof OffscreenCanvas !== 'undefined') {
106
- txtCanvas = new OffscreenCanvas(300, 150);
107
- }
108
- Element.#txtCanvas = txtCanvas;
109
- }
110
- return txtCanvas;
111
- }
112
-
113
- #context?: RenderContext;
114
- #attrs: ElementAttributes;
115
-
116
- protected rendered: boolean;
117
- protected style?: ElementStyle;
118
- protected registry?: Registry;
119
-
120
- #fontInfo: Required<FontInfo>;
121
- #fontScale: number;
122
- #text = '';
123
- #metricsValid = false;
124
- #textMetrics: TextMetrics = {
125
- fontBoundingBoxAscent: 0,
126
- fontBoundingBoxDescent: 0,
127
- actualBoundingBoxAscent: 0,
128
- actualBoundingBoxDescent: 0,
129
- actualBoundingBoxLeft: 0,
130
- actualBoundingBoxRight: 0,
131
- width: 0,
132
- alphabeticBaseline: 0,
133
- emHeightAscent: 0,
134
- emHeightDescent: 0,
135
- hangingBaseline: 0,
136
- ideographicBaseline: 0,
137
- };
138
-
139
- #height: number = 0;
140
- #width: number = 0;
141
- protected xShift: number = 0;
142
- protected yShift: number = 0;
143
- protected x: number = 0;
144
- protected y: number = 0;
145
-
146
- constructor(category?: string) {
147
- this.#attrs = {
148
- id: Element.newID(),
149
- type: category ?? (<typeof Element>this.constructor).CATEGORY,
150
- class: '',
151
- };
152
-
153
- this.rendered = false;
154
- this.#fontInfo = Metrics.getFontInfo(this.#attrs.type);
155
- this.#fontScale = Metrics.get(`${this.#attrs.type}.fontScale`);
156
-
157
- // If a default registry exist, then register with it right away.
158
- Registry.getDefaultRegistry()?.register(this);
159
- }
160
-
161
- /**
162
- * Adds a child Element to the Element, which lets it inherit the
163
- * same style as the parent when setGroupStyle() is called.
164
- *
165
- * Examples of children are noteheads and stems. Modifiers such
166
- * as Accidentals are generally not set as children.
167
- *
168
- * Note that StaveNote calls setGroupStyle() when setStyle() is called.
169
- */
170
- addChildElement(child: Element): this {
171
- this.children.push(child);
172
- return this;
173
- }
174
-
175
- getCategory(): string {
176
- return this.#attrs.type;
177
- }
178
-
179
- /**
180
- * Set the element style used to render.
181
- *
182
- * Example:
183
- * ```typescript
184
- * element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
185
- * element.draw();
186
- * ```
187
- * Note: If the element draws additional sub-elements (ie.: Modifiers in a Stave),
188
- * the style can be applied to all of them by means of the context:
189
- * ```typescript
190
- * element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
191
- * element.getContext().setFillStyle('red');
192
- * element.getContext().setStrokeStyle('red');
193
- * element.draw();
194
- * ```
195
- * or using drawWithStyle:
196
- * ```typescript
197
- * element.setStyle({ fillStyle: 'red', strokeStyle: 'red' });
198
- * element.drawWithStyle();
199
- * ```
200
- */
201
- setStyle(style: ElementStyle | undefined): this {
202
- this.style = style;
203
- return this;
204
- }
205
-
206
- /** Set the element & associated children style used for rendering. */
207
- setGroupStyle(style: ElementStyle): this {
208
- this.style = style;
209
- this.children.forEach((child) => child.setGroupStyle(style));
210
- return this;
211
- }
212
-
213
- /** Get the element style used for rendering. */
214
- getStyle(): ElementStyle | undefined {
215
- return this.style;
216
- }
217
-
218
- /** Apply the element style to `context`. */
219
- applyStyle(
220
- context: RenderContext | undefined = this.#context,
221
- style: ElementStyle | undefined = this.getStyle()
222
- ): this {
223
- if (!style) return this;
224
- if (!context) return this;
225
-
226
- context.save();
227
- if (style.shadowColor) context.setShadowColor(style.shadowColor);
228
- if (style.shadowBlur) context.setShadowBlur(style.shadowBlur);
229
- if (style.fillStyle) context.setFillStyle(style.fillStyle);
230
- if (style.strokeStyle) context.setStrokeStyle(style.strokeStyle);
231
- if (style.lineWidth) context.setLineWidth(style.lineWidth);
232
- if (style.lineDash) context.setLineDash(style.lineDash.split(' ').map(Number));
233
-
234
- return this;
235
- }
236
-
237
- /** Restore the style of `context`. */
238
- restoreStyle(
239
- context: RenderContext | undefined = this.#context,
240
- style: ElementStyle | undefined = this.getStyle()
241
- ): this {
242
- if (!style) return this;
243
- if (!context) return this;
244
- context.restore();
245
- return this;
246
- }
247
-
248
- /**
249
- * Draw the element and all its sub-elements (ie.: Modifiers in a Stave)
250
- * with the element's style (see `getStyle()` and `setStyle()`)
251
- */
252
- drawWithStyle(): void {
253
- this.checkContext();
254
- this.applyStyle();
255
- this.draw();
256
- this.restoreStyle();
257
- }
258
-
259
- /** Draw an element. */
260
- draw(): void {
261
- throw new RuntimeError('Element', 'Draw not defined');
262
- }
263
-
264
- /** Check if it has a class label (An element can have multiple class labels). */
265
- hasClass(className: string): boolean {
266
- if (!this.#attrs.class) return false;
267
- return this.#attrs.class?.split(' ').indexOf(className) !== -1;
268
- }
269
-
270
- /** Add a class label (An element can have multiple class labels). */
271
- addClass(className: string): this {
272
- if (this.hasClass(className)) return this;
273
- if (!this.#attrs.class) this.#attrs.class = `${className}`;
274
- else this.#attrs.class = `${this.#attrs.class} ${className}`;
275
- this.registry?.onUpdate({
276
- id: this.#attrs.id,
277
- name: 'class',
278
- value: className,
279
- oldValue: undefined,
280
- });
281
- return this;
282
- }
283
-
284
- /** Remove a class label (An element can have multiple class labels). */
285
- removeClass(className: string): this {
286
- if (!this.hasClass(className)) return this;
287
- const arr = this.#attrs.class?.split(' ');
288
- if (arr) {
289
- arr.splice(arr.indexOf(className));
290
- this.#attrs.class = arr.join(' ');
291
- }
292
- this.registry?.onUpdate({
293
- id: this.#attrs.id,
294
- name: 'class',
295
- value: undefined,
296
- oldValue: className,
297
- });
298
- return this;
299
- }
300
-
301
- /** Call back from registry after the element is registered. */
302
- onRegister(registry: Registry): this {
303
- this.registry = registry;
304
- return this;
305
- }
306
-
307
- /** Return the rendered status. */
308
- isRendered(): boolean {
309
- return this.rendered;
310
- }
311
-
312
- /** Set the rendered status. */
313
- setRendered(rendered = true): this {
314
- this.rendered = rendered;
315
- return this;
316
- }
317
-
318
- /** Return the element attributes. */
319
- getAttributes(): ElementAttributes {
320
- return this.#attrs;
321
- }
322
-
323
- /** Return an attribute, such as 'id', 'type' or 'class'. */
324
- // eslint-disable-next-line
325
- getAttribute(name: string): any {
326
- return this.#attrs[name];
327
- }
328
-
329
- /** Return associated SVGElement. */
330
- getSVGElement(suffix: string = ''): SVGElement | undefined {
331
- const id = prefix(this.#attrs.id + suffix);
332
- const element = document.getElementById(id);
333
- if (element) return element as unknown as SVGElement;
334
- }
335
-
336
- /** Set an attribute such as 'id', 'class', or 'type'. */
337
- setAttribute(name: string, value: string | undefined): this {
338
- const oldID = this.#attrs.id;
339
- const oldValue = this.#attrs[name];
340
- this.#attrs[name] = value;
341
- // Register with old id to support id changes.
342
- this.registry?.onUpdate({ id: oldID, name, value, oldValue });
343
- return this;
344
- }
345
-
346
- /** Get the boundingBox. */
347
- getBoundingBox(): BoundingBox {
348
- return new BoundingBox(
349
- this.x + this.xShift,
350
- this.y + this.yShift - this.textMetrics.actualBoundingBoxAscent,
351
- this.width,
352
- this.height
353
- );
354
- }
355
-
356
- /** Return the context, such as an SVGContext or CanvasContext object. */
357
- getContext(): RenderContext | undefined {
358
- return this.#context;
359
- }
360
-
361
- /** Set the context to an SVGContext or CanvasContext object */
362
- setContext(context?: RenderContext): this {
363
- this.#context = context;
364
- return this;
365
- }
366
-
367
- /** Validate and return the rendering context. */
368
- checkContext(): RenderContext {
369
- return defined(this.#context, 'NoContext', 'No rendering context attached to instance.');
370
- }
371
-
372
- //////////////////////////////////////////////////////////////////////////////////////////////////
373
- // Font Handling
374
-
375
- /**
376
- * Provide a CSS compatible font string (e.g., 'bold 16px Arial') that will be applied
377
- * to text (not glyphs).
378
- */
379
- set font(f: string) {
380
- this.setFont(f);
381
- }
382
-
383
- /** Returns the CSS compatible font string for the text font. */
384
- get font(): string {
385
- return Font.toCSSString(this.#fontInfo);
386
- }
387
-
388
- /**
389
- * Set the element's text font family, size, weight, style
390
- * (e.g., `Arial`, `10pt`, `bold`, `italic`).
391
- *
392
- * This attribute does not determine the font used for musical Glyphs like treble clefs.
393
- *
394
- * @param font is 1) a `FontInfo` object or
395
- * 2) a string formatted as CSS font shorthand (e.g., 'bold 10pt Arial') or
396
- * 3) a string representing the font family (at least one of `size`, `weight`, or `style` must also be provided).
397
- * @param size a string specifying the font size and unit (e.g., '16pt'), or a number (the unit is assumed to be 'pt').
398
- * @param weight is a string (e.g., 'bold', 'normal') or a number (100, 200, ... 900).
399
- * @param style is a string (e.g., 'italic', 'normal').
400
- * If no arguments are provided, then the font is set to the default font.
401
- * Each Element subclass may specify its own default by overriding the static `TEXT_FONT` property.
402
- */
403
- setFont(font?: string | FontInfo, size?: string | number, weight?: string | number, style?: string): this {
404
- const defaultTextFont: Required<FontInfo> = Metrics.getFontInfo(this.#attrs.type);
405
-
406
- const fontIsObject = typeof font === 'object';
407
- const fontIsString = typeof font === 'string';
408
- const sizeWeightStyleAreUndefined = size === undefined && weight === undefined && style === undefined;
409
-
410
- this.#metricsValid = false;
411
- if (fontIsObject) {
412
- // `font` is case 1) a FontInfo object
413
- this.#fontInfo = { ...defaultTextFont, ...font };
414
- } else if (fontIsString && sizeWeightStyleAreUndefined) {
415
- // `font` is case 2) CSS font shorthand.
416
- this.#fontInfo = Font.fromCSSString(font);
417
- } else {
418
- // `font` is case 3) a font family string (e.g., 'Times New Roman').
419
- // The other parameters represent the size, weight, and style.
420
- // It is okay for `font` to be undefined while one or more of the other arguments is provided.
421
- // Following CSS conventions, unspecified params are reset to the default.
422
- this.#fontInfo = Font.validate(
423
- font ?? defaultTextFont.family,
424
- size ?? defaultTextFont.size,
425
- weight ?? defaultTextFont.weight,
426
- style ?? defaultTextFont.style
427
- );
428
- }
429
- return this;
430
- }
431
-
432
- /**
433
- * Get the css string describing this Element's text font. e.g.,
434
- * 'bold 10pt Arial'.
435
- */
436
- getFont(): string {
437
- return Font.toCSSString(this.#fontInfo);
438
- }
439
-
440
- /** Return a copy of the current FontInfo object. */
441
- get fontInfo(): Required<FontInfo> {
442
- // We can cast to Required<FontInfo> here, because
443
- // we just called resetFont() above to ensure this.fontInfo is set.
444
- return this.#fontInfo;
445
- }
446
-
447
- /** Set the current FontInfo object. */
448
- set fontInfo(fontInfo: FontInfo) {
449
- this.setFont(fontInfo);
450
- }
451
-
452
- /** Change the font size, while keeping everything else the same. */
453
- setFontSize(size?: string | number): this {
454
- const fontInfo = this.fontInfo;
455
- this.setFont(fontInfo.family, size, fontInfo.weight, fontInfo.style);
456
- return this;
457
- }
458
-
459
- /**
460
- * @returns a CSS font-size string (e.g., '18pt', '12px', '1em').
461
- * See Element.fontSizeInPixels or Element.fontSizeInPoints if you need to get a number for calculation purposes.
462
- */
463
- getFontSize(): string {
464
- return this.fontSize;
465
- }
466
-
467
- getFontScale(): number {
468
- return this.#fontScale;
469
- }
470
-
471
- /**
472
- * The size is 1) a string of the form '10pt' or '16px', compatible with the CSS font-size property.
473
- * or 2) a number, which is interpreted as a point size (i.e. 12 == '12pt').
474
- */
475
- set fontSize(size: string | number) {
476
- this.setFontSize(size);
477
- }
478
-
479
- /** @returns a CSS font-size string (e.g., '18pt', '12px', '1em'). */
480
- get fontSize(): string {
481
- let size = this.fontInfo.size;
482
- if (typeof size === 'number') {
483
- size = `${size}pt`;
484
- }
485
- return size;
486
- }
487
-
488
- /** @returns the font size in `pt`. */
489
- get fontSizeInPoints(): number {
490
- return Font.convertSizeToPointValue(this.fontSize);
491
- }
492
-
493
- /** @returns the font size in `px`. */
494
- get fontSizeInPixels(): number {
495
- return Font.convertSizeToPixelValue(this.fontSize);
496
- }
497
-
498
- /** @returns a CSS font-style string (e.g., 'italic'). */
499
- get fontStyle(): string {
500
- return this.fontInfo.style;
501
- }
502
-
503
- /** Set the font style. */
504
- set fontStyle(style: string) {
505
- const fontInfo = this.fontInfo;
506
- this.setFont(fontInfo.family, fontInfo.size, fontInfo.weight, style);
507
- }
508
-
509
- /**
510
- * @returns a CSS font-weight string (e.g., 'bold').
511
- * As in CSS, font-weight is always returned as a string, even if it was set as a number.
512
- */
513
- get fontWeight(): string {
514
- return this.fontInfo.weight + '';
515
- }
516
-
517
- /** Set the font weight. */
518
- set fontWeight(weight: string | number) {
519
- const fontInfo = this.fontInfo;
520
- this.setFont(fontInfo.family, fontInfo.size, weight, fontInfo.style);
521
- }
522
-
523
- /** Get element width. */
524
- getWidth(): number {
525
- return this.width;
526
- }
527
-
528
- get width(): number {
529
- if (!this.#metricsValid) this.measureText();
530
- return this.#width;
531
- }
532
-
533
- /** Set element width. */
534
- setWidth(width: number): this {
535
- this.width = width;
536
- return this;
537
- }
538
-
539
- set width(width: number) {
540
- if (!this.#metricsValid) this.measureText();
541
- this.#width = width;
542
- }
543
-
544
- /** Set the X coordinate. */
545
- setX(x: number): this {
546
- this.x = x;
547
- return this;
548
- }
549
-
550
- /** Get the X coordinate. */
551
- getX(): number {
552
- return this.x;
553
- }
554
- /** Get the Y coordinate. */
555
- getY(): number {
556
- return this.y;
557
- }
558
-
559
- /** Set the Y coordinate. */
560
- setY(y: number): this {
561
- this.y = y;
562
- return this;
563
- }
564
-
565
- /** Shift element down `yShift` pixels. Negative values shift up. */
566
- setYShift(yShift: number): this {
567
- this.yShift = yShift;
568
- return this;
569
- }
570
-
571
- /** Get shift element `yShift`. */
572
- getYShift(): number {
573
- return this.yShift;
574
- }
575
-
576
- /** Set shift element right `xShift` pixels. Negative values shift left. */
577
- setXShift(xShift: number): this {
578
- this.xShift = xShift;
579
- return this;
580
- }
581
-
582
- /** Get shift element `xShift`. */
583
- getXShift(): number {
584
- return this.xShift;
585
- }
586
-
587
- /** Set element text. */
588
- setText(text: string): this {
589
- this.text = text;
590
- return this;
591
- }
592
-
593
- set text(text: string) {
594
- this.#metricsValid = false;
595
- this.#text = text;
596
- }
597
-
598
- /** Get element text. */
599
- getText(): string {
600
- return this.#text;
601
- }
602
-
603
- get text(): string {
604
- return this.#text;
605
- }
606
-
607
- /** Render the element text. */
608
- renderText(ctx: RenderContext, xPos: number, yPos: number): void {
609
- ctx.save();
610
- ctx.setFont(this.#fontInfo);
611
- ctx.fillText(this.#text, xPos + this.x + this.xShift, yPos + this.y + this.yShift);
612
- this.children.forEach((child) => {
613
- ctx.setFont(child.#fontInfo);
614
- ctx.fillText(child.#text, xPos + child.x + child.xShift, yPos + child.y + child.yShift);
615
- });
616
- ctx.restore();
617
- }
618
-
619
- /** Measure the text using the textFont. */
620
- measureText(): TextMetrics {
621
- // TODO: What about SVG.getBBox()?
622
- // https://developer.mozilla.org/en-US/docs/Web/API/SVGGraphicsElement/getBBox
623
- const context = Element.getTextMeasurementCanvas()?.getContext('2d');
624
- if (!context) {
625
- // eslint-disable-next-line no-console
626
- console.warn('Element: No context for txtCanvas. Returning empty text metrics.');
627
- return this.#textMetrics;
628
- }
629
- context.font = Font.toCSSString(Font.validate(this.#fontInfo));
630
- this.#textMetrics = context.measureText(this.#text);
631
- this.#height = this.#textMetrics.actualBoundingBoxAscent + this.#textMetrics.actualBoundingBoxDescent;
632
- this.#width = this.#textMetrics.width;
633
- this.#metricsValid = true;
634
- return this.#textMetrics;
635
- }
636
-
637
- /** Measure the text using the FontInfo related with key. */
638
- static measureWidth(text: string, key = ''): number {
639
- const context = Element.getTextMeasurementCanvas()?.getContext('2d');
640
- if (!context) {
641
- // eslint-disable-next-line no-console
642
- console.warn('Element: No context for txtCanvas. Returning empty text metrics.');
643
- return 0;
644
- }
645
- context.font = Font.toCSSString(Metrics.getFontInfo(key));
646
- return context.measureText(text).width;
647
- }
648
-
649
- /** Get the text metrics. */
650
- getTextMetrics(): TextMetrics {
651
- return this.textMetrics;
652
- }
653
-
654
- get textMetrics(): TextMetrics {
655
- if (!this.#metricsValid) this.measureText();
656
- return this.#textMetrics;
657
- }
658
-
659
- /** Get the text height. */
660
- getHeight() {
661
- return this.height;
662
- }
663
-
664
- get height() {
665
- if (!this.#metricsValid) this.measureText();
666
- return this.#height;
667
- }
668
-
669
- set height(height: number) {
670
- if (!this.#metricsValid) this.measureText();
671
- this.#height = height;
672
- }
673
-
674
- setOriginX(x: number): void {
675
- const bbox = this.getBoundingBox();
676
- const originX = Math.abs((bbox.getX() - this.xShift) / bbox.getW());
677
- const xShift = (x - originX) * bbox.getW();
678
- this.xShift = -xShift;
679
- }
680
-
681
- setOriginY(y: number): void {
682
- const bbox = this.getBoundingBox();
683
- const originY = Math.abs((bbox.getY() - this.yShift) / bbox.getH());
684
- const yShift = (y - originY) * bbox.getH();
685
- this.yShift = -yShift;
686
- }
687
-
688
- setOrigin(x: number, y: number): void {
689
- this.setOriginX(x);
690
- this.setOriginY(y);
691
- }
692
- }