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/parser.ts DELETED
@@ -1,263 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- //
3
- // A generic text parsing class for VexFlow.
4
-
5
- import { log, RuntimeError } from './util';
6
-
7
- // To enable logging for this class. Set `VexFlow.Parser.DEBUG` to `true`.
8
- // eslint-disable-next-line
9
- function L(...args: any[]): void {
10
- if (Parser.DEBUG) log('VexFlow.Parser', args);
11
- }
12
-
13
- const NO_ERROR_POS = -1;
14
-
15
- export type Match = string | Match[] | null;
16
- export type RuleFunction = () => Rule;
17
- export type TriggerFunction = (state?: { matches: Match[] }) => void;
18
-
19
- export interface Rule {
20
- // Lexer Rules
21
- token?: string; // The token property is a string that is compiled into a RegExp.
22
- noSpace?: boolean;
23
-
24
- // Parser Rules
25
- expect?: RuleFunction[];
26
- zeroOrMore?: boolean;
27
- oneOrMore?: boolean;
28
- maybe?: boolean;
29
- or?: boolean;
30
- run?: TriggerFunction;
31
- }
32
-
33
- export interface Result {
34
- success: boolean;
35
-
36
- // Lexer Results
37
- pos?: number;
38
- incrementPos?: number;
39
- matchedString?: string;
40
-
41
- // Parser Results
42
- matches?: Match[];
43
- numMatches?: number;
44
- results?: GroupedResults;
45
- errorPos?: number; // Set to NO_ERROR if successful. N if there is an error in the string.
46
- }
47
-
48
- // Represents a mixed array containing Result and/or Result[].
49
- // The grouping is determined by the structure of the Grammar.
50
- export type GroupedResults = (Result | Result[])[];
51
-
52
- // Converts parser results into an easy to reference list that can be
53
- // used in triggers. This function returns:
54
- // - nested array in which the leaf elements are string or null
55
- // - string (including empty strings)
56
- // - null
57
- function flattenMatches(r: Result | Result[]): Match {
58
- if ('matchedString' in r) return r.matchedString as string; // string
59
- if ('results' in r) return flattenMatches(r.results as Result[]);
60
- const results = r as Result[];
61
- if (results.length === 1) return flattenMatches(results[0]);
62
- if (results.length === 0) return null;
63
- return results.map(flattenMatches); // nested array
64
- }
65
-
66
- export interface Grammar {
67
- begin(): RuleFunction;
68
- }
69
-
70
- // This is the base parser class. Given an arbitrary context-free grammar, it
71
- // can parse any line and execute code when specific rules are met (e.g.,
72
- // when a string is terminated.)
73
- export class Parser {
74
- static DEBUG: boolean = false;
75
-
76
- protected grammar: Grammar;
77
-
78
- protected line: string; // Use RegExp to extract tokens from this line.
79
- protected pos: number;
80
- protected errorPos: number;
81
-
82
- // For an example of a simple grammar, take a look at tests/parser_tests.ts or
83
- // the EasyScore grammar in easyscore.ts.
84
- constructor(grammar: Grammar) {
85
- this.grammar = grammar;
86
- this.line = '';
87
- this.pos = 0;
88
- this.errorPos = NO_ERROR_POS;
89
- }
90
-
91
- // Parse `line` using current grammar. Returns `{success: true}` if the
92
- // line parsed correctly, otherwise returns `{success: false, errorPos: N}`
93
- // where `errorPos` is the location of the error in the string.
94
- parse(line: string): Result {
95
- this.line = line;
96
- this.pos = 0;
97
- this.errorPos = NO_ERROR_POS;
98
- const result = this.expect(this.grammar.begin());
99
- result.errorPos = this.errorPos;
100
- return result;
101
- }
102
-
103
- matchFail(returnPos: number): void {
104
- if (this.errorPos === NO_ERROR_POS) this.errorPos = this.pos;
105
- this.pos = returnPos;
106
- }
107
-
108
- matchSuccess(): void {
109
- this.errorPos = NO_ERROR_POS;
110
- }
111
-
112
- // Look for `token` in this.line[this.pos], and return success
113
- // if one is found. `token` is specified as a regular expression.
114
- matchToken(token: string, noSpace: boolean = false): Result {
115
- const regexp = noSpace ? new RegExp('^((' + token + '))') : new RegExp('^((' + token + ')\\s*)');
116
- const workingLine = this.line.slice(this.pos);
117
- const result = workingLine.match(regexp);
118
- if (result !== null) {
119
- return {
120
- success: true,
121
- matchedString: result[2],
122
- incrementPos: result[1].length,
123
- pos: this.pos,
124
- };
125
- } else {
126
- return { success: false, pos: this.pos };
127
- }
128
- }
129
-
130
- // Execute rule to match a sequence of tokens (or rules). If `maybe` is
131
- // set, then return success even if the token is not found, but reset
132
- // the position before exiting.
133
- // TODO: expectOne(...) is never called with the `maybe` parameter.
134
- expectOne(rule: Rule, maybe: boolean = false): Result {
135
- const results: GroupedResults = [];
136
- const pos = this.pos;
137
-
138
- let allMatches = true;
139
- let oneMatch = false;
140
- maybe = maybe === true || rule.maybe === true;
141
-
142
- // Execute all sub rules in sequence.
143
- if (rule.expect) {
144
- for (const next of rule.expect) {
145
- const localPos = this.pos;
146
- const result = this.expect(next);
147
-
148
- // If `rule.or` is set, then return success if any one
149
- // of the subrules match, else all subrules must match.
150
- if (result.success) {
151
- results.push(result);
152
- oneMatch = true;
153
- if (rule.or) break;
154
- } else {
155
- allMatches = false;
156
- if (!rule.or) {
157
- this.pos = localPos;
158
- break;
159
- }
160
- }
161
- }
162
- }
163
-
164
- const gotOne = (rule.or && oneMatch) || allMatches;
165
- const success = gotOne || maybe === true;
166
- const numMatches = gotOne ? 1 : 0;
167
- if (maybe && !gotOne) this.pos = pos;
168
- if (success) {
169
- this.matchSuccess();
170
- } else {
171
- this.matchFail(pos);
172
- }
173
- return { success, results, numMatches };
174
- }
175
-
176
- // Try to match multiple (one or more) instances of the rule. If `maybe` is set,
177
- // then a failed match is also a success (but the position is reset).
178
- expectOneOrMore(rule: Rule, maybe: boolean = false): Result {
179
- const results: GroupedResults = [];
180
- const pos = this.pos;
181
- let numMatches = 0;
182
- let more = true;
183
-
184
- do {
185
- const result = this.expectOne(rule);
186
- if (result.success && result.results) {
187
- numMatches++;
188
- results.push(result.results as Result[]);
189
- } else {
190
- more = false;
191
- }
192
- } while (more);
193
-
194
- const success = numMatches > 0 || maybe === true;
195
- if (maybe && !(numMatches > 0)) this.pos = pos;
196
- if (success) {
197
- this.matchSuccess();
198
- } else {
199
- this.matchFail(pos);
200
- }
201
- return { success, results, numMatches };
202
- }
203
-
204
- // Match zero or more instances of `rule`. Offloads to `expectOneOrMore`.
205
- expectZeroOrMore(rule: Rule): Result {
206
- return this.expectOneOrMore(rule, true);
207
- }
208
-
209
- // Execute the rule produced by the provided `rules` function. This
210
- // offloads to one of the above matchers and consolidates the results. It is also
211
- // responsible for executing any code triggered by the rule (in `rule.run`.)
212
- expect(ruleFunc: RuleFunction): Result {
213
- L('Evaluating rule function:', ruleFunc);
214
- if (!ruleFunc) {
215
- throw new RuntimeError('Invalid rule function');
216
- }
217
- let result: Result;
218
-
219
- // Get rule from Grammar class.
220
- // expect(...) handles both lexing & parsing:
221
- // - lexer rules produce tokens.
222
- // - parser rules produce expressions which may trigger code via the
223
- // { run: () => ... } trigger functions in easyscore.ts.
224
- // These functions build the VexFlow objects that are displayed on screen.
225
- const rule: Rule = ruleFunc.bind(this.grammar)();
226
- if (rule.token) {
227
- // A lexer rule has a `token` property.
228
- // Base case: parse the regex and throw an error if the
229
- // line doesn't match.
230
- result = this.matchToken(rule.token, rule.noSpace === true);
231
- if (result.success) {
232
- // Token match! Update position and throw away parsed portion
233
- // of string.
234
- this.pos += result.incrementPos as number;
235
- }
236
- } else if (rule.expect) {
237
- // A parser rule has an `expect` property.
238
- if (rule.oneOrMore) {
239
- result = this.expectOneOrMore(rule);
240
- } else if (rule.zeroOrMore) {
241
- result = this.expectZeroOrMore(rule);
242
- } else {
243
- result = this.expectOne(rule);
244
- }
245
- } else {
246
- L(rule);
247
- throw new RuntimeError('Bad grammar! No `token` or `expect` property ' + rule);
248
- }
249
-
250
- // If there's a trigger attached to this rule, then run it.
251
- // Make the matches accessible through `state.matches` in the
252
- // `run: (state) => ...` trigger.
253
- const matches: Match[] = [];
254
- result.matches = matches;
255
- if (result.results) {
256
- result.results.forEach((r) => matches.push(flattenMatches(r)));
257
- }
258
- if (rule.run && result.success) {
259
- rule.run({ matches });
260
- }
261
- return result;
262
- }
263
- }
@@ -1,273 +0,0 @@
1
- // Copyright (c) 2023-present VexFlow contributors: https://github.com/vexflow/vexflow/graphs/contributors
2
- // MIT License
3
-
4
- import { Element } from './element';
5
- import { Glyphs } from './glyphs';
6
- import { Metrics } from './metrics';
7
- import { RenderContext } from './rendercontext';
8
- import { StaveNote } from './stavenote';
9
- import { Tables } from './tables';
10
- import { Category } from './typeguard';
11
- import { log, RuntimeError } from './util';
12
-
13
- // eslint-disable-next-line
14
- function L(...args: any[]) {
15
- if (PedalMarking.DEBUG) log('VexFlow.PedalMarking', args);
16
- }
17
-
18
- /**
19
- * Draws a pedal glyph with the provided `name` on a rendering `context`
20
- * at the coordinates `x` and `y. Takes into account the glyph data
21
- * coordinate shifts.
22
- */
23
- function drawPedalGlyph(name: string, ctx: RenderContext, x: number, y: number): void {
24
- const glyph = new Element(PedalMarking.CATEGORY);
25
- glyph.setText(PedalMarking.GLYPHS[name] ?? name);
26
- glyph.renderText(ctx, x - (glyph.getWidth() - Tables.STAVE_LINE_DISTANCE) / 2, y);
27
- }
28
-
29
- /**
30
- * PedalMarking implements different types of pedal markings. These notation
31
- * elements indicate to the performer when to depress and release the a pedal.
32
- *
33
- * In order to create "Sostenuto", and "una corda" markings, you must set
34
- * custom text for the release/depress pedal markings.
35
- */
36
- export class PedalMarking extends Element {
37
- /** To enable logging for this class. Set `VexFlow.PedalMarking.DEBUG` to `true`. */
38
- static DEBUG: boolean = false;
39
-
40
- static get CATEGORY(): string {
41
- return Category.PedalMarking;
42
- }
43
-
44
- protected line: number;
45
- protected type: number;
46
- protected customDepressText: string;
47
- protected customReleaseText: string;
48
-
49
- public renderOptions: {
50
- color: string;
51
- bracketHeight: number;
52
- textMarginRight: number;
53
- bracketLineWidth: number;
54
- };
55
- protected notes: StaveNote[];
56
-
57
- /** Glyph data */
58
- static readonly GLYPHS: Record<string, string> = {
59
- pedalDepress: Glyphs.keyboardPedalPed,
60
- pedalRelease: Glyphs.keyboardPedalUp,
61
- };
62
-
63
- /** Pedal type as number. */
64
- static readonly type = {
65
- TEXT: 1,
66
- BRACKET: 2,
67
- MIXED: 3,
68
- };
69
-
70
- /** Pedal type as string. */
71
- static readonly typeString: Record<string, number> = {
72
- text: PedalMarking.type.TEXT,
73
- bracket: PedalMarking.type.BRACKET,
74
- mixed: PedalMarking.type.MIXED,
75
- };
76
-
77
- /**
78
- * Create a sustain pedal marking. Returns the defaults PedalMarking.
79
- * Which uses the traditional "Ped" and "*"" markings.
80
- */
81
- static createSustain(notes: StaveNote[]): PedalMarking {
82
- const pedal = new PedalMarking(notes);
83
- return pedal;
84
- }
85
-
86
- /** Create a sostenuto pedal marking */
87
- static createSostenuto(notes: StaveNote[]): PedalMarking {
88
- const pedal = new PedalMarking(notes);
89
- pedal.setType(PedalMarking.type.MIXED);
90
- pedal.setCustomText('Sost. Ped.');
91
- return pedal;
92
- }
93
-
94
- /** Create an una corda pedal marking */
95
- static createUnaCorda(notes: StaveNote[]): PedalMarking {
96
- const pedal = new PedalMarking(notes);
97
- pedal.setType(PedalMarking.type.TEXT);
98
- pedal.setCustomText('una corda', 'tre corda');
99
- return pedal;
100
- }
101
-
102
- constructor(notes: StaveNote[]) {
103
- super();
104
-
105
- this.notes = notes;
106
- this.type = PedalMarking.type.TEXT;
107
- this.line = 0;
108
-
109
- // Custom text for the release/depress markings
110
- this.customDepressText = '';
111
- this.customReleaseText = '';
112
-
113
- this.renderOptions = {
114
- bracketHeight: 10,
115
- textMarginRight: 6,
116
- bracketLineWidth: 1,
117
- color: 'black',
118
- };
119
- }
120
-
121
- /** Set pedal type. */
122
- setType(type: string | number): this {
123
- type = typeof type === 'string' ? PedalMarking.typeString[type] : type;
124
-
125
- if (type >= PedalMarking.type.TEXT && type <= PedalMarking.type.MIXED) {
126
- this.type = type;
127
- }
128
- return this;
129
- }
130
-
131
- /**
132
- * Set custom text for the `depress`/`release` pedal markings. No text is
133
- * set if the parameter is falsy.
134
- */
135
- setCustomText(depress: string, release?: string): this {
136
- this.customDepressText = depress || '';
137
- this.customReleaseText = release || '';
138
- return this;
139
- }
140
-
141
- /** Set the staff line to render the markings on. */
142
- setLine(line: number): this {
143
- this.line = line;
144
- return this;
145
- }
146
-
147
- /** Draw the bracket based pedal markings. */
148
- drawBracketed(): void {
149
- const ctx = this.checkContext();
150
- let isPedalDepressed = false;
151
- let prevX: number;
152
- let prevY: number;
153
-
154
- // Iterate through each note
155
- this.notes.forEach((note, index, notes) => {
156
- // Each note triggers the opposite pedal action
157
- isPedalDepressed = !isPedalDepressed;
158
-
159
- // Get the initial coordinates for the note
160
- const x = note.getAbsoluteX();
161
- const y = note.checkStave().getYForBottomText(this.line + 3);
162
-
163
- // Throw if current note is positioned before the previous note
164
- if (x < prevX) {
165
- throw new RuntimeError('InvalidConfiguration', 'The notes provided must be in order of ascending x positions');
166
- }
167
-
168
- // Determine if the previous or next note are the same
169
- // as the current note. We need to keep track of this for
170
- // when adjustments are made for the release+depress action
171
- const nextNoteIsSame = notes[index + 1] === note;
172
- const prevNoteIsSame = notes[index - 1] === note;
173
-
174
- let xShift = 0;
175
-
176
- if (isPedalDepressed) {
177
- // Adjustment for release+depress
178
- xShift = prevNoteIsSame ? 5 : 0;
179
-
180
- if (this.type === PedalMarking.type.MIXED && !prevNoteIsSame) {
181
- // For MIXED style, start with text instead of bracket
182
- if (this.customDepressText) {
183
- // If we have custom text, use instead of the default "Ped" glyph
184
- const textWidth = ctx.measureText(this.customDepressText).width;
185
- ctx.fillText(this.customDepressText, x - textWidth / 2, y);
186
- xShift = textWidth / 2 + this.renderOptions.textMarginRight;
187
- } else {
188
- // Render the Ped glyph in position
189
- drawPedalGlyph('pedalDepress', ctx, x, y);
190
- xShift = 20 + this.renderOptions.textMarginRight;
191
- }
192
- } else {
193
- // Draw start bracket
194
- ctx.beginPath();
195
- ctx.moveTo(x, y - this.renderOptions.bracketHeight);
196
- ctx.lineTo(x + xShift, y);
197
- ctx.stroke();
198
- ctx.closePath();
199
- }
200
- } else {
201
- // Adjustment for release+depress
202
- xShift = nextNoteIsSame ? -5 : 0;
203
-
204
- // Draw end bracket
205
- ctx.beginPath();
206
- ctx.moveTo(prevX, prevY);
207
- ctx.lineTo(x + xShift, y);
208
- ctx.lineTo(x, y - this.renderOptions.bracketHeight);
209
- ctx.stroke();
210
- ctx.closePath();
211
- }
212
-
213
- // Store previous coordinates
214
- prevX = x + xShift;
215
- prevY = y;
216
- });
217
- }
218
-
219
- /**
220
- * Draw the text based pedal markings. This defaults to the traditional
221
- * "Ped" and "*"" symbols if no custom text has been provided.
222
- */
223
- drawText(): void {
224
- const ctx = this.checkContext();
225
- let isPedalDepressed = false;
226
-
227
- // Iterate through each note, placing glyphs or custom text accordingly
228
- this.notes.forEach((note) => {
229
- isPedalDepressed = !isPedalDepressed;
230
- const stave = note.checkStave();
231
- const x = note.getAbsoluteX();
232
- const y = stave.getYForBottomText(this.line + 3);
233
-
234
- let textWidth = 0;
235
- if (isPedalDepressed) {
236
- if (this.customDepressText) {
237
- textWidth = ctx.measureText(this.customDepressText).width;
238
- ctx.fillText(this.customDepressText, x - textWidth / 2, y);
239
- } else {
240
- drawPedalGlyph('pedalDepress', ctx, x, y);
241
- }
242
- } else {
243
- if (this.customReleaseText) {
244
- textWidth = ctx.measureText(this.customReleaseText).width;
245
- ctx.fillText(this.customReleaseText, x - textWidth / 2, y);
246
- } else {
247
- drawPedalGlyph('pedalRelease', ctx, x, y);
248
- }
249
- }
250
- });
251
- }
252
-
253
- /** Render the pedal marking in position on the rendering context. */
254
- draw(): void {
255
- const ctx = this.checkContext();
256
- this.setRendered();
257
-
258
- ctx.save();
259
- ctx.setStrokeStyle(this.renderOptions.color);
260
- ctx.setFillStyle(this.renderOptions.color);
261
- ctx.setFont(Metrics.getFontInfo('PedalMarking.text'));
262
- L('Rendering Pedal Marking');
263
-
264
- if (this.type === PedalMarking.type.BRACKET || this.type === PedalMarking.type.MIXED) {
265
- ctx.setLineWidth(this.renderOptions.bracketLineWidth);
266
- this.drawBracketed();
267
- } else if (this.type === PedalMarking.type.TEXT) {
268
- this.drawText();
269
- }
270
-
271
- ctx.restore();
272
- }
273
- }
package/src/registry.ts DELETED
@@ -1,146 +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
- // ## Description
6
- //
7
- // This file implements a registry for VexFlow elements. It allows users
8
- // to track, query, and manage some subset of generated elements, and
9
- // dynamically get and set attributes.
10
- //
11
- // There are two ways to register with a registry:
12
- //
13
- // 1) Explicitly call `registry.register(elem:Element, id?:string)`, or,
14
- // 2) Call `Registry.enableDefaultRegistry(registry)` when ready, and all future
15
- // elements will automatically register with it.
16
- //
17
- // Once an element is registered, selected attributes are tracked and indexed by
18
- // the registry. This allows fast look up of elements by attributes like id, type,
19
- // and class.
20
-
21
- import { Element } from './element';
22
- import { RuntimeError } from './util';
23
-
24
- // Indexes are represented as maps of maps of maps. This allows
25
- // for both multi-labeling (e.g., an element can have multiple classes)
26
- // and efficient lookup.
27
- class Index {
28
- // [attributeName][attributeValue][elementId] => Element
29
- [key: string]: { [key: string]: { [key: string]: Element } };
30
- constructor() {
31
- this.id = {};
32
- this.type = {};
33
- this.class = {};
34
- }
35
- }
36
-
37
- export interface RegistryUpdate {
38
- id: string;
39
- name: string;
40
- value: string | undefined;
41
- oldValue: string | undefined;
42
- }
43
-
44
- export class Registry {
45
- static #defaultRegistry?: Registry;
46
-
47
- static getDefaultRegistry(): Registry | undefined {
48
- return Registry.#defaultRegistry;
49
- }
50
-
51
- // If you call `enableDefaultRegistry`, any new elements will auto-register with
52
- // the provided registry as soon as they're constructed.
53
- static enableDefaultRegistry(registry: Registry): void {
54
- Registry.#defaultRegistry = registry;
55
- }
56
-
57
- static disableDefaultRegistry(): void {
58
- Registry.#defaultRegistry = undefined;
59
- }
60
-
61
- protected index: Index;
62
-
63
- constructor() {
64
- this.index = new Index();
65
- }
66
-
67
- clear(): this {
68
- this.index = new Index();
69
- return this;
70
- }
71
-
72
- setIndexValue(name: string, value: string, id: string, elem: Element): void {
73
- const index = this.index;
74
- if (!index[name][value]) {
75
- index[name][value] = {};
76
- }
77
- index[name][value][id] = elem;
78
- }
79
-
80
- // Updates the indexes for element 'id'. If an element's attribute changes
81
- // from A -> B, make sure to remove the element from A.
82
- updateIndex({ id, name, value, oldValue }: RegistryUpdate): void {
83
- const elem = this.getElementById(id);
84
- if (oldValue !== undefined && this.index[name][oldValue]) {
85
- delete this.index[name][oldValue][id];
86
- }
87
- if (value && elem) {
88
- this.setIndexValue(name, value, elem.getAttribute('id'), elem);
89
- }
90
- }
91
-
92
- /**
93
- * Register element `elem` with this registry.
94
- * This adds the element to its index and watches it for attribute changes.
95
- * @param elem
96
- * @param id
97
- * @returns this
98
- */
99
- register(elem: Element, id?: string): this {
100
- id = id || elem.getAttribute('id');
101
- if (!id) {
102
- throw new RuntimeError("Can't add element without `id` attribute to registry");
103
- }
104
-
105
- // Manually add id to index, then update other indexes.
106
- elem.setAttribute('id', id);
107
- this.setIndexValue('id', id, id, elem);
108
- this.updateIndex({ id, name: 'type', value: elem.getAttribute('type'), oldValue: undefined });
109
- elem.onRegister(this);
110
- return this;
111
- }
112
-
113
- getElementById(id: string): Element | undefined {
114
- return this.index.id?.[id]?.[id]; // return undefined if the id is not found.
115
- }
116
-
117
- getElementsByAttribute(attribute: string, value: string): Element[] {
118
- const indexAttr = this.index[attribute];
119
- if (indexAttr) {
120
- const indexAttrVal = indexAttr[value];
121
- if (indexAttrVal) {
122
- const keys = Object.keys(indexAttrVal);
123
- return keys.map((k) => indexAttrVal[k]);
124
- }
125
- }
126
- return [];
127
- }
128
-
129
- getElementsByType(type: string): Element[] {
130
- return this.getElementsByAttribute('type', type);
131
- }
132
-
133
- getElementsByClass(className: string): Element[] {
134
- return this.getElementsByAttribute('class', className);
135
- }
136
-
137
- // This is called by the element when an attribute value changes. If an indexed
138
- // attribute changes, then update the local index.
139
- onUpdate(info: RegistryUpdate): this {
140
- const allowedNames = ['id', 'type', 'class'];
141
- if (allowedNames.includes(info.name)) {
142
- this.updateIndex(info);
143
- }
144
- return this;
145
- }
146
- }