Skip to content

Commit

Permalink
Include and use the 14 standard fonts files.
Browse files Browse the repository at this point in the history
  • Loading branch information
brendandahl committed Dec 11, 2020
1 parent 00b4f86 commit 34a28ab
Show file tree
Hide file tree
Showing 26 changed files with 352 additions and 63 deletions.
4 changes: 4 additions & 0 deletions examples/node/pdf2png/pdf2png.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ var pdfjsLib = require("pdfjs-dist/es5/build/pdf.js");
var CMAP_URL = "../../../node_modules/pdfjs-dist/cmaps/";
var CMAP_PACKED = true;

// Where the standard fonts are located.
var STANDARD_FONT_DATA_URL = "../../../node_modules/pdfjs-dist/standard_fonts/";

// Loading file from file system into typed array.
var pdfPath =
process.argv[2] || "../../../web/compressed.tracemonkey-pldi-09.pdf";
Expand All @@ -64,6 +67,7 @@ var loadingTask = pdfjsLib.getDocument({
data: data,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
standardFontDataUrl: STANDARD_FONT_DATA_URL,
});
loadingTask.promise
.then(function (pdfDocument) {
Expand Down
Binary file added external/standard_fonts/FoxitDingbats.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitFixed.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitFixedBold.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitFixedBoldItalic.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitFixedItalic.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSans.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSansBold.pfb
Binary file not shown.
Binary file not shown.
Binary file added external/standard_fonts/FoxitSansItalic.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSerif.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSerifBold.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSerifBoldItalic.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSerifItalic.pfb
Binary file not shown.
Binary file added external/standard_fonts/FoxitSymbol.pfb
Binary file not shown.
27 changes: 27 additions & 0 deletions external/standard_fonts/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2014 PDFium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11 changes: 11 additions & 0 deletions external/standard_fonts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The files in this directory were extracted from Pdfium

Original copyright notice:

```
Copyright 2014 PDFium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
```
40 changes: 40 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,14 @@ function buildGeneric(defines, dir) {
base: "external/bcmaps",
})
.pipe(gulp.dest(dir + "web/cmaps")),
gulp
.src(
["external/standard_fonts/*.pfb", "external/standard_fonts/LICENSE"],
{
base: "external/standard_fonts",
}
)
.pipe(gulp.dest(dir + "web/standard_fonts")),
preprocessHTML("web/viewer.html", defines).pipe(gulp.dest(dir + "web")),
preprocessCSS("web/viewer.css", "generic", defines, true)
.pipe(
Expand Down Expand Up @@ -991,6 +999,14 @@ function buildMinified(defines, dir) {
base: "external/bcmaps",
})
.pipe(gulp.dest(dir + "web/cmaps")),
gulp
.src(
["external/standard_fonts/*.pfb", "external/standard_fonts/LICENSE"],
{
base: "external/standard_fonts",
}
)
.pipe(gulp.dest(dir + "web/standard_fonts")),

preprocessHTML("web/viewer.html", defines).pipe(gulp.dest(dir + "web")),
preprocessCSS("web/viewer.css", "minified", defines, true)
Expand Down Expand Up @@ -1216,6 +1232,14 @@ gulp.task(
base: "external/bcmaps",
})
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web/cmaps")),
gulp
.src(
["external/standard_fonts/*.pfb", "external/standard_fonts/LICENSE"],
{
base: "external/standard_fonts",
}
)
.pipe(gulp.dest(MOZCENTRAL_CONTENT_DIR + "web/standard_fonts")),

preprocessHTML("web/viewer.html", defines).pipe(
gulp.dest(MOZCENTRAL_CONTENT_DIR + "web")
Expand Down Expand Up @@ -1296,6 +1320,17 @@ gulp.task(
base: "external/bcmaps",
})
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + "web/cmaps")),
gulp
.src(
[
"external/standard_fonts/*.pfb",
"external/standard_fonts/LICENSE",
],
{
base: "external/standard_fonts",
}
)
.pipe(gulp.dest(CHROME_BUILD_CONTENT_DIR + "web/standard_fonts")),

