Merge pull request #4815 from yurydelendik/loadFont
Refactors loadFont for translateFont be async
This commit is contained in:
commit
fe27a76004
@ -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;
|
var self = this;
|
||||||
return this.loadFont(fontName, fontRef, this.xref, resources,
|
return this.loadFont(fontName, fontRef, this.xref, resources).then(
|
||||||
operatorList).then(function (font) {
|
function (translated) {
|
||||||
state.font = font;
|
if (!translated.font.isType3Font) {
|
||||||
var loadedName = font.loadedName;
|
return translated;
|
||||||
if (!font.sent) {
|
}
|
||||||
var fontData = font.translated.exportData();
|
return translated.loadType3Data(self, resources, operatorList).then(
|
||||||
|
function () {
|
||||||
self.handler.send('commonobj', [
|
return translated;
|
||||||
loadedName,
|
|
||||||
'Font',
|
|
||||||
fontData
|
|
||||||
]);
|
|
||||||
font.sent = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return loadedName;
|
|
||||||
});
|
});
|
||||||
|
}).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;
|
|
||||||
try {
|
// TODO move promises into translate font
|
||||||
translated = this.translateFont(preEvaluatedFont, xref);
|
var translatedPromise;
|
||||||
} catch (e) {
|
try {
|
||||||
UnsupportedManager.notify(UNSUPPORTED_FEATURES.font);
|
translatedPromise = Promise.resolve(
|
||||||
translated = new ErrorFont(e instanceof Error ? e.message : e);
|
this.translateFont(preEvaluatedFont, xref));
|
||||||
}
|
} catch (e) {
|
||||||
font.translated = translated;
|
translatedPromise = Promise.reject(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
var loadCharProcsPromise = Promise.resolve();
|
translatedPromise.then(function (translatedFont) {
|
||||||
if (font.translated.loadCharProcs) {
|
fontCapability.resolve(new TranslatedFont(font.loadedName,
|
||||||
var charProcs = font.get('CharProcs').getAll();
|
translatedFont, font));
|
||||||
var fontResources = (font.get('Resources') || resources);
|
}, function (reason) {
|
||||||
var charProcKeys = Object.keys(charProcs);
|
// TODO fontCapability.reject?
|
||||||
var charProcOperatorList = {};
|
UnsupportedManager.notify(UNSUPPORTED_FEATURES.font);
|
||||||
for (var i = 0, n = charProcKeys.length; i < n; ++i) {
|
fontCapability.resolve(new TranslatedFont(font.loadedName,
|
||||||
loadCharProcsPromise = loadCharProcsPromise.then(function (key) {
|
new ErrorFont(reason instanceof Error ? reason.message : reason),
|
||||||
var glyphStream = charProcs[key];
|
font));
|
||||||
var operatorList = new OperatorList();
|
});
|
||||||
return this.getOperatorList(glyphStream, fontResources,
|
return fontCapability.promise;
|
||||||
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.
|
|
||||||
if (parentOperatorList) {
|
|
||||||
parentOperatorList.addDependencies(operatorList.dependencies);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}.bind(this, charProcKeys[i]));
|
|
||||||
}
|
|
||||||
loadCharProcsPromise = loadCharProcsPromise.then(function () {
|
|
||||||
font.translated.charProcOperatorList = charProcOperatorList;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return loadCharProcsPromise.then(function () {
|
|
||||||
font.loaded = true;
|
|
||||||
fontCapability.resolve(font);
|
|
||||||
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
|
||||||
|
@ -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 = {
|
||||||
|
@ -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();
|
||||||
|
@ -1195,7 +1195,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
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1342,7 +1342,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);
|
||||||
@ -1362,7 +1362,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…
x
Reference in New Issue
Block a user