XFA -- Add template object

- Specifications: http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.364.2157&rep=rep1&type=pdf#page=596
This commit is contained in:
Calixte Denizet 2021-02-01 15:12:19 +01:00
parent 0d85de980b
commit 652ff57897
4 changed files with 3495 additions and 1 deletions

View File

@ -14,10 +14,12 @@
*/
import { ConfigNamespace } from "./config.js";
import { TemplateNamespace } from "./template.js";
import { XdpNamespace } from "./xdp.js";
const NamespaceSetUp = {
config: ConfigNamespace,
template: TemplateNamespace,
xdp: XdpNamespace,
};

3218
src/core/xfa/template.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -13,6 +13,8 @@
* limitations under the License.
*/
const measurementPattern = /([+-]?)([0-9]+\.?[0-9]*)(.*)/;
function getInteger({ data, defaultValue, validate }) {
if (!data) {
return defaultValue;
@ -25,6 +27,18 @@ function getInteger({ data, defaultValue, validate }) {
return defaultValue;
}
function getFloat({ data, defaultValue, validate }) {
if (!data) {
return defaultValue;
}
data = data.trim();
const n = parseFloat(data);
if (!isNaN(n) && validate(n)) {
return n;
}
return defaultValue;
}
function getKeyword({ data, defaultValue, validate }) {
if (!data) {
return defaultValue;
@ -44,4 +58,106 @@ function getStringOption(data, options) {
});
}
export { getInteger, getKeyword, getStringOption };
function getMeasurement(str, def = "0") {
def = def || "0";
if (!str) {
return getMeasurement(def);
}
const match = str.trim().match(measurementPattern);
if (!match) {
return getMeasurement(def);
}
const [, sign, valueStr, unit] = match;
const value = parseFloat(valueStr);
if (isNaN(value)) {
return getMeasurement(def);
}
return {
value: sign === "-" ? -value : value,
unit,
};
}
function getRatio(data) {
if (!data) {
return { num: 1, den: 1 };
}
const ratio = data
.trim()
.split(/\s*:\s*/)
.map(x => parseFloat(x))
.filter(x => !isNaN(x));
if (ratio.length === 1) {
ratio.push(1);
}
if (ratio.length === 0) {
return { num: 1, den: 1 };
}
const [num, den] = ratio;
return { num, den };
}
function getRelevant(data) {
if (!data) {
return [];
}
return data
.trim()
.split(/\s+/)
.map(e => {
return {
excluded: e[0] === "-",
viewname: e.substring(1),
};
});
}
function getColor(data, def = [0, 0, 0]) {
let [r, g, b] = def;
if (!data) {
return { r, g, b };
}
const color = data
.trim()
.split(/\s*,\s*/)
.map(c => Math.min(Math.max(0, parseInt(c.trim(), 10)), 255))
.map(c => (isNaN(c) ? 0 : c));
if (color.length < 3) {
return { r, g, b };
}
[r, g, b] = color;
return { r, g, b };
}
function getBBox(data) {
const def = getMeasurement("-1");
if (!data) {
return { x: def, y: def, width: def, height: def };
}
const bbox = data
.trim()
.split(/\s*,\s*/)
.map(m => getMeasurement(m, "-1"));
if (bbox.length < 4 || bbox[2].value < 0 || bbox[3].value < 0) {
return { x: def, y: def, width: def, height: def };
}
const [x, y, width, height] = bbox;
return { x, y, width, height };
}
export {
getBBox,
getColor,
getFloat,
getInteger,
getKeyword,
getMeasurement,
getRatio,
getRelevant,
getStringOption,
};

View File

@ -46,12 +46,133 @@ describe("XFAParser", function () {
<validate>foobar</validate>
</acrobat>
</config>
<template baseProfile="full" xmlns="http://www.xfa.org/schema/xfa-template/3.3">
<extras>
<float>1.23</float>
<boolean>1</boolean>
<integer>314</integer>
<float>2.71</float>
</extras>
<subform>
<proto>
<area x="hello" y="-3.14in" relevant="-foo +bar" />
<color value="111, 222, 123" />
<color value="111, abc, 123" />
<medium imagingBBox="1,2in,3.4cm,5.67px" />
<medium imagingBBox="1,2in,-3cm,4px" />
</proto>
</subform>
</template>
</xdp:xdp>
`;
const attributes = {
id: "",
name: "",
use: "",
usehref: "",
};
const mediumAttributes = {
id: "",
long: { value: 0, unit: "" },
orientation: "portrait",
short: { value: 0, unit: "" },
stock: "",
trayIn: "auto",
trayOut: "auto",
use: "",
usehref: "",
};
const colorAttributes = {
cSpace: "SRGB",
id: "",
use: "",
usehref: "",
};
const root = new XFAParser().parse(xml);
const expected = {
uuid: "1234",
timeStamp: "",
template: {
baseProfile: "full",
extras: {
...attributes,
float: [
{ ...attributes, $content: 1.23 },
{ ...attributes, $content: 2.71 },
],
boolean: { ...attributes, $content: 1 },
integer: { ...attributes, $content: 314 },
},
subform: {
access: "open",
allowMacro: 0,
anchorType: "topLeft",
colSpan: 1,
columnWidths: [{ value: 0, unit: "" }],
h: { value: 0, unit: "" },
hAlign: "left",
id: "",
layout: "position",
locale: "",
maxH: { value: 0, unit: "" },
maxW: { value: 0, unit: "" },
mergeMode: "consumeData",
minH: { value: 0, unit: "" },
minW: { value: 0, unit: "" },
name: "",
presence: "visible",
relevant: [],
restoreState: "manual",
scope: "name",
use: "",
usehref: "",
w: { value: 0, unit: "" },
x: { value: 0, unit: "" },
y: { value: 0, unit: "" },
proto: {
area: {
...attributes,
colSpan: 1,
x: { value: 0, unit: "" },
y: { value: -3.14, unit: "in" },
relevant: [
{ excluded: true, viewname: "foo" },
{ excluded: false, viewname: "bar" },
],
},
color: [
{
...colorAttributes,
value: { r: 111, g: 222, b: 123 },
},
{
...colorAttributes,
value: { r: 111, g: 0, b: 123 },
},
],
medium: [
{
...mediumAttributes,
imagingBBox: {
x: { value: 1, unit: "" },
y: { value: 2, unit: "in" },
width: { value: 3.4, unit: "cm" },
height: { value: 5.67, unit: "px" },
},
},
{
...mediumAttributes,
imagingBBox: {
x: { value: -1, unit: "" },
y: { value: -1, unit: "" },
width: { value: -1, unit: "" },
height: { value: -1, unit: "" },
},
},
],
},
},
},
config: {
acrobat: {
acrobat7: {
@ -89,5 +210,42 @@ describe("XFAParser", function () {
};
expect(root[$dump]()).toEqual(expected);
});
it("should parse a xfa document and check namespaces", function () {
const xml = `
<?xml version="1.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
<config xmlns:foo="http:/www.foo.com" xmlns="http://www.xfa.org/schema/xci/3.1/">
<present xmlns="http://www.mozilla.org">
<pdf name="hello">
<adobeExtensionLevel>
7
</adobeExtensionLevel>
</pdf>
</present>
<acrobat>
<foo:submitUrl>http://a.b.c</foo:submitUrl>
<submitUrl>http://c.b.a</submitUrl>
</acrobat>
</config>
<template baseProfile="full" xmlns="http://www.allizom.org">
<extras>
<float>1.23</float>
</extras>
</template>
</xdp:xdp>
`;
const root = new XFAParser().parse(xml);
const expected = {
uuid: "",
timeStamp: "",
config: {
acrobat: {
submitUrl: { $content: "http://c.b.a" },
},
},
};
expect(root[$dump]()).toEqual(expected);
});
});
});