Skip to content

Commit 2a43792

Browse files
committed
XFA - Convert some template properties into CSS ones
- implement few positioning properties: position, width, height, anchor; - implement font element; - implement fill element (used by font) and its children (linear, radial, ...); - font property is inherited from ancestor container (see https://www.pdfa.org/wp-content/uploads/2020/07/XFA-3_3.pdf#page=43) so let CSS handles that stuff; - in order to reduce the number of properties to set, only set non default properties and put the default in CSS; - set a background to some containers to be able to see them (will be removed in a future commit).
1 parent 84a0758 commit 2a43792

File tree

8 files changed

+504
-44
lines changed

8 files changed

+504
-44
lines changed

src/core/xfa/html_utils.js

+96-31
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,122 @@
1313
* limitations under the License.
1414
*/
1515

16-
const converters = {
16+
import { $toStyle, XFAObject } from "./xfa_object.js";
17+
import { warn } from "../../shared/util.js";
18+
19+
const dimConverters = {
1720
pt: x => x,
1821
cm: x => Math.round((x / 2.54) * 72),
1922
mm: x => Math.round((x / (10 * 2.54)) * 72),
2023
in: x => Math.round(x * 72),
2124
};
2225

2326
function measureToString(m) {
24-
const conv = converters[m.unit];
27+
if (!m) {
28+
return "0px";
29+
}
30+
const conv = dimConverters[m.unit];
2531
if (conv) {
2632
return `${conv(m.value)}px`;
2733
}
2834
return `${m.value}${m.unit}`;
2935
}
3036

31-
function setWidthHeight(node, style) {
32-
if (node.w) {
33-
style.width = measureToString(node.w);
34-
} else {
35-
if (node.maxW && node.maxW.value > 0) {
36-
style.maxWidth = measureToString(node.maxW);
37+
const converters = {
38+
anchorType(node, style) {
39+
if (!("transform" in style)) {
40+
style.transform = "";
3741
}
38-
if (node.minW && node.minW.value > 0) {
39-
style.minWidth = measureToString(node.minW);
42+
switch (node.anchorType) {
43+
case "bottomCenter":
44+
style.transform += "translate(-50%, -100%)";
45+
break;
46+
case "bottomLeft":
47+
style.transform += "translate(0,-100%)";
48+
break;
49+
case "bottomRight":
50+
style.transform += "translate(-100%,-100%)";
51+
break;
52+
case "middleCenter":
53+
style.transform += "translate(-50%,-50%)";
54+
break;
55+
case "middleLeft":
56+
style.transform += "translate(0,-50%)";
57+
break;
58+
case "middleRight":
59+
style.transform += "translate(-100%,-50%)";
60+
break;
61+
case "topCenter":
62+
style.transform += "translate(-50%,0)";
63+
break;
64+
case "topRight":
65+
style.transform += "translate(-100%,0)";
66+
break;
67+
}
68+
},
69+
dimensions(node, style) {
70+
if (node.w) {
71+
style.width = measureToString(node.w);
72+
} else {
73+
if (node.maxW && node.maxW.value > 0) {
74+
style.maxWidth = measureToString(node.maxW);
75+
}
76+
if (node.minW && node.minW.value > 0) {
77+
style.minWidth = measureToString(node.minW);
78+
}
4079
}
41-
}
4280

43-
if (node.h) {
44-
style.height = measureToString(node.h);
45-
} else {
46-
if (node.maxH && node.maxH.value > 0) {
47-
style.maxHeight = measureToString(node.maxH);
81+
if (node.h) {
82+
style.height = measureToString(node.h);
83+
} else {
84+
if (node.maxH && node.maxH.value > 0) {
85+
style.maxHeight = measureToString(node.maxH);
86+
}
87+
if (node.minH && node.minH.value > 0) {
88+
style.minHeight = measureToString(node.minH);
89+
}
4890
}
49-
if (node.minH && node.minH.value > 0) {
50-
style.minHeight = measureToString(node.minH);
91+
},
92+
position(node, style) {
93+
if (node.x !== "" || node.y !== "") {
94+
style.position = "absolute";
95+
style.left = measureToString(node.x);
96+
style.top = measureToString(node.y);
5197
}
52-
}
53-
}
98+
},
99+
rotate(node, style) {
100+
if (node.rotate) {
101+
if (!("transform" in style)) {
102+
style.transform = "";
103+
}
104+
style.transform += `rotate(-${node.rotate}deg)`;
105+
style.transformOrigin = "top left";
106+
}
107+
},
108+
};
54109

55-
function setPosition(node, style) {
56-
style.transform = "";
57-
if (node.rotate) {
58-
style.transform = `rotate(-${node.rotate}deg) `;
59-
style.transformOrigin = "top left";
60-
}
110+
function toStyle(node, ...names) {
111+
const style = Object.create(null);
112+
for (const name of names) {
113+
const value = node[name];
114+
if (value === null) {
115+
continue;
116+
}
117+
if (value instanceof XFAObject) {
118+
const newStyle = value[$toStyle]();
119+
if (newStyle) {
120+
Object.assign(style, newStyle);
121+
} else {
122+
warn(`(DEBUG) - XFA - style for ${name} not implemented yet`);
123+
}
124+
continue;
125+
}
61126

62-
if (node.x !== "" || node.y !== "") {
63-
style.position = "absolute";
64-
style.left = node.x ? measureToString(node.x) : "0pt";
65-
style.top = node.y ? measureToString(node.y) : "0pt";
127+
if (converters.hasOwnProperty(name)) {
128+
converters[name](node, style);
129+
}
66130
}
131+
return style;
67132
}
68133

69-
export { measureToString, setPosition, setWidthHeight };
134+
export { measureToString, toStyle };

0 commit comments

Comments
 (0)