From 700defddccc7470075b9ab1d14a26a421abdadfe Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 22 Aug 2025 09:15:46 +0200 Subject: [PATCH] Save SVG files with the actual drawing size First render to a temporary wxSVGFileDC, then use the size of the bounding box as input for the actual SVG size. Use a slightly larger size to account for lines drawn with a bigger wxPen width. --- samples/drawing/drawing.cpp | 17 ++++++++++------- samples/svg/svgtest.cpp | 11 ++++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/samples/drawing/drawing.cpp b/samples/drawing/drawing.cpp index 1b5972bc3c..a25070bb12 100644 --- a/samples/drawing/drawing.cpp +++ b/samples/drawing/drawing.cpp @@ -2790,13 +2790,16 @@ void MyFrame::OnSave(wxCommandEvent& WXUNUSED(event)) wxGraphicsRenderer* tempRenderer = m_canvas->GetRenderer(); m_canvas->UseGraphicRenderer(nullptr); #endif - wxSVGFileDC svgdc(dlg.GetPath(), - canvasSize.GetWidth(), - canvasSize.GetHeight(), - 72, - "Drawing sample"); - svgdc.SetBitmapHandler(new wxSVGBitmapEmbedHandler()); - m_canvas->Draw(svgdc); + wxSize svgSize; + wxSVGFileDC tempSvgDC("", svgSize.x, svgSize.y, 72, "Drawing sample"); + m_canvas->Draw(tempSvgDC); + + svgSize = wxSize(tempSvgDC.MaxX(), tempSvgDC.MaxY()); + svgSize.IncBy(15); // account for wxPen width exceeding bounds + + wxSVGFileDC svgDC(dlg.GetPath(), svgSize.x, svgSize.y, 72, "Drawing sample"); + svgDC.SetBitmapHandler(new wxSVGBitmapEmbedHandler()); + m_canvas->Draw(svgDC); #if wxUSE_GRAPHICS_CONTEXT m_canvas->UseGraphicRenderer(tempRenderer); #endif diff --git a/samples/svg/svgtest.cpp b/samples/svg/svgtest.cpp index 327f0e5625..cae01d86af 100644 --- a/samples/svg/svgtest.cpp +++ b/samples/svg/svgtest.cpp @@ -235,8 +235,17 @@ MyPage::MyPage(wxNotebook *parent, int index) bool MyPage::OnSave(const wxString& filename) { - wxSVGFileDC svgDC(filename, 600, 650, 72, pageNames[m_index]); + wxSize svgSize; + wxSVGFileDC tempSvgDC("", svgSize.x, svgSize.y, 72, pageNames[m_index]); + OnDraw(tempSvgDC); + + svgSize = wxSize(tempSvgDC.MaxX(), tempSvgDC.MaxY()); + svgSize.IncBy(15); // account for wxPen width exceeding bounds + + wxSVGFileDC svgDC(filename, svgSize.x, svgSize.y, 72, pageNames[m_index]); + svgDC.SetBitmapHandler(new wxSVGBitmapEmbedHandler()); OnDraw(svgDC); + return svgDC.IsOk(); }