preprocessHTML("web/viewer.html", defines).pipe(
gulp.dest(CHROME_BUILD_CONTENT_DIR + "web")
Expand Down Expand Up @@ -2029,6 +2064,11 @@ gulp.task(
gulp
.src(GENERIC_DIR + "web/cmaps/**/*", { base: GENERIC_DIR + "web" })
.pipe(gulp.dest(DIST_DIR)),
gulp
.src(GENERIC_DIR + "web/standard_fonts/**/*", {
base: GENERIC_DIR + "web",
})
.pipe(gulp.dest(DIST_DIR)),
gulp
.src([
GENERIC_DIR + "build/pdf.js",
Expand Down
64 changes: 62 additions & 2 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ import {
Ref,
RefSet,
} from "./primitives.js";
import { DecodeStream, Stream } from "./stream.js";
import {
ErrorFont,
Font,
FontFlags,
getFontType,
getStandardFontName,
IdentityToUnicodeMap,
ToUnicodeMap,
} from "./fonts.js";
Expand Down Expand Up @@ -85,7 +87,6 @@ import {
} from "./image_utils.js";
import { bidi } from "./bidi.js";
import { ColorSpace } from "./colorspace.js";
import { DecodeStream } from "./stream.js";
import { getGlyphsUnicode } from "./glyphlist.js";
import { getMetrics } from "./metrics.js";
import { MurmurHash3_64 } from "./murmurhash3.js";
Expand Down Expand Up @@ -387,6 +388,48 @@ class PartialEvaluator {
return data;
}

async fetchStandardFontData(name) {
const standardFontNameToFileName = {
Courier: "FoxitFixed",
"Courier-Bold": "FoxitFixedBold",
"Courier-BoldOblique": "FoxitFixedBoldItalic",
"Courier-Oblique": "FoxitFixedItalic",
Helvetica: "FoxitSans",
"Helvetica-Bold": "FoxitSansBold",
"Helvetica-BoldOblique": "FoxitSansBoldItalic",
"Helvetica-Oblique": "FoxitSansItalic",
"Times-Roman": "FoxitSerif",
"Times-Bold": "FoxitSerifBold",
"Times-BoldItalic": "FoxitSerifBoldItalic",
"Times-Italic": "FoxitSerifItalic",
Symbol: "FoxitSymbol",
ZapfDingbats: "FoxitDingbats",
};

const readableStream = this.handler.sendWithStream(
"FetchStandardFontData",
{
filename: standardFontNameToFileName[name],
}
);
const reader = readableStream.getReader();

const data = await new Promise(function (resolve, reject) {
function pump() {
reader.read().then(function ({ value, done }) {
if (done) {
return;
}
resolve(value.data);
pump();
}, reject);
}
pump();
});

return data;
}

async buildFormXObject(
resources,
xobj,
Expand Down Expand Up @@ -3376,13 +3419,21 @@ class PartialEvaluator {
properties = {
type,
name: baseFontName,
loadedName: baseDict.loadedName,
widths: metrics.widths,
defaultWidth: metrics.defaultWidth,
flags,
firstChar,
lastChar,
};
const widths = dict.get("Widths");
const standardFontName = getStandardFontName(baseFontName);
let file = null;
if (standardFontName) {
const data = await this.fetchStandardFontData(standardFontName);
file = new Stream(data);
properties.isStandardFont = true;
}
return this.extractDataStructures(dict, dict, properties).then(
newProperties => {
if (widths) {
Expand All @@ -3398,7 +3449,7 @@ class PartialEvaluator {
newProperties
);
}
return new Font(baseFontName, null, newProperties);
return new Font(baseFontName, file, newProperties);
}
);
}
Expand Down Expand Up @@ -3441,6 +3492,7 @@ class PartialEvaluator {
throw new FormatError("invalid font name");
}

let isStandardFont = false;
var fontFile = descriptor.get("FontFile", "FontFile2", "FontFile3");
if (fontFile) {
if (fontFile.dict) {
Expand All @@ -3452,6 +3504,13 @@ class PartialEvaluator {
var length2 = fontFile.dict.get("Length2");
var length3 = fontFile.dict.get("Length3");
}
} else if (type === "Type1") {
const standardFontName = getStandardFontName(fontName.name);
if (standardFontName) {
const data = await this.fetchStandardFontData(standardFontName);
fontFile = new Stream(data);
isStandardFont = true;
}
}

properties = {
Expand All @@ -3462,6 +3521,7 @@ class PartialEvaluator {
length1,
length2,
length3,
isStandardFont,
loadedName: baseDict.loadedName,
composite,
fixedPitch: false,
Expand Down
22 changes: 21 additions & 1 deletion src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,12 @@ function getFontType(type, subtype) {
}
}

function getStandardFontName(name) {
const fontName = name.replace(/[,_]/g, "-").replace(/\s/g, "");
const stdFontMap = getStdFontMap();
return stdFontMap[fontName];
}

// Some bad PDF generators, e.g. Scribus PDF, include glyph names
// in a 'uniXXXX' format -- attempting to recover proper ones.
function recoverGlyphName(name, glyphsUnicodeMap) {
Expand Down Expand Up @@ -3393,7 +3399,17 @@ function type1FontGlyphMapping(properties, builtInEncoding, glyphNames) {
var glyphId, charCode, baseEncoding;
var isSymbolicFont = !!(properties.flags & FontFlags.Symbolic);

if (properties.baseEncodingName) {
if (properties.isStandardFont) {
baseEncoding = builtInEncoding;
for (charCode = 0; charCode < baseEncoding.length; charCode++) {
glyphId = glyphNames.indexOf(baseEncoding[charCode]);
if (glyphId >= 0) {
charCodeToGlyphId[charCode] = glyphId;
} else {
charCodeToGlyphId[charCode] = 0; // notdef
}
}
} else if (properties.baseEncodingName) {
// If a valid base encoding name was used, the mapping is initialized with
// that.
baseEncoding = getEncoding(properties.baseEncodingName);
Expand Down Expand Up @@ -3912,6 +3928,9 @@ var CFFFont = (function CFFFontClosure() {
}

var encoding = cff.encoding ? cff.encoding.encoding : null;
if (properties.isStandardFont) {
encoding = properties.defaultEncoding;
}
charCodeToGlyphId = type1FontGlyphMapping(properties, encoding, charsets);
return charCodeToGlyphId;
},
Expand All @@ -3931,4 +3950,5 @@ export {
ToUnicodeMap,
IdentityToUnicodeMap,
getFontType,
getStandardFontName,
};
22 changes: 17 additions & 5 deletions src/core/standard_fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ import { getLookupTableFactory } from "./core_utils.js";
* fonts and their acronyms.
*/
const getStdFontMap = getLookupTableFactory(function (t) {
// The standard 14 fonts:
t["Times-Roman"] = "Times-Roman";
t.Helvetica = "Helvetica";
t.Courier = "Courier";
t.Symbol = "Symbol";
t["Times-Bold"] = "Times-Bold";
t["Helvetica-Bold"] = "Helvetica-Bold";
t["Courier-Bold"] = "Courier-Bold";
t.ZapfDingbats = "ZapfDingbats";
t["Times-Italic"] = "Times-Italic";
t["Helvetica-Oblique"] = "Helvetica-Oblique";
t["Courier-Oblique"] = "Courier-Oblique";
t["Times-BoldItalic"] = "Times-BoldItalic";
t["Helvetica-BoldOblique"] = "Helvetica-BoldOblique";
t["Courier-BoldOblique"] = "Courier-BoldOblique";

// Extra mappings
t.ArialNarrow = "Helvetica";
t["ArialNarrow-Bold"] = "Helvetica-Bold";
t["ArialNarrow-BoldItalic"] = "Helvetica-BoldOblique";
Expand All @@ -40,7 +57,6 @@ const getStdFontMap = getLookupTableFactory(function (t) {
t["Arial-BoldMT"] = "Helvetica-Bold";
t["Arial-ItalicMT"] = "Helvetica-Oblique";
t.ArialMT = "Helvetica";
t["Courier-Bold"] = "Courier-Bold";
t["Courier-BoldItalic"] = "Courier-BoldOblique";
t["Courier-Italic"] = "Courier-Oblique";
t.CourierNew = "Courier";
Expand All @@ -51,12 +67,8 @@ const getStdFontMap = getLookupTableFactory(function (t) {
t["CourierNewPS-BoldMT"] = "Courier-Bold";
t["CourierNewPS-ItalicMT"] = "Courier-Oblique";
t.CourierNewPSMT = "Courier";
t.Helvetica = "Helvetica";
t["Helvetica-Bold"] = "Helvetica-Bold";
t["Helvetica-BoldItalic"] = "Helvetica-BoldOblique";
t["Helvetica-BoldOblique"] = "Helvetica-BoldOblique";
t["Helvetica-Italic"] = "Helvetica-Oblique";
t["Helvetica-Oblique"] = "Helvetica-Oblique";
t["Symbol-Bold"] = "Symbol";
t["Symbol-BoldItalic"] = "Symbol";
t["Symbol-Italic"] = "Symbol";
Expand Down

0 comments on commit 34a28ab

Please sign in to comment.