1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-28 23:28:16 +02:00

XFA - Improve text layout

- support paragraph margins, line height, letter spacing, ...
  - compute missing dimensions from fields based almost on the dimensions of caption contents.
This commit is contained in:
Calixte Denizet 2021-07-02 17:53:27 +02:00
parent d80651e572
commit f7d3b22480
5 changed files with 359 additions and 99 deletions

View file

@ -193,25 +193,81 @@ class XhtmlObject extends XmlObject {
}
}
[$pushGlyphs](measure) {
[$pushGlyphs](measure, mustPop = true) {
const xfaFont = Object.create(null);
const margin = {
top: NaN,
bottom: NaN,
left: NaN,
right: NaN,
};
let lineHeight = null;
for (const [key, value] of this.style
.split(";")
.map(s => s.split(":", 2))) {
if (!key.startsWith("font-")) {
continue;
}
if (key === "font-family") {
xfaFont.typeface = stripQuotes(value);
} else if (key === "font-size") {
xfaFont.size = getMeasurement(value);
} else if (key === "font-weight") {
xfaFont.weight = value;
} else if (key === "font-style") {
xfaFont.posture = value;
switch (key) {
case "font-family":
xfaFont.typeface = stripQuotes(value);
break;
case "font-size":
xfaFont.size = getMeasurement(value);
break;
case "font-weight":
xfaFont.weight = value;
break;
case "font-style":
xfaFont.posture = value;
break;
case "letter-spacing":
xfaFont.letterSpacing = getMeasurement(value);
break;
case "margin":
const values = value.split(/ \t/).map(x => getMeasurement(x));
switch (values.length) {
case 1:
margin.top =
margin.bottom =
margin.left =
margin.right =
values[0];
break;
case 2:
margin.top = margin.bottom = values[0];
margin.left = margin.right = values[1];
break;
case 3:
margin.top = values[0];
margin.bottom = values[2];
margin.left = margin.right = values[1];
break;
case 4:
margin.top = values[0];
margin.left = values[1];
margin.bottom = values[2];
margin.right = values[3];
break;
}
break;
case "margin-top":
margin.top = getMeasurement(value);
break;
case "margin-bottom":
margin.bottom = getMeasurement(value);
break;
case "margin-left":
margin.left = getMeasurement(value);
break;
case "margin-right":
margin.right = getMeasurement(value);
break;
case "line-height":
lineHeight = getMeasurement(value);
break;
}
}
measure.pushFont(xfaFont);
measure.pushData(xfaFont, margin, lineHeight);
if (this[$content]) {
measure.addString(this[$content]);
} else {
@ -223,7 +279,10 @@ class XhtmlObject extends XmlObject {
child[$pushGlyphs](measure);
}
}
measure.popFont();
if (mustPop) {
measure.popFont();
}
}
[$toHTML](availableSpace) {
@ -377,8 +436,10 @@ class P extends XhtmlObject {
}
[$pushGlyphs](measure) {
super[$pushGlyphs](measure);
super[$pushGlyphs](measure, /* mustPop = */ false);
measure.addString("\n");
measure.addPara();
measure.popFont();
}
[$text]() {