Chapter 8. Text and font handling

Table of Contents

8.1. Different types of fonts
8.2. Font families and font styles
8.3. Understanding text alignment and anchor point
8.4. Rotating text
8.5. Formatting text paragraphs
8.6. Adding custom TTF fonts
8.7. Inserting Unicode entities
8.7.1. The utility class "SymChar"
8.7.2. Graph example with Greek labels
8.8. Character encoding
8.8.1. Japanese encoding options
8.8.2. Chinese encoding options
8.8.3. Cyrillic encoding options
8.8.4. Hebrew encoding options
8.8.5. Greek encoding options

Before reading this chapter please make sure that your installation supports TTF fonts if you intend to use them (See Section 3.2). The rest of this chapter assumes that a working installation with TTF fonts is available when needed.

Note

It is highly recommended to create a setup where TTF fonts work since the visual quality is significantly better than for the built-in bitmap fonts. In addition the bitmap fonts are restricted to only display plain 7-bit ASCII characters which means that no accented characters can be displayed, for example the Scandinavian characters "åäö".

Note

All files in the library are encoded in utf-8.

8.1. Different types of fonts

The library supports two fundamental types of fonts.

  1. Bitmap fonts

    There are three built in bitmap fonts. They are available as font families FF_FONT0, FF_FONT1 and FF_FONT2. The advantage with bitmap fonts is that they are always available in all installations of GD. However, bitmap fonts only supports 7-bit ASCII so if you need to display any character from the extended character set it is not possible to use bitmap fonts. The available sizes of the bitmap fonts are also limited, the three available size corresponds to the three families where FF_FONT0 is the smallest and FF_FONT2 the largest available bitmap font.

    Bitmap fonts also has a more "rugged" look since they do not use anti-aliasing.

    Example: The following script lines shows a typical use of bit map font specified for the title of a graph

    1
    2
    3
    4
    5
    6
    
    <?php
    $graph = new Graph(....);
     
    //  Adjust the title to use the largest built-in bitmap font in bold face
    $graph->title->SetFont(FF_FONT2,FS_BOLD);
    ?>

    The remainder of this chapter will describe this in more details.

  2. TTF Fonts (True Type Fonts)

    True Type Fonts (TTF) give a much better visual quality of the text. They are available in all sizes and there are many more font family to choose from. All font family specification apart from the three bitmap fonts are TTF fonts. In order to use these fonts the installation must be configured in the right way which is described in Section 3.2. It is necessary to use TTF fonts in order to display extended character sets (outside the traditional 7-bit ASCII). TTF fonts must also be used when inserting unicode entities as described in Section 8.7

    In order to be able to use TTF fonts it is necessary to check if the installations supports this. See Section 3.2

    Example: The following script lines shows a typical use of the Arial TTF fonts for the title of a graph

    1
    2
    3
    4
    5
    6
    
    <?php
    $graph = new Graph(....);
     
    //  Adjust the title to use 14pt Arial bold face
    $graph->title->SetFont(FF_ARIAL,FS_BOLD,14);
    ?>