Skip to content

Commit

Permalink
Merge pull request mozilla#13591 from calixteman/xfa_default_font
Browse files Browse the repository at this point in the history
XFA - Match font family correctly
  • Loading branch information
calixteman authored Jun 21, 2021
2 parents bdfcdd6 + 7cdbc98 commit 2e6d3d6
Show file tree
Hide file tree
Showing 13 changed files with 221 additions and 57 deletions.
5 changes: 4 additions & 1 deletion src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,9 @@ class PDFDocument {
if (!(descriptor instanceof Dict)) {
continue;
}
const fontFamily = descriptor.get("FontFamily");
let fontFamily = descriptor.get("FontFamily");
// For example, "Wingdings 3" is not a valid font name in the css specs.
fontFamily = fontFamily.replace(/[ ]+([0-9])/g, "$1");
const fontWeight = descriptor.get("FontWeight");

// Angle is expressed in degrees counterclockwise in PDF
Expand Down Expand Up @@ -956,6 +958,7 @@ class PDFDocument {
})
);
}

await Promise.all(promises);
this.xfaFactory.setFonts(pdfFonts);
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ function createNameTable(name, proto) {
class Font {
constructor(name, file, properties) {
this.name = name;
this.psName = null;
this.mimetype = null;
this.disableFontFace = false;

Expand Down Expand Up @@ -2730,6 +2731,7 @@ class Font {
// ... using existing 'name' table as prototype
const namePrototype = readNameTable(tables.name);
tables.name.data = createNameTable(name, namePrototype);
this.psName = namePrototype[0][6] || null;
}

const builder = new OpenTypeFileBuilder(header.version);
Expand Down
25 changes: 4 additions & 21 deletions src/core/xfa/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
* limitations under the License.
*/

import { $fonts, $toHTML } from "./xfa_object.js";
import { $globalData, $toHTML } from "./xfa_object.js";
import { Binder } from "./bind.js";
import { FontFinder } from "./fonts.js";
import { warn } from "../../shared/util.js";
import { XFAParser } from "./parser.js";

Expand All @@ -23,6 +24,7 @@ class XFAFactory {
try {
this.root = new XFAParser().parse(XFAFactory._createDocument(data));
this.form = new Binder(this.root).bind();
this.form[$globalData].template = this.form;
} catch (e) {
warn(`XFA - an error occured during parsing and binding: ${e}`);
}
Expand Down Expand Up @@ -56,26 +58,7 @@ class XFAFactory {
}

setFonts(fonts) {
this.form[$fonts] = Object.create(null);
for (const font of fonts) {
const cssFontInfo = font.cssFontInfo;
const name = cssFontInfo.fontFamily;
if (!this.form[$fonts][name]) {
this.form[$fonts][name] = Object.create(null);
}
let property = "regular";
if (cssFontInfo.italicAngle !== "0") {
if (parseFloat(cssFontInfo.fontWeight) >= 700) {
property = "bolditalic";
} else {
property = "italic";
}
} else if (parseFloat(cssFontInfo.fontWeight) >= 700) {
property = "bold";
}

this.form[$fonts][name][property] = font;
}
this.form[$globalData].fontFinder = new FontFinder(fonts);
}

getPages() {
Expand Down
157 changes: 157 additions & 0 deletions src/core/xfa/fonts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/* Copyright 2021 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { warn } from "../../shared/util.js";

class FontFinder {
constructor(pdfFonts) {
this.fonts = new Map();
this.cache = new Map();
this.warned = new Set();
this.defaultFont = null;
for (const pdfFont of pdfFonts) {
const cssFontInfo = pdfFont.cssFontInfo;
const name = cssFontInfo.fontFamily;
let font = this.fonts.get(name);
if (!font) {
font = Object.create(null);
this.fonts.set(name, font);
if (!this.defaultFont) {
this.defaultFont = font;
}
}
let property = "";
if (cssFontInfo.italicAngle !== "0") {
if (parseFloat(cssFontInfo.fontWeight) >= 700) {
property = "bolditalic";
} else {
property = "italic";
}
} else if (parseFloat(cssFontInfo.fontWeight) >= 700) {
property = "bold";
}

if (!property) {
if (
pdfFont.name.includes("Bold") ||
(pdfFont.psName && pdfFont.psName.includes("Bold"))
) {
property = "bold";
}
if (
pdfFont.name.includes("Italic") ||
pdfFont.name.endsWith("It") ||
(pdfFont.psName &&
(pdfFont.psName.includes("Italic") ||
pdfFont.psName.endsWith("It")))
) {
property += "italic";
}
}

if (!property) {
property = "regular";
}

font[property] = pdfFont;
}

for (const pdfFont of this.fonts.values()) {
if (!pdfFont.regular) {
pdfFont.regular = pdfFont.italic || pdfFont.bold || pdfFont.bolditalic;
}
}
}

getDefault() {
return this.defaultFont;
}

find(fontName, mustWarn = true) {
let font = this.fonts.get(fontName) || this.cache.get(fontName);
if (font) {
return font;
}

const pattern = /,|-| |bolditalic|bold|italic|regular|it/gi;
let name = fontName.replace(pattern, "");
font = this.fonts.get(name);
if (font) {
this.cache.set(fontName, font);
return font;
}
name = name.toLowerCase();

const maybe = [];
for (const [family, pdfFont] of this.fonts.entries()) {
if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
maybe.push(pdfFont);
}
}

if (maybe.length === 0) {
for (const [, pdfFont] of this.fonts.entries()) {
if (
pdfFont.regular.name &&
pdfFont.regular.name
.replace(pattern, "")
.toLowerCase()
.startsWith(name)
) {
maybe.push(pdfFont);
}
}
}

if (maybe.length === 0) {
name = name.replace(/psmt|mt/gi, "");
for (const [family, pdfFont] of this.fonts.entries()) {
if (family.replace(pattern, "").toLowerCase().startsWith(name)) {
maybe.push(pdfFont);
}
}
}

if (maybe.length === 0) {
for (const pdfFont of this.fonts.values()) {
if (
pdfFont.regular.name &&
pdfFont.regular.name
.replace(pattern, "")
.toLowerCase()
.startsWith(name)
) {
maybe.push(pdfFont);
}
}
}

if (maybe.length >= 1) {
if (maybe.length !== 1 && mustWarn) {
warn(`XFA - Too many choices to guess the correct font: ${fontName}`);
}
this.cache.set(fontName, maybe[0]);
return maybe[0];
}

if (mustWarn && !this.warned.has(fontName)) {
this.warned.add(fontName);
warn(`XFA - Cannot find the font: ${fontName}`);
}
return null;
}
}

export { FontFinder };
17 changes: 12 additions & 5 deletions src/core/xfa/html_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ function setMinMaxDimensions(node, style) {
}
}

function layoutText(text, xfaFont, fonts, width) {
const measure = new TextMeasure(xfaFont, fonts);
function layoutText(text, xfaFont, fontFinder, width) {
const measure = new TextMeasure(xfaFont, fontFinder);
if (typeof text === "string") {
measure.addString(text);
} else {
Expand Down Expand Up @@ -448,13 +448,20 @@ function fixTextIndent(styles) {
}
}

function getFonts(family) {
function getFonts(family, fontFinder) {
if (family.startsWith("'") || family.startsWith('"')) {
family = family.slice(1, family.length - 1);
}

const fonts = [`"${family}"`, `"${family}-PdfJS-XFA"`];
return fonts.join(",");
const pdfFont = fontFinder.find(family);
if (pdfFont) {
const { fontFamily } = pdfFont.regular.cssFontInfo;
if (fontFamily !== family) {
return `"${family}","${fontFamily}"`;
}
}

return `"${family}"`;
}

export {
Expand Down
4 changes: 4 additions & 0 deletions src/core/xfa/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
$clean,
$content,
$finalize,
$globalData,
$isCDATAXml,
$nsAttributes,
$onChild,
Expand All @@ -33,6 +34,7 @@ class XFAParser extends XMLParserBase {
super();
this._builder = new Builder();
this._stack = [];
this._globalData = Object.create(null);
this._ids = new Map();
this._current = this._builder.buildRoot(this._ids);
this._errorCode = XMLParserErrorCode.NoError;
Expand Down Expand Up @@ -135,6 +137,7 @@ class XFAParser extends XMLParserBase {
namespace,
prefixes,
});
node[$globalData] = this._globalData;

if (isEmpty) {
// No children: just push the node into its parent.
Expand All @@ -154,6 +157,7 @@ class XFAParser extends XMLParserBase {
const node = this._current;
if (node[$isCDATAXml]() && typeof node[$content] === "string") {
const parser = new XFAParser();
parser._globalData = this._globalData;
const root = parser.parse(node[$content]);
node[$content] = null;
node[$onChild](root);
Expand Down
10 changes: 5 additions & 5 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import {
$extra,
$finalize,
$flushHTML,
$fonts,
$getAvailableSpace,
$getChildren,
$getContainedChildren,
$getNextPage,
$getParent,
$getSubformParent,
$getTemplateRoot,
$globalData,
$hasItem,
$hasSettableValue,
$ids,
Expand Down Expand Up @@ -1441,7 +1441,7 @@ class Draw extends XFAObject {

if ((this.w === "" || this.h === "") && this.value) {
const maxWidth = this.w === "" ? availableSpace.width : this.w;
const fonts = this[$getTemplateRoot]()[$fonts];
const fontFinder = this[$globalData].fontFinder;
let font = this.font;
if (!font) {
let parent = this[$getParent]();
Expand All @@ -1464,15 +1464,15 @@ class Draw extends XFAObject {
const res = layoutText(
this.value.exData[$content],
font,
fonts,
fontFinder,
maxWidth
);
width = res.width;
height = res.height;
} else {
const text = this.value[$text]();
if (text) {
const res = layoutText(text, font, fonts, maxWidth);
const res = layoutText(text, font, fontFinder, maxWidth);
width = res.width;
height = res.height;
}
Expand Down Expand Up @@ -2660,7 +2660,7 @@ class Font extends XFAObject {
style.fontSize = fontSize;
}

style.fontFamily = getFonts(this.typeface);
style.fontFamily = getFonts(this.typeface, this[$globalData].fontFinder);

if (this.underline !== 0) {
style.textDecoration = "underline";
Expand Down
Loading

0 comments on commit 2e6d3d6

Please sign in to comment.