Skip to content

Commit a720cd6

Browse files
HackbrettXXXAnna-Lena Lumpp
andauthored
Fix leftover measurement svg (#274)
properly clean up leftover measurement elements in body Co-authored-by: Anna-Lena Lumpp <[email protected]>
1 parent 0598244 commit a720cd6

File tree

8 files changed

+41
-15
lines changed

8 files changed

+41
-15
lines changed

src/context/context.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,32 @@ export class Context {
3838
? values.attributeState.clone()
3939
: AttributeState.default()
4040
this.viewport = values.viewport
41-
this.refsHandler = values.refsHandler ?? null
42-
this.styleSheets = values.styleSheets ?? null
43-
this.textMeasure = values.textMeasure ?? new TextMeasure()
41+
this.refsHandler = values.refsHandler
42+
this.styleSheets = values.styleSheets
43+
this.textMeasure = values.textMeasure
4444
this.transform = values.transform ?? this.pdf.unitMatrix
4545
this.withinClipPath = values.withinClipPath ?? false
4646
this.withinUse = values.withinUse ?? false
4747
}
4848

49-
clone(values: Partial<ContextOptions> = {}): Context {
49+
clone(
50+
values: {
51+
viewport?: Viewport
52+
attributeState?: AttributeState
53+
transform?: Matrix
54+
withinClipPath?: boolean
55+
withinUse?: boolean
56+
} = {}
57+
): Context {
5058
return new Context(this.pdf, {
51-
svg2pdfParameters: values.svg2pdfParameters ?? this.svg2pdfParameters,
59+
svg2pdfParameters: this.svg2pdfParameters,
5260
attributeState: values.attributeState
5361
? values.attributeState.clone()
5462
: this.attributeState.clone(),
5563
viewport: values.viewport ?? this.viewport,
56-
refsHandler: values.refsHandler ?? this.refsHandler,
57-
styleSheets: values.styleSheets ?? this.styleSheets,
58-
textMeasure: values.textMeasure ?? this.textMeasure,
64+
refsHandler: this.refsHandler,
65+
styleSheets: this.styleSheets,
66+
textMeasure: this.textMeasure,
5967
transform: values.transform ?? this.transform,
6068
withinClipPath: values.withinClipPath ?? this.withinClipPath,
6169
withinUse: values.withinUse ?? this.withinUse
@@ -69,7 +77,7 @@ export interface ContextOptions {
6977
attributeState?: AttributeState
7078
refsHandler: ReferencesHandler
7179
styleSheets: StyleSheets
72-
textMeasure?: TextMeasure
80+
textMeasure: TextMeasure
7381
transform?: Matrix
7482
withinClipPath?: boolean
7583
withinUse?: boolean

src/nodes/clippath.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ export class ClipPath extends NonRenderedNode {
2929
styleSheets: context.styleSheets,
3030
viewport: context.viewport,
3131
withinClipPath: true,
32-
svg2pdfParameters: context.svg2pdfParameters
32+
svg2pdfParameters: context.svg2pdfParameters,
33+
textMeasure: context.textMeasure
3334
})
3435
)
3536
}

src/nodes/image.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export class ImageNode extends GraphicsNode {
6868
refsHandler: new ReferencesHandler(idMap),
6969
styleSheets: context.styleSheets,
7070
viewport: new Viewport(width, height),
71-
svg2pdfParameters: context.svg2pdfParameters
71+
svg2pdfParameters: context.svg2pdfParameters,
72+
textMeasure: context.textMeasure
7273
})
7374
)
7475
return

src/nodes/marker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class MarkerNode extends NonRenderedNode {
1919
refsHandler: parentContext.refsHandler,
2020
styleSheets: parentContext.styleSheets,
2121
viewport: parentContext.viewport,
22-
svg2pdfParameters: parentContext.svg2pdfParameters
22+
svg2pdfParameters: parentContext.svg2pdfParameters,
23+
textMeasure: parentContext.textMeasure
2324
})
2425

2526
// "Properties do not inherit from the element referencing the 'marker' into the contents of the

src/nodes/pattern.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ export class Pattern extends NonRenderedNode {
3030
refsHandler: context.refsHandler,
3131
styleSheets: context.styleSheets,
3232
viewport: context.viewport,
33-
svg2pdfParameters: context.svg2pdfParameters
33+
svg2pdfParameters: context.svg2pdfParameters,
34+
textMeasure: context.textMeasure
3435
})
3536
)
3637
}

src/nodes/use.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ export class Use extends GraphicsNode {
6666
styleSheets: context.styleSheets,
6767
withinUse: true,
6868
viewport: refNodeOpensViewport ? new Viewport(width!, height!) : context.viewport,
69-
svg2pdfParameters: context.svg2pdfParameters
69+
svg2pdfParameters: context.svg2pdfParameters,
70+
textMeasure: context.textMeasure
7071
})
7172
const color = context.attributeState.color
7273
await context.refsHandler.getRendered(id, color, node =>

src/svg2pdf.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ColorFill } from './fill/ColorFill'
66
import { jsPDF } from 'jspdf'
77
import { StyleSheets } from './context/stylesheets'
88
import { Viewport } from './context/viewport'
9+
import { TextMeasure } from './context/textmeasure'
910

1011
export async function svg2pdf(
1112
element: Element,
@@ -28,7 +29,15 @@ export async function svg2pdf(
2829

2930
const svg2pdfParameters = { ...options, element }
3031

31-
const context = new Context(pdf, { refsHandler, styleSheets, viewport, svg2pdfParameters })
32+
const textMeasure = new TextMeasure()
33+
34+
const context = new Context(pdf, {
35+
refsHandler,
36+
styleSheets,
37+
viewport,
38+
svg2pdfParameters,
39+
textMeasure
40+
})
3241

3342
pdf.advancedAPI()
3443
pdf.saveGraphicsState()

test/unit/all.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ for (const test of window.tests) {
3535
// await svg2pdf(svgElement, pdf, svg2pdfOptions)
3636

3737
comparePdf(pdf.output(), `/test/specs/${name}/reference.pdf`, debug)
38+
39+
if (document.querySelector('svg')) {
40+
expect.fail('svg measuring element must not remain in document')
41+
}
3842
})
3943
})
4044
}

0 commit comments

Comments
 (0)