Ticket #14489: fix_wxSVGFileDC_text_position.patch

File fix_wxSVGFileDC_text_position.patch, 4.7 KB (added by neilc, 6 years ago)
  • samples/svg/svgtest.cpp

    From 28e08f461f96bd521721bdd38b12806f21a80b46 Mon Sep 17 00:00:00 2001
    From: Neil Chittenden <neilchittenden@quintessa.org>
    Date: Tue, 17 Jul 2012 16:30:41 +0100
    Subject: [PATCH] Fixed incorrect SVG text coordinates in wxSVGFileDC.
    
    ---
     samples/svg/svgtest.cpp |   48 ++++++++++++++++++++++++++++++++++++++++++++++-
     src/common/dcsvg.cpp    |   11 ++++++---
     2 files changed, 54 insertions(+), 5 deletions(-)
    
    diff --git a/samples/svg/svgtest.cpp b/samples/svg/svgtest.cpp
    index 159a15c..77a57d0 100644
    a b  
    4040    #include "../sample.xpm"
    4141#endif
    4242
     43#include <math.h>
     44
    4345class MyChild;
    4446class MyCanvas;
    4547
    MyCanvas::MyCanvas(MyChild *parent, const wxPoint& pos, const wxSize& size) 
    321323    SetBackgroundColour(wxColour(wxT("WHITE")));
    322324
    323325    m_child = parent;
    324     m_index = m_child->GetFrame()->GetCountOfChildren() % 7;
     326    m_index = m_child->GetFrame()->GetCountOfChildren() % 8;
    325327}
    326328
    327329// Define the repainting behaviour
    void MyCanvas::OnDraw(wxDC& dc) 
    493495#endif // wxUSE_STATUSBAR
    494496            break;
    495497
     498        case 7:
     499            wxString txtStr;
     500            wxCoord txtX, txtY, txtW, txtH, txtDescent, txtEL;
     501            wxCoord txtPad = 0;
     502
     503            wP = *wxRED_PEN;
     504            dc.SetPen(wP);
     505            //dc.SetBackgroundMode(wxBRUSHSTYLE_SOLID);
     506            //dc.SetTextBackground(*wxBLUE);
     507
     508            // Horizontal text
     509            txtStr = wxT("Horizontal string");
     510            dc.GetTextExtent(txtStr, &txtW, &txtH, &txtDescent, &txtEL);
     511            txtX = 50;
     512            txtY = 300;
     513            dc.DrawRectangle(txtX, txtY, txtW + 2*txtPad, txtH + 2*txtPad);
     514            dc.DrawText(txtStr, txtX + txtPad, txtY + txtPad);
     515
     516            // Vertical text
     517            txtStr = wxT("Vertical string");
     518            dc.GetTextExtent(txtStr, &txtW, &txtH, &txtDescent, &txtEL);
     519            txtX = 50;
     520            txtY = 250;
     521            dc.DrawRectangle(txtX, txtY - (txtW + 2*txtPad), txtH + 2*txtPad, txtW + 2*txtPad);
     522            dc.DrawRotatedText(txtStr, txtX + txtPad, txtY - txtPad, 90);
     523           
     524            // 45 degree text
     525            txtStr = wxT("45 deg string");
     526            dc.GetTextExtent(txtStr, &txtW, &txtH, &txtDescent, &txtEL);
     527            double lenW = (double)(txtW + 2*txtPad) / sqrt(2.0);
     528            double lenH = (double)(txtH + 2*txtPad) / sqrt(2.0);
     529            double padding = (double)txtPad / sqrt(2.0);
     530            txtX = 150;
     531            txtY = 200;
     532            dc.DrawLine(txtX - padding, txtY, txtX + lenW, txtY - lenW); // top
     533            dc.DrawLine(txtX + lenW, txtY - lenW, txtX - padding + lenH + lenW, txtY + (lenH - lenW));
     534            dc.DrawLine(txtX - padding, txtY, txtX - padding + lenH, txtY + lenH);
     535            dc.DrawLine(txtX - padding + lenH, txtY + lenH, txtX - padding + lenH + lenW, txtY + (lenH - lenW)); // bottom
     536            dc.DrawRotatedText(txtStr, txtX, txtY, 45);           
     537#if wxUSE_STATUSBAR
     538            s = wxT("Text position test page");
     539#endif // wxUSE_STATUSBAR
     540            break;
     541
    496542    }
    497543#if wxUSE_STATUSBAR
    498544    m_child->SetStatusText(s);
  • src/common/dcsvg.cpp

    diff --git a/src/common/dcsvg.cpp b/src/common/dcsvg.cpp
    index 9aeb7e9..8355c61 100644
    a b void wxSVGFileDCImpl::DoDrawRotatedText(const wxString& sText, wxCoord x, wxCoor 
    253253    CalcBoundingBox((wxCoord)(x + w*cos(rad)), (wxCoord)(y - h*sin(rad)));
    254254
    255255    // wxT("bottom left") and wxT("bottom right")
    256     x += (wxCoord)(h*sin(rad));
    257     y += (wxCoord)(h*cos(rad));
    258     CalcBoundingBox(x, y);
    259256    CalcBoundingBox((wxCoord)(x + h*sin(rad)), (wxCoord)(y + h*cos(rad)));
     257    CalcBoundingBox((wxCoord)(x + h*sin(rad) + w*cos(rad)), (wxCoord)(y + h*cos(rad) - w*sin(rad)));
    260258
    261259    if (m_backgroundMode == wxBRUSHSTYLE_SOLID)
    262260    {
    263261        // draw background first
    264262        // just like DoDrawRectangle except we pass the text color to it and set the border to a 1 pixel wide text background
    265263
    266         sTmp.Printf ( wxT(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "), x,y+desc-h, w, h );
     264        sTmp.Printf ( wxT(" <rect x=\"%d\" y=\"%d\" width=\"%d\" height=\"%d\" "), x, y, w, h );
    267265        s = sTmp + wxT("style=\"") + wxBrushString(m_textBackgroundColour);
    268266        s += wxT("stroke-width:1; ") + wxPenString(m_textBackgroundColour);
    269267        sTmp.Printf ( wxT("\" transform=\"rotate( %s %d %d )  \" />"), NumStr(-angle), x,y );
    270268        s += sTmp + wxT("\n");
    271269        write(s);
    272270    }
     271
     272    // convert x,y to SVG text x,y (the coordinates of the text baseline)
     273    x = (wxCoord)(x + (h-desc)*sin(rad));
     274    y = (wxCoord)(y + (h-desc)*cos(rad));
     275
    273276    //now do the text itself
    274277    s.Printf (wxT(" <text x=\"%d\" y=\"%d\" "),x,y );
    275278