Use const references in SVG helper functions

Add helper function to get pen style.
Use Col2SVG for wxBrush colours.
This commit is contained in:
Maarten Bent
2019-08-10 20:17:55 +02:00
parent 215f844502
commit e7c03a5646

View File

@@ -46,7 +46,7 @@ namespace
// C locale (i.e. always using "." for the decimal separator) and with the // C locale (i.e. always using "." for the decimal separator) and with the
// fixed precision (which is 2 for some unknown reason but this is what it was // fixed precision (which is 2 for some unknown reason but this is what it was
// in this code originally). // in this code originally).
inline wxString NumStr(double f) inline wxString NumStr(double const& f)
{ {
if ( f == 0 ) if ( f == 0 )
return wxS("0.00"); return wxS("0.00");
@@ -56,10 +56,11 @@ inline wxString NumStr(double f)
// Return the colour representation as HTML-like "#rrggbb" string and also // Return the colour representation as HTML-like "#rrggbb" string and also
// returns its alpha as opacity number in 0..1 range. // returns its alpha as opacity number in 0..1 range.
wxString Col2SVG(wxColour c, float *opacity) wxString Col2SVG(wxColour c, float *opacity = NULL)
{ {
if ( c.Alpha() != wxALPHA_OPAQUE ) if ( c.Alpha() != wxALPHA_OPAQUE )
{ {
if ( opacity )
*opacity = c.Alpha() / 255.0f; *opacity = c.Alpha() / 255.0f;
// Remove the alpha before using GetAsString(wxC2S_HTML_SYNTAX) as it // Remove the alpha before using GetAsString(wxC2S_HTML_SYNTAX) as it
@@ -68,13 +69,14 @@ wxString Col2SVG(wxColour c, float *opacity)
} }
else // No alpha. else // No alpha.
{ {
if ( opacity )
*opacity = 1.; *opacity = 1.;
} }
return c.GetAsString(wxC2S_HTML_SYNTAX); return c.GetAsString(wxC2S_HTML_SYNTAX);
} }
wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID) wxString wxStrokeString(wxColour const& c, int style = wxPENSTYLE_SOLID)
{ {
float opacity; float opacity;
wxString s = wxS("stroke:") + Col2SVG(c, &opacity) + wxS(";"); wxString s = wxS("stroke:") + Col2SVG(c, &opacity) + wxS(";");
@@ -94,12 +96,13 @@ wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID)
break; break;
default: default:
wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Pen Style not available")); wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Pen Style not available"));
break;
} }
return s; return s;
} }
wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID) wxString wxFillString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
{ {
float opacity; float opacity;
wxString s = wxS("fill:") + Col2SVG(c, &opacity) + wxS(";"); wxString s = wxS("fill:") + Col2SVG(c, &opacity) + wxS(";");
@@ -119,13 +122,13 @@ wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
break; break;
default: default:
wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Brush Style not available")); wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Brush Style not available"));
break;
} }
return s; return s;
} }
static wxString wxGetPenPattern(wxPen const& pen)
wxString wxGetPenPattern(wxPen& pen)
{ {
wxString s; wxString s;
@@ -190,7 +193,44 @@ wxString wxGetPenPattern(wxPen& pen)
return s; return s;
} }
wxString wxGetBrushStyleName(wxBrush& brush) wxString wxGetPenStyle(wxPen const& pen)
{
wxString penStyle;
penStyle += wxString::Format(wxS("stroke-width:%d;"), pen.GetWidth());
switch (pen.GetCap())
{
case wxCAP_PROJECTING:
penStyle += wxS(" stroke-linecap:square;");
break;
case wxCAP_BUTT:
penStyle += wxS(" stroke-linecap:butt;");
break;
case wxCAP_ROUND:
default:
penStyle += wxS(" stroke-linecap:round;");
break;
}
switch (pen.GetJoin())
{
case wxJOIN_BEVEL:
penStyle += wxS(" stroke-linejoin:bevel;");
break;
case wxJOIN_MITER:
penStyle += wxS(" stroke-linejoin:miter;");
break;
case wxJOIN_ROUND:
default:
penStyle += wxS(" stroke-linejoin:round;");
break;
}
return penStyle;
}
wxString wxGetBrushStyleName(wxBrush const& brush)
{ {
wxString brushStyle; wxString brushStyle;
@@ -227,13 +267,13 @@ wxString wxGetBrushStyleName(wxBrush& brush)
return brushStyle; return brushStyle;
} }
wxString wxGetBrushFill(wxBrush& brush) wxString wxGetBrushFill(wxBrush const& brush)
{ {
wxString s; wxString s;
wxString brushStyle = wxGetBrushStyleName(brush); wxString brushStyle = wxGetBrushStyleName(brush);
if (!brushStyle.IsEmpty()) if (!brushStyle.IsEmpty())
s = wxS("fill=\"url(#") + brushStyle + brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX).substr(1) + wxS(")\""); s = wxS("fill=\"url(#") + brushStyle + Col2SVG(brush.GetColour()).substr(1) + wxS(")\"");
return s; return s;
} }
@@ -267,7 +307,7 @@ wxString wxCreateBrushFill(wxBrush& brush)
break; break;
} }
wxString brushColourStr = brush.GetColour().GetAsString(wxC2S_HTML_SYNTAX); wxString brushColourStr = Col2SVG(brush.GetColour());
s += wxString::Format(wxS(" <pattern id=\"%s%s\" patternUnits=\"userSpaceOnUse\" width=\"8\" height=\"8\">\n"), s += wxString::Format(wxS(" <pattern id=\"%s%s\" patternUnits=\"userSpaceOnUse\" width=\"8\" height=\"8\">\n"),
patternName, brushColourStr.substr(1)); patternName, brushColourStr.substr(1));
@@ -609,8 +649,8 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
style += wxString::Format(wxS("font-size:%spt; "), NumStr(m_font.GetFractionalPointSize())); style += wxString::Format(wxS("font-size:%spt; "), NumStr(m_font.GetFractionalPointSize()));
style += wxString::Format(wxS("text-decoration:%s; "), textDecoration); style += wxString::Format(wxS("text-decoration:%s; "), textDecoration);
style += wxString::Format(wxS("%s %s stroke-width:0; "), style += wxString::Format(wxS("%s %s stroke-width:0; "),
wxBrushString(m_textForegroundColour), wxFillString(m_textForegroundColour),
wxPenString(m_textForegroundColour)); wxStrokeString(m_textForegroundColour));
style += wxS("white-space: pre;"); style += wxS("white-space: pre;");
style += wxS("\""); style += wxS("\"");
@@ -637,8 +677,8 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
// draw text background // draw text background
const wxString rectStyle = wxString::Format( const wxString rectStyle = wxString::Format(
wxS("style=\"%s %s stroke-width:1;\""), wxS("style=\"%s %s stroke-width:1;\""),
wxBrushString(m_textBackgroundColour), wxFillString(m_textBackgroundColour),
wxPenString(m_textBackgroundColour)); wxStrokeString(m_textBackgroundColour));
const wxString rectTransform = wxString::Format( const wxString rectTransform = wxString::Format(
wxS("transform=\"rotate(%s %d %d)\""), wxS("transform=\"rotate(%s %d %d)\""),
@@ -1063,46 +1103,17 @@ void wxSVGFileDCImpl::NewGraphicsIfNeeded()
void wxSVGFileDCImpl::DoStartNewGraphics() void wxSVGFileDCImpl::DoStartNewGraphics()
{ {
wxString s, sPenCap, sPenJoin, sLast; wxString s;
switch ( m_pen.GetCap() ) s = wxString::Format(wxS("<g style=\"%s %s %s\" transform=\"translate(%s %s) scale(%s %s)\">\n"),
{ wxGetPenStyle(m_pen),
case wxCAP_PROJECTING: wxFillString(m_brush.GetColour(), m_brush.GetStyle()),
sPenCap = wxS("stroke-linecap:square;"); wxStrokeString(m_pen.GetColour(), m_pen.GetStyle()),
break; NumStr((m_deviceOriginX - m_logicalOriginX) * m_signX),
case wxCAP_BUTT: NumStr((m_deviceOriginY - m_logicalOriginY) * m_signY),
sPenCap = wxS("stroke-linecap:butt;");
break;
case wxCAP_ROUND:
default:
sPenCap = wxS("stroke-linecap:round;");
}
switch (m_pen.GetJoin())
{
case wxJOIN_BEVEL:
sPenJoin = wxS("stroke-linejoin:bevel;");
break;
case wxJOIN_MITER:
sPenJoin = wxS("stroke-linejoin:miter;");
break;
case wxJOIN_ROUND:
default:
sPenJoin = wxS("stroke-linejoin:round;");
}
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_scaleX * m_signX),
NumStr(m_scaleY * m_signY)); NumStr(m_scaleY * m_signY));
s = wxString::Format(wxS("<g style=\"%s %s %s %s %s>\n"),
wxBrushString(m_brush.GetColour(), m_brush.GetStyle()),
wxPenString(m_pen.GetColour(), m_pen.GetStyle()),
sPenCap, sPenJoin, sLast);
write(s); write(s);
} }