From 215f844502405c9b82b93d929eb2c8a1361274ca Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Sat, 10 Aug 2019 19:33:33 +0200 Subject: [PATCH] Improve whitespace formatting in generated SVG Write coordinates first, then styles. --- src/common/dcsvg.cpp | 149 +++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 68 deletions(-) diff --git a/src/common/dcsvg.cpp b/src/common/dcsvg.cpp index 6229cd863f..f8b2176255 100644 --- a/src/common/dcsvg.cpp +++ b/src/common/dcsvg.cpp @@ -48,7 +48,10 @@ namespace // in this code originally). inline wxString NumStr(double f) { - return wxString::FromCDouble(f, 2); + if ( f == 0 ) + return wxS("0.00"); + else + return wxString::FromCDouble(f, 2); } // Return the colour representation as HTML-like "#rrggbb" string and also @@ -74,7 +77,7 @@ wxString Col2SVG(wxColour c, float *opacity) wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID) { float opacity; - wxString s = wxS("stroke:") + Col2SVG(c, &opacity) + wxS("; "); + wxString s = wxS("stroke:") + Col2SVG(c, &opacity) + wxS(";"); switch ( style ) { @@ -84,10 +87,10 @@ wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID) case wxPENSTYLE_LONG_DASH: case wxPENSTYLE_DOT_DASH: case wxPENSTYLE_USER_DASH: - s += wxString::Format(wxS("stroke-opacity:%s; "), NumStr(opacity)); + s += wxString::Format(wxS(" stroke-opacity:%s;"), NumStr(opacity)); break; case wxPENSTYLE_TRANSPARENT: - s += wxS("stroke-opacity:0.0; "); + s += wxS(" stroke-opacity:0.0;"); break; default: wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Pen Style not available")); @@ -99,7 +102,7 @@ wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID) wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID) { float opacity; - wxString s = wxS("fill:") + Col2SVG(c, &opacity) + wxS("; "); + wxString s = wxS("fill:") + Col2SVG(c, &opacity) + wxS(";"); switch ( style ) { @@ -109,10 +112,10 @@ wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID) case wxBRUSHSTYLE_CROSS_HATCH: case wxBRUSHSTYLE_VERTICAL_HATCH: case wxBRUSHSTYLE_HORIZONTAL_HATCH: - s += wxString::Format(wxS("fill-opacity:%s; "), NumStr(opacity)); + s += wxString::Format(wxS(" fill-opacity:%s;"), NumStr(opacity)); break; case wxBRUSHSTYLE_TRANSPARENT: - s += wxS("fill-opacity:0.0; "); + s += wxS(" fill-opacity:0.0;"); break; default: wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Brush Style not available")); @@ -139,16 +142,16 @@ wxString wxGetPenPattern(wxPen& pen) switch (pen.GetStyle()) { case wxPENSTYLE_DOT: - s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\" "), w * 2, w * 5); + s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\""), w * 2, w * 5); break; case wxPENSTYLE_SHORT_DASH: - s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\" "), w * 10, w * 8); + s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\""), w * 10, w * 8); break; case wxPENSTYLE_LONG_DASH: - s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\" "), w * 15, w * 8); + s = wxString::Format(wxS("stroke-dasharray=\"%f,%f\""), w * 15, w * 8); break; case wxPENSTYLE_DOT_DASH: - s = wxString::Format(wxS("stroke-dasharray=\"%f,%f,%f,%f\" "), w * 8, w * 8, w * 2, w * 8); + s = wxString::Format(wxS("stroke-dasharray=\"%f,%f,%f,%f\""), w * 8, w * 8, w * 2, w * 8); break; case wxPENSTYLE_USER_DASH: { @@ -164,7 +167,7 @@ wxString wxGetPenPattern(wxPen& pen) s << ","; } } - s += wxS("\" "); + s += wxS("\""); break; } case wxPENSTYLE_STIPPLE_MASK_OPAQUE: @@ -230,7 +233,7 @@ wxString wxGetBrushFill(wxBrush& brush) wxString brushStyle = wxGetBrushStyleName(brush); if (!brushStyle.IsEmpty()) - s = wxS(" fill=\"url(#") + brushStyle + brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX).substr(1) + wxS(")\""); + s = wxS("fill=\"url(#") + brushStyle + brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX).substr(1) + wxS(")\""); return s; } @@ -242,32 +245,35 @@ wxString wxCreateBrushFill(wxBrush& brush) if (!patternName.IsEmpty()) { - patternName += brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX).substr(1); - s = wxS("\n"); - s += wxS(" \n\n"); + wxString brushColourStr = brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX); + + s += wxString::Format(wxS(" \n"), + patternName, brushColourStr.substr(1)); + s += wxString::Format(wxS(" \n"), + brushColourStr, pattern); + s += wxS(" \n"); } return s; @@ -490,7 +496,8 @@ void wxSVGFileDCImpl::DoDrawLine(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2) NewGraphicsIfNeeded(); wxString s; - s = wxString::Format(wxS(" \n"), wxGetPenPattern(m_pen), x1, y1, x2, y2); + s = wxString::Format(wxS(" \n"), + x1, y1, x2, y2, wxGetPenPattern(m_pen)); write(s); @@ -505,8 +512,9 @@ void wxSVGFileDCImpl::DoDrawLines(int n, const wxPoint points[], wxCoord xoffset NewGraphicsIfNeeded(); wxString s; - s = wxS(" \n"); + s += wxString::Format(wxS("\" style=\"fill:none\" %s/>\n"), + wxGetPenPattern(m_pen)); write(s); } @@ -523,12 +532,16 @@ void wxSVGFileDCImpl::DoDrawLines(int n, const wxPoint points[], wxCoord xoffset void wxSVGFileDCImpl::DoDrawPoint(wxCoord x1, wxCoord y1) { - wxString s; NewGraphicsIfNeeded(); - s = wxS("\n"); + + wxString s; + + s = wxS(" \n "); write(s); + DoDrawLine(x1, y1, x1, y1); - s = wxS("\n"); + + s = wxS(" \n"); write(s); } @@ -666,10 +679,10 @@ void wxSVGFileDCImpl::DoDrawRoundedRectangle(wxCoord x, wxCoord y, wxCoord width NewGraphicsIfNeeded(); wxString s; - s = wxString::Format(wxS(" \n"), + x, y, width, height, NumStr(radius), + wxGetPenPattern(m_pen), wxGetBrushFill(m_brush)); - s += wxS("/>\n"); write(s); CalcBoundingBox(x, y); @@ -681,22 +694,21 @@ void wxSVGFileDCImpl::DoDrawPolygon(int n, const wxPoint points[], wxPolygonFillMode fillStyle) { NewGraphicsIfNeeded(); + wxString s; - s = wxS(" \n"); + + s += wxString::Format(wxS("\" %s %s style=\"fill-rule:%s;\"/>\n"), + wxGetPenPattern(m_pen), wxGetBrushFill(m_brush), + (fillStyle == wxODDEVEN_RULE) ? wxS("evenodd") : wxS("nonzero")); + write(s); } @@ -750,8 +762,8 @@ void wxSVGFileDCImpl::DoDrawEllipse(wxCoord x, wxCoord y, wxCoord width, wxCoord int rw = width / 2; wxString s; - s = wxString::Format(wxS(" \n"); write(s); @@ -803,8 +815,8 @@ void wxSVGFileDCImpl::DoDrawArc(wxCoord x1, wxCoord y1, wxCoord x2, wxCoord y2, if (x1 == x2 && y1 == y2) { // drawing full circle fails with default arc. Draw two half arcs instead. - s = wxString::Format(wxS(" \n"); + s += wxString::Format(wxS("\" %s/>\n"), wxGetPenPattern(m_pen)); write(s); } @@ -883,15 +894,15 @@ void wxSVGFileDCImpl::DoDrawEllipticArc(wxCoord x, wxCoord y, wxCoord w, wxCoord { // Drawing full circle fails with default arc. Draw two half arcs instead. fArc = 1; - arcPath = wxString::Format(wxS(" \n"); + arcFill += wxString::Format(wxS(" L%s %s z\" %s/>\n"), + NumStr(xc), NumStr(yc), wxGetPenPattern(m_pen)); write(arcFill); } wxDCBrushChanger setTransp(*GetOwner(), *wxTRANSPARENT_BRUSH); NewGraphicsIfNeeded(); - wxString arcLine = arcPath + wxS("\"/>\n"); + wxString arcLine = wxString::Format(wxS("%s\" %s/>\n"), + arcPath, wxGetPenPattern(m_pen)); write(arcLine); } @@ -1051,45 +1063,46 @@ void wxSVGFileDCImpl::NewGraphicsIfNeeded() void wxSVGFileDCImpl::DoStartNewGraphics() { - wxString s, sBrush, sPenCap, sPenJoin, sPenStyle, sLast; - - sBrush = wxS(""), + sLast = wxString::Format(wxS("stroke-width:%d\" transform=\"translate(%s %s) scale(%s %s)\""), m_pen.GetWidth(), NumStr((m_deviceOriginX - m_logicalOriginX)* m_signX), NumStr((m_deviceOriginY - m_logicalOriginY)* m_signY), NumStr(m_scaleX * m_signX), NumStr(m_scaleY * m_signY)); - s = sBrush + sPenCap + sPenJoin + sPenStyle + sLast + wxS("\n"); + + s = wxString::Format(wxS("\n"), + wxBrushString(m_brush.GetColour(), m_brush.GetStyle()), + wxPenString(m_pen.GetColour(), m_pen.GetStyle()), + sPenCap, sPenJoin, sLast); write(s); }