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.
This commit is contained in:
Maarten Bent
2025-09-14 14:23:21 +02:00
parent 1534390071
commit 700defddcc
2 changed files with 20 additions and 8 deletions
+10 -7
View File
@@ -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
+10 -1
View File
@@ -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();
}