Skip to content

Commit f3e353a

Browse files
committed
Fix code duplication in the rasterization logic in test/driver.js
Now that the rasterization logic is encapsulated in a class, we can easily move the container creation into a separate static method.
1 parent 8d72727 commit f3e353a

File tree

1 file changed

+36
-60
lines changed

1 file changed

+36
-60
lines changed

test/driver.js

+36-60
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,26 @@ class Rasterize {
175175
return shadow(this, "xfaStylePromise", loadStyles(styles));
176176
}
177177

178+
static createContainer(viewport) {
179+
const svg = document.createElementNS(SVG_NS, "svg:svg");
180+
svg.setAttribute("width", `${viewport.width}px`);
181+
svg.setAttribute("height", `${viewport.height}px`);
182+
183+
const foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
184+
foreignObject.setAttribute("x", "0");
185+
foreignObject.setAttribute("y", "0");
186+
foreignObject.setAttribute("width", `${viewport.width}px`);
187+
foreignObject.setAttribute("height", `${viewport.height}px`);
188+
189+
const style = document.createElement("style");
190+
foreignObject.appendChild(style);
191+
192+
const div = document.createElement("div");
193+
foreignObject.appendChild(div);
194+
195+
return { svg, foreignObject, style, div };
196+
}
197+
178198
static async annotationLayer(
179199
ctx,
180200
viewport,
@@ -185,36 +205,21 @@ class Rasterize {
185205
renderForms = false
186206
) {
187207
try {
188-
// Building SVG with size of the viewport.
189-
const svg = document.createElementNS(SVG_NS, "svg:svg");
190-
svg.setAttribute("width", viewport.width + "px");
191-
svg.setAttribute("height", viewport.height + "px");
192-
193-
// Adding element to host our HTML (style + annotation layer div).
194-
const foreignObject = document.createElementNS(
195-
SVG_NS,
196-
"svg:foreignObject"
197-
);
198-
foreignObject.setAttribute("x", "0");
199-
foreignObject.setAttribute("y", "0");
200-
foreignObject.setAttribute("width", viewport.width + "px");
201-
foreignObject.setAttribute("height", viewport.height + "px");
202-
const style = document.createElement("style");
203-
foreignObject.appendChild(style);
204-
const div = document.createElement("div");
208+
const { svg, foreignObject, style, div } =
209+
Rasterize.createContainer(viewport);
205210
div.className = "annotationLayer";
206211

207-
// Rendering annotation layer as HTML.
208212
const [common, overrides] = await Rasterize.annotationStylePromise;
209-
style.textContent = common + "\n" + overrides;
213+
style.textContent = `${common}\n${overrides}`;
210214

211-
const annotation_viewport = viewport.clone({ dontFlip: true });
215+
const annotationViewport = viewport.clone({ dontFlip: true });
212216
const annotationImageMap = await convertCanvasesToImages(
213217
annotationCanvasMap
214218
);
215219

220+
// Rendering annotation layer as HTML.
216221
const parameters = {
217-
viewport: annotation_viewport,
222+
viewport: annotationViewport,
218223
div,
219224
annotations,
220225
page,
@@ -238,27 +243,12 @@ class Rasterize {
238243

239244
static async textLayer(ctx, viewport, textContent, enhanceTextSelection) {
240245
try {
241-
// Building SVG with size of the viewport.
242-
const svg = document.createElementNS(SVG_NS, "svg:svg");
243-
svg.setAttribute("width", viewport.width + "px");
244-
svg.setAttribute("height", viewport.height + "px");
245-
// items are transformed to have 1px font size
246-
svg.setAttribute("font-size", 1);
247-
248-
// Adding element to host our HTML (style + text layer div).
249-
const foreignObject = document.createElementNS(
250-
SVG_NS,
251-
"svg:foreignObject"
252-
);
253-
foreignObject.setAttribute("x", "0");
254-
foreignObject.setAttribute("y", "0");
255-
foreignObject.setAttribute("width", viewport.width + "px");
256-
foreignObject.setAttribute("height", viewport.height + "px");
257-
const style = document.createElement("style");
258-
foreignObject.appendChild(style);
259-
const div = document.createElement("div");
246+
const { svg, foreignObject, style, div } =
247+
Rasterize.createContainer(viewport);
260248
div.className = "textLayer";
261-
foreignObject.appendChild(div);
249+
250+
// Items are transformed to have 1px font size.
251+
svg.setAttribute("font-size", 1);
262252

263253
const [cssRules] = await Rasterize.textStylePromise;
264254
style.textContent = cssRules;
@@ -290,26 +280,13 @@ class Rasterize {
290280
isPrint
291281
) {
292282
try {
293-
// Building SVG with size of the viewport.
294-
const svg = document.createElementNS(SVG_NS, "svg:svg");
295-
svg.setAttribute("width", viewport.width + "px");
296-
svg.setAttribute("height", viewport.height + "px");
297-
const foreignObject = document.createElementNS(
298-
SVG_NS,
299-
"svg:foreignObject"
300-
);
301-
foreignObject.setAttribute("x", "0");
302-
foreignObject.setAttribute("y", "0");
303-
foreignObject.setAttribute("width", viewport.width + "px");
304-
foreignObject.setAttribute("height", viewport.height + "px");
305-
const style = document.createElement("style");
306-
foreignObject.appendChild(style);
307-
const div = document.createElement("div");
308-
foreignObject.appendChild(div);
283+
const { svg, foreignObject, style, div } =
284+
Rasterize.createContainer(viewport);
309285

310286
const [common, overrides] = await Rasterize.xfaStylePromise;
311-
style.textContent = fontRules + "\n" + common + "\n" + overrides;
287+
style.textContent = `${fontRules}\n${common}\n${overrides}`;
312288

289+
// Rendering XFA layer as HTML.
313290
XfaLayer.render({
314291
xfa,
315292
div,
@@ -319,8 +296,7 @@ class Rasterize {
319296
intent: isPrint ? "print" : "display",
320297
});
321298

322-
// Some unsupported type of images (e.g. tiff)
323-
// lead to errors.
299+
// Some unsupported type of images (e.g. tiff) lead to errors.
324300
await resolveImages(div, /* silentErrors = */ true);
325301
svg.appendChild(foreignObject);
326302

0 commit comments

Comments
 (0)