Stop wxRichTextCtrl Text Shifting

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
miclus
Can't get richer than this
Can't get richer than this
Posts: 747
Joined: Tue Mar 31, 2009 2:11 am

Stop wxRichTextCtrl Text Shifting

Post by miclus »

Hi. I've noticed a weird issue that doesn't happen on all fonts. But, if you highlight text (select the text) in a wxRichTextCtrl, the text will shift depending where you are highlighting. For instance, in the sample, if I try to select the line that begins with "Naturally" as seen in the picture, starting with the N, when I get to the t, the text shifts a little. I'm using Windows 7 64.

richtext.png
richtext.png (7.51 KiB) Viewed 1783 times
User avatar
doublemax
Moderator
Moderator
Posts: 19116
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Stop wxRichTextCtrl Text Shifting

Post by doublemax »

The chance that any bug/quirk in wxRTC ever gets fixed is practically zero. The code base is hard to dig through and the original author doesn't support it any more.
Use the source, Luke!
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Stop wxRichTextCtrl Text Shifting

Post by ONEEYEMAN »

Hi,
It is possible that its the font that is wide and it needs shifting to accomodate it.
.
Try a different font.

Thank you.
miclus
Can't get richer than this
Can't get richer than this
Posts: 747
Joined: Tue Mar 31, 2009 2:11 am

Re: Stop wxRichTextCtrl Text Shifting

Post by miclus »

Another followup on this. I identified the code that was causing it. It appears it was patched in version 3.1.4 in textmeasure.cpp in the function DoGetTextExtent() with this code:

Code: Select all

    // the result computed by GetTextExtentPoint32() may be too small as it
    // accounts for under/overhang of the first/last character while we want
    // just the bounding rect for this string so adjust the width as needed
    // when using italic fonts as the difference is really noticeable for them
    // (it may still exist, but seems to be at most 1px for the other fonts,
    // and calling GetCharABCWidths() is pretty slow and much slower than
    // calling GetTextExtentPoint32() itself, so avoid its overhead unless it's
    // really, really necessary).
    const wxFont font = GetFont();
    if ( font.IsOk() && font.GetStyle() != wxFONTSTYLE_NORMAL && len > 0 )
    {
        ABC widthABC;
        const wxChar chFirst = *string.begin();
        if ( ::GetCharABCWidths(m_hdc, chFirst, chFirst, &widthABC) )
        {
            if ( widthABC.abcA < 0 )
                sizeRect.cx -= widthABC.abcA;

            if ( len > 1 )
            {
                const wxChar chLast = *string.rbegin();
                ::GetCharABCWidths(m_hdc, chLast, chLast, &widthABC);
            }
            //else: we already have the width of the last character

            if ( widthABC.abcC < 0 )
                sizeRect.cx -= widthABC.abcC;
        }
        //else: GetCharABCWidths() failed, not a TrueType font?
    }
So, in previous versions, they only tested for len > 0, but this new code only runs for italic text. Incidentally, the shifting problem will still occur when this code runs with italic text. Personally, I edited it to never even do that code segment, even for italic text, because it isn't clear to me what exactly that code is needed for.
Post Reply