vexflow 5.1.0 → 5.1.1
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.
- package/README.md +17 -6
- package/build/cjs/vexflow-bravura.js +2 -2
- package/build/cjs/vexflow-bravura.js.map +1 -1
- package/build/cjs/vexflow-core.js +2 -2
- package/build/cjs/vexflow-core.js.map +1 -1
- package/build/cjs/vexflow-debug-with-tests.js +144 -14
- package/build/cjs/vexflow-debug-with-tests.js.map +1 -1
- package/build/cjs/vexflow-debug.js +144 -14
- package/build/cjs/vexflow-debug.js.map +1 -1
- package/build/cjs/vexflow.js +2 -2
- package/build/cjs/vexflow.js.map +1 -1
- package/build/esm/src/element.js +7 -2
- package/build/esm/src/font.js +3 -1
- package/build/esm/src/metrics.js +4 -3
- package/build/esm/src/textmetricscache.js +93 -0
- package/build/esm/src/version.js +3 -3
- package/build/esm/tests/node/metrics-tests.mjs +278 -0
- package/build/types/src/element.d.ts +2 -0
- package/build/types/src/textmetricscache.d.ts +21 -0
- package/changelog/CHANGELOG.md +15 -0
- package/package.json +3 -1
package/build/esm/src/element.js
CHANGED
|
@@ -2,6 +2,7 @@ import { BoundingBox } from './boundingbox.js';
|
|
|
2
2
|
import { Font } from './font.js';
|
|
3
3
|
import { Metrics } from './metrics.js';
|
|
4
4
|
import { Registry } from './registry.js';
|
|
5
|
+
import { TextMetricsCache } from './textmetricscache.js';
|
|
5
6
|
import { defined, prefix, RuntimeError } from './util.js';
|
|
6
7
|
export class Element {
|
|
7
8
|
static get CATEGORY() {
|
|
@@ -12,6 +13,10 @@ export class Element {
|
|
|
12
13
|
}
|
|
13
14
|
static setTextMeasurementCanvas(canvas) {
|
|
14
15
|
Element.txtCanvas = canvas;
|
|
16
|
+
Element.clearTextMeasurementCache();
|
|
17
|
+
}
|
|
18
|
+
static clearTextMeasurementCache() {
|
|
19
|
+
TextMetricsCache.clear();
|
|
15
20
|
}
|
|
16
21
|
static getTextMeasurementCanvas() {
|
|
17
22
|
let txtCanvas = Element.txtCanvas;
|
|
@@ -377,7 +382,7 @@ export class Element {
|
|
|
377
382
|
return this._textMetrics;
|
|
378
383
|
}
|
|
379
384
|
context.font = Font.toCSSString(Font.validate(this.fontInfo));
|
|
380
|
-
this._textMetrics =
|
|
385
|
+
this._textMetrics = Object.assign({}, TextMetricsCache.measure(context, this.text));
|
|
381
386
|
this._height = this._textMetrics.actualBoundingBoxAscent + this._textMetrics.actualBoundingBoxDescent;
|
|
382
387
|
this._width = this._textMetrics.width;
|
|
383
388
|
this.metricsValid = true;
|
|
@@ -391,7 +396,7 @@ export class Element {
|
|
|
391
396
|
return 0;
|
|
392
397
|
}
|
|
393
398
|
context.font = Font.toCSSString(Metrics.getFontInfo(key));
|
|
394
|
-
return
|
|
399
|
+
return TextMetricsCache.measure(context, text).width;
|
|
395
400
|
}
|
|
396
401
|
getTextMetrics() {
|
|
397
402
|
return this.textMetrics;
|
package/build/esm/src/font.js
CHANGED
|
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { Metrics } from './metrics.js';
|
|
11
|
+
import { TextMetricsCache } from './textmetricscache.js';
|
|
11
12
|
export var FontWeight;
|
|
12
13
|
(function (FontWeight) {
|
|
13
14
|
FontWeight["NORMAL"] = "normal";
|
|
@@ -185,7 +186,8 @@ export class Font {
|
|
|
185
186
|
fontFaceSet = self.fonts;
|
|
186
187
|
}
|
|
187
188
|
fontFaceSet === null || fontFaceSet === void 0 ? void 0 : fontFaceSet.add(fontFace);
|
|
188
|
-
|
|
189
|
+
TextMetricsCache.clear();
|
|
190
|
+
return fontFaceLoadPromise.finally(() => TextMetricsCache.clear());
|
|
189
191
|
});
|
|
190
192
|
}
|
|
191
193
|
static getURLForFont(fontName) {
|
package/build/esm/src/metrics.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { TextMetricsCache } from './textmetricscache.js';
|
|
2
2
|
export class Metrics {
|
|
3
3
|
static clear(key) {
|
|
4
|
+
TextMetricsCache.clear();
|
|
4
5
|
if (key) {
|
|
5
6
|
this.cacheFont.delete(key);
|
|
6
7
|
this.cacheStyle.delete(key);
|
|
@@ -21,7 +22,7 @@ export class Metrics {
|
|
|
21
22
|
};
|
|
22
23
|
this.cacheFont.set(key, font);
|
|
23
24
|
}
|
|
24
|
-
return
|
|
25
|
+
return Object.assign({}, font);
|
|
25
26
|
}
|
|
26
27
|
static getFontFamily(key) {
|
|
27
28
|
var _a;
|
|
@@ -50,7 +51,7 @@ export class Metrics {
|
|
|
50
51
|
};
|
|
51
52
|
this.cacheStyle.set(key, style);
|
|
52
53
|
}
|
|
53
|
-
return
|
|
54
|
+
return Object.assign({}, style);
|
|
54
55
|
}
|
|
55
56
|
static get(key, defaultValue) {
|
|
56
57
|
var _a;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
export class TextMetricsCache {
|
|
2
|
+
static clear() {
|
|
3
|
+
this.entries.clear();
|
|
4
|
+
}
|
|
5
|
+
static fontsReady() {
|
|
6
|
+
var _a;
|
|
7
|
+
const fonts = typeof document !== 'undefined'
|
|
8
|
+
? document.fonts
|
|
9
|
+
: globalThis.fonts;
|
|
10
|
+
if (fonts !== this.fonts) {
|
|
11
|
+
for (const type of ['loading', 'loadingdone', 'loadingerror']) {
|
|
12
|
+
(_a = this.fonts) === null || _a === void 0 ? void 0 : _a.removeEventListener(type, this.onFontsChanged);
|
|
13
|
+
fonts === null || fonts === void 0 ? void 0 : fonts.addEventListener(type, this.onFontsChanged);
|
|
14
|
+
}
|
|
15
|
+
this.fonts = fonts;
|
|
16
|
+
this.clear();
|
|
17
|
+
}
|
|
18
|
+
if ((fonts === null || fonts === void 0 ? void 0 : fonts.size) !== this.fontCount || (fonts === null || fonts === void 0 ? void 0 : fonts.status) !== this.fontStatus) {
|
|
19
|
+
this.fontCount = fonts === null || fonts === void 0 ? void 0 : fonts.size;
|
|
20
|
+
this.fontStatus = fonts === null || fonts === void 0 ? void 0 : fonts.status;
|
|
21
|
+
this.clear();
|
|
22
|
+
}
|
|
23
|
+
return (fonts === null || fonts === void 0 ? void 0 : fonts.status) !== 'loading';
|
|
24
|
+
}
|
|
25
|
+
static snapshot(metrics) {
|
|
26
|
+
return Object.freeze({
|
|
27
|
+
width: metrics.width,
|
|
28
|
+
actualBoundingBoxLeft: metrics.actualBoundingBoxLeft,
|
|
29
|
+
actualBoundingBoxRight: metrics.actualBoundingBoxRight,
|
|
30
|
+
actualBoundingBoxAscent: metrics.actualBoundingBoxAscent,
|
|
31
|
+
actualBoundingBoxDescent: metrics.actualBoundingBoxDescent,
|
|
32
|
+
fontBoundingBoxAscent: metrics.fontBoundingBoxAscent,
|
|
33
|
+
fontBoundingBoxDescent: metrics.fontBoundingBoxDescent,
|
|
34
|
+
emHeightAscent: metrics.emHeightAscent,
|
|
35
|
+
emHeightDescent: metrics.emHeightDescent,
|
|
36
|
+
hangingBaseline: metrics.hangingBaseline,
|
|
37
|
+
alphabeticBaseline: metrics.alphabeticBaseline,
|
|
38
|
+
ideographicBaseline: metrics.ideographicBaseline,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
static measure(context, text) {
|
|
42
|
+
var _a, _b, _c;
|
|
43
|
+
if (context !== this.context) {
|
|
44
|
+
this.context = context;
|
|
45
|
+
this.clear();
|
|
46
|
+
}
|
|
47
|
+
const cacheable = this.fontsReady();
|
|
48
|
+
let lang = context.lang;
|
|
49
|
+
let direction = context.direction;
|
|
50
|
+
const canvas = context.canvas;
|
|
51
|
+
if (canvas && 'ownerDocument' in canvas) {
|
|
52
|
+
if (direction === 'inherit') {
|
|
53
|
+
direction = canvas.isConnected
|
|
54
|
+
? ((_a = canvas.ownerDocument.defaultView) === null || _a === void 0 ? void 0 : _a.getComputedStyle(canvas).direction) || 'ltr'
|
|
55
|
+
: canvas.style.direction || canvas.dir || 'ltr';
|
|
56
|
+
}
|
|
57
|
+
if (!lang || lang === 'inherit') {
|
|
58
|
+
lang = (_c = (_b = canvas.closest('[lang]')) === null || _b === void 0 ? void 0 : _b.getAttribute('lang')) !== null && _c !== void 0 ? _c : canvas.ownerDocument.documentElement.lang;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
const key = JSON.stringify([
|
|
62
|
+
context.font,
|
|
63
|
+
direction,
|
|
64
|
+
context.textAlign,
|
|
65
|
+
context.textBaseline,
|
|
66
|
+
context.fontKerning,
|
|
67
|
+
context.fontStretch,
|
|
68
|
+
context.fontVariantCaps,
|
|
69
|
+
context.letterSpacing,
|
|
70
|
+
context.wordSpacing,
|
|
71
|
+
context.textRendering,
|
|
72
|
+
lang,
|
|
73
|
+
text,
|
|
74
|
+
]);
|
|
75
|
+
const cached = cacheable ? this.entries.get(key) : undefined;
|
|
76
|
+
if (cached) {
|
|
77
|
+
this.entries.delete(key);
|
|
78
|
+
this.entries.set(key, cached);
|
|
79
|
+
return cached;
|
|
80
|
+
}
|
|
81
|
+
const metrics = this.snapshot(context.measureText(text));
|
|
82
|
+
if (cacheable && key.length <= this.maxKeyLength) {
|
|
83
|
+
if (this.entries.size >= this.maxEntries)
|
|
84
|
+
this.entries.delete(this.entries.keys().next().value);
|
|
85
|
+
this.entries.set(key, metrics);
|
|
86
|
+
}
|
|
87
|
+
return metrics;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
TextMetricsCache.maxEntries = 2048;
|
|
91
|
+
TextMetricsCache.maxKeyLength = 4096;
|
|
92
|
+
TextMetricsCache.entries = new Map();
|
|
93
|
+
TextMetricsCache.onFontsChanged = () => TextMetricsCache.clear();
|
package/build/esm/src/version.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export const VERSION = '5.1.
|
|
2
|
-
export const ID = '
|
|
3
|
-
export const DATE = '2026-11-
|
|
1
|
+
export const VERSION = '5.1.1';
|
|
2
|
+
export const ID = '271f9428354588de1f6c83a065428c8a5c442513';
|
|
3
|
+
export const DATE = '2026-11-16T03:47:09.194Z';
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import assert from 'node:assert/strict';
|
|
11
|
+
import test from 'node:test';
|
|
12
|
+
const { Element, Font, Metrics, MetricsDefaults } = await import(new URL('../../build/esm/entry/vexflow-core.js', import.meta.url));
|
|
13
|
+
function canvas() {
|
|
14
|
+
let calls = 0;
|
|
15
|
+
const context = {
|
|
16
|
+
font: '',
|
|
17
|
+
direction: 'ltr',
|
|
18
|
+
textAlign: 'start',
|
|
19
|
+
textBaseline: 'alphabetic',
|
|
20
|
+
measureText(text) {
|
|
21
|
+
calls++;
|
|
22
|
+
return Object.create({
|
|
23
|
+
width: text.length * 5,
|
|
24
|
+
actualBoundingBoxLeft: 1,
|
|
25
|
+
actualBoundingBoxRight: 9,
|
|
26
|
+
actualBoundingBoxAscent: 8,
|
|
27
|
+
actualBoundingBoxDescent: 2,
|
|
28
|
+
fontBoundingBoxAscent: 11,
|
|
29
|
+
fontBoundingBoxDescent: 4,
|
|
30
|
+
emHeightAscent: 10,
|
|
31
|
+
emHeightDescent: 3,
|
|
32
|
+
alphabeticBaseline: 0,
|
|
33
|
+
hangingBaseline: 7,
|
|
34
|
+
ideographicBaseline: -3,
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
return { getContext: () => context, context, calls: () => calls };
|
|
39
|
+
}
|
|
40
|
+
test('font and style records preserve caller isolation without deep cloning', () => {
|
|
41
|
+
for (const [get, key] of [
|
|
42
|
+
[Metrics.getFontInfo.bind(Metrics), 'StaveNote'],
|
|
43
|
+
[Metrics.getStyle.bind(Metrics), 'StaveNote'],
|
|
44
|
+
]) {
|
|
45
|
+
const original = get(key);
|
|
46
|
+
const edited = get(key);
|
|
47
|
+
assert.notEqual(edited, original);
|
|
48
|
+
for (const field of Object.keys(edited))
|
|
49
|
+
edited[field] = 'changed';
|
|
50
|
+
assert.deepEqual(get(key), original);
|
|
51
|
+
}
|
|
52
|
+
const first = new Element();
|
|
53
|
+
const second = new Element();
|
|
54
|
+
first.fontInfo.size = 99;
|
|
55
|
+
first.getStyle().lineDash = '2,4';
|
|
56
|
+
assert.notEqual(second.fontInfo.size, 99);
|
|
57
|
+
assert.notEqual(second.getStyle().lineDash, '2,4');
|
|
58
|
+
});
|
|
59
|
+
test('elements and width lookups share native measurements but not mutable results', () => {
|
|
60
|
+
const measured = canvas();
|
|
61
|
+
Element.setTextMeasurementCanvas(measured);
|
|
62
|
+
const first = new Element().setText('abc');
|
|
63
|
+
const metrics = first.measureText();
|
|
64
|
+
assert.equal(metrics.width, 15);
|
|
65
|
+
assert.equal(first.getHeight(), 10);
|
|
66
|
+
assert.equal(metrics.fontBoundingBoxAscent, 11);
|
|
67
|
+
assert.equal(metrics.ideographicBaseline, -3);
|
|
68
|
+
metrics.width = 999;
|
|
69
|
+
metrics.actualBoundingBoxAscent = 100;
|
|
70
|
+
const second = new Element().setText('abc');
|
|
71
|
+
assert.equal(second.getWidth(), 15);
|
|
72
|
+
assert.equal(second.getHeight(), 10);
|
|
73
|
+
assert.equal(Element.measureWidth('abc', 'Element'), 15);
|
|
74
|
+
assert.equal(first.measureText().width, 15);
|
|
75
|
+
assert.equal(measured.calls(), 1);
|
|
76
|
+
});
|
|
77
|
+
test('text, font, shaping, alignment, and baseline settings are distinct cache keys', () => {
|
|
78
|
+
const measured = canvas();
|
|
79
|
+
Element.setTextMeasurementCanvas(measured);
|
|
80
|
+
const el = new Element().setText('one');
|
|
81
|
+
el.measureText();
|
|
82
|
+
el.setText('two').measureText();
|
|
83
|
+
el.setFontSize(22).measureText();
|
|
84
|
+
assert.equal(measured.calls(), 3);
|
|
85
|
+
for (const [name, value] of Object.entries({
|
|
86
|
+
direction: 'rtl',
|
|
87
|
+
textAlign: 'right',
|
|
88
|
+
textBaseline: 'top',
|
|
89
|
+
fontKerning: 'none',
|
|
90
|
+
fontStretch: 'expanded',
|
|
91
|
+
fontVariantCaps: 'small-caps',
|
|
92
|
+
letterSpacing: '1px',
|
|
93
|
+
wordSpacing: '2px',
|
|
94
|
+
textRendering: 'optimizeLegibility',
|
|
95
|
+
lang: 'ja',
|
|
96
|
+
})) {
|
|
97
|
+
const before = measured.calls();
|
|
98
|
+
measured.context[name] = value;
|
|
99
|
+
el.measureText();
|
|
100
|
+
el.measureText();
|
|
101
|
+
assert.equal(measured.calls(), before + 1, name);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
test('canvas replacement and either form of Metrics.clear invalidate shared measurements', () => {
|
|
105
|
+
const first = canvas();
|
|
106
|
+
Element.setTextMeasurementCanvas(first);
|
|
107
|
+
Element.measureWidth('same');
|
|
108
|
+
Element.measureWidth('same');
|
|
109
|
+
assert.equal(first.calls(), 1);
|
|
110
|
+
for (const key of [undefined, 'Element']) {
|
|
111
|
+
Metrics.clear(key);
|
|
112
|
+
Element.measureWidth('same');
|
|
113
|
+
}
|
|
114
|
+
assert.equal(first.calls(), 3);
|
|
115
|
+
Element.setTextMeasurementCanvas(first);
|
|
116
|
+
Element.measureWidth('same');
|
|
117
|
+
assert.equal(first.calls(), 4, 'reinstalling a reconfigured canvas also clears the cache');
|
|
118
|
+
const second = canvas();
|
|
119
|
+
Element.setTextMeasurementCanvas(second);
|
|
120
|
+
Element.measureWidth('same');
|
|
121
|
+
assert.equal(second.calls(), 1);
|
|
122
|
+
Element.clearTextMeasurementCache();
|
|
123
|
+
Element.measureWidth('same');
|
|
124
|
+
assert.equal(second.calls(), 2, 'external font registration can invalidate measurements explicitly');
|
|
125
|
+
});
|
|
126
|
+
test('inherited canvas direction and language participate in measurement keys', () => {
|
|
127
|
+
const measured = canvas();
|
|
128
|
+
let direction = 'ltr';
|
|
129
|
+
let language = 'en';
|
|
130
|
+
Object.assign(measured, {
|
|
131
|
+
ownerDocument: {
|
|
132
|
+
defaultView: { getComputedStyle: () => ({ direction }) },
|
|
133
|
+
documentElement: { lang: 'en' },
|
|
134
|
+
},
|
|
135
|
+
closest: () => ({ getAttribute: () => language }),
|
|
136
|
+
isConnected: true,
|
|
137
|
+
});
|
|
138
|
+
Object.assign(measured.context, { canvas: measured, direction: 'inherit', lang: 'inherit' });
|
|
139
|
+
Element.setTextMeasurementCanvas(measured);
|
|
140
|
+
Element.measureWidth('text');
|
|
141
|
+
Element.measureWidth('text');
|
|
142
|
+
assert.equal(measured.calls(), 1);
|
|
143
|
+
direction = 'rtl';
|
|
144
|
+
Element.measureWidth('text');
|
|
145
|
+
language = 'ja';
|
|
146
|
+
Element.measureWidth('text');
|
|
147
|
+
assert.equal(measured.calls(), 3);
|
|
148
|
+
});
|
|
149
|
+
test('category font/style caches refresh after edits to defaults', () => {
|
|
150
|
+
const key = 'MeasurementTest';
|
|
151
|
+
try {
|
|
152
|
+
MetricsDefaults[key] = { fontSize: 12, fillStyle: '#123456' };
|
|
153
|
+
Metrics.clear(key);
|
|
154
|
+
assert.equal(Metrics.getFontInfo(key).size, 12);
|
|
155
|
+
assert.equal(Metrics.getStyle(key).fillStyle, '#123456');
|
|
156
|
+
MetricsDefaults[key].fontSize = 24;
|
|
157
|
+
MetricsDefaults[key].fillStyle = '#abcdef';
|
|
158
|
+
Metrics.clear(key);
|
|
159
|
+
assert.equal(Metrics.getFontInfo(key).size, 24);
|
|
160
|
+
assert.equal(Metrics.getStyle(key).fillStyle, '#abcdef');
|
|
161
|
+
}
|
|
162
|
+
finally {
|
|
163
|
+
delete MetricsDefaults[key];
|
|
164
|
+
Metrics.clear(key);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
test('cache capacity is bounded, hot entries survive, and very long keys are not retained', () => {
|
|
168
|
+
const measured = canvas();
|
|
169
|
+
Element.setTextMeasurementCanvas(measured);
|
|
170
|
+
Element.measureWidth('old');
|
|
171
|
+
for (let i = 0; i < 2200; i++) {
|
|
172
|
+
Element.measureWidth('hot');
|
|
173
|
+
Element.measureWidth(`entry ${i}`);
|
|
174
|
+
}
|
|
175
|
+
const before = measured.calls();
|
|
176
|
+
Element.measureWidth('hot');
|
|
177
|
+
assert.equal(measured.calls(), before);
|
|
178
|
+
Element.measureWidth('old');
|
|
179
|
+
assert.equal(measured.calls(), before + 1);
|
|
180
|
+
Element.measureWidth('x'.repeat(5000));
|
|
181
|
+
Element.measureWidth('x'.repeat(5000));
|
|
182
|
+
assert.equal(measured.calls(), before + 3);
|
|
183
|
+
});
|
|
184
|
+
class Fonts extends EventTarget {
|
|
185
|
+
constructor() {
|
|
186
|
+
super(...arguments);
|
|
187
|
+
this.status = 'loaded';
|
|
188
|
+
this.size = 0;
|
|
189
|
+
}
|
|
190
|
+
add() {
|
|
191
|
+
this.size++;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
test('document and worker font loading, errors, and synchronous set changes invalidate the cache', () => {
|
|
195
|
+
const oldDocument = globalThis.document;
|
|
196
|
+
const oldFonts = globalThis.fonts;
|
|
197
|
+
try {
|
|
198
|
+
for (const location of ['document', 'worker']) {
|
|
199
|
+
const fonts = new Fonts();
|
|
200
|
+
if (location === 'document')
|
|
201
|
+
globalThis.document = { fonts };
|
|
202
|
+
else {
|
|
203
|
+
delete globalThis.document;
|
|
204
|
+
globalThis.fonts = fonts;
|
|
205
|
+
}
|
|
206
|
+
const measured = canvas();
|
|
207
|
+
Element.setTextMeasurementCanvas(measured);
|
|
208
|
+
Element.measureWidth('glyph');
|
|
209
|
+
fonts.status = 'loading';
|
|
210
|
+
Element.measureWidth('glyph');
|
|
211
|
+
Element.measureWidth('glyph');
|
|
212
|
+
assert.equal(measured.calls(), 3, 'fallback metrics are not shared while loading');
|
|
213
|
+
fonts.status = 'loaded';
|
|
214
|
+
Element.measureWidth('glyph');
|
|
215
|
+
Element.measureWidth('glyph');
|
|
216
|
+
assert.equal(measured.calls(), 4);
|
|
217
|
+
for (const type of ['loading', 'loadingdone', 'loadingerror']) {
|
|
218
|
+
fonts.dispatchEvent(new Event(type));
|
|
219
|
+
Element.measureWidth('glyph');
|
|
220
|
+
}
|
|
221
|
+
assert.equal(measured.calls(), 7);
|
|
222
|
+
fonts.size++;
|
|
223
|
+
Element.measureWidth('glyph');
|
|
224
|
+
fonts.size--;
|
|
225
|
+
Element.measureWidth('glyph');
|
|
226
|
+
assert.equal(measured.calls(), 9, 'loaded faces can be added/deleted without loading events');
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
if (oldDocument === undefined)
|
|
231
|
+
delete globalThis.document;
|
|
232
|
+
else
|
|
233
|
+
globalThis.document = oldDocument;
|
|
234
|
+
if (oldFonts === undefined)
|
|
235
|
+
delete globalThis.fonts;
|
|
236
|
+
else
|
|
237
|
+
globalThis.fonts = oldFonts;
|
|
238
|
+
Metrics.clear();
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
test('Font.load clears measurements when a font finishes loading or fails', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
242
|
+
const oldFace = globalThis.FontFace;
|
|
243
|
+
let settle;
|
|
244
|
+
globalThis.FontFace = class {
|
|
245
|
+
load() {
|
|
246
|
+
return new Promise((resolve, reject) => {
|
|
247
|
+
settle = { resolve, reject };
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
try {
|
|
252
|
+
for (const fails of [false, true]) {
|
|
253
|
+
const measured = canvas();
|
|
254
|
+
Element.setTextMeasurementCanvas(measured);
|
|
255
|
+
Element.measureWidth('glyph');
|
|
256
|
+
const loading = Font.load('Test', 'data:font/otf;base64,AA==');
|
|
257
|
+
Element.measureWidth('glyph');
|
|
258
|
+
assert.equal(measured.calls(), 2, 'load start clears old results');
|
|
259
|
+
if (fails) {
|
|
260
|
+
settle.reject(new Error('font failed'));
|
|
261
|
+
yield assert.rejects(loading, /font failed/);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
settle.resolve({ family: 'Test' });
|
|
265
|
+
yield loading;
|
|
266
|
+
}
|
|
267
|
+
Element.measureWidth('glyph');
|
|
268
|
+
assert.equal(measured.calls(), 3, 'settlement clears fallback results');
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
finally {
|
|
272
|
+
if (oldFace === undefined)
|
|
273
|
+
delete globalThis.FontFace;
|
|
274
|
+
else
|
|
275
|
+
globalThis.FontFace = oldFace;
|
|
276
|
+
Metrics.clear();
|
|
277
|
+
}
|
|
278
|
+
}));
|
|
@@ -73,6 +73,8 @@ export declare class Element {
|
|
|
73
73
|
/** Canvas used to measure text. See measureText(): TextMetrics. */
|
|
74
74
|
private static txtCanvas?;
|
|
75
75
|
static setTextMeasurementCanvas(canvas: HTMLCanvasElement | OffscreenCanvas): void;
|
|
76
|
+
/** Clear shared measurements after external font registration or custom context changes. */
|
|
77
|
+
static clearTextMeasurementCache(): void;
|
|
76
78
|
static getTextMeasurementCanvas(): HTMLCanvasElement | OffscreenCanvas | undefined;
|
|
77
79
|
private context?;
|
|
78
80
|
protected attrs: ElementAttributes;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
type MeasurementContext = CanvasRenderingContext2D | OffscreenCanvasRenderingContext2D;
|
|
2
|
+
/** Internal cache shared by Element.measureText() and Element.measureWidth(). */
|
|
3
|
+
export declare class TextMetricsCache {
|
|
4
|
+
private static readonly maxEntries;
|
|
5
|
+
private static readonly maxKeyLength;
|
|
6
|
+
private static entries;
|
|
7
|
+
private static context?;
|
|
8
|
+
private static fonts?;
|
|
9
|
+
private static fontCount?;
|
|
10
|
+
private static fontStatus?;
|
|
11
|
+
private static readonly onFontsChanged;
|
|
12
|
+
/** Also used by Metrics.clear() and explicit canvas/font replacement. */
|
|
13
|
+
static clear(): void;
|
|
14
|
+
/** Track document/worker fonts without retaining listeners on a replaced document. */
|
|
15
|
+
private static fontsReady;
|
|
16
|
+
/** Preserve all TextMetrics fields, which are prototype getters in browsers. */
|
|
17
|
+
private static snapshot;
|
|
18
|
+
/** Key shaping and bounds settings as well as font/text; never cache fallback during loading. */
|
|
19
|
+
static measure(context: MeasurementContext, text: string): Readonly<TextMetrics>;
|
|
20
|
+
}
|
|
21
|
+
export {};
|
package/changelog/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
# 5.1.1
|
|
2
|
+
|
|
3
|
+
## Fixes and performance
|
|
4
|
+
|
|
5
|
+
- Cache repeated text measurements with bounded storage and clear cached metrics when fonts change.
|
|
6
|
+
- Keep copied metrics and styles independent without deep-cloning unrelated state.
|
|
7
|
+
- Document UTF-8 encoding in the HTML setup example.
|
|
8
|
+
|
|
9
|
+
## Explorer
|
|
10
|
+
|
|
11
|
+
- Use a compact layout with an 80% preview and the inspector beside the score.
|
|
12
|
+
- Add locally bundled CodeJar editing and Prism syntax highlighting.
|
|
13
|
+
- Format all examples consistently and add explanatory comments.
|
|
14
|
+
- Fix the keyboard shortcut regression test and isolate browser state between tests.
|
|
15
|
+
|
|
1
16
|
# 5.1.0
|
|
2
17
|
|
|
3
18
|
## Features
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vexflow",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.1",
|
|
4
4
|
"description": "A JavaScript library for rendering music notation and guitar tablature.",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -86,6 +86,7 @@
|
|
|
86
86
|
"@vexflow-fonts/sebastiantext": "^1.0.0",
|
|
87
87
|
"canvas": "3.2.3",
|
|
88
88
|
"circular-dependency-plugin": "^5.2.2",
|
|
89
|
+
"codejar": "5.0.0",
|
|
89
90
|
"concurrently": "^9.1.2",
|
|
90
91
|
"eslint": "^9.28.0",
|
|
91
92
|
"eslint-config-prettier": "^10.1.5",
|
|
@@ -106,6 +107,7 @@
|
|
|
106
107
|
"opener": "^1.5.2",
|
|
107
108
|
"opentype.js": "^1.3.4",
|
|
108
109
|
"prettier": "^3.5.3",
|
|
110
|
+
"prismjs": "1.30.0",
|
|
109
111
|
"puppeteer": "^25.17.0",
|
|
110
112
|
"qunit": "^2.24.1",
|
|
111
113
|
"recursive-copy": "^2.0.14",
|