14

I am implementing a text editor in C++ just using the vanilla Win32 API and I'm trying to find the best way to implement syntax highlighting. I know that there are existing controls out there like scintilla, but I'm doing this for fun so I want to do most of the work myself. I also want it to be fast and lightweight.

From what I've learned so far, it looks like the most low level option for drawing text in GDI is the TextOut function. However, if I need to keep changing the font color then that means I will need to make many calls to TextOut in order to draw one body of text with mixed formatting. Is this inefficient? When syntax highlighting and rich text controls are implemented, would they be likely to use TextOut behind the scenes or is there some other way? Is every other method of drawing text in GDI just a higher level wrapper around TextOut?

Graeme Hill
  • 623
  • 6
  • 11
  • 1
    Getting it correct is already a challenge. [Displaying Text with Uniscribe](http://msdn.microsoft.com/en-us/library/dd317792.aspx) is a good introduction to the problems that you'll need to address. – MSalters Apr 05 '11 at 07:28

3 Answers3

15

Both DrawText and TextOut are wrappers for ExtTextOut, so ExtTextOut is the low-level API. In my experience, ExtTextOut is pretty fast, so I doubt you'll see any performance issues with ExtTextOut itself. However, creating/selecting fonts can be a source of performance issues, so if you are switching back and forth between fonts, you can realize significant performance gains by caching and reusing fonts (HFONT) rather than CreateFont / SelectObject / DeleteObject each time. Basically, the first time you call SelectObject after creating a new font, Windows will perform a font matching process to find the best physical font for the logical font that you have requested. This is a fairly complex process, so you want to minimize the number of times that occurs in situations where performance is important.

I developed a rich edit control many years ago that was essentially a mini version of Microsoft Word. I used ExtTextOut as the main workhorse for all text output. The control would maintain a font cache of the most recently used fonts (default cache size was 10 fonts). It supported WYSIWYG layout, so it was actually doing all layout using a printer DC and fonts, then would render a screen compatible version using a screen DC and similar fonts, so there was a lot of extra work going on that likely is not applicable to your situation. Even so, performance was excellent running on typical hardware of the day (e.g., 266 mhz Pentium).

cbranch
  • 4,709
  • 2
  • 27
  • 25
  • 2
    It's not entirely accurate. For instance, `ExtTextOut` in turn uses API's like Uniscribe. And that in turn may do font substitution for you. So if you're looking for "the" low-level API, Uniscribe may be better. It has primitives like [`ScriptItemize`](http://msdn.microsoft.com/en-us/library/dd368556.aspx) "Breaks a Unicode string into individually shapeable items." – MSalters Apr 05 '11 at 07:22
  • 7
    @MSalters: It's true that ExtTextOut may use Uniscribe APIs, but Uniscribe ends up calling back to ExtTextOut to do the actual rendering, so I would still argue that ExtTextOut is "the" low-level API (see this link for details: [www.catch22.net/tuts/neatpad/11](http://www.catch22.net/tuts/neatpad/11)). That said, Uniscribe might be a better choice in terms of functionality, but the "fastest way" to draw text is ExtTextOut. – cbranch Apr 05 '11 at 18:37
  • "DrawText and TextOut are wrappers for ExtTextOut": This is correct for Windows 7, but does not apply to Windows XP. Apart from that PolyTextOut is the only text drawing API that does not call to ExtTextOut. Might be interesting to test the speed of PolyTextOut. – Elmue Oct 18 '16 at 14:46
  • 1
    Another speed improvement is to avoid APIs like GetTextExtentPoint32 and others to measure a string before drawing it. If you have to draw multiple strings one behind the other (for example each word with a different color) you can get the current drawing position by GetCurrentPositionEx and change it with MoveToEx. – Elmue Oct 18 '16 at 14:54
  • 1
    How do you cache fonts when you deal with juggling DC handles in the first place? The DC you obtain by calling `BeginPaint`, for instance, is copied from a cached DC which has default font selected, so how do you cache a font [and a font selection]? The way I have understood it, when you get a DC like that you _have_ to select the font all over, even if you already have a font handle ready from before. – Armen Michaeli May 04 '18 at 14:30
6

Instead of contemplating which "draw text" function is the fastest, it's probably far more advantageous to consider, "How can I minimize the amount of text I have to render at all", by being smart about what to redraw/invalidate as text changes, or how one can cache rendered text for scrolling.

Ana Betts
  • 73,868
  • 16
  • 141
  • 209
  • Interesting point. I had not looked into caching. Thanks for the idea. – Graeme Hill Apr 05 '11 at 15:37
  • 3
    This is not an answer to the question. If you are for example drawing a large log output into a window or a ListBox, there is nothing that could be reused or omitted. – Elmue Oct 18 '16 at 14:34
1

For complex usage you probably want DrawText as it gives you more control than TextOut. It has some basic formatting support, but less than you'll need for an editor. The next step up is the rich text editor from the common controls library, which pretty much takes care of all that for you.

Tim Sylvester
  • 22,897
  • 2
  • 80
  • 94
  • 1
    Is `DrawText` just a convenience function that acts as a wrapper around `TextOut` or is it completely separate? I did some very simple tests and found that `TextOut` is noticeably faster. I am aware of the rich edit controls like the RICHEDIT window class from Riched32.dll, but I want to implement the rich text control myself if possible using low level functions. – Graeme Hill Apr 05 '11 at 05:18
  • 1
    Win32 APIs are mostly a "black box", but if you record `DrawText` calls to a metafile and inspect it, you'll see a sequence of calls to `ExtTextOutW`. – Tim Sylvester Apr 05 '11 at 17:57