Draw text in a memory buffer

Christoph Borgolte cb at delta-h.de
Fri Jun 29 05:18:25 PDT 2007


Here is an example how I do this using the wxGraphicsContext class.
Admittedly I'm not drawing text but lines. But if you strip things down
you will find all neccessary code to create an app and draw to a
wxMemoryDC (using wxGC or not).

regards
chris


/snipped
----------8<---------------8<---------------8<---------------8<---------------8<---------------8<---------------8<---------------8<---------------8<-----


#include <boost/shared_ptr.hpp>

#include "wx/wxprec.h"
#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif
#include <wx/filename.h>
#include <wx/cmdline.h>
#include <wx/textctrl.h>
#include "readnet.h"

void drawToGraphicsContext(
        boost::shared_ptr<wxGraphicsContext> gc,
        int w,
        int h,
        size_t m_sz,
        wxPoint2DDouble* m_b,
        wxPoint2DDouble* m_e,
        double xmin,
        double ymin,
        double xmax,
        double ymax
        )
{
    std::cout << "Drawing Edges: " << std::endl;
    boost::progress_timer pt;
    double dw(w);
    dw /= xmax-xmin;
    double dh(h);
    dh /= ymax-ymin;

    double sc = std::min(dw,dh);

    double yof = ymax-ymin;
    yof += fabs(yof*dh - yof*sc)/(2*sc);

    double xof = xmax-xmin;
    xof = fabs(xof*dw - xof*sc)/(2*sc);

    gc->Scale(sc,-sc);
    gc->Translate(xof,-yof);
    gc->SetFont(*wxNORMAL_FONT, *wxBLACK);
    gc->SetPen(*wxBLACK_PEN);
    gc->SetBrush(*wxWHITE_BRUSH);

    //Partitionieren
    size_t i = 0;
    size_t part = 1000;
    size_t cnt = m_sz/part;
    progress_display show_progress(cnt);
    for (; i < cnt; ++i){
        gc->StrokeLines(part, m_b+i*part, m_e+i*part);
        ++show_progress;
    }
    gc->StrokeLines(m_sz%part, m_b+i*part, m_e+i*part);
    ++show_progress;
}


////////////////////////////////////////////////////////////
//
// class MyApp
//
   
class MyApp : public wxApp
{
    public:
        MyApp():
            time_(clock()) {}

        ~MyApp()
        {
            double dTime( clock() - time_ );
            dTime /= CLOCKS_PER_SEC;
            std::cout << "Time needed:" << dTime << " sec." << std::endl;
        }
    public:
        virtual bool OnInit();
        virtual int OnRun()
        {
            exit(0);
        }

    private:
        clock_t time_;
        boost::shared_ptr<wxStreamToTextRedirector> red_;
};

IMPLEMENT_APP(MyApp)


bool MyApp::OnInit()
{

    //if ( !wxApp::OnInit() )
    //   return false;

    ::wxInitAllImageHandlers();


        long w = 1024;
    long h = 780;


    /**
     * CmdLineParser
     */
    wxCmdLineParser parser (argc, argv);
    parser.SetLogo(argv[0]);
    parser.AddParam(_("Eingabedatei"), wxCMD_LINE_VAL_STRING,
wxCMD_LINE_OPTION_MANDATORY);
    parser.AddParam(_("Ausgabedatei"), wxCMD_LINE_VAL_STRING,
wxCMD_LINE_OPTION_MANDATORY);
    parser.AddOption(wxT("w"), wxT("width"),_("Bildbreite"),
wxCMD_LINE_VAL_NUMBER);
    parser.AddOption(wxT("h"), wxT("height"),_("Bildhoehe"),
wxCMD_LINE_VAL_NUMBER);
    parser.AddSwitch(wxT("r"), wxT("redirect"),_("stdout in Frame
ausgeben"));

    wxString in_file, out_file;

    bool doRedirect = false;
    if (parser.Parse() == 0) {
        in_file = parser.GetParam(0);
        out_file = parser.GetParam(1);

        long lval;
        if ( parser.Found( wxT("width"), &lval) ){
            w = lval;
        }
        if ( parser.Found( wxT("height"), &lval) ){
            h = lval;
        }
        if ( parser.Found( wxT("redirect")) ){
            doRedirect = true;
        }
    }
    else
       return false;

    if (doRedirect) {
        wxFrame* frm = new wxFrame(NULL,-1, argv[0]);
        wxTextCtrl* ctrl = new wxTextCtrl(frm, -1, wxString(),
                wxDefaultPosition, wxDefaultSize,
                wxTE_MULTILINE);
        red_.reset(new wxStreamToTextRedirector (ctrl));
        frm->Show();
    }


    wxString fnm = in_file;
    wxFileName fn(fnm);
    if (!fn.FileExists()) {
        std::cerr << "Datei existiert nicht: " <<
fn.GetFullPath().mb_str() << std::endl;
        return false;
    }

    std::string f(fnm.ToAscii());
    ReadNet rn(f);
    rn.read();

    wxPoint2DDouble* b;
    wxPoint2DDouble* e;
    size_t sz = rn.getEdges(b,e);

    double xx,xn,yx,yn;
    rn.getBBX(xn,yn, xx, yx);

    wxBitmap bmp(w,h);
    wxMemoryDC mdc;
    mdc.SelectObject(bmp);
    mdc.Clear();
    boost::shared_ptr<wxGraphicsContext> gc(
wxGraphicsContext::Create(mdc));
    drawToGraphicsContext(gc, w, h, sz, b, e, xn, yn, xx, yx );
    mdc.SelectObject(wxNullBitmap);
    //speichern
    bmp.SaveFile(out_file,wxBITMAP_TYPE_PNG);
   
    return true;
}





More information about the wx-users mailing list