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

Add a cache for glyphs #4453

Merged
merged 1 commit into from
Mar 19, 2014
Merged
Changes from all 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
50 changes: 39 additions & 11 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -2127,6 +2127,29 @@ function adjustWidths(properties) {
properties.defaultWidth *= scale;
}

var Glyph = (function GlyphClosure() {
function Glyph(fontChar, unicode, accent, width, vmetric, operatorList) {
this.fontChar = fontChar;
this.unicode = unicode;
this.accent = accent;
this.width = width;
this.vmetric = vmetric;
this.operatorList = operatorList;
}

Glyph.prototype.matchesForCache =
function(fontChar, unicode, accent, width, vmetric, operatorList) {
return this.fontChar === fontChar &&
this.unicode === unicode &&
this.accent === accent &&
this.width === width &&
this.vmetric === vmetric &&
this.operatorList === operatorList;
};

return Glyph;
})();

/**
* 'Font' is the class the outside world should use, it encapsulate all the font
* decoding logics whatever type it is (assuming the font type is supported).
Expand All @@ -2144,6 +2167,8 @@ var Font = (function FontClosure() {
this.loadCharProcs = properties.coded;
this.sizes = [];

this.glyphCache = {};

var names = name.split('+');
names = names.length > 1 ? names[1] : names[0];
names = names.split(/[-,_]/g)[0];
Expand Down Expand Up @@ -4305,9 +4330,9 @@ var Font = (function FontClosure() {
width = isNum(width) ? width : this.defaultWidth;
var vmetric = this.vmetrics && this.vmetrics[widthCode];

var unicodeChars = this.toUnicode[charcode] || charcode;
if (typeof unicodeChars === 'number') {
unicodeChars = String.fromCharCode(unicodeChars);
var unicode = this.toUnicode[charcode] || charcode;
if (typeof unicode === 'number') {
unicode = String.fromCharCode(unicode);
}

// First try the toFontChar map, if it's not there then try falling
Expand All @@ -4332,14 +4357,17 @@ var Font = (function FontClosure() {
};
}

return {
fontChar: String.fromCharCode(fontCharCode),
unicode: unicodeChars,
accent: accent,
width: width,
vmetric: vmetric,
operatorList: operatorList
};
var fontChar = String.fromCharCode(fontCharCode);

var glyph = this.glyphCache[charcode];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are caching at the glyph level, does it make sense to keep the cache in charsToGlyph around still?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner (glyph) cache reduces the number of glyph objects created. The outer (glyphs) cache reduces the number of glyphs arrays created. They're independent, and both useful.

if (!glyph ||
!glyph.matchesForCache(fontChar, unicode, accent, width, vmetric,
operatorList)) {
glyph = new Glyph(fontChar, unicode, accent, width, vmetric,
operatorList);
this.glyphCache[charcode] = glyph;
}
return glyph;
},

charsToGlyphs: function Font_charsToGlyphs(chars) {
Expand Down