Skip to content

Commit

Permalink
Fix leftover measurement svg (#274)
Browse files Browse the repository at this point in the history
properly clean up leftover measurement elements in body

Co-authored-by: Anna-Lena Lumpp <anna-lena.lumpp@yworks.com>
  • Loading branch information
HackbrettXXX and Anna-Lena Lumpp committed Aug 15, 2023
1 parent 0598244 commit a720cd6
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 15 deletions.
26 changes: 17 additions & 9 deletions src/context/context.ts
Expand Up @@ -38,24 +38,32 @@ export class Context {
? values.attributeState.clone()
: AttributeState.default()
this.viewport = values.viewport
this.refsHandler = values.refsHandler ?? null
this.styleSheets = values.styleSheets ?? null
this.textMeasure = values.textMeasure ?? new TextMeasure()
this.refsHandler = values.refsHandler
this.styleSheets = values.styleSheets
this.textMeasure = values.textMeasure
this.transform = values.transform ?? this.pdf.unitMatrix
this.withinClipPath = values.withinClipPath ?? false
this.withinUse = values.withinUse ?? false
}

clone(values: Partial<ContextOptions> = {}): Context {
clone(
values: {
viewport?: Viewport
attributeState?: AttributeState
transform?: Matrix
withinClipPath?: boolean
withinUse?: boolean
} = {}
): Context {
return new Context(this.pdf, {
svg2pdfParameters: values.svg2pdfParameters ?? this.svg2pdfParameters,
svg2pdfParameters: this.svg2pdfParameters,
attributeState: values.attributeState
? values.attributeState.clone()
: this.attributeState.clone(),
viewport: values.viewport ?? this.viewport,
refsHandler: values.refsHandler ?? this.refsHandler,
styleSheets: values.styleSheets ?? this.styleSheets,
textMeasure: values.textMeasure ?? this.textMeasure,
refsHandler: this.refsHandler,
styleSheets: this.styleSheets,
textMeasure: this.textMeasure,
transform: values.transform ?? this.transform,
withinClipPath: values.withinClipPath ?? this.withinClipPath,
withinUse: values.withinUse ?? this.withinUse
Expand All @@ -69,7 +77,7 @@ export interface ContextOptions {
attributeState?: AttributeState
refsHandler: ReferencesHandler
styleSheets: StyleSheets
textMeasure?: TextMeasure
textMeasure: TextMeasure
transform?: Matrix
withinClipPath?: boolean
withinUse?: boolean
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/clippath.ts
Expand Up @@ -29,7 +29,8 @@ export class ClipPath extends NonRenderedNode {
styleSheets: context.styleSheets,
viewport: context.viewport,
withinClipPath: true,
svg2pdfParameters: context.svg2pdfParameters
svg2pdfParameters: context.svg2pdfParameters,
textMeasure: context.textMeasure
})
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/image.ts
Expand Up @@ -68,7 +68,8 @@ export class ImageNode extends GraphicsNode {
refsHandler: new ReferencesHandler(idMap),
styleSheets: context.styleSheets,
viewport: new Viewport(width, height),
svg2pdfParameters: context.svg2pdfParameters
svg2pdfParameters: context.svg2pdfParameters,
textMeasure: context.textMeasure
})
)
return
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/marker.ts
Expand Up @@ -19,7 +19,8 @@ export class MarkerNode extends NonRenderedNode {
refsHandler: parentContext.refsHandler,
styleSheets: parentContext.styleSheets,
viewport: parentContext.viewport,
svg2pdfParameters: parentContext.svg2pdfParameters
svg2pdfParameters: parentContext.svg2pdfParameters,
textMeasure: parentContext.textMeasure
})

// "Properties do not inherit from the element referencing the 'marker' into the contents of the
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/pattern.ts
Expand Up @@ -30,7 +30,8 @@ export class Pattern extends NonRenderedNode {
refsHandler: context.refsHandler,
styleSheets: context.styleSheets,
viewport: context.viewport,
svg2pdfParameters: context.svg2pdfParameters
svg2pdfParameters: context.svg2pdfParameters,
textMeasure: context.textMeasure
})
)
}
Expand Down
3 changes: 2 additions & 1 deletion src/nodes/use.ts
Expand Up @@ -66,7 +66,8 @@ export class Use extends GraphicsNode {
styleSheets: context.styleSheets,
withinUse: true,
viewport: refNodeOpensViewport ? new Viewport(width!, height!) : context.viewport,
svg2pdfParameters: context.svg2pdfParameters
svg2pdfParameters: context.svg2pdfParameters,
textMeasure: context.textMeasure
})
const color = context.attributeState.color
await context.refsHandler.getRendered(id, color, node =>
Expand Down
11 changes: 10 additions & 1 deletion src/svg2pdf.ts
Expand Up @@ -6,6 +6,7 @@ import { ColorFill } from './fill/ColorFill'
import { jsPDF } from 'jspdf'
import { StyleSheets } from './context/stylesheets'
import { Viewport } from './context/viewport'
import { TextMeasure } from './context/textmeasure'

export async function svg2pdf(
element: Element,
Expand All @@ -28,7 +29,15 @@ export async function svg2pdf(

const svg2pdfParameters = { ...options, element }

const context = new Context(pdf, { refsHandler, styleSheets, viewport, svg2pdfParameters })
const textMeasure = new TextMeasure()

const context = new Context(pdf, {
refsHandler,
styleSheets,
viewport,
svg2pdfParameters,
textMeasure
})

pdf.advancedAPI()
pdf.saveGraphicsState()
Expand Down
4 changes: 4 additions & 0 deletions test/unit/all.spec.js
Expand Up @@ -35,6 +35,10 @@ for (const test of window.tests) {
// await svg2pdf(svgElement, pdf, svg2pdfOptions)

comparePdf(pdf.output(), `/test/specs/${name}/reference.pdf`, debug)

if (document.querySelector('svg')) {
expect.fail('svg measuring element must not remain in document')
}
})
})
}

0 comments on commit a720cd6

Please sign in to comment.