[wxDC::GetTextExtent()] Generates different values after wxMemoryDC::SelectObject()

Mario Figueiredo marfig at gmail.com
Mon Sep 3 05:13:24 PDT 2007


Hello everyone,

This is a short code snippet that, while not related to my own code,
better displays the problem and its easier to copy/paste:

    wxMemoryDC dc;

    wxString str("Mountains");
    wxCoord w, h;

    wxLogDebug(wxT("Before SelectObject():"), NULL);

    for (int i = wxFONTFAMILY_DEFAULT; i != wxFONTFAMILY_UNKNOWN; ++i) {

        dc.SetFont (wxFont(8, i, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));

        dc.GetTextExtent(str, &w, &h);
        wxLogDebug(wxT("%d, %s"), w, dc.GetFont().GetFamilyString());

    }

    wxBitmap base(68, 51); // arbitrary size
    dc.SelectObject(base);

    wxLogDebug(wxT("After SelectObject():"), NULL);

    for (int i = wxFONTFAMILY_DEFAULT; i != wxFONTFAMILY_UNKNOWN; ++i) {

        dc.SetFont (wxFont(8, i, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));

        dc.GetTextExtent(str, &w, &h);
        wxLogDebug(wxT("%d, %s"), w, dc.GetFont().GetFamilyString());

    }

Result: (Library 2.8.4, Windows XP/SP2, Visual C++ 2005
Express/SP1/SDK Server 2003 R2)

		03:10:42: Before SelectObject():
		03:10:42: 49, wxDEFAULT
		03:10:42: 42, wxDECORATIVE
		03:10:42: 49, wxROMAN
		03:10:42: 31, wxSCRIPT
		03:10:42: 49, wxSWISS
		03:10:42: 63, wxMODERN
		03:10:42: 63, wxTELETYPE
		03:10:42: After SelectObject():
		03:10:42: 50, wxDEFAULT
		03:10:42: 43, wxDECORATIVE
		03:10:42: 49, wxROMAN
		03:10:42: 31, wxSCRIPT
		03:10:42: 49, wxSWISS
		03:10:42: 63, wxMODERN
		03:10:42: 63, wxTELETYPE

This is clearly to do with fonts. However, I'm unsure if this is a
bug, or something else is happening in the background I'm not aware
of.

I do need to calculate GetTextExtent() before building the bitmap for
the wxMemoryDC because it will be part of the calculation needed to
determine that bitmap size. Later I need that value again (which I
expect to be the same) to actually draw the text on the DC.

So far I've been using wxFONTFAMILY_DEFAULT, but on my particular
case, I can choose another font family where this behavior is not
displayed. However I would like to learn more about this issue.
Particularly if it is a bug, or something else I'm not aware of.

Note that DECORATIVE also display similar behavior.




More information about the wx-users mailing list