mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-22 16:18:08 +02:00
XFA - Get line height from the font
- when the CSS line-height property is set to 'normal' then the value depends of the user agent. So use a line height based on the font itself and if for any reasons this value is not available use 1.2 as default. - it's a partial fix for https://bugzilla.mozilla.org/show_bug.cgi?id=1717681.
This commit is contained in:
parent
9441245320
commit
e82446fa5a
9 changed files with 70 additions and 34 deletions
|
@ -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 };
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
@ -472,20 +473,25 @@ 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 {
|
||||
|
@ -493,12 +499,12 @@ export {
|
|||
createWrapper,
|
||||
fixDimensions,
|
||||
fixTextIndent,
|
||||
getFonts,
|
||||
isPrintOnly,
|
||||
layoutClass,
|
||||
layoutText,
|
||||
measureToString,
|
||||
setAccess,
|
||||
setFontFamily,
|
||||
setMinMaxDimensions,
|
||||
toStyle,
|
||||
};
|
||||
|
|
|
@ -70,12 +70,12 @@ import {
|
|||
createWrapper,
|
||||
fixDimensions,
|
||||
fixTextIndent,
|
||||
getFonts,
|
||||
isPrintOnly,
|
||||
layoutClass,
|
||||
layoutText,
|
||||
measureToString,
|
||||
setAccess,
|
||||
setFontFamily,
|
||||
setMinMaxDimensions,
|
||||
toStyle,
|
||||
} from "./html_utils.js";
|
||||
|
@ -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";
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { selectFont } from "./fonts.js";
|
||||
|
||||
const WIDTH_FACTOR = 1.2;
|
||||
const HEIGHT_FACTOR = 1.2;
|
||||
|
||||
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
@ -206,4 +213,5 @@ export {
|
|||
getRelevant,
|
||||
getStringOption,
|
||||
HTMLResult,
|
||||
stripQuotes,
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue