Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   Related Pages  

Text and Fonts

Text State

In libpdf++, the appearance of text is controlled by its state. Text state consists of of font, font size, horizonal scale etc. In libpdf++, each CText object contains a CState object to store its state. In short, if you want to change the font of a text string, you have to create another text state object with a different font.

Fonts in libpdf++ is represented by the CFont class. Currently libpdf++ supports the following types of fonts:

  1. Adobe standard fonts
  2. Truetype fonts
Each of the above fonts will appear as a derived class: the Adobe standard fonts is represented by the CStandard class. All of these classes are in the font namespace.

Adobe Standard Fonts

There are 13 Adobe standard fonts. According to the PDF specification, they must be provided by all PDF viewers. The 13 fonts are:

  1. Times Roman
  2. Times Bold
  3. Times Italic
  4. Times BoldItalic
  5. Helvetica
  6. Helvetica Bold
  7. Helvetica Oblique
  8. Helvetica BoldOblique
  9. Courier
  10. Courier Bold
  11. Courier Oblique
  12. Courier BoldOblique
  13. Symbol
  14. ZapfDingbats
Each of these fonts has a corresponding enum in the CStandard class. As mentioned in the tutorial, creating a standard font is as easy as:

    using namespace pdf::font ;
    CStandard *tmr = new CStandard( CStandard::times_roman ) ;

As you can guess, the CStandard class is a derived class of the CFont class, which is the base class of all font classes.

TrueType Fonts

Using a truetype font is simple in libpdf++, just give the name of the truetype font file:

    using namespace pdf::font ;
    CTrueType *trebuc = new CTrueType( "trebuc.ttf" ) ;

Sometimes the truetype font file contain more than one font. In that case we will have to specify the index of the font in the file. It can be done by calling the CTrueType constructor. See the class reference for details.

Unlike the standard fonts, truetypes fonts may not be avaliable for the viewer to display. As a result, the truetype font file will be embedded in the PDF document. As of this version (0.0.2) of libpdf++, the whole font file will be embedded. In the future, it will be improved such that only used character will be embedded.

Using Fonts with Text

Fonts must be added to the document object before writing the document into a file (i.e. calling CDoc::Write():

    using namespace pdf::font ;
    CTrueType *trebuc = new CTrueType( "trebuc.ttf" ) ;
    doc.AddFont( trebuc ) ;

Once a font is added to the document, the document will delete it in its destructor. We don't need to delete it ourselves.

Now that we have a usable font object. We can put it in a text state object in order to use it. As mentioned in the tutorial, a text state contain other text formatting properties also: character spacing, horizonal scaling etc. Read the reference to see exactly what you can do with it.

    text::CState state( trebuc, 12 ) ;
    page.DrawText( CText( "hello world!", state,
                          text::CAbsolutePos( 50, 758 ) ) ) ;

Here we construct a text state object which specifies to use 12 point Trebuchet as font, while others keeping default. The values 50 and 758 is the text position. The text position is an absolute position of (50, 758) in user space units (1/72 inches by default). Absolute position is relative to the origin, which is the lower left corner of the document. Upward and right are treated as positive.

Text Width

In order to ease the layout of text in a PDF document, libpdf++ can calcuate the width of a text string. This is done by the CText::Width() function. Just call it and it will calculate the width of the text and return it. The unit of measurement is in user space (1/72 inches by default).

Generated on Sun Feb 2 09:17:07 2003 for libpdf++ by doxygen1.2.16