vexflow-native 0.0.2 → 0.2.0

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 (43) hide show
  1. package/README.md +60 -13
  2. package/lib/module/SkiaVexflowContext.js +285 -0
  3. package/lib/module/SkiaVexflowContext.js.map +1 -0
  4. package/lib/module/VexflowCanvas.js +103 -0
  5. package/lib/module/VexflowCanvas.js.map +1 -0
  6. package/lib/module/colorUtils.js +34 -0
  7. package/lib/module/colorUtils.js.map +1 -0
  8. package/lib/module/colorUtils.test.js +37 -0
  9. package/lib/module/colorUtils.test.js.map +1 -0
  10. package/lib/module/constants.js +30 -0
  11. package/lib/module/constants.js.map +1 -0
  12. package/lib/module/index.js +5 -3
  13. package/lib/module/index.js.map +1 -1
  14. package/lib/module/types.js +2 -0
  15. package/lib/module/types.js.map +1 -0
  16. package/lib/module/utils.js +79 -0
  17. package/lib/module/utils.js.map +1 -0
  18. package/lib/typescript/src/SkiaVexflowContext.d.ts +101 -0
  19. package/lib/typescript/src/SkiaVexflowContext.d.ts.map +1 -0
  20. package/lib/typescript/src/VexflowCanvas.d.ts +17 -0
  21. package/lib/typescript/src/VexflowCanvas.d.ts.map +1 -0
  22. package/lib/typescript/src/colorUtils.d.ts +2 -0
  23. package/lib/typescript/src/colorUtils.d.ts.map +1 -0
  24. package/lib/typescript/src/colorUtils.test.d.ts +2 -0
  25. package/lib/typescript/src/colorUtils.test.d.ts.map +1 -0
  26. package/lib/typescript/src/constants.d.ts +21 -0
  27. package/lib/typescript/src/constants.d.ts.map +1 -0
  28. package/lib/typescript/src/index.d.ts +6 -1
  29. package/lib/typescript/src/index.d.ts.map +1 -1
  30. package/lib/typescript/src/types.d.ts +7 -0
  31. package/lib/typescript/src/types.d.ts.map +1 -0
  32. package/lib/typescript/src/utils.d.ts +7 -0
  33. package/lib/typescript/src/utils.d.ts.map +1 -0
  34. package/package.json +7 -3
  35. package/src/SkiaVexflowContext.ts +462 -0
  36. package/src/VexflowCanvas.tsx +145 -0
  37. package/src/colorUtils.test.ts +52 -0
  38. package/src/colorUtils.ts +55 -0
  39. package/src/constants.tsx +34 -0
  40. package/src/index.ts +8 -0
  41. package/src/types.ts +7 -0
  42. package/src/utils.tsx +108 -0
  43. package/src/index.tsx +0 -3
