Skip to content

Commit

Permalink
Use the actual bounds of the SVG DOM for exporting, ignoring offsets …
Browse files Browse the repository at this point in the history
…of the getBBox call caused by stroke widths or because of offsets of invisible outer nodes. Do a best guess to add the same offset on the right/bottom.

Fixes #185
  • Loading branch information
NiklasRentzCAU committed Aug 6, 2024
1 parent f436ff1 commit 4874784
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/klighd-core/src/klighd-svg-exporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ export class KlighdSvgExporter extends SvgExporter {
// Just don't copy the styles. This would overwrite any styles set by the SVG renderer and we do not need any other styles that may get copied here.
// So overwrite Sprotty's copyStyles method with an empty method.
}

protected getBounds(root: SModelRootImpl, document: Document) {
const svgElement = document.querySelector('svg')
if (svgElement) {
// Get the actual bounding box of the SVG element, including the stroke width.
// should use { stroke: true } argument here, but it's not supported in chromium.
const box = svgElement.getBBox()
// Instead, remove the x/y offset and assume that the diagram is at 0/0 and that the offset on the other side is the same.
const xOffset = box.x
const yOffset = box.y
box.x = 0
box.y = 0
box.width += xOffset * 2
box.height += yOffset * 2

return box
}

return super.getBounds(root, document)
}
}

0 comments on commit 4874784

Please sign in to comment.