Mirrored Text

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Mirrored Text

Post by rsb »

Is there a way to draw mirrored text to a wxDC object.

I tried the wxMirrorDC class but it didn't work. I also tried the SetLayoutDirection method and
it didn't work either.

Thank you.

Using:
- wxWidgets 3.1.0
- Window 7 Professional
- Visual Studio 2015.
User avatar
doublemax
Moderator
Moderator
Posts: 19160
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Mirrored Text

Post by doublemax »

Code: Select all

  void OnPaint( wxPaintEvent &evt )
  {
    wxPaintDC pdc(this);

    wxGraphicsContext *gc = wxGraphicsContext::Create( pdc );
    wxGraphicsFont gf = gc->CreateFont( 24, "Lucida Sans Unicode" );
    gc->SetFont(gf);

    // normal text
    gc->DrawText("some text", 150,30 );

    wxGraphicsMatrix gmc = gc->CreateMatrix();
    gmc.Scale( -1.0, 1.0 );
    gc->SetTransform( gmc );

    // mirrored text
    // beware that coordinates are affected by the negative x-scale, too
    gc->DrawText("some text", -130, 30 );
    delete gc;
  }
I tried the same with the wPaintDC directly, but it didn't work.

I'm also not sure if this would work on all platforms. Tested on Windows only.
Use the source, Luke!
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 469
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Mirrored Text

Post by New Pagodi »

In addition to the above, you can use a transformation matrix for the DC itself without wrapping it into a graphics context first. For example to mirror across the line x=50, you could do something like

Code: Select all

    wxAffineMatrix2D mat = dc.GetTransformMatrix();
    mat.Translate(50, 0);
    mat.Mirror();
    dc.SetTransformMatrix(mat);
    dc.DrawText("Hello",0,0);
    dc.ResetTransformMatrix();
To mirror across the line y=50, you would change this slightly to

Code: Select all

    wxAffineMatrix2D mat=dc.GetTransformMatrix();
    mat.Translate(0, 50);
    mat.Mirror(wxVERTICAL);
    dc.SetTransformMatrix(mat);
    dc.DrawText("Hello",0,0);
    dc.ResetTransformMatrix();
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: Mirrored Text

Post by rsb »

Thank you both, I'll try and let you know.
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: Mirrored Text

Post by rsb »

We're not drawing inside a paint event handler so I couldn't use the first option.

The second worked well when mirroring around the x or y axis.

How would the second option work for text displayed at a 45 degree rotation
or anything other than 0, 90, 180, 270.

Thanks.
New Pagodi
Super wx Problem Solver
Super wx Problem Solver
Posts: 469
Joined: Tue Jun 20, 2006 6:47 pm
Contact:

Re: Mirrored Text

Post by New Pagodi »

You can use the Rotate method to add a rotation factor to the matrix.

Just remember that when dealing with matrices, the order of operation matters. For example, this code:

Code: Select all

    mat.Translate(50, 50);
    mat.Mirror(wxVERTICAL);
    mat.Rotate(45*3.14/180);
    dc.SetTransformMatrix(mat);
    dc.DrawText("Hello",0,0);
results in:
mirror_then_rotate.png
mirror_then_rotate.png (694 Bytes) Viewed 1526 times
And changing the order slightly:

Code: Select all

    mat.Translate(50, 50);
    mat.Rotate(45*3.14/180);
    mat.Mirror(wxVERTICAL);
    dc.SetTransformMatrix(mat);
    dc.DrawText("Hello",0,0);
results in:
rotate_then_mirror.png
rotate_then_mirror.png (651 Bytes) Viewed 1526 times
rsb
I live to help wx-kind
I live to help wx-kind
Posts: 170
Joined: Fri May 29, 2015 7:26 pm

Re: Mirrored Text

Post by rsb »

Thank you.
Post Reply