Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/core/xfa/xhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,7 @@ class XhtmlObject extends XmlObject {
}
}

[$pushGlyphs](measure, mustPop = true) {
const xfaFont = Object.create(null);
[$pushGlyphs](measure, mustPop = true, xfaFont = Object.create(null)) {
const margin = {
top: NaN,
bottom: NaN,
Expand Down Expand Up @@ -374,9 +373,7 @@ class B extends XhtmlObject {
}

[$pushGlyphs](measure) {
measure.pushFont({ weight: "bold" });
super[$pushGlyphs](measure);
measure.popFont();
super[$pushGlyphs](measure, /* mustPop = */ true, { weight: "bold" });
}
}

Expand Down Expand Up @@ -464,9 +461,7 @@ class I extends XhtmlObject {
}

[$pushGlyphs](measure) {
measure.pushFont({ posture: "italic" });
super[$pushGlyphs](measure);
measure.popFont();
super[$pushGlyphs](measure, /* mustPop = */ true, { posture: "italic" });
}
}

Expand Down
1 change: 1 addition & 0 deletions test/pdfs/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -1065,3 +1065,4 @@
!issue19954_reduced.pdf
!issue1936_reduced.pdf
!issue22462.pdf
!xfa_issue22023.pdf
Binary file added test/pdfs/xfa_issue22023.pdf
Binary file not shown.
9 changes: 9 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -15343,5 +15343,14 @@
"rounds": 1,
"type": "eq",
"about": "Short loca offsets overflow after duplicating glyph 0."
},
{
"id": "xfa_issue22023",
"file": "pdfs/xfa_issue22023.pdf",
"md5": "44b75da68e9ccc5c24dc4f9ea05399bf",
"rounds": 1,
"enableXfa": true,
"type": "eq",
"about": "XFA rich text with b and i elements."
}
]
87 changes: 87 additions & 0 deletions test/unit/xfa_tohtml_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,91 @@ describe("XFAFactory", function () {
const p = searchHtmlNode(pages, "name", "p");
expect(p.attributes.style.fontSize).toEqual("13.86px");
});

it("should measure b and i elements like the equivalent styles", async () => {
const draws = {
regular: "Hello World",
b: "<b>Hello</b> World",
boldSpan: `<span style="font-weight:bold">Hello</span> World`,
i: "<i>Hello</i> World",
italicSpan: `<span style="font-style:italic">Hello</span> World`,
bi: "<b><i>Hello</i></b> World",
boldItalicSpan: `<span style="font-weight:bold;font-style:italic">Hello</span> World`,
bInSpan: `<span style="font-size:24pt"><b>Hello</b></span><br/>World`,
boldSpanInSpan: `<span style="font-size:24pt"><span style="font-weight:bold">Hello</span></span><br/>World`,
bWithStyle: `<b style="font-size:24pt;font-weight:normal">Hello</b>`,
spanWithStyle: `<span style="font-size:24pt">Hello</span>`,
};
const xml = `
<?xml version="1.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<template xmlns="http://www.xfa.org/schema/xfa-template/3.3">
<subform name="root" mergeMode="matchTemplate">
<pageSet>
<pageArea>
<contentArea x="0pt" w="456pt" h="789pt"/>
<medium stock="default" short="456pt" long="789pt"/>
</pageArea>
</pageSet>
<subform name="draws" layout="tb">
${Object.entries(draws)
.map(
([name, html]) => `
<draw name="${name}">
<font typeface="Helvetica" size="12pt"/>
<value>
<exData contentType="text/html">
<body xmlns="http://www.w3.org/1999/xhtml"><p>${html}</p></body>
</exData>
</value>
</draw>`
)
.join("")}
</subform>
</subform>
</template>
</xdp:xdp>
`;
const factory = new XFAFactory(new Map([["xdp:xdp", xml]]));
// Glyph widths differ per variant, so the selected font shows in the size.
const makeFont = (name, fontWeight, italicAngle, width) => ({
name,
cssFontInfo: { fontFamily: "Helvetica", fontWeight, italicAngle },
lineHeight: 1.2,
lineGap: 0.2,
defaultWidth: width,
encodeString: str => [str],
charsToGlyphs: str => Array.from(str, unicode => ({ unicode, width })),
});
factory.setFonts([
makeFont("Helvetica", 400, 0, 500),
makeFont("Helvetica-Bold", 700, 0, 600),
makeFont("Helvetica-Oblique", 400, -12, 550),
makeFont("Helvetica-BoldOblique", 700, -12, 650),
]);

expect(await factory.getNumPages()).toEqual(1);

const pages = await factory.getPages();
const container = searchHtmlNode(pages, "xfaName", "draws", true);
const sizes = new Map(
container.children.map(({ attributes, children }) => {
const { width, height } = attributes.style;
return [children[0].attributes.xfaName, [width, height]];
})
);

expect(sizes.size).toEqual(Object.keys(draws).length);
expect(sizes.get("b")).not.toEqual(sizes.get("regular"));
expect(sizes.get("i")).not.toEqual(sizes.get("regular"));
for (const [element, span] of [
["b", "boldSpan"],
["i", "italicSpan"],
["bi", "boldItalicSpan"],
["bInSpan", "boldSpanInSpan"],
["bWithStyle", "spanWithStyle"],
]) {
expect(sizes.get(element)).withContext(element).toEqual(sizes.get(span));
}
});
});
8 changes: 8 additions & 0 deletions web/xfa_layer_builder.css
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
color: blue;
}

.xfaLayer b {
font-weight: bold;
}

.xfaLayer i {
font-style: italic;
}

.xfaRich li {
margin-left: 3em;
}
Expand Down
Loading