Merge pull request #13141 from calixteman/xfa_text

XFA -- Display text content
This commit is contained in:
Brendan Dahl 2021-04-12 14:34:38 -07:00 committed by GitHub
commit a53cd1cc1e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 552 additions and 164 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { $getParent, $toStyle, XFAObject } from "./xfa_object.js"; import { $extra, $getParent, $toStyle, XFAObject } from "./xfa_object.js";
import { warn } from "../../shared/util.js"; import { warn } from "../../shared/util.js";
function measureToString(m) { function measureToString(m) {
@ -56,8 +56,17 @@ const converters = {
} }
}, },
dimensions(node, style) { dimensions(node, style) {
if (node.w) { const parent = node[$getParent]();
style.width = measureToString(node.w); const extra = parent[$extra];
let width = node.w;
if (extra && extra.columnWidths) {
width = extra.columnWidths[extra.currentColumn];
extra.currentColumn =
(extra.currentColumn + 1) % extra.columnWidths.length;
}
if (width !== "") {
style.width = measureToString(width);
} else { } else {
style.width = "auto"; style.width = "auto";
if (node.maxW > 0) { if (node.maxW > 0) {
@ -66,7 +75,7 @@ const converters = {
style.minWidth = measureToString(node.minW); style.minWidth = measureToString(node.minW);
} }
if (node.h) { if (node.h !== "") {
style.height = measureToString(node.h); style.height = measureToString(node.h);
} else { } else {
style.height = "auto"; style.height = "auto";
@ -108,6 +117,53 @@ const converters = {
break; break;
} }
}, },
hAlign(node, style) {
switch (node.hAlign) {
case "justifyAll":
style.textAlign = "justify-all";
break;
case "radix":
// TODO: implement this correctly !
style.textAlign = "left";
break;
default:
style.textAlign = node.hAlign;
}
},
borderMarginPadding(node, style) {
// Get border width in order to compute margin and padding.
const borderWidths = [0, 0, 0, 0];
const marginWidths = [0, 0, 0, 0];
const marginNode = node.margin
? [
node.margin.topInset,
node.margin.rightInset,
node.margin.bottomInset,
node.margin.leftInset,
]
: [0, 0, 0, 0];
if (node.border) {
Object.assign(style, node.border[$toStyle](borderWidths, marginWidths));
}
if (borderWidths.every(x => x === 0)) {
// No border: margin & padding are padding
if (node.margin) {
Object.assign(style, node.margin[$toStyle]());
}
style.padding = style.margin;
delete style.margin;
} else {
style.padding =
measureToString(marginNode[0] - borderWidths[0] - marginWidths[0]) +
" " +
measureToString(marginNode[1] - borderWidths[1] - marginWidths[1]) +
" " +
measureToString(marginNode[2] - borderWidths[2] - marginWidths[2]) +
" " +
measureToString(marginNode[3] - borderWidths[3] - marginWidths[3]);
}
},
}; };
function layoutClass(node) { function layoutClass(node) {
@ -155,4 +211,26 @@ function toStyle(node, ...names) {
return style; return style;
} }
export { layoutClass, measureToString, toStyle }; function addExtraDivForMargin(html) {
const style = html.attributes.style;
if (style.margin) {
const padding = style.margin;
delete style.margin;
const width = style.width || "auto";
const height = style.height || "auto";
style.width = "100%";
style.height = "100%";
return {
name: "div",
attributes: {
style: { padding, width, height },
},
children: [html],
};
}
return html;
}
export { addExtraDivForMargin, layoutClass, measureToString, toStyle };

View File

@ -14,6 +14,7 @@
*/ */
import { import {
$acceptWhitespace,
$clean, $clean,
$finalize, $finalize,
$nsAttributes, $nsAttributes,
@ -49,6 +50,11 @@ class XFAParser extends XMLParserBase {
} }
onText(text) { onText(text) {
if (this._current[$acceptWhitespace]()) {
this._current[$onText](text);
return;
}
if (this._whiteRegex.test(text)) { if (this._whiteRegex.test(text)) {
return; return;
} }

View File

@ -40,6 +40,12 @@ import {
XFAObjectArray, XFAObjectArray,
} from "./xfa_object.js"; } from "./xfa_object.js";
import { $buildXFAObject, NamespaceIds } from "./namespaces.js"; import { $buildXFAObject, NamespaceIds } from "./namespaces.js";
import {
addExtraDivForMargin,
layoutClass,
measureToString,
toStyle,
} from "./html_utils.js";
import { import {
getBBox, getBBox,
getColor, getColor,
@ -51,7 +57,6 @@ import {
getRelevant, getRelevant,
getStringOption, getStringOption,
} from "./utils.js"; } from "./utils.js";
import { layoutClass, measureToString, toStyle } from "./html_utils.js";
import { Util, warn } from "../../shared/util.js"; import { Util, warn } from "../../shared/util.js";
const TEMPLATE_NS_ID = NamespaceIds.template.id; const TEMPLATE_NS_ID = NamespaceIds.template.id;
@ -131,6 +136,36 @@ class Area extends XFAObject {
[$isTransparent]() { [$isTransparent](