Skip to content

Commit 3ee89ed

Browse files
committed
Replace old font with case insensitve same name
Doesn't affect our -gui but could affect other users, like extensions
1 parent bb8fd4b commit 3ee89ed

File tree

2 files changed

+71
-4
lines changed

2 files changed

+71
-4
lines changed

src/engine/tw-font-manager.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,22 @@ const log = require('../util/log');
2222
*/
2323
const removeInvalidCharacters = font => font.replace(/[^-\w ]/g, '');
2424

25+
/**
26+
* @param {InternalFont[]} fonts Modified in-place
27+
* @param {InternalFont} newFont
28+
* @returns {InternalFont|null}
29+
*/
30+
const addOrUpdateFont = (fonts, newFont) => {
31+
let oldFont;
32+
const oldIndex = fonts.findIndex(i => i.family.toLowerCase() === newFont.family.toLowerCase());
33+
if (oldIndex !== -1) {
34+
oldFont = fonts[oldIndex];
35+
fonts.splice(oldIndex, 1);
36+
}
37+
fonts.push(newFont);
38+
return oldFont;
39+
};
40+
2541
class FontManager extends EventEmitter {
2642
/**
2743
* @param {Runtime} runtime
@@ -129,11 +145,14 @@ class FontManager extends EventEmitter {
129145
if (!this.isValidSystemFont(family)) {
130146
throw new Error('Invalid system font family');
131147
}
132-
this.fonts.push({
148+
const oldFont = addOrUpdateFont(this.fonts, {
133149
system: true,
134150
family,
135151
fallback
136152
});
153+
if (oldFont && !oldFont.system) {
154+
this.updateRenderer();
155+
}
137156
this.changed();
138157
}
139158

@@ -146,14 +165,12 @@ class FontManager extends EventEmitter {
146165
if (!this.isValidCustomFont(family)) {
147166
throw new Error('Invalid custom font family');
148167
}
149-
150-
this.fonts.push({
168+
addOrUpdateFont(this.fonts, {
151169
system: false,
152170
family,
153171
fallback,
154172
asset
155173
});
156-
157174
this.updateRenderer();
158175
this.changed();
159176
}

test/integration/tw_font_manager.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,53 @@ test('restrict removes existing fonts', t => {
714714

715715
t.end();
716716
});
717+
718+
test('overriding existing fonts', t => {
719+
let setCustomFontsCalls = 0;
720+
const mockRenderer = {
721+
setLayerGroupOrdering: () => {},
722+
setCustomFonts: () => {
723+
setCustomFontsCalls++;
724+
}
725+
};
726+
727+
const rt = new Runtime();
728+
rt.attachRenderer(mockRenderer);
729+
rt.attachStorage(makeTestStorage());
730+
const {fontManager, storage} = rt;
731+
732+
let changeEvents = 0;
733+
fontManager.on('change', () => {
734+
changeEvents++;
735+
});
736+
737+
const asset = storage.createAsset(
738+
storage.AssetType.Font,
739+
'ttf',
740+
new Uint8Array([11, 12, 13]),
741+
null,
742+
true
743+
);
744+
745+
fontManager.addCustomFont('TestFont', 'sans-serif', asset);
746+
t.equal(changeEvents, 1);
747+
t.equal(setCustomFontsCalls, 1);
748+
t.same(fontManager.getFonts().map(i => i.name), ['TestFont']);
749+
750+
fontManager.addSystemFont('TestFonT', 'sans-serif');
751+
t.equal(changeEvents, 2);
752+
t.equal(setCustomFontsCalls, 2);
753+
t.same(fontManager.getFonts().map(i => i.name), ['TestFonT']);
754+
755+
fontManager.addSystemFont('TestFONT', 'sans-serif');
756+
t.equal(changeEvents, 3);
757+
t.equal(setCustomFontsCalls, 2);
758+
t.same(fontManager.getFonts().map(i => i.name), ['TestFONT']);
759+
760+
fontManager.addCustomFont('TESTFONT', 'sans-serif', asset);
761+
t.equal(changeEvents, 4);
762+
t.equal(setCustomFontsCalls, 3);
763+
t.same(fontManager.getFonts().map(i => i.name), ['TESTFONT']);
764+
765+
t.end();
766+
});

0 commit comments

Comments
 (0)