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

XFA - Add the possibily to layout and measure text

- some containers doesn't always have their 2 dimensions and those dimensions re based on contents;
  - so in order to measure text, we must get the glyph widths (for the xfa fonts) before starting the layout;
  - implement a word-wrap algorithm;
  - handle font change during text layout.
This commit is contained in:
Calixte Denizet 2021-06-14 19:16:42 +02:00
parent 335d4cb2fc
commit 8eeb7ab4a3
12 changed files with 416 additions and 91 deletions

View file

@ -18,8 +18,10 @@ import {
$childrenToHTML,
$content,
$extra,
$getChildren,
$nodeName,
$onText,
$pushGlyphs,
$text,
$toHTML,
XmlObject,
@ -167,6 +169,39 @@ class XhtmlObject extends XmlObject {
}
}
[$pushGlyphs](measure) {
const xfaFont = Object.create(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 = 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;
}
}
measure.pushFont(xfaFont);
if (this[$content]) {
measure.addString(this[$content]);
} else {
for (const child of this[$getChildren]()) {
if (child[$nodeName] === "#text") {
measure.addString(child[$content]);
continue;
}
child[$pushGlyphs](measure);
}
}
measure.popFont();
}
[$toHTML](availableSpace) {
const children = [];
this[$extra] = {
@ -202,6 +237,12 @@ class B extends XhtmlObject {
constructor(attributes) {
super(attributes, "b");
}
[$pushGlyphs](measure) {
measure.pushFont({ weight: "bold" });
super[$pushGlyphs](measure);
measure.popFont();
}
}
class Body extends XhtmlObject {
@ -230,6 +271,10 @@ class Br extends XhtmlObject {
return "\n";
}
[$pushGlyphs](measure) {
measure.addString("\n");
}
[$toHTML](availableSpace) {
return HTMLResult.success({
name: "br",
@ -282,6 +327,12 @@ class I extends XhtmlObject {
constructor(attributes) {
super(attributes, "i");
}
[$pushGlyphs](measure) {
measure.pushFont({ posture: "italic" });
super[$pushGlyphs](measure);
measure.popFont();
}
}
class Li extends XhtmlObject {
@ -301,6 +352,11 @@ class P extends XhtmlObject {
super(attributes, "p");
}
[$pushGlyphs](measure) {
super[$pushGlyphs](measure);
measure.addString("\n");
}
[$text]() {
return super[$text]() + "\n";
}