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
// fixed precision (which is 2 for some unknown reason but this is what it was
// in this code originally).
inline wxString NumStr(double f)
inline wxString NumStr(double const& f)
{
if ( f == 0 )
return wxS("0.00");
@ -56,11 +56,12 @@ inline wxString NumStr(double f)
// Return the colour representation as HTML-like "#rrggbb" string and also
// 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 )
{
*opacity = c.Alpha() / 255.0f;
if ( opacity )
*opacity = c.Alpha() / 255.0f;
// Remove the alpha before using GetAsString(wxC2S_HTML_SYNTAX) as it
// doesn't support colours with alpha channel.
@ -68,13 +69,14 @@ wxString Col2SVG(wxColour c, float *opacity)
}
else // No alpha.
{
*opacity = 1.;
if ( opacity )
*opacity = 1.;
}
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;
wxString s = wxS("stroke:") + Col2SVG(c, &opacity) + wxS(";");
@ -94,12 +96,13 @@ wxString wxPenString(wxColour c, int style = wxPENSTYLE_SOLID)
break;
default:
wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Pen Style not available"));
break;
}
return s;
}
wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
wxString wxFillString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
{
float opacity;
wxString s = wxS("fill:") + Col2SVG(c, &opacity) + wxS(";");
@ -119,13 +122,13 @@ wxString wxBrushString(wxColour c, int style = wxBRUSHSTYLE_SOLID)
break;
default:
wxASSERT_MSG(false, wxS("wxSVGFileDC::Requested Brush Style not available"));
break;
}
return s;
}
static
wxString wxGetPenPattern(wxPen& pen)
wxString wxGetPenPattern(wxPen const& pen)
{
wxString s;
@ -190,7 +193,44 @@ wxString wxGetPenPattern(wxPen& pen)
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;
@ -227,13 +267,13 @@ wxString wxGetBrushStyleName(wxBrush& brush)
return brushStyle;
}
wxString wxGetBrushFill(wxBrush& brush)
wxString wxGetBrushFill(wxBrush const& brush)
{
wxString s;
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 + Col2SVG(brush.GetColour()).substr(1) + wxS(")\"");
return s;
}
@ -267,7 +307,7 @@ wxString wxCreateBrushFill(wxBrush& brush)
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"),
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("text-decoration:%s; "), textDecoration);
style += wxString::Format(wxS("%s %s stroke-width:0; "),
wxBrushString(m_textForegroundColour),
wxPenString(m_textForegroundColour));
wxFillString(m_textForegroundColour),
wxStrokeString(m_textForegroundColour));
style += wxS("white-space: pre;");
style += wxS("\"");
@ -637,8 +677,8 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
// draw text background
const wxString rectStyle = wxString::Format(
wxS("style=\"%s %s stroke-width:1;\""),
wxBrushString(m_textBackgroundColour),
wxPenString(m_textBackgroundColour));
wxFillString(m_textBackgroundColour),
wxStrokeString(m_textBackgroundColour));
const wxString rectTransform = wxString::Format(
wxS("transform=\"rotate(%s %d %d)\""),
@ -1063,46 +1103,17 @@ void wxSVGFileDCImpl::NewGraphicsIfNeeded()
void wxSVGFileDCImpl::DoStartNewGraphics()
{
wxString s, sPenCap, sPenJoin, sLast;
wxString s;
switch ( m_pen.GetCap() )
{
case wxCAP_PROJECTING:
sPenCap = wxS("stroke-linecap:square;");
break;
case wxCAP_BUTT:
sPenCap = wxS("stroke-linecap:butt;");
break;
case wxCAP_ROUND:
default:
sPenCap = wxS("stroke-linecap:round;");
}
s = wxString::Format(wxS("<g style=\"%s %s %s\" transform=\"translate(%s %s) scale(%s %s)\">\n"),
wxGetPenStyle(m_pen),
wxFillString(m_brush.GetColour(), m_brush.GetStyle()),
wxStrokeString(m_pen.GetColour(), m_pen.GetStyle()),
NumStr((m_deviceOriginX - m_logicalOriginX) * m_signX),
NumStr((m_deviceOriginY - m_logicalOriginY) * m_signY),
NumStr(m_scaleX * m_signX),
NumStr(m_scaleY * m_signY));
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_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);
}