Fast Graphical String Truncation

January 17, 2008 at 9:51 pm Leave a comment

This is an article about low-level graphics primitives.
It applies to any 2D API ( f.e. Win32 GDI ).


Suppose you have a string that needs to be drawn inside a box of fixed size.

But the size of the string is not always the same. It might be a lot larger.


The string does not fit into the box. You cannot expand the box, so the only thing you can do, is truncate the string. But to do this nicely, you want to end the string with “…”, the venerable ellipsis.

String truncation

One solution is to truncate the string itself :


This looks nice, but it is a slow operation. There are two reasons for this :

  1. The width of the text can only be determined by calling a special API function that in essence renders the text.
  2. Replacing 3 characters by “…” will involve some string copying no matter how you twist and turn.

The problem lies mainly in the fact that doing this can almost never be done in one go, you need a couple of iterations to end up with a string which will fit the box.
There are a couple of possible algorithms but they all involve multiple API function calls to get the width of the text, and multiple string copies to compose all the different candidate strings.

In our case you could wind up trying “This is a…””This is a …”, “This is a t…”, “This is a te…”.

String Image Truncation

There is a faster solution however, which involves no string operations, only graphical operations.
To truncate a string graphically you need to peform these steps :

  • Query for the width of “…”
  • Query for the size of your subject string
  • Store the current clipregion = orginal clipregion
  • Clip the device to a box with the size of the string minus the width of “…” minus 1 extra pixel for nicer visual results
  • Draw the subject string
  • Re-set the original clipregion
  • Draw the “…” to the right of where you just drew the subject string

Of course the specifics of your API might require some extra steps, or a slightly different approach ( f.i. your API might offer push-pop clipregion semantics so you wouldn’t have to query the orginal clipregion )

This type of string truncation is much faster since it involves no direct string manipulation and at most two API calls to get text sizes. It’s also more determinstic so it depends less on the length of the string.

Look below for the result. It might look a little weird at first, but it is quite sleek and certainly a good enough solution if speed and simplicity are your main concerns.



Entry filed under: Command Line.

Best Wishes Homies C++ quick trick : named constructors

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed

Recent Posts


%d bloggers like this: