Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

CTextRender class

4.63/5 (22 votes)
20 Oct 2005CPOL2 min read 1   1.9K  
An article on an alternative for TextOut (ExtTextOut) function.

TextRender - Sample project.jpg

Introduction

This article is about improvements made for the TextOut GDI function. With the provided text rendering class, one will be able to perform simple text formatting using RTF tags ("\b", "\i", "\u", "\par", "\r", "\n") and also simple text alignment (left, center, right, top, middle, bottom) in a given bounding rectangle.

Please note that you are not restricted in any way to use this class only in MFC applications.

Background

There are articles on CodeProject on similar subjects, but this is just a first step in developing an easy-to-use but powerful text-formatting function.

Using the code

To use the code include the header file "TextRender.h" in your project. Then add an instance of the CTextRender class. Call the EnhDrawText method with the provided text and that's it.

#include "TextRender.h"

// hDC is the device context for text rendering

CTextRender m_TextRender;
char text[] = "\\b1Hello, World !!!\\b0\\par\\par\\i1Thi"
              "s is a simple demonstration\\par of new\\i0"
              " \\ul1EnhDrawText\\ul0 \\i1function...\\i0";
RECT margins = {10, 5, 10, 5};
RECT textRect = {100, 100, 300, 300};
DWORD textAlignment = THA_CENTER | TVA_MIDDLE;
m_TextRender.EnhDrawText( hDC, text, strlen(text),
             &textRect, &margins, textAlignment );

Now, let's see what the other arguments of EnhDrawText are. The first argument is the handle of the device context for text rendering. The second and third arguments are the text with the formatting tags and the length of the text, respectively. The fourth argument is the text bounding rectangle (all word-breaking will be done in this rectangle). The fifth argument is text margins, and finally the last argument is the text alignment flag. It can have the following values for horizontal alignment:

  • THA_LEFT
  • THA_CENTER
  • THA_RIGHT

Also, it can take the following values for vertical alignment:

  • TVA_TOP
  • TVA_MIDDLE
  • TVA_BOTTOM

Combine these values to achieve different results.

Text formatting tags

The text formatting is performed using basic RTF (Rich Text Format) tags. The following tags are supported in this version of CTextRender class:

  • \b1 - bold text on
  • \b0 - bold text off
  • \i1 - italic text on
  • \i0 - italic text off
  • \u1 - underline text on
  • \u0 - underline text off
  • \par - new line flag, (which is the same as the following combination)
  • \r - carriage-return flag, (if stands alone does nothing)
  • \n - line-feed flag, (can stand alone)

Calculating text height

To calculate the formatted text height use the CalculateTextHeight method of the CTextRender class. See an example below:

int textHeight = CalculateTextHeight( hDC, text, strlen(text),
                           textRect, margins, width, height );

// To set width and height arguments do next
// width = textRect->right - textRect->left - (margins->right+margins->left);
// height = textRect->bottom - textRect->top - (margins->top+margins->bottom);

Points of Interest

Working on this problem I realised that basic support for text rendering through ExtTextOut or even DrawTextEx is not enough, so I am trying to upgrade these GDI functions to the level they can be used for advanced text rendering.

History

In this version of CTextRender class basic text formatting and alignment is supported. For the next upgrade it is planned to extend the list of supported RTF tags and to add text justification, and also a UNICODE text support.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)