English 中文(简体)
How to speed up rotated text output in MFC
原标题:
  • 时间:2010-01-15 10:21:05
  •  标签:
  • c++
  • mfc
  • cdc

I have a MFC application that displays annotated maps, which can include a large amount of text. While the size and font of the text does not tend to change much, the rotation of the text varies considerably, in order to be aligned with the surrounding line work. This basically means that I have to do create and select a new font into the display context each time the rotation changes. Something like;

if (TextRotationChanges)
{
    m_pFont = new CFont;
    m_lf.lfEscapement = NewRotation;
    m_pFont->CreateFontIndirect(&m_lf);
}
CFont *OldFont = m_pDC->SelectObject(m_pFont);
m_pDC->TextOut(x,y,text,strlen(text));
m_pDC->SelectObject(OldFont);

This is obviously slow when dealing with large amounts of text. Is there any way of speeding this up without going to a different display engine such as D3D or OpenGL? Put another way, can I change the text rotation in the existing selected font?

n.b. I m already carrying out other obvious optimizations, like ensuring text is on screen at a visible size prior to attempting to draw it.

最佳回答

Creating and destroying many GDI object can be slow. What you can do is create 360 fonts at the startup of your program so that you can SelectObject() from a lookup table with pre-made fonts at the correct rotation, rather than creating them on-demand. Or you can rotate your text by not using lfEscapement but by using SetWorldTransform() with the appropriate rotation matrix (again, you could cache rotation matrices for speed). You d have to test if it will actually give you a speed gain.

See my question here SetWorldTransform() and font rotation for an issue I had/have with that approach, though (haven t had time to go back and look into it).

问题回答

Are you sure that the problem is the font and not the TextOut?
Oleg s idea of using a back buffer isn t bad if you want to avoid flickering.

If I was going to use a graphic engine I d try Cairo because it s designed specifically for that kind of jobs.
(it can render directly on win32 DC surfaces)

You should first draw it on the invisible DC and than copy to your DC.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签