Browse Source

Merge pull request #2124 from yurydelendik/fallback-font

Provides right fallback fonts for text layer
Brendan Dahl 13 years ago
parent
commit
8dc49a3c9b
  1. 7
      src/canvas.js
  2. 23
      src/evaluator.js
  3. 10
      src/fonts.js
  4. 2
      test/driver.js
  5. 4
      web/viewer.js

7
src/canvas.js

@ -596,8 +596,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
(fontObj.bold ? 'bold' : 'normal'); (fontObj.bold ? 'bold' : 'normal');
var italic = fontObj.italic ? 'italic' : 'normal'; var italic = fontObj.italic ? 'italic' : 'normal';
var serif = fontObj.isSerifFont ? 'serif' : 'sans-serif'; var typeface = '"' + name + '", ' + fontObj.fallbackName;
var typeface = '"' + name + '", ' + serif;
// Some font backends cannot handle fonts below certain size. // Some font backends cannot handle fonts below certain size.
// Keeping the font at minimal size and using the fontSizeScale to change // Keeping the font at minimal size and using the fontSizeScale to change
@ -792,7 +791,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
if (textSelection) if (textSelection)
this.textLayer.appendText(text, font.loadedName, fontSize); this.textLayer.appendText(text, font.fallbackName, fontSize);
return text; return text;
}, },
@ -856,7 +855,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
} }
if (textSelection) if (textSelection)
this.textLayer.appendText(text, font.loadedName, fontSize); this.textLayer.appendText(text, font.fallbackName, fontSize);
}, },
nextLineShowText: function CanvasGraphics_nextLineShowText(text) { nextLineShowText: function CanvasGraphics_nextLineShowText(text) {
this.nextLine(); this.nextLine();

23
src/evaluator.js

@ -825,21 +825,41 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
} }
} }
// Heuristic: detection of monospace font by checking all non-zero widths
var isMonospace = true, firstWidth = defaultWidth;
for (var glyph in glyphsWidths) {
var glyphWidth = glyphsWidths[glyph];
if (!glyphWidth)
continue;
if (!firstWidth) {
firstWidth = glyphWidth;
continue;
}
if (firstWidth != glyphWidth) {
isMonospace = false;
break;
}
}
if (isMonospace)
properties.flags |= FontFlags.FixedPitch;
properties.defaultWidth = defaultWidth; properties.defaultWidth = defaultWidth;
properties.widths = glyphsWidths; properties.widths = glyphsWidths;
}, },
getBaseFontMetrics: function PartialEvaluator_getBaseFontMetrics(name) { getBaseFontMetrics: function PartialEvaluator_getBaseFontMetrics(name) {
var defaultWidth = 0, widths = []; var defaultWidth = 0, widths = [], monospace = false;
var glyphWidths = Metrics[stdFontMap[name] || name]; var glyphWidths = Metrics[stdFontMap[name] || name];
if (isNum(glyphWidths)) { if (isNum(glyphWidths)) {
defaultWidth = glyphWidths; defaultWidth = glyphWidths;
monospace = true;
} else { } else {
widths = glyphWidths; widths = glyphWidths;
} }
return { return {
defaultWidth: defaultWidth, defaultWidth: defaultWidth,
monospace: monospace,
widths: widths widths: widths
}; };
}, },
@ -893,6 +913,7 @@ var PartialEvaluator = (function PartialEvaluatorClosure() {
var fontNameWoStyle = baseFontName.split('-')[0]; var fontNameWoStyle = baseFontName.split('-')[0];
var flags = (serifFonts[fontNameWoStyle] || var flags = (serifFonts[fontNameWoStyle] ||
(fontNameWoStyle.search(/serif/gi) != -1) ? FontFlags.Serif : 0) | (fontNameWoStyle.search(/serif/gi) != -1) ? FontFlags.Serif : 0) |
(metrics.monospace ? FontFlags.FixedPitch : 0) |
(symbolsFonts[fontNameWoStyle] ? FontFlags.Symbolic : (symbolsFonts[fontNameWoStyle] ? FontFlags.Symbolic :
FontFlags.Nonsymbolic); FontFlags.Nonsymbolic);

10
src/fonts.js

@ -1522,17 +1522,13 @@ var Font = (function FontClosure() {
names = names.split(/[-,_]/g)[0]; names = names.split(/[-,_]/g)[0];
this.isSerifFont = !!(properties.flags & FontFlags.Serif); this.isSerifFont = !!(properties.flags & FontFlags.Serif);
this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic); this.isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);
this.isMonospace = !!(properties.flags & FontFlags.FixedPitch);
var type = properties.type; var type = properties.type;
this.type = type; this.type = type;
// If the font is to be ignored, register it like an already loaded font this.fallbackName = this.isMonospace ? 'monospace' :
// to avoid the cost of waiting for it be be loaded by the platform. this.isSerifFont ? 'serif' : 'sans-serif';
if (properties.ignore) {
this.loadedName = this.isSerifFont ? 'serif' : 'sans-serif';
this.loading = false;
return;
}
this.differences = properties.differences; this.differences = properties.differences;
this.widths = properties.widths; this.widths = properties.widths;

2
test/driver.js

@ -181,7 +181,7 @@ SimpleTextLayerBuilder.prototype = {
ctx.fill(); ctx.fill();
var textContent = bidi(text, -1); var textContent = bidi(text, -1);
ctx.font = fontHeight + 'px sans-serif'; ctx.font = fontHeight + 'px ' + fontName;
ctx.fillStyle = 'black'; ctx.fillStyle = 'black';
ctx.fillText(textContent, text.geom.x, text.geom.y); ctx.fillText(textContent, text.geom.x, text.geom.y);
} }

4
web/viewer.js

@ -1846,7 +1846,7 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
var textDiv = textDivs.shift(); var textDiv = textDivs.shift();
textLayerDiv.appendChild(textDiv); textLayerDiv.appendChild(textDiv);
ctx.font = textDiv.style.fontSize + ' sans-serif'; ctx.font = textDiv.style.fontSize + ' ' + textDiv.style.fontFamily;
var width = ctx.measureText(textDiv.textContent).width; var width = ctx.measureText(textDiv.textContent).width;
if (width > 0) { if (width > 0) {
@ -1888,9 +1888,9 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
// vScale and hScale already contain the scaling to pixel units // vScale and hScale already contain the scaling to pixel units
var fontHeight = fontSize * text.geom.vScale; var fontHeight = fontSize * text.geom.vScale;
textDiv.dataset.canvasWidth = text.canvasWidth * text.geom.hScale; textDiv.dataset.canvasWidth = text.canvasWidth * text.geom.hScale;
textDiv.dataset.fontName = fontName;
textDiv.style.fontSize = fontHeight + 'px'; textDiv.style.fontSize = fontHeight + 'px';
textDiv.style.fontFamily = fontName;
textDiv.style.left = text.geom.x + 'px'; textDiv.style.left = text.geom.x + 'px';
textDiv.style.top = (text.geom.y - fontHeight) + 'px'; textDiv.style.top = (text.geom.y - fontHeight) + 'px';
textDiv.textContent = PDFJS.bidi(text, -1); textDiv.textContent = PDFJS.bidi(text, -1);

Loading…
Cancel
Save