Browse Source

Support standards fonts

Vivien Nicolas 14 years ago
parent
commit
aa41a75f6e
  1. 87
      fonts.js
  2. 36
      pdf.js

87
fonts.js

@ -15,11 +15,51 @@ var kMaxFontFileSize = 200000;
var kMaxWaitForFontFace = 1000; var kMaxWaitForFontFace = 1000;
/** /**
* Hold a map of decoded fonts and of the standard fourteen Type1 fonts and * Hold a map of decoded fonts and of the standard fourteen Type1
* their acronyms. * fonts and their acronyms.
* TODO Add the standard fourteen Type1 fonts list by default
* http://cgit.freedesktop.org/poppler/poppler/tree/poppler/GfxFont.cc#n65
*/ */
var stdFontMap = {
"Arial": "Helvetica",
"Arial_Bold": "Helvetica-Bold",
"Arial_BoldItalic": "Helvetica-BoldOblique",
"Arial_Italic": "Helvetica-Oblique",
"Arial_BoldItalicMT": "Helvetica-BoldOblique",
"Arial_BoldMT": "Helvetica-Bold",
"Arial_ItalicMT": "Helvetica-Oblique",
"ArialMT": "Helvetica",
"Courier_Bold": "Courier-Bold",
"Courier_BoldItalic": "Courier-BoldOblique",
"Courier_Italic": "Courier-Oblique",
"CourierNew": "Courier",
"CourierNew_Bold": "Courier-Bold",
"CourierNew_BoldItalic": "Courier-BoldOblique",
"CourierNew_Italic": "Courier-Oblique",
"CourierNewPS_BoldItalicMT": "Courier-BoldOblique",
"CourierNewPS_BoldMT": "Courier-Bold",
"CourierNewPS_ItalicMT": "Courier-Oblique",
"CourierNewPSMT": "Courier",
"Helvetica_Bold": "Helvetica-Bold",
"Helvetica_BoldItalic": "Helvetica-BoldOblique",
"Helvetica_Italic": "Helvetica-Oblique",
"Symbol_Bold": "Symbol",
"Symbol_BoldItalic": "Symbol",
"Symbol_Italic": "Symbol",
"TimesNewRoman": "Times-Roman",
"TimesNewRoman_Bold": "Times-Bold",
"TimesNewRoman_BoldItalic": "Times-BoldItalic",
"TimesNewRoman_Italic": "Times-Italic",
"TimesNewRomanPS": "Times-Roman",
"TimesNewRomanPS_Bold": "Times-Bold",
"TimesNewRomanPS_BoldItalic": "Times-BoldItalic",
"TimesNewRomanPS_BoldItalicMT": "Times-BoldItalic",
"TimesNewRomanPS_BoldMT": "Times-Bold",
"TimesNewRomanPS_Italic": "Times-Italic",
"TimesNewRomanPS_ItalicMT": "Times-Italic",
"TimesNewRomanPSMT": "Times-Roman",
"TimesNewRomanPSMT_Bold": "Times-Bold",
"TimesNewRomanPSMT_BoldItalic": "Times-BoldItalic",
"TimesNewRomanPSMT_Italic": "Times-Italic"
};
var FontMeasure = (function FontMeasure() { var FontMeasure = (function FontMeasure() {
var kScalePrecision = 50; var kScalePrecision = 50;
@ -39,7 +79,12 @@ var FontMeasure = (function FontMeasure() {
measureCache = null measureCache = null
} }
ctx.font = (size * kScalePrecision) + 'px "' + font.loadedName + '"'; var name = font.loadedName;
var bold = font.bold ? "bold" : "normal";
var italic = font.italic ? "italic" : "normal";
size *= kScalePrecision;
var rule = bold + " " + italic + " " + size + 'px "' + name + '"';
ctx.font = rule;
}, },
measureText: function fonts_measureText(text) { measureText: function fonts_measureText(text) {
var width; var width;
@ -78,19 +123,20 @@ var FontLoader = {
var font = fonts[i]; var font = fonts[i];
var obj = new Font(font.name, font.file, font.properties); var obj = new Font(font.name, font.file, font.properties);
obj.loading = true;
objs.push(obj); objs.push(obj);
var str = ''; var str = '';
var data = obj.data; var data = obj.data;
var length = data.length; if (data) {
for (var j = 0; j < length; j++) var length = data.length;
str += String.fromCharCode(data[j]); for (var j = 0; j < length; j++)
str += String.fromCharCode(data[j]);
var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
if (rule) { var rule = isWorker ? obj.bindWorker(str) : obj.bindDOM(str);
rules.push(rule); if (rule) {
names.push(obj.loadedName); rules.push(rule);
names.push(obj.loadedName);
}
} }
} }
@ -350,6 +396,16 @@ var Font = (function Font() {
// to avoid the cost of waiting for it be be loaded by the platform. // to avoid the cost of waiting for it be be loaded by the platform.
if (properties.ignore) { if (properties.ignore) {
this.loadedName = 'Arial'; this.loadedName = 'Arial';
this.loading = false;
return;
}
if (!file) {
var fontName = stdFontMap[name];
this.bold = (fontName.indexOf("Bold") != -1);
this.italic = (fontName.indexOf("Oblique") != -1);
this.loadedName = fontName.split("-")[0];
this.loading = false;
return; return;
} }
@ -385,10 +441,11 @@ var Font = (function Font() {
} }
this.data = data; this.data = data;
this.textMatrix = properties.textMatrix || IDENTITY_MATRIX;
this.type = properties.type; this.type = properties.type;
this.textMatrix = properties.textMatrix;
this.loadedName = getUniqueName(); this.loadedName = getUniqueName();
this.compositeFont = properties.compositeFont; this.compositeFont = properties.compositeFont;
this.loading = true;
}; };
var numFonts = 0; var numFonts = 0;

36
pdf.js

@ -3662,11 +3662,6 @@ var PartialEvaluator = (function() {
assertWellFormed(IsName(fontName), 'invalid font name'); assertWellFormed(IsName(fontName), 'invalid font name');
fontName = fontName.name.replace(/[\+,\-]/g, '_'); fontName = fontName.name.replace(/[\+,\-]/g, '_');
var fontFile = descriptor.get('FontFile', 'FontFile2', 'FontFile3');
if (!fontFile)
error('FontFile not found for font: ' + fontName);
fontFile = xref.fetchIfRef(fontFile);
var encodingMap = {}; var encodingMap = {};
var charset = []; var charset = [];
if (compositeFont) { if (compositeFont) {
@ -3821,10 +3816,15 @@ var PartialEvaluator = (function() {
} }
} }
if (fontFile && fontFile.dict) { var fontFile = descriptor.get('FontFile', 'FontFile2', 'FontFile3');
var fileType = fontFile.dict.get('Subtype'); if (fontFile) {
if (fileType) fontFile = xref.fetchIfRef(fontFile);
fileType = fileType.name;
if (fontFile.dict) {
var fileType = fontFile.dict.get('Subtype');
if (fileType)
fileType = fileType.name;
}
} }
var widths = fontDict.get('Widths'); var widths = fontDict.get('Widths');
@ -4154,26 +4154,30 @@ var CanvasGraphics = (function() {
if (!font) if (!font)
return; return;
var fontName = ''; var name = '';
var fontObj = font.fontObj; var fontObj = font.fontObj;
if (fontObj) if (fontObj)
fontName = fontObj.loadedName; name = fontObj.loadedName;
if (!fontName) { if (!name) {
// TODO: fontDescriptor is not available, fallback to default font // TODO: fontDescriptor is not available, fallback to default font
fontName = 'sans-serif'; name = 'sans-serif';
} }
this.current.font = fontObj; this.current.font = fontObj;
this.current.fontSize = size; this.current.fontSize = size;
if (this.ctx.$setFont) { if (this.ctx.$setFont) {
this.ctx.$setFont(fontName, size); this.ctx.$setFont(name, size);
} else { } else {
FontMeasure.setActive(fontObj, size); FontMeasure.setActive(fontObj, size);
size = (size <= kRasterizerMin) ? size * kScalePrecision : size; size = (size <= kRasterizerMin) ? size * kScalePrecision : size;
this.ctx.font = size + 'px "' + fontName + '"';
var bold = fontObj.bold ? "bold" : "normal";
var italic = fontObj.italic ? "italic" : "normal";
var rule = bold + " " + italic + " " + size + 'px "' + name + '"';
this.ctx.font = rule;
} }
}, },
setTextRenderingMode: function(mode) { setTextRenderingMode: function(mode) {
@ -4224,7 +4228,7 @@ var CanvasGraphics = (function() {
scaleFactorX = scaleFactorY = kScalePrecision; scaleFactorX = scaleFactorY = kScalePrecision;
ctx.scale(1 / scaleFactorX, 1 / scaleFactorY); ctx.scale(1 / scaleFactorX, 1 / scaleFactorY);
} }
ctx.transform.apply(ctx, font.textMatrix); ctx.transform.apply(ctx, font.textMatrix || IDENTITY_MATRIX);
text = font.charsToUnicode(text); text = font.charsToUnicode(text);
} }

Loading…
Cancel
Save