Skip to content

Commit

Permalink
Merge pull request #13614 from calixteman/line_height
Browse files Browse the repository at this point in the history
XFA - Get line height from the font
  • Loading branch information
calixteman committed Jun 23, 2021
2 parents 9441245 + e82446f commit 2bae399
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 34 deletions.
15 changes: 14 additions & 1 deletion src/core/xfa/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,17 @@ class FontFinder {
}
}

export { FontFinder };
function selectFont(xfaFont, typeface) {
if (xfaFont.posture === "italic") {
if (xfaFont.weight === "bold") {
return typeface.bolditalic;
}
return typeface.italic;
} else if (xfaFont.weight === "bold") {
return typeface.bold;
}

return typeface.regular;
}

export { FontFinder, selectFont };
32 changes: 19 additions & 13 deletions src/core/xfa/html_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import {
$toStyle,
XFAObject,
} from "./xfa_object.js";
import { getMeasurement } from "./utils.js";
import { getMeasurement, stripQuotes } from "./utils.js";
import { selectFont } from "./fonts.js";
import { TextMeasure } from "./text.js";
import { warn } from "../../shared/util.js";

Expand Down Expand Up @@ -472,33 +473,38 @@ function isPrintOnly(node) {
);
}

function getFonts(family, fontFinder) {
if (family.startsWith("'") || family.startsWith('"')) {
family = family.slice(1, family.length - 1);
}
function setFontFamily(xfaFont, fontFinder, style) {
const name = stripQuotes(xfaFont.typeface);
const typeface = fontFinder.find(name);

const pdfFont = fontFinder.find(family);
if (pdfFont) {
const { fontFamily } = pdfFont.regular.cssFontInfo;
if (fontFamily !== family) {
return `"${family}","${fontFamily}"`;
style.fontFamily = `"${name}"`;
if (typeface) {
const { fontFamily } = typeface.regular.cssFontInfo;
if (fontFamily !== name) {
style.fontFamily += `,"${fontFamily}"`;
}
if (style.lineHeight) {
// Already something so don't overwrite.
return;
}
const pdfFont = selectFont(xfaFont, typeface);
if (pdfFont && pdfFont.lineHeight > 0) {
style.lineHeight = pdfFont.lineHeight;
}
}

return `"${family}"`;
}

export {
computeBbox,
createWrapper,
fixDimensions,
fixTextIndent,
getFonts,
isPrintOnly,
layoutClass,
layoutText,
measureToString,
setAccess,
setFontFamily,
setMinMaxDimensions,
toStyle,
};
4 changes: 2 additions & 2 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ import {
createWrapper,
fixDimensions,
fixTextIndent,
getFonts,
isPrintOnly,
layoutClass,
layoutText,
measureToString,
setAccess,
setFontFamily,
setMinMaxDimensions,
toStyle,
} from "./html_utils.js";
Expand Down Expand Up @@ -2695,7 +2695,7 @@ class Font extends XFAObject {
style.fontSize = fontSize;
}

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

if (this.underline !== 0) {
style.textDecoration = "underline";
Expand Down
15 changes: 3 additions & 12 deletions src/core/xfa/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* limitations under the License.
*/

import { selectFont } from "./fonts.js";

const WIDTH_FACTOR = 1.2;
const HEIGHT_FACTOR = 1.2;

Expand All @@ -30,18 +32,7 @@ class FontInfo {
return;
}

this.pdfFont = null;
if (xfaFont.posture === "italic") {
if (xfaFont.weight === "bold") {
this.pdfFont = typeface.bolditalic;
} else {
this.pdfFont = typeface.italic;
}
} else if (xfaFont.weigth === "bold") {
this.pdfFont = typeface.bold;
} else {
this.pdfFont = typeface.regular;
}
this.pdfFont = selectFont(xfaFont, typeface);

if (!this.pdfFont) {
[this.pdfFont, this.xfaFont] = this.defaultFont(fontFinder);
Expand Down
8 changes: 8 additions & 0 deletions src/core/xfa/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const dimConverters = {
};
const measurementPattern = /([+-]?[0-9]+\.?[0-9]*)(.*)/;

function stripQuotes(str) {
if (str.startsWith("'") || str.startsWith('"')) {
return str.slice(1, str.length - 1);
}
return str;
}

function getInteger({ data, defaultValue, validate }) {
if (!data) {
return defaultValue;
Expand Down Expand Up @@ -206,4 +213,5 @@ export {
getRelevant,
getStringOption,
HTMLResult,
stripQuotes,
};
16 changes: 14 additions & 2 deletions src/core/xfa/xhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
XmlObject,
} from "./xfa_object.js";
import { $buildXFAObject, NamespaceIds } from "./namespaces.js";
import { fixTextIndent, getFonts, measureToString } from "./html_utils.js";
import { fixTextIndent, measureToString, setFontFamily } from "./html_utils.js";
import { getMeasurement, HTMLResult } from "./utils.js";

const XHTML_NS_ID = NamespaceIds.xhtml.id;
Expand Down Expand Up @@ -92,7 +92,7 @@ const StyleMapping = new Map([
["margin-right", value => measureToString(getMeasurement(value))],
["margin-top", value => measureToString(getMeasurement(value))],
["text-indent", value => measureToString(getMeasurement(value))],
["font-family", (value, fontFinder) => getFonts(value, fontFinder)],
["font-family", value => value],
]);

const spacesRegExp = /\s+/g;
Expand Down Expand Up @@ -128,6 +128,18 @@ function mapStyle(styleStr, fontFinder) {
}
}

if (style.fontFamily) {
setFontFamily(
{
typeface: style.fontFamily,
weight: style.fontWeight || "normal",
posture: style.fontStyle || "normal",
},
fontFinder,
style
);
}

fixTextIndent(style);
return style;
}
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/xfa_bug1717681.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://bugzilla.mozilla.org/attachment.cgi?id=9228400
8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -930,6 +930,14 @@
"link": true,
"type": "load"
},
{ "id": "xfa_bug1717681",
"file": "pdfs/xfa_bug1717681.pdf",
"md5": "435b1eae7e017b1a932fe204d1ba8be5",
"link": true,
"rounds": 1,
"enableXfa": true,
"type": "eq"
},
{ "id": "xfa_bug1716380",
"file": "pdfs/xfa_bug1716380.pdf",
"md5": "1351f816f0509fe750ca61ef2bd40872",
Expand Down
5 changes: 1 addition & 4 deletions web/xfa_layer_builder.css
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
left: 0;
z-index: 200;
transform-origin: 0 0;
line-height: 1.2;
}

.xfaLayer * {
Expand Down Expand Up @@ -55,10 +56,6 @@
margin-left: 3em;
}

.xfaLayer p {
margin-bottom: -1px;
}

.xfaFont {
color: black;
font-weight: normal;
Expand Down

0 comments on commit 2bae399

Please sign in to comment.