Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't reload the entire font range when a glyph is missing in a font #9375

Merged
merged 9 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/render/glyph_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type Entry = {
// null means we've requested the range, but the glyph wasn't included in the result.
glyphs: {[id: number]: StyleGlyph | null},
requests: {[range: number]: Array<Callback<{[_: number]: StyleGlyph | null}>>},
ranges: {[range: number]: boolean | null},
tinySDF?: TinySDF
};

Expand Down Expand Up @@ -52,7 +53,8 @@ class GlyphManager {
if (!entry) {
entry = this.entries[stack] = {
glyphs: {},
requests: {}
requests: {},
ranges: {}
};
}

Expand All @@ -75,6 +77,11 @@ class GlyphManager {
return;
}

if (entry.ranges[range]) {
callback(null, {stack, id, glyph});
return;
}

let requests = entry.requests[range];
if (!requests) {
requests = entry.requests[range] = [];
Expand All @@ -86,6 +93,7 @@ class GlyphManager {
entry.glyphs[+id] = response[+id];
}
}
entry.ranges[range] = true;
}
for (const cb of requests) {
cb(err, response);
Expand Down
30 changes: 30 additions & 0 deletions test/unit/render/glyph_manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ test('GlyphManager requests 0-255 PBF', (t) => {
});
});

test('GlyphManager doesn\'t request twice 0-255 PBF if a glyph is missing', (t) => {
const identityTransform = (url) => ({url});
const stub = t.stub(GlyphManager, 'loadGlyphRange').callsFake((stack, range, urlTemplate, transform, callback) => {
t.equal(stack, 'Arial Unicode MS');
t.equal(range, 0);
t.equal(urlTemplate, 'https://localhost/fonts/v1/{fontstack}/{range}.pbf');
t.equal(transform, identityTransform);
setImmediate(() => callback(null, glyphs));
});

const manager = new GlyphManager(identityTransform);
manager.setURL('https://localhost/fonts/v1/{fontstack}/{range}.pbf');

manager.getGlyphs({'Arial Unicode MS': [0.5]}, (err) => {
t.ifError(err);
t.equal(manager.entries['Arial Unicode MS'].ranges[0], true);
t.equal(stub.calledOnce, true);

// We remove all requests as in getGlyphs code.
delete manager.entries['Arial Unicode MS'].requests[0];

manager.getGlyphs({'Arial Unicode MS': [0.5]}, (err) => {
t.ifError(err);
t.equal(manager.entries['Arial Unicode MS'].ranges[0], true);
t.equal(stub.calledOnce, true);
t.end();
});
});
});

test('GlyphManager requests remote CJK PBF', (t) => {
t.stub(GlyphManager, 'loadGlyphRange').callsFake((stack, range, urlTemplate, transform, callback) => {
setImmediate(() => callback(null, glyphs));
Expand Down