Browse Source

Refactors loadFont for translateFont be async; fixes type3 dup data

Yury Delendik 11 years ago
parent
commit
e5a0d89da9
  1. 173
      src/core/evaluator.js
  2. 23
      src/core/fonts.js
  3. 1
      src/core/obj.js
  4. 7
      src/display/canvas.js

173
src/core/evaluator.js

@ -292,28 +292,26 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
fontArgs = fontArgs.slice(); fontArgs = fontArgs.slice();
fontName = fontArgs[0].name; fontName = fontArgs[0].name;
} }
var self = this;
return this.loadFont(fontName, fontRef, this.xref, resources,
operatorList).then(function (font) {
state.font = font;
var loadedName = font.loadedName;
if (!font.sent) {
var fontData = font.translated.exportData();
self.handler.send('commonobj', [
loadedName,
'Font',
fontData
]);
font.sent = true;
}
return loadedName; var self = this;
return this.loadFont(fontName, fontRef, this.xref, resources).then(
function (translated) {
if (!translated.font.isType3Font) {
return translated;
}
return translated.loadType3Data(self, resources, operatorList).then(
function () {
return translated;
});
}).then(function (translated) {
state.font = translated.font;
translated.send(self.handler);
return translated.loadedName;
}); });
}, },
handleText: function PartialEvaluator_handleText(chars, state) { handleText: function PartialEvaluator_handleText(chars, state) {
var font = state.font.translated; var font = state.font;
var glyphs = font.charsToGlyphs(chars); var glyphs = font.charsToGlyphs(chars);
var isAddToPathSet = !!(state.textRenderingMode & var isAddToPathSet = !!(state.textRenderingMode &
TextRenderingMode.ADD_TO_PATH_FLAG); TextRenderingMode.ADD_TO_PATH_FLAG);
@ -439,16 +437,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
}, },
loadFont: function PartialEvaluator_loadFont(fontName, font, xref, loadFont: function PartialEvaluator_loadFont(fontName, font, xref,
resources, resources) {
parentOperatorList) {
function errorFont() { function errorFont() {
return Promise.resolve({ return Promise.resolve(new TranslatedFont('g_font_error',
translated: new ErrorFont('Font ' + fontName + ' is not available'), new ErrorFont('Font ' + fontName + ' is not available'), font));
loadedName: 'g_font_error'
});
} }
var fontRef; var fontRef;
if (font) { // Loading by ref. if (font) { // Loading by ref.
assert(isRef(font)); assert(isRef(font));
@ -471,6 +465,12 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
return errorFont(); return errorFont();
} }
// We are holding font.translated references just for fontRef that are not
// dictionaries (Dict). See explanation below.
if (font.translated) {
return font.translated;
}
var fontCapability = createPromiseCapability(); var fontCapability = createPromiseCapability();
var preEvaluatedFont = this.preEvaluateFont(font, xref); var preEvaluatedFont = this.preEvaluateFont(font, xref);
@ -487,8 +487,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var aliasFontRef = fontAliases[hash].aliasRef; var aliasFontRef = fontAliases[hash].aliasRef;
if (aliasFontRef && this.fontCache.has(aliasFontRef)) { if (aliasFontRef && this.fontCache.has(aliasFontRef)) {
this.fontCache.putAlias(fontRef, aliasFontRef); this.fontCache.putAlias(fontRef, aliasFontRef);
var cachedFont = this.fontCache.get(fontRef); return this.fontCache.get(fontRef);
return cachedFont;
} }
} }
@ -517,49 +516,28 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
font.loadedName = 'g_font_' + (fontRefIsDict ? font.loadedName = 'g_font_' + (fontRefIsDict ?
fontName.replace(/\W/g, '') : fontID); fontName.replace(/\W/g, '') : fontID);
if (!font.translated) { font.translated = fontCapability.promise;
var translated;
// TODO move promises into translate font
var translatedPromise;
try { try {
translated = this.translateFont(preEvaluatedFont, xref); translatedPromise = Promise.resolve(
this.translateFont(preEvaluatedFont, xref));
} catch (e) { } catch (e) {
UnsupportedManager.notify(UNSUPPORTED_FEATURES.font); translatedPromise = Promise.reject(e);
translated = new ErrorFont(e instanceof Error ? e.message : e);
} }
font.translated = translated;
}
var loadCharProcsPromise = Promise.resolve();
if (font.translated.loadCharProcs) {
var charProcs = font.get('CharProcs').getAll();
var fontResources = (font.get('Resources') || resources);
var charProcKeys = Object.keys(charProcs);
var charProcOperatorList = {};
for (var i = 0, n = charProcKeys.length; i < n; ++i) {
loadCharProcsPromise = loadCharProcsPromise.then(function (key) {
var glyphStream = charProcs[key];
var operatorList = new OperatorList();
return this.getOperatorList(glyphStream, fontResources,
operatorList).
then(function () {
charProcOperatorList[key] = operatorList.getIR();
// Add the dependencies to the parent operator list so they are translatedPromise.then(function (translatedFont) {
// resolved before sub operator list is executed synchronously. fontCapability.resolve(new TranslatedFont(font.loadedName,
if (parentOperatorList) { translatedFont, font));
parentOperatorList.addDependencies(operatorList.dependencies); }, function (reason) {
} // TODO fontCapability.reject?
}); UnsupportedManager.notify(UNSUPPORTED_FEATURES.font);
}.bind(this, charProcKeys[i])); fontCapability.resolve(new TranslatedFont(font.loadedName,
} new ErrorFont(reason instanceof Error ? reason.message : reason),
loadCharProcsPromise = loadCharProcsPromise.then(function () { font));
font.translated.charProcOperatorList = charProcOperatorList;
}); });
}
return loadCharProcsPromise.then(function () {
font.loaded = true;
fontCapability.resolve(font);
return fontCapability.promise; return fontCapability.promise;
}, fontCapability.reject);
}, },
buildPath: function PartialEvaluator_buildPath(operatorList, fn, args) { buildPath: function PartialEvaluator_buildPath(operatorList, fn, args) {
@ -830,11 +808,10 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
function handleSetFont(fontName, fontRef) { function handleSetFont(fontName, fontRef) {
return self.loadFont(fontName, fontRef, xref, resources, null). return self.loadFont(fontName, fontRef, xref, resources).
then(function (font) { then(function (translated) {
var fontTranslated = textState.font = font.translated; textState.font = translated.font;
textState.fontMatrix = fontTranslated.fontMatrix ? textState.fontMatrix = translated.font.fontMatrix ||
fontTranslated.fontMatrix :
FONT_IDENTITY_MATRIX; FONT_IDENTITY_MATRIX;
}); });
} }
@ -1609,7 +1586,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
this.extractWidths(dict, xref, descriptor, properties); this.extractWidths(dict, xref, descriptor, properties);
if (type.name === 'Type3') { if (type.name === 'Type3') {
properties.coded = true; properties.isType3Font = true;
} }
return new Font(fontName.name, fontFile, properties); return new Font(fontName.name, fontFile, properties);
@ -1619,6 +1596,64 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
return PartialEvaluator; return PartialEvaluator;
})(); })();
var TranslatedFont = (function TranslatedFontClosure() {
function TranslatedFont(loadedName, font, dict) {
this.loadedName = loadedName;
this.font = font;
this.dict = dict;
this.type3Loaded = null;
this.sent = false;
}
TranslatedFont.prototype = {
send: function (handler) {
if (this.sent) {
return;
}
var fontData = this.font.exportData();
handler.send('commonobj', [
this.loadedName,
'Font',
fontData
]);
this.sent = true;
},
loadType3Data: function (evaluator, resources, parentOperatorList) {
assert(this.font.isType3Font);
if (this.type3Loaded) {
return this.type3Loaded;
}
var translatedFont = this.font;
var loadCharProcsPromise = Promise.resolve();
var charProcs = this.dict.get('CharProcs').getAll();
var fontResources = this.dict.get('Resources') || resources;
var charProcKeys = Object.keys(charProcs);
var charProcOperatorList = {};
for (var i = 0, n = charProcKeys.length; i < n; ++i) {
loadCharProcsPromise = loadCharProcsPromise.then(function (key) {
var glyphStream = charProcs[key];
var operatorList = new OperatorList();
return evaluator.getOperatorList(glyphStream, fontResources,
operatorList).
then(function () {
charProcOperatorList[key] = operatorList.getIR();
// Add the dependencies to the parent operator list so they are
// resolved before sub operator list is executed synchronously.
parentOperatorList.addDependencies(operatorList.dependencies);
});
}.bind(this, charProcKeys[i]));
}
this.type3Loaded = loadCharProcsPromise.then(function () {
translatedFont.charProcOperatorList = charProcOperatorList;
});
return this.type3Loaded;
}
};
return TranslatedFont;
})();
var OperatorList = (function OperatorListClosure() { var OperatorList = (function OperatorListClosure() {
var CHUNK_SIZE = 1000; var CHUNK_SIZE = 1000;
var CHUNK_SIZE_ABOUT = CHUNK_SIZE - 5; // close to chunk size var CHUNK_SIZE_ABOUT = CHUNK_SIZE - 5; // close to chunk size

23
src/core/fonts.js

@ -2118,23 +2118,23 @@ function adjustWidths(properties) {
} }
var Glyph = (function GlyphClosure() { var Glyph = (function GlyphClosure() {
function Glyph(fontChar, unicode, accent, width, vmetric, operatorList) { function Glyph(fontChar, unicode, accent, width, vmetric, operatorListId) {
this.fontChar = fontChar; this.fontChar = fontChar;
this.unicode = unicode; this.unicode = unicode;
this.accent = accent; this.accent = accent;
this.width = width; this.width = width;
this.vmetric = vmetric; this.vmetric = vmetric;
this.operatorList = operatorList; this.operatorListId = operatorListId;
} }
Glyph.prototype.matchesForCache = Glyph.prototype.matchesForCache =
function(fontChar, unicode, accent, width, vmetric, operatorList) { function(fontChar, unicode, accent, width, vmetric, operatorListId) {
return this.fontChar === fontChar && return this.fontChar === fontChar &&
this.unicode === unicode && this.unicode === unicode &&
this.accent === accent && this.accent === accent &&
this.width === width && this.width === width &&
this.vmetric === vmetric && this.vmetric === vmetric &&
this.operatorList === operatorList; this.operatorListId === operatorListId;
}; };
return Glyph; return Glyph;
@ -2154,8 +2154,7 @@ var Font = (function FontClosure() {
this.name = name; this.name = name;
this.loadedName = properties.loadedName; this.loadedName = properties.loadedName;
this.coded = properties.coded; this.isType3Font = properties.isType3Font;
this.loadCharProcs = properties.coded;
this.sizes = []; this.sizes = [];
this.glyphCache = {}; this.glyphCache = {};
@ -4376,7 +4375,7 @@ var Font = (function FontClosure() {
}, },
charToGlyph: function Font_charToGlyph(charcode) { charToGlyph: function Font_charToGlyph(charcode) {
var fontCharCode, width, operatorList; var fontCharCode, width, operatorListId;
var widthCode = charcode; var widthCode = charcode;
if (this.cMap && charcode in this.cMap.map) { if (this.cMap && charcode in this.cMap.map) {
@ -4398,9 +4397,9 @@ var Font = (function FontClosure() {
fontCharCode = mapSpecialUnicodeValues(fontCharCode); fontCharCode = mapSpecialUnicodeValues(fontCharCode);
} }
if (this.type === 'Type3') { if (this.isType3Font) {
// Font char code in this case is actually a glyph name. // Font char code in this case is actually a glyph name.
operatorList = this.charProcOperatorList[fontCharCode]; operatorListId = fontCharCode;
} }
var accent = null; var accent = null;
@ -4418,9 +4417,9 @@ var Font = (function FontClosure() {
var glyph = this.glyphCache[charcode]; var glyph = this.glyphCache[charcode];
if (!glyph || if (!glyph ||
!glyph.matchesForCache(fontChar, unicode, accent, width, vmetric, !glyph.matchesForCache(fontChar, unicode, accent, width, vmetric,
operatorList)) { operatorListId)) {
glyph = new Glyph(fontChar, unicode, accent, width, vmetric, glyph = new Glyph(fontChar, unicode, accent, width, vmetric,
operatorList); operatorListId);
this.glyphCache[charcode] = glyph; this.glyphCache[charcode] = glyph;
} }
return glyph; return glyph;
@ -4485,6 +4484,8 @@ var Font = (function FontClosure() {
var ErrorFont = (function ErrorFontClosure() { var ErrorFont = (function ErrorFontClosure() {
function ErrorFont(error) { function ErrorFont(error) {
this.error = error; this.error = error;
this.loadedName = 'g_font_error';
this.loading = false;
} }
ErrorFont.prototype = { ErrorFont.prototype = {

1
src/core/obj.js

@ -513,7 +513,6 @@ var Catalog = (function CatalogClosure() {
}); });
return Promise.all(promises).then(function (fonts) { return Promise.all(promises).then(function (fonts) {
for (var i = 0, ii = fonts.length; i < ii; i++) { for (var i = 0, ii = fonts.length; i < ii; i++) {
delete fonts[i].sent;
delete fonts[i].translated; delete fonts[i].translated;
} }
this.fontCache.clear(); this.fontCache.clear();

7
src/display/canvas.js

@ -1196,7 +1196,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.current.font = fontObj; this.current.font = fontObj;
this.current.fontSize = size; this.current.fontSize = size;
if (fontObj.coded) { if (fontObj.isType3Font) {
return; // we don't need ctx.font for Type3 fonts return; // we don't need ctx.font for Type3 fonts
} }
@ -1343,7 +1343,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
// Type3 fonts - each glyph is a "mini-PDF" // Type3 fonts - each glyph is a "mini-PDF"
if (font.coded) { if (font.isType3Font) {
ctx.save(); ctx.save();
ctx.transform.apply(ctx, current.textMatrix); ctx.transform.apply(ctx, current.textMatrix);
ctx.translate(current.x, current.y); ctx.translate(current.x, current.y);
@ -1363,7 +1363,8 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
this.save(); this.save();
ctx.scale(fontSize, fontSize); ctx.scale(fontSize, fontSize);
ctx.transform.apply(ctx, fontMatrix); ctx.transform.apply(ctx, fontMatrix);
this.executeOperatorList(glyph.operatorList); var operatorList = font.charProcOperatorList[glyph.operatorListId];
this.executeOperatorList(operatorList);
this.restore(); this.restore();
var transformed = Util.applyTransform([glyph.width, 0], fontMatrix); var transformed = Util.applyTransform([glyph.width, 0], fontMatrix);

Loading…
Cancel
Save