From c34f04b7582d632008313034a0bfecb6b45a1d7e Mon Sep 17 00:00:00 2001 From: Maarten Bent Date: Fri, 2 Aug 2019 23:00:14 +0200 Subject: [PATCH] Refactor wxSVGFileDCImpl::DoDrawRotatedText Create the style string outside the loop, it can determined once and used for each line. --- src/common/dcsvg.cpp | 111 ++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 59 deletions(-) diff --git a/src/common/dcsvg.cpp b/src/common/dcsvg.cpp index 6b9cab2b3b..470c12ca22 100644 --- a/src/common/dcsvg.cpp +++ b/src/common/dcsvg.cpp @@ -535,11 +535,9 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor const double dx = heightLine * sin(rad); const double dy = heightLine * cos(rad); - // wxS("upper left") and wxS("upper right") + // Update bounding box: upper left, upper right, bottom left, bottom right CalcBoundingBox(x, y); CalcBoundingBox((wxCoord)(x + w * cos(rad)), (wxCoord)(y - h * sin(rad))); - - // wxS("bottom left") and wxS("bottom right") CalcBoundingBox((wxCoord)(x + h * sin(rad)), (wxCoord)(y + h * cos(rad))); CalcBoundingBox((wxCoord)(x + h * sin(rad) + w * cos(rad)), (wxCoord)(y + h * cos(rad) - w * sin(rad))); @@ -555,73 +553,68 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor write(s); } + // Create text style string + wxString fontstyle; + switch (m_font.GetStyle()) + { + case wxFONTSTYLE_MAX: + wxFAIL_MSG(wxS("invalid font style value")); + wxFALLTHROUGH; + case wxFONTSTYLE_NORMAL: + fontstyle = wxS("normal"); + break; + case wxFONTSTYLE_ITALIC: + fontstyle = wxS("italic"); + break; + case wxFONTSTYLE_SLANT: + fontstyle = wxS("oblique"); + break; + } + + wxString textDecoration; + if (m_font.GetUnderlined()) + textDecoration += wxS(" underline"); + if (m_font.GetStrikethrough()) + textDecoration += wxS(" line-through"); + if (textDecoration.IsEmpty()) + textDecoration = wxS(" none"); + + wxString style = wxS("style=\""); + style += wxString::Format(wxS("font-family:%s; "), m_font.GetFaceName()); + style += wxString::Format(wxS("font-weight:%d; "), m_font.GetWeight()); + style += wxString::Format(wxS("font-style:%s; "), fontstyle); + style += wxString::Format(wxS("font-size:%dpt; "), m_font.GetPointSize()); + style += wxString::Format(wxS("text-decoration:%s; "), textDecoration); + style += wxString::Format(wxS("%s %s stroke-width:0; "), + wxBrushString(m_textForegroundColour), + wxPenString(m_textForegroundColour)); + style += wxS("\""); + style += wxS(" xml:space=\"preserve\""); + // Draw all text line by line const wxArrayString lines = wxSplit(sText, '\n', '\0'); for (size_t lineNum = 0; lineNum < lines.size(); lineNum++) { // convert x,y to SVG text x,y (the coordinates of the text baseline) wxCoord ww, hh, desc; - DoGetTextExtent(lines[lineNum], &ww, &hh, &desc); - int xx = x + wxRound(lineNum * dx) + (hh - desc) * sin(rad); - int yy = y + wxRound(lineNum * dy) + (hh - desc) * cos(rad); + wxString const& line = lines[lineNum]; + DoGetTextExtent(line, &ww, &hh, &desc); + const int xx = x + wxRound(lineNum * dx) + (hh - desc) * sin(rad); + const int yy = y + wxRound(lineNum * dy) + (hh - desc) * cos(rad); - //now do the text itself - s += wxString::Format(wxS(" 0) - s += wxS("style=\"font-family:") + fontName + wxS("; "); - else - s += wxS("style=\" "); - - wxString fontweight = wxString::Format(wxS("%d"), m_font.GetWeight()); - - s += wxS("font-weight:") + fontweight + wxS("; "); - - wxString fontstyle; - switch (m_font.GetStyle()) - { - case wxFONTSTYLE_MAX: - wxFAIL_MSG(wxS("invalid font style value")); - wxFALLTHROUGH; - - case wxFONTSTYLE_NORMAL: - fontstyle = wxS("normal"); - break; - - case wxFONTSTYLE_ITALIC: - fontstyle = wxS("italic"); - break; - - case wxFONTSTYLE_SLANT: - fontstyle = wxS("oblique"); - break; - } - - wxASSERT_MSG(!fontstyle.empty(), wxS("unknown font style value")); - - s += wxS("font-style:") + fontstyle + wxS("; "); - - wxString textDecoration; - if (m_font.GetUnderlined()) - textDecoration += wxS(" underline"); - if (m_font.GetStrikethrough()) - textDecoration += wxS(" line-through"); - if (textDecoration.IsEmpty()) - textDecoration = wxS(" none"); - - s += wxS("text-decoration:") + textDecoration + wxS("; "); - - s += wxString::Format(wxS("font-size:%dpt; "), m_font.GetPointSize()); - //text will be solid, unless alpha value isn't opaque in the foreground colour - s += wxBrushString(m_textForegroundColour) + wxPenString(m_textForegroundColour); - s += wxString::Format(wxS("stroke-width:0;\" transform=\"rotate(%s %d %d)\""), NumStr(-angle), xx, yy); - s += wxS(" xml:space=\"preserve\">"); + s = wxString::Format( + wxS(" %s\n"), + xx, yy, ww, style, transform, #if wxUSE_MARKUP - s += wxMarkupParser::Quote(lines[lineNum]) + wxS("\n"); + wxMarkupParser::Quote(line) #else - s += lines[lineNum] + wxS("\n"); + line #endif + ); write(s); }