Support multi-line text in wxSVGFileDC.

This commit is contained in:
Maarten Bent
2016-03-15 21:47:10 +01:00
parent 5084c6d423
commit b4c9927892

View File

@@ -367,11 +367,14 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
NewGraphicsIfNeeded(); NewGraphicsIfNeeded();
wxString s, sTmp; wxString s, sTmp;
// calculate bounding box // Get extent of whole text.
wxCoord w, h, desc; wxCoord w, h, heightLine;
DoGetTextExtent(sText, &w, &h, &desc); GetOwner()->GetMultiLineTextExtent(sText, &w, &h, &heightLine);
double rad = wxDegToRad(angle); // Compute the shift for the origin of the next line.
const double rad = wxDegToRad(angle);
const double dx = heightLine * sin(rad);
const double dy = heightLine * cos(rad);
// wxT("upper left") and wxT("upper right") // wxT("upper left") and wxT("upper right")
CalcBoundingBox(x, y); CalcBoundingBox(x, y);
@@ -394,22 +397,28 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
write(s); write(s);
} }
// 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) // convert x,y to SVG text x,y (the coordinates of the text baseline)
x = (wxCoord)(x + (h-desc)*sin(rad)); wxCoord ww, hh, desc;
y = (wxCoord)(y + (h-desc)*cos(rad)); 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);
//now do the text itself //now do the text itself
s.Printf (wxT(" <text x=\"%d\" y=\"%d\" "),x,y ); s.Printf (wxT(" <text x=\"%d\" y=\"%d\" "), xx, yy );
sTmp = m_font.GetFaceName(); sTmp = m_font.GetFaceName();
if (sTmp.Len() > 0) s += wxT("style=\"font-family:") + sTmp + wxT("; "); if (sTmp.Len() > 0) s += wxT("style=\"font-family:") + sTmp + wxT("; ");
else s += wxT("style=\" "); else s += wxT("style=\" ");
wxString fontweight; wxString fontweight;
switch ( m_font.GetWeight() ) switch (m_font.GetWeight())
{ {
case wxFONTWEIGHT_MAX: case wxFONTWEIGHT_MAX:
wxFAIL_MSG( wxS("invalid font weight value") ); wxFAIL_MSG(wxS("invalid font weight value"));
wxFALLTHROUGH; wxFALLTHROUGH;
case wxFONTWEIGHT_NORMAL: case wxFONTWEIGHT_NORMAL:
@@ -425,15 +434,15 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
break; break;
} }
wxASSERT_MSG( !fontweight.empty(), wxS("unknown font weight value") ); wxASSERT_MSG(!fontweight.empty(), wxS("unknown font weight value"));
s += wxT("font-weight:") + fontweight + wxT("; "); s += wxT("font-weight:") + fontweight + wxT("; ");
wxString fontstyle; wxString fontstyle;
switch ( m_font.GetStyle() ) switch (m_font.GetStyle())
{ {
case wxFONTSTYLE_MAX: case wxFONTSTYLE_MAX:
wxFAIL_MSG( wxS("invalid font style value") ); wxFAIL_MSG(wxS("invalid font style value"));
wxFALLTHROUGH; wxFALLTHROUGH;
case wxFONTSTYLE_NORMAL: case wxFONTSTYLE_NORMAL:
@@ -449,20 +458,21 @@ void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor
break; break;
} }
wxASSERT_MSG( !fontstyle.empty(), wxS("unknown font style value") ); wxASSERT_MSG(!fontstyle.empty(), wxS("unknown font style value"));
s += wxT("font-style:") + fontstyle + wxT("; "); s += wxT("font-style:") + fontstyle + wxT("; ");
sTmp.Printf (wxT("font-size:%dpt; "), m_font.GetPointSize() ); sTmp.Printf(wxT("font-size:%dpt; "), m_font.GetPointSize());
s += sTmp; s += sTmp;
//text will be solid, unless alpha value isn't opaque in the foreground colour //text will be solid, unless alpha value isn't opaque in the foreground colour
s += wxBrushString(m_textForegroundColour) + wxPenString(m_textForegroundColour); s += wxBrushString(m_textForegroundColour) + wxPenString(m_textForegroundColour);
sTmp.Printf ( wxT("stroke-width:0;\" transform=\"rotate( %s %d %d ) \" >"), NumStr(-angle), x,y ); sTmp.Printf ( wxT("stroke-width:0;\" transform=\"rotate( %s %d %d ) \" >"), NumStr(-angle), xx, yy );
s += sTmp + wxMarkupParser::Quote(sText) + wxT("</text> ") + wxT("\n"); s += sTmp + wxMarkupParser::Quote(lines[lineNum]) + wxT("</text> ") + wxT("\n");
if (m_OK) if (m_OK)
{ {
write(s); write(s);
} }
}
} }
void wxSVGFileDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height) void wxSVGFileDCImpl::DoDrawRectangle(wxCoord x, wxCoord y, wxCoord width, wxCoord height)