Skip to content

Commit c281d99

Browse files
committed
Be more general in font check
1 parent b2aa6c2 commit c281d99

5 files changed

Lines changed: 10 additions & 90 deletions

File tree

‎src/lib/font_family_names.ts‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎src/lib/index.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ lib.cleanNumber = require('./clean_number').default;
219219

220220
lib.slugify = require('./slugify');
221221

222-
lib.fontFamilyNames = require('./font_family_names').fontFamilyNames;
223-
224222
lib.ensureNumber = function ensureNumber(v) {
225223
if (!isNumeric(v)) return BADNUM;
226224
v = Number(v);

‎src/plot_api/plot_api.js‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -418,26 +418,20 @@ function _doPlot(gd, data, layout, config) {
418418
}
419419

420420
/**
421-
* Redraw a plot after the web fonts that its text uses finish loading.
421+
* Redraw a plot after the web fonts that load during its draw finish loading.
422422
*
423423
* A plot that draws before its web font loads measures its text with the fallback font.
424-
* The redraw happens after the draw that calls this function, once every face of a used family
425-
* that started or finished loading during the draw settles. A face that fails to load still settles.
424+
* The redraw happens after the draw that calls this function, once every face that started or
425+
* finished loading during the draw settles. A face that fails to load still settles.
426+
* A font that other text loads during the draw also causes one redraw.
426427
* A later draw or `Plots.purge` cancels a pending redraw.
427428
*
428429
* @param gd - The graph div that finished a draw
429430
* @param unloadedFaces - The faces in `document.fonts` that were not loaded when the draw started
430431
*/
431432
async function redrawAfterFontLoads(gd, unloadedFaces) {
432433
// The draw started these loads, or they finished during the draw
433-
const changedFaces = unloadedFaces.filter((face) => ['loaded', 'loading'].includes(face.status));
434-
if (!changedFaces.length) return;
435-
436-
const usedFamilies = new Set();
437-
for (const node of gd.querySelectorAll('[style*="font-family"]')) {
438-
for (const family of Lib.fontFamilyNames(node.style.fontFamily)) usedFamilies.add(family);
439-
}
440-
const faces = changedFaces.filter((face) => usedFamilies.has(Lib.fontFamilyNames(face.family)[0]));
434+
const faces = unloadedFaces.filter((face) => ['loaded', 'loading'].includes(face.status));
441435
if (!faces.length) return;
442436

443437
const token = {};

‎test/jasmine/tests/lib_test.js‎

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1996,45 +1996,6 @@ describe('Test lib.js:', function () {
19961996
});
19971997
});
19981998

1999-
describe('fontFamilyNames', () => {
2000-
it('should split a font stack into lowercase names', () => {
2001-
expect(Lib.fontFamilyNames('"Open Sans", verdana, Arial, sans-serif')).toEqual([
2002-
'open sans',
2003-
'verdana',
2004-
'arial',
2005-
'sans-serif'
2006-
]);
2007-
});
2008-
2009-
it('should return a single unquoted name', () => {
2010-
expect(Lib.fontFamilyNames('ReproMono')).toEqual(['repromono']);
2011-
});
2012-
2013-
it('should strip double and single quotes', () => {
2014-
expect(Lib.fontFamilyNames(`"Open Sans", 'Fira Code'`)).toEqual(['open sans', 'fira code']);
2015-
});
2016-
2017-
it('should keep a comma inside quotes as part of the name', () => {
2018-
expect(Lib.fontFamilyNames('"Foo, Bar", serif')).toEqual(['foo, bar', 'serif']);
2019-
expect(Lib.fontFamilyNames(`'Foo, Bar', serif`)).toEqual(['foo, bar', 'serif']);
2020-
});
2021-
2022-
it('should keep a longer name whole instead of matching a shorter one', () => {
2023-
expect(Lib.fontFamilyNames('"Open Sans Condensed", sans-serif')).toEqual([
2024-
'open sans condensed',
2025-
'sans-serif'
2026-
]);
2027-
});
2028-
2029-
it('should ignore spaces around names', () => {
2030-
expect(Lib.fontFamilyNames(' "Open Sans" , verdana ')).toEqual(['open sans', 'verdana']);
2031-
});
2032-
2033-
it('should return an empty array for an empty value', () => {
2034-
expect(Lib.fontFamilyNames('')).toEqual([]);
2035-
});
2036-
});
2037-
20381999
describe('isPlotDiv', function () {
20392000
it('should work on plain objects', function () {
20402001
expect(Lib.isPlotDiv({})).toBe(false);

‎test/jasmine/tests/plot_api_test.js‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3327,11 +3327,11 @@ describe('web font loading', () => {
33273327
});
33283328

33293329
// Each test adds a new family, so that no earlier measurement can be in the bBox cache
3330-
const addFace = (name = `PlotlyTestFont${++faceCount}`) => {
3331-
face = new FontFace(name, `url(${fontUrl})`);
3330+
const addFace = () => {
3331+
const family = `PlotlyTestFont${++faceCount}`;
3332+
face = new FontFace(family, `url(${fontUrl})`);
33323333
document.fonts.add(face);
3333-
// The family comes back as CSS, with quotes when the name needs them
3334-
return face.family;
3334+
return family;
33353335
};
33363336

33373337
const makeFigure = (family) => ({
@@ -3365,24 +3365,7 @@ describe('web font loading', () => {
33653365
.then(done, done.fail);
33663366
});
33673367

3368-
it('should redraw a plot whose quoted font family contains a comma', (done) => {
3369-
const fig = makeFigure(addFace(`PlotlyTest, Font${++faceCount}`));
3370-
let widthBeforeLoad;
3371-
3372-
Plotly.newPlot(gd, fig.data, fig.layout)
3373-
.then(() => {
3374-
expect(face.status).not.toBe('loaded');
3375-
widthBeforeLoad = legendWidth(gd);
3376-
return new Promise((resolve) => gd.once('plotly_afterplot', resolve));
3377-
})
3378-
.then(() => {
3379-
expect(face.status).toBe('loaded');
3380-
expect(legendWidth(gd)).not.toBe(widthBeforeLoad);
3381-
})
3382-
.then(done, done.fail);
3383-
});
3384-
3385-
it('should not redraw a plot that does not use the loaded font', (done) => {
3368+
it('should not redraw a plot when a font loads after its draw', (done) => {
33863369
addFace();
33873370
const fig = makeFigure('Arial');
33883371

0 commit comments

Comments
 (0)