@@ -0,0 +1,462 @@
1
+ import {
2
+ BlendMode,
3
+ PaintStyle,
4
+ Skia,
5
+ type SkCanvas,
6
+ type SkFont,
7
+ type SkPaint,
8
+ type SkPath,
9
+ } from '@shopify/react-native-skia';
10
+ import { RenderContext as VexflowRenderContext } from 'vexflow';
11
+ import type { FontInfo } from 'vexflow';
12
+
13
+ import {
14
+ DEFAULT_FILL_STYLE,
15
+ DEFAULT_FONT,
16
+ DEFAULT_LINE_WIDTH,
17
+ DEFAULT_STROKE_STYLE,
18
+ LINE_CAP_MAP,
19
+ LINE_JOIN_MAP,
20
+ } from './constants';
21
+ import { resolveScoreColor } from './colorUtils';
22
+ import { type LineCap, type LineJoin } from './types';
23
+ import {
24
+ createFont,
25
+ getAdvanceWidth,
26
+ parseCssFontShorthand,
27
+ toPxFontSize,
28
+ } from './utils';
29
+
30
+ /* eslint-disable no-void */
31
+
32
+ type RenderState = {
33
+ fillStyle: string;
34
+ strokeStyle: string;
35
+ lineWidth: number;
36
+ lineCap: LineCap;
37
+ lineJoin: LineJoin;
38
+ font: string;
39
+ };
40
+
41
+ type SkiaVexflowContextOptions = {
42
+ defaultFillStyle?: string;
43
+ defaultStrokeStyle?: string;
44
+ };
45
+
46
+ const isWarnDisabled = true;
47
+
48
+ function notImplemented(methodName: string): void {
49
+ if (!isWarnDisabled) {
50
+ console.warn(`SkiaVexflowContext: ${methodName} is not implemented yet.`);
51
+ }
52
+ }
53
+
54
+ // MDN copied types
55
+ type CanvasLineCap = 'butt' | 'round' | 'square';
56
+
57
+ interface DOMMatrix2DInit {
58
+ a?: number;
59
+ b?: number;
60
+ c?: number;
61
+ d?: number;
62
+ e?: number;
63
+ f?: number;
64
+ m11?: number;
65
+ m12?: number;
66
+ m21?: number;
67
+ m22?: number;
68
+ m41?: number;
69
+ m42?: number;
70
+ }
71
+
72
+ interface CanvasGradient {
73
+ /**
74
+ * The **`CanvasGradient.addColorStop()`** method adds a new color stop, defined by an `offset` and a `color`, to a given canvas gradient.
75
+ *
76
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CanvasGradient/addColorStop)
77
+ */
78
+ addColorStop(offset: number, color: string): void;
79
+ }
80
+
81
+ interface CanvasPattern {
82
+ /**
83
+ * The **`CanvasPattern.setTransform()`** method uses a DOMMatrix object as the pattern's transformation matrix and invokes it on the pattern.
84
+ *
85
+ * [MDN Reference](https://developer.mozilla.org/docs/Web/API/CanvasPattern/setTransform)
86
+ */
87
+ setTransform(transform?: DOMMatrix2DInit): void;
88
+ }
89
+
90
+ export default class SkiaVexflowContext implements VexflowRenderContext {
91
+ static get CATEGORY() {
92
+ return 'SkiaVexflowContext';
93
+ }
94
+
95
+ private canvas: SkCanvas;
96
+ private currentPath: SkPath;
97
+ private fillPaint: SkPaint;
98
+ private strokePaint: SkPaint;
99
+ private textFont: SkFont;
100
+ private bravuraFont: SkFont | null;
101
+ private defaultFillStyle: string;
102
+ private defaultStrokeStyle: string;
103
+ private stateStack: RenderState[] = [];
104
+
105
+ private state: RenderState = {
106
+ fillStyle: DEFAULT_FILL_STYLE,
107
+ strokeStyle: DEFAULT_STROKE_STYLE,
108
+ lineWidth: DEFAULT_LINE_WIDTH,
109
+ lineCap: 'butt',
110
+ lineJoin: 'miter',
111
+ font: DEFAULT_FONT,
112
+ };
113
+
114
+ constructor(
115
+ canvas: SkCanvas,
116
+ bravuraFont: SkFont | null = null,
117
+ options: SkiaVexflowContextOptions = {}
118
+ ) {
119
+ this.canvas = canvas;
120
+ this.bravuraFont = bravuraFont;
121
+
122
+ this.currentPath = Skia.Path.Make();
123
+
124
+ this.fillPaint = Skia.Paint();
125
+ this.fillPaint.setStyle(PaintStyle.Fill);
126
+ this.fillPaint.setAntiAlias(true);
127
+
128
+ this.strokePaint = Skia.Paint();
129
+ this.strokePaint.setStyle(PaintStyle.Stroke);
130
+ this.strokePaint.setAntiAlias(true);
131
+
132
+ this.textFont = createFont(12, 'Bravura', this.bravuraFont);
133
+
134
+ if (options.defaultFillStyle !== undefined) {
135
+ this.state.fillStyle = options.defaultFillStyle;
136
+ }
137
+
138
+ if (options.defaultStrokeStyle !== undefined) {
139
+ this.state.strokeStyle = options.defaultStrokeStyle;
140
+ }
141
+
142
+ this.defaultFillStyle = this.state.fillStyle;
143
+ this.defaultStrokeStyle = this.state.strokeStyle;
144
+
145
+ this.applyState();
146
+ }
147
+
148
+ private applyState(): void {
149
+ this.fillPaint.setColor(Skia.Color(this.state.fillStyle));
150
+
151
+ this.strokePaint.setColor(Skia.Color(this.state.strokeStyle));
152
+ this.strokePaint.setStrokeWidth(this.state.lineWidth);
153
+ this.strokePaint.setStrokeCap(LINE_CAP_MAP[this.state.lineCap]);
154
+ this.strokePaint.setStrokeJoin(LINE_JOIN_MAP[this.state.lineJoin]);
155
+
156
+ const parsed = parseCssFontShorthand(this.state.font);
157
+ this.textFont = createFont(parsed.sizePx, parsed.family, this.bravuraFont);
158
+ }
159
+
160
+ clear() {
161
+ this.canvas.clear(Skia.Color('transparent'));
162
+ }
163
+
164
+ setFillStyle(style: string) {
165
+ this.fillStyle = style;
166
+ return this;
167
+ }
168
+
169
+ setBackgroundFillStyle(style: string) {
170
+ void style;
171
+ notImplemented('setBackgroundFillStyle');
172
+ return this;
173
+ }
174
+
175
+ setStrokeStyle(style: string) {
176
+ this.strokeStyle = style;
177
+ return this;
178
+ }
179
+
180
+ setShadowColor(color: string) {
181
+ void color;
182
+ notImplemented('setShadowColor');
183
+ return this;
184
+ }
185
+
186
+ setShadowBlur(blur: number) {
187
+ void blur;
188
+ notImplemented('setShadowBlur');
189
+ return this;
190
+ }
191
+
192
+ setLineWidth(width: number) {
193
+ this.state.lineWidth = width;
194
+ this.strokePaint.setStrokeWidth(width);
195
+ return this;
196
+ }
197
+
198
+ setLineCap(capType: CanvasLineCap) {
199
+ const cap: LineCap =
200
+ capType === 'round' || capType === 'square' ? capType : 'butt';
201
+ this.state.lineCap = cap;
202
+ this.strokePaint.setStrokeCap(LINE_CAP_MAP[cap]);
203
+ return this;
204
+ }
205
+
206
+ setLineDash(dashPattern: number[]) {
207
+ void dashPattern;
208
+ notImplemented('setLineDash');
209
+ return this;
210
+ }
211
+
212
+ scale(x: number, y: number) {
213
+ this.canvas.scale(x, y);
214
+ return this;
215
+ }
216
+
217
+ rect(x: number, y: number, width: number, height: number) {
218
+ this.currentPath.addRect(Skia.XYWHRect(x, y, width, height));
219
+ return this;
220
+ }
221
+
222
+ resize(width: number, height: number) {
223
+ void width;
224
+ void height;
225
+ notImplemented('resize');
226
+ return this;
227
+ }
228
+
229
+ fillRect(x: number, y: number, width: number, height: number) {
230
+ this.canvas.drawRect(Skia.XYWHRect(x, y, width, height), this.fillPaint);
231
+ return this;
232
+ }
233
+
234
+ clearRect(x: number, y: number, width: number, height: number) {
235
+ const clearPaint = Skia.Paint();
236
+ clearPaint.setBlendMode(BlendMode.Clear);
237
+ this.canvas.drawRect(Skia.XYWHRect(x, y, width, height), clearPaint);
238
+ return this;
239
+ }
240
+
241
+ pointerRect(x: number, y: number, width: number, height: number) {
242
+ void x;
243
+ void y;
244
+ void width;
245
+ void height;
246
+ notImplemented('pointerRect');
247
+ return this;
248
+ }
249
+
250
+ beginPath() {
251
+ this.currentPath = Skia.Path.Make();
252
+ return this;
253
+ }
254
+
255
+ moveTo(x: number, y: number) {
256
+ this.currentPath.moveTo(x, y);
257
+ return this;
258
+ }
259
+
260
+ lineTo(x: number, y: number) {
261
+ this.currentPath.lineTo(x, y);
262
+ return this;
263
+ }
264
+
265
+ bezierCurveTo(
266
+ cp1x: number,
267
+ cp1y: number,
268
+ cp2x: number,
269
+ cp2y: number,
270
+ x: number,
271
+ y: number
272
+ ) {
273
+ this.currentPath.cubicTo(cp1x, cp1y, cp2x, cp2y, x, y);
274
+ return this;
275
+ }
276
+
277
+ quadraticCurveTo(cpx: number, cpy: number, x: number, y: number) {
278
+ this.currentPath.quadTo(cpx, cpy, x, y);
279
+ return this;
280
+ }
281
+
282
+ arc(
283
+ x: number,
284
+ y: number,
285
+ radius: number,
286
+ startAngle: number,
287
+ endAngle: number,
288
+ anticlockwise: boolean
289
+ ) {
290
+ const startDeg = (startAngle * 180) / Math.PI;
291
+ const endDeg = (endAngle * 180) / Math.PI;
292
+ const sweep = anticlockwise ? startDeg - endDeg : endDeg - startDeg;
293
+
294
+ this.currentPath.addArc(
295
+ Skia.XYWHRect(x - radius, y - radius, radius * 2, radius * 2),
296
+ startDeg,
297
+ sweep
298
+ );
299
+ return this;
300
+ }
301
+
302
+ fill(attributes?: any) {
303
+ void attributes;
304
+ this.canvas.drawPath(this.currentPath, this.fillPaint);
305
+ return this;
306
+ }
307
+
308
+ stroke() {
309
+ this.canvas.drawPath(this.currentPath, this.strokePaint);
310
+ return this;
311
+ }
312
+
313
+ closePath() {
314
+ this.currentPath.close();
315
+ return this;
316
+ }
317
+
318
+ fillText(text: string, x: number, y: number) {
319
+ this.canvas.drawText(text, x, y, this.fillPaint, this.textFont);
320
+ return this;
321
+ }
322
+
323
+ save() {
324
+ this.canvas.save();
325
+ this.stateStack.push({ ...this.state });
326
+ return this;
327
+ }
328
+
329
+ restore() {
330
+ this.canvas.restore();
331
+ const prevState = this.stateStack.pop();
332
+ if (prevState) {
333
+ this.state = prevState;
334
+ this.applyState();
335
+ }
336
+
337
+ return this;
338
+ }
339
+
340
+ openGroup(cls?: string, id?: string) {
341
+ void cls;
342
+ void id;
343
+ notImplemented('openGroup');
344
+ return null;
345
+ }
346
+
347
+ closeGroup() {
348
+ notImplemented('closeGroup');
349
+ }
350
+
351
+ openRotation(angleDegrees: number, x: number, y: number) {
352
+ void angleDegrees;
353
+ void x;
354
+ void y;
355
+ notImplemented('openRotation');
356
+ }
357
+
358
+ closeRotation() {
359
+ notImplemented('closeRotation');
360
+ }
361
+
362
+ add(child: any) {
363
+ void child;
364
+ notImplemented('add');
365
+ }
366
+
367
+ measureText(text: string) {
368
+ const rect = this.textFont.measureText(text);
369
+ const width = getAdvanceWidth(this.textFont, text);
370
+ const height = rect.height;
371
+
372
+ return {
373
+ x: rect.x,
374
+ y: rect.y,
375
+ width,
376
+ height,
377
+ };
378
+ }
379
+
380
+ set fillStyle(style: string | CanvasGradient | CanvasPattern) {
381
+ if (typeof style === 'string') {
382
+ const resolvedStyle = resolveScoreColor(style, this.defaultFillStyle);
383
+ this.state.fillStyle = resolvedStyle;
384
+ this.fillPaint.setColor(Skia.Color(resolvedStyle));
385
+ return;
386
+ }
387
+
388
+ notImplemented('set fillStyle (gradient/pattern)');
389
+ }
390
+
391
+ get fillStyle() {
392
+ return this.state.fillStyle;
393
+ }
394
+
395
+ set strokeStyle(style: string | CanvasGradient | CanvasPattern) {
396
+ if (typeof style === 'string') {
397
+ const resolvedStyle = resolveScoreColor(style, this.defaultStrokeStyle);
398
+ this.state.strokeStyle = resolvedStyle;
399
+ this.strokePaint.setColor(Skia.Color(resolvedStyle));
400
+ return;
401
+ }
402
+
403
+ notImplemented('set strokeStyle (gradient/pattern)');
404
+ }
405
+
406
+ get strokeStyle() {
407
+ return this.state.strokeStyle;
408
+ }
409
+
410
+ setFont(
411
+ font?: string | FontInfo,
412
+ size?: string | number,
413
+ weight?: string | number,
414
+ style?: string
415
+ ) {
416
+ if (typeof font === 'object' && font !== null) {
417
+ const fontSize = toPxFontSize(font.size);
418
+ const family = Array.isArray(font.family)
419
+ ? font.family.join(', ')
420
+ : font.family;
421
+ const fontWeight = font.weight ? ` ${font.weight}` : '';
422
+ const fontStyle = font.style ? ` ${font.style}` : '';
423
+
424
+ this.font = `${fontStyle}${fontWeight} ${fontSize}px ${
425
+ family ?? 'sans-serif'
426
+ }`.trim();
427
+ return this;
428
+ }
429
+
430
+ if (
431
+ typeof font === 'string' &&
432
+ size === undefined &&
433
+ weight === undefined &&
434
+ style === undefined
435
+ ) {
436
+ this.font = font;
437
+ return this;
438
+ }
439
+
440
+ const fontSize = toPxFontSize(size);
441
+ const fontWeight = weight ? ` ${weight}` : '';
442
+ const fontStyle = style ? ` ${style}` : '';
443
+ const family = font || 'sans-serif';
444
+
445
+ this.font = `${fontStyle}${fontWeight} ${fontSize}px ${family}`.trim();
446
+ return this;
447
+ }
448
+
449
+ getFont() {
450
+ return this.state.font;
451
+ }
452
+
453
+ set font(font: string) {
454
+ this.state.font = font;
455
+ const parsed = parseCssFontShorthand(font);
456
+ this.textFont = createFont(parsed.sizePx, parsed.family, this.bravuraFont);
457
+ }
458
+
459
+ get font() {
460
+ return this.state.font;
461
+ }
462
+ }
@@ -0,0 +1,145 @@
1
+ import {
2
+ Canvas,
3
+ Picture,
4
+ Skia,
5
+ useFont,
6
+ type SkFont,
7
+ type SkPicture,
8
+ } from '@shopify/react-native-skia';
9
+ import { useMemo } from 'react';
10
+ import { Element } from 'vexflow';
11
+ import { Text, useWindowDimensions } from 'react-native';
12
+
13
+ import SkiaVexflowContext from './SkiaVexflowContext';
14
+ import { VEXFLOW_SCORE_COLORS } from './constants';
15
+ import { createFont, getAdvanceWidth, parseCssFontShorthand } from './utils';
16
+
17
+ const CANVAS_HEIGHT = 180;
18
+
19
+ export type VexflowCanvasDrawArgs = {
20
+ ctx: SkiaVexflowContext;
21
+ width: number;
22
+ height: number;
23
+ };
24
+
25
+ type VexflowCanvasProps = {
26
+ onDraw: (args: VexflowCanvasDrawArgs) => void;
27
+ font: Parameters<typeof useFont>[0];
28
+ width?: number;
29
+ height?: number;
30
+ colorScheme?: 'light' | 'dark';
31
+ };
32
+
33
+ const fontName = 'vexflowFont';
34
+
35
+ function createTextMeasurementCanvas(skiaFont: SkFont | null) {
36
+ let currentFont = createFont(12, fontName, skiaFont);
37
+
38
+ const measureContext = {
39
+ measureText: (text: string) => {
40
+ const rect = currentFont.measureText(text);
41
+ const width = getAdvanceWidth(currentFont, text);
42
+ const ascent = Math.max(0, -rect.y);
43
+ const descent = Math.max(0, rect.y + rect.height);
44
+
45
+ return {
46
+ width,
47
+ actualBoundingBoxAscent: ascent,
48
+ actualBoundingBoxDescent: descent,
49
+ fontBoundingBoxAscent: ascent,
50
+ fontBoundingBoxDescent: descent,
51
+ actualBoundingBoxLeft: rect.x,
52
+ actualBoundingBoxRight: rect.x + rect.width,
53
+ };
54
+ },
55
+ };
56
+
57
+ Object.defineProperty(measureContext, 'font', {
58
+ get() {
59
+ return '';
60
+ },
61
+ set(value: string) {
62
+ try {
63
+ const parsed = parseCssFontShorthand(value);
64
+ currentFont = createFont(parsed.sizePx, parsed.family, skiaFont);
65
+ } catch {
66
+ currentFont = createFont(12, fontName, skiaFont);
67
+ }
68
+ },
69
+ });
70
+
71
+ return {
72
+ getContext: (type: string) =>
73
+ type === '2d' ? (measureContext as any) : null,
74
+ };
75
+ }
76
+
77
+ function createVexflowPicture(
78
+ width: number,
79
+ height: number,
80
+ onDraw: (args: VexflowCanvasDrawArgs) => void,
81
+ skiaFont: SkFont | null,
82
+ scoreColors: {
83
+ fill: string;
84
+ stroke: string;
85
+ }
86
+ ): SkPicture {
87
+ const recorder = Skia.PictureRecorder();
88
+ const canvas = recorder.beginRecording(Skia.XYWHRect(0, 0, width, height));
89
+
90
+ const ctx = new SkiaVexflowContext(canvas, skiaFont, {
91
+ defaultFillStyle: scoreColors.fill,
92
+ defaultStrokeStyle: scoreColors.stroke,
93
+ });
94
+
95
+ onDraw({ ctx, width, height });
96
+
97
+ return recorder.finishRecordingAsPicture();
98
+ }
99
+
100
+ export default function VexflowCanvas({
101
+ onDraw,
102
+ font,
103
+ width,
104
+ height = CANVAS_HEIGHT,
105
+ colorScheme = 'light',
106
+ }: VexflowCanvasProps) {
107
+ const { width: windowWidth } = useWindowDimensions();
108
+ const scoreColors =
109
+ colorScheme === 'dark'
110
+ ? VEXFLOW_SCORE_COLORS.dark
111
+ : VEXFLOW_SCORE_COLORS.light;
112
+
113
+ const canvasWidth = width ?? Math.max(300, Math.floor(windowWidth) - 32);
114
+ const skiaFont = useFont(font, 30);
115
+
116
+ const pictureInfo = useMemo(() => {
117
+ if (!skiaFont) {
118
+ return null;
119
+ }
120
+
121
+ Element.setTextMeasurementCanvas(
122
+ createTextMeasurementCanvas(skiaFont) as unknown as HTMLCanvasElement
123
+ );
124
+
125
+ const picture = createVexflowPicture(
126
+ canvasWidth,
127
+ height,
128
+ onDraw,
129
+ skiaFont,
130
+ scoreColors
131
+ );
132
+
133
+ return { picture, width: canvasWidth, height };
134
+ }, [skiaFont, canvasWidth, height, onDraw, scoreColors]);
135
+
136
+ if (!pictureInfo) {
137
+ return <Text>Loading font...</Text>;
138
+ }
139
+
140
+ return (
141
+ <Canvas style={{ width: pictureInfo.width, height: pictureInfo.height }}>
142
+ <Picture picture={pictureInfo.picture} />
143
+ </Canvas>
144
+ );
145
+ }
@@ -0,0 +1,52 @@
1
+ import { describe, expect, it } from '@jest/globals';
2
+
3
+ import { resolveScoreColor } from './colorUtils';
4
+
5
+ describe('resolveScoreColor', () => {
6
+ it('maps black tokens to active theme score color', () => {
7
+ const themeColor = '#F9FAFB';
8
+
9
+ expect(resolveScoreColor('black', themeColor)).toBe(themeColor);
10
+ expect(resolveScoreColor('#000', themeColor)).toBe(themeColor);
11
+ expect(resolveScoreColor('#000000', themeColor)).toBe(themeColor);
12
+ expect(resolveScoreColor('rgb(0, 0, 0)', themeColor)).toBe(themeColor);
13
+ expect(resolveScoreColor('rgba(0, 0, 0, 1)', themeColor)).toBe(themeColor);
14
+ });
15
+
16
+ it('maps VexFlow default ledger line tokens to active theme score color', () => {
17
+ const themeColor = '#F9FAFB';
18
+
19
+ expect(resolveScoreColor('#444', themeColor)).toBe(themeColor);
20
+ expect(resolveScoreColor('#444444', themeColor)).toBe(themeColor);
21
+ expect(resolveScoreColor('rgb(68, 68, 68)', themeColor)).toBe(themeColor);
22
+ expect(resolveScoreColor('rgba(68, 68, 68, 1)', themeColor)).toBe(
23
+ themeColor
24
+ );
25
+ });
26
+
27
+ it('maps VexFlow default tab stave and bend tokens to active theme score color', () => {
28
+ const themeColor = '#F9FAFB';
29
+
30
+ expect(resolveScoreColor('#777', themeColor)).toBe(themeColor);
31
+ expect(resolveScoreColor('#777777', themeColor)).toBe(themeColor);
32
+ expect(resolveScoreColor('rgb(119, 119, 119)', themeColor)).toBe(
33
+ themeColor
34
+ );
35
+ expect(resolveScoreColor('rgba(119, 119, 119, 1)', themeColor)).toBe(
36
+ themeColor
37
+ );
38
+ expect(resolveScoreColor('#999', themeColor)).toBe(themeColor);
39
+ expect(resolveScoreColor('#999999', themeColor)).toBe(themeColor);
40
+ expect(resolveScoreColor('rgb(153, 153, 153)', themeColor)).toBe(
41
+ themeColor
42
+ );
43
+ expect(resolveScoreColor('rgba(153, 153, 153, 1)', themeColor)).toBe(
44
+ themeColor
45
+ );
46
+ });
47
+
48
+ it('preserves non-black custom colors', () => {
49
+ const themeColor = '#F9FAFB';
50
+ expect(resolveScoreColor('#ef4444', themeColor)).toBe('#ef4444');
51
+ });
52
+ });
@@ -0,0 +1,55 @@
1
+ const SCORE_THEME_HEX_TOKENS = new Set([
2
+ '#000',
3
+ '#000000',
4
+ '#444',
5
+ '#444444',
6
+ '#777',
7
+ '#777777',
8
+ '#999',
9
+ '#999999',
10
+ ]);
11
+ const BLACK_KEYWORD = 'black';
12
+ const BLACK_RGB_REGEX = /^rgb\(0,0,0\)$/;
13
+ const BLACK_RGBA_REGEX = /^rgba\(0,0,0,(1|1\.0+|0?\.\d+)\)$/;
14
+ const LEDGER_RGB_REGEX = /^rgb\(68,68,68\)$/;
15
+ const LEDGER_RGBA_REGEX = /^rgba\(68,68,68,(1|1\.0+|0?\.\d+)\)$/;
16
+ const BEND_RGB_REGEX = /^rgb\(119,119,119\)$/;
17
+ const BEND_RGBA_REGEX = /^rgba\(119,119,119,(1|1\.0+|0?\.\d+)\)$/;
18
+ const TAB_STAVE_RGB_REGEX = /^rgb\(153,153,153\)$/;
19
+ const TAB_STAVE_RGBA_REGEX = /^rgba\(153,153,153,(1|1\.0+|0?\.\d+)\)$/;
20
+
21
+ function normalizeColorToken(style: string): string {
22
+ return style.trim().toLowerCase().replace(/\s+/g, '');
23
+ }
24
+
25
+ function isBlackColorToken(style: string): boolean {
26
+ const normalized = normalizeColorToken(style);
27
+
28
+ if (SCORE_THEME_HEX_TOKENS.has(normalized) || normalized === BLACK_KEYWORD) {
29
+ return true;
30
+ }
31
+
32
+ if (
33
+ BLACK_RGB_REGEX.test(normalized) ||
34
+ BLACK_RGBA_REGEX.test(normalized) ||
35
+ LEDGER_RGB_REGEX.test(normalized) ||
36
+ LEDGER_RGBA_REGEX.test(normalized) ||
37
+ BEND_RGB_REGEX.test(normalized) ||
38
+ BEND_RGBA_REGEX.test(normalized) ||
39
+ TAB_STAVE_RGB_REGEX.test(normalized) ||
40
+ TAB_STAVE_RGBA_REGEX.test(normalized)
41
+ ) {
42
+ return true;
43
+ }
44
+
45
+ return false;
46
+ }
47
+
48
+ // Replaces VexFlow default black values with the active score theme color.
49
+ export function resolveScoreColor(style: string, themeColor: string): string {
50
+ if (isBlackColorToken(style)) {
51
+ return themeColor;
52
+ }
53
+
54
+ return style;
55
+ }