Font::TTF::Font - Memory representation of a font


Font::TTF::Font - Memory representation of a font


Here is the regression test (you provide your own font). Run it once and then again on the output of the first run. There should be no differences between the outputs of the two runs.

    $f = Font::TTF::Font->open($ARGV[0]);
    # force a read of all the tables
    $f->tables_do(sub { $_[0]->read; });
    # force read of all glyphs (use read_dat to use lots of memory!)
    # $f->{'loca'}->glyphs_do(sub { $_[0]->read; });
    $f->{'loca'}->glyphs_do(sub { $_[0]->read_dat; });
    # NB. no need to $g->update since $f->{'glyf'}->out will do it for us
    $f->release;            # clear up memory forcefully!


A Truetype font consists of a header containing a directory of tables which constitute the rest of the file. This class holds that header and directory and also creates objects of the appropriate type for each table within the font. Note that it does not read each table into memory, but creates a short reference which can be read using the form:


Classes are included that support many of the different TrueType tables. For those for which no special code exists, the table type table is used, which defaults to the Font::TTF::Table manpage. The current tables which are supported are:

    table       Font::TTF::Table      - for unknown tables
    GDEF        Font::TTF::GDEF
    GPOS        Font::TTF::GPOS
    GSUB        Font::TTF::GSUB
    LTSH        Font::TTF::LTSH
    OS/2        Font::TTF::OS_2
    PCLT        Font::TTF::PCLT
    bsln        Font::TTF::Bsln
    cmap        Font::TTF::Cmap       - see also Font::TTF::OldCmap
    cvt         Font::TTF::Cvt_
    fdsc        Font::TTF::Fdsc
    feat        Font::TTF::Feat
    fmtx        Font::TTF::Fmtx
    fpgm        Font::TTF::Fpgm
    glyf        Font::TTF::Glyf       - see also Font::TTF::Glyph
    hdmx        Font::TTF::Hdmx
    head        Font::TTF::Head
    hhea        Font::TTF::Hhea
    hmtx        Font::TTF::Hmtx
    kern        Font::TTF::Kern       - see alternative Font::TTF::AATKern
    loca        Font::TTF::Loca
    maxp        Font::TTF::Maxp
    mort        Font::TTF::Mort       - see also Font::TTF::OldMort
    name        Font::TTF::Name
    post        Font::TTF::Post
    prep        Font::TTF::Prep
    prop        Font::TTF::Prop
    vhea        Font::TTF::Vhea
    vmtx        Font::TTF::Vmtx

Links are:

the Font::TTF::Table manpage the Font::TTF::GDEF manpage the Font::TTF::GPOS manpage the Font::TTF::GSUB manpage the Font::TTF::LTSH manpage the Font::TTF::OS_2 manpage the Font::TTF::PCLT manpage the Font::TTF::Bsln manpage the Font::TTF::Cmap manpage the Font::TTF::Cvt_ manpage the Font::TTF::Fdsc manpage the Font::TTF::Feat manpage the Font::TTF::Fmtx manpage the Font::TTF::Fpgm manpage the Font::TTF::Glyf manpage the Font::TTF::Hdmx manpage the Font::TTF::Head manpage the Font::TTF::Hhea manpage the Font::TTF::Hmtx manpage the Font::TTF::Kern manpage the Font::TTF::Loca manpage the Font::TTF::Maxp manpage the Font::TTF::Mort manpage the Font::TTF::Name manpage the Font::TTF::Post manpage the Font::TTF::Prep manpage the Font::TTF::Prop manpage the Font::TTF::Vhea manpage the Font::TTF::Vmtx manpage the Font::TTF::OldCmap manpage the Font::TTF::Glyph manpage the Font::TTF::AATKern manpage the Font::TTF::OldMort manpage


Instance variables begin with a space (and have lengths greater than the 4 characters which make up table names).

This is used during output to disable the creation of the file checksum in the head table. For example, during DSIG table creation, this flag will be set to ensure that the file checksum is left at zero.

fname (R)
Contains the filename of the font which this object was read from.

The file handle which reflects the source file for this font.

Contains the offset from the beginning of the read file of this particular font directory, thus providing support for TrueType Collections.


Font::TTF::Font->AddTable($tablename, $class)

Adds the given class to be used when representing the given table name. It also 'requires' the class for you.


For those people who like making fonts without reading them. This subroutine will require all the table code for the various table types for you. Not needed if using Font::TTF::Font::read before using a table.


Creates a new font object and initialises with the given properties. This is primarily for use when a TTF is embedded somewhere. Notice that the properties are automatically preceded by a space when inserted into the object. This is in order that fields do not clash with tables.


Reads the header and directory for the given font file and creates appropriate objects for each table in the font.


Reads a Truetype font directory starting from the current location in the file. This has been separated from the open function to allow support for embedded TTFs for example in TTCs. Also reads the head and maxp tables immediately.

$f->out($fname [, @tablelist])

Writes a TTF file consisting of the tables in tablelist. The list is checked to ensure that only tables that exist are output. (This means that you can't have non table information stored in the font object with key length of exactly 4)

In many cases the user simply wants to output all the tables in alphabetical order. This can be done by not including a @tablelist, in which case the subroutine will output all the defined tables in the font in alphabetical order.

Returns $f on success and undef on failure, including warnings.

All output files must include the head table.

$f->out_xml($filename [, @tables])

Outputs the font in XML format

$f->XML_start($context, $tag, %attrs)

Handles start messages from the XML parser. Of particular interest to us are <font> and <table>.


Sends update to all the tables in the font and then resets all the isDirty flags on each table. The data structure in now consistent as a font (we hope).


Dirties all the tables in the font


Calls &func for each table in the font. Calls the table in alphabetical sort order as per the order in the directory:

    &func($table, $name);


Releases ALL of the memory used by the TTF font and all of its component objects. After calling this method, do NOT expect to have anything left in the Font::TTF::Font object.

NOTE, that it is important that you call this method on any Font::TTF::Font object when you wish to destruct it and free up its memory. Internally, we track things in a structure that can result in circular references, and without calling 'release()' these will not properly get cleaned up by Perl. Once you've called this method, though, don't expect to be able to do anything else with the Font::TTF::Font object; it'll have no internal state whatsoever.

Developer note: As part of the brute-force cleanup done here, this method will throw a warning message whenever unexpected key values are found within the Font::TTF::Font object. This is done to help ensure that any unexpected and unfreed values are brought to your attention so that you can bug us to keep the module updated properly; otherwise the potential for memory leaks due to dangling circular references will exist.


Bugs abound aplenty I am sure. There is a lot of code here and plenty of scope. The parts of the code which haven't been implemented yet are:

Version 4 format types are not supported yet.

Format type 2 (MBCS) has not been implemented yet and therefore may cause somewhat spurious results for this table type.

Only type 0 & type 2 tables are supported (type 1 & type 3 yet to come).

The current Font::TTF::Font::out method does not support the writing of TrueType Collections.

In addition there are weaknesses or features of this module library

Apart from these, I try to keep the code in a state of ``no known bugs'', which given the amount of testing this code has had, is not a guarantee of high quality, yet.

For more details see the appropriate class files.


Martin Hosken

Copyright Martin Hosken 1998.

No warranty or expression of effectiveness, least of all regarding anyone's safety, is implied in this software or documentation.


The Perl TTF module is licensed under the Perl Artistic License.

 Font::TTF::Font - Memory representation of a font