Font::TTFMetrics

A parser for the TTF file
Download

Font::TTFMetrics Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Malay Kumar Basu
  • Publisher web site:
  • http://search.cpan.org/~malay/

Font::TTFMetrics Tags


Font::TTFMetrics Description

A parser for the TTF file Font::TTFMetrics is a Perl module that encapsulates the font metrics of a true type font file. A true type font file contains several tables which need to be parsed before any useful information could be gathered about the font. There is the excellent module for parsing TTF font in CPAN by Martin Hosken, Font::TTF. But in my opinion the use of Font::TTF requires intimate knowledge of TTF font format. This module was written to support the use of TTF in Pastel 2D graphics library in Perl. Three factors prompted me to write this module: first, I required a fast module to access TTF file. Second, all the access required was read-only. Last, I wanted a user friendly, higher level API to access TTF file.Each font file actually contains several informations the most important information is how a particular character will display on screen. The shape of a character (glyph) is determined by a series of points. The points are generally lines or points on curved path. For details see the TTF specification. Remember, the points actually determines the outline of the curve.TTF file stores the glyph shape in the "glyf" table of the font. The first glyph described in this table will be always a particular glyph, called "missing-glyph" which is shown in case the font file doesnot contains the glyph that a software wants.Each character in computer is actually a number. You can find what number corresponds to the character, you can call ord() on the character. This value is called the ordinal value of the character. If you just use common english typically the number of any character falls between 32-126, commonly called as ASCII. If you use some more extra character not commonly found in key-board like "degree" then your character code will fall between 0-255, commonly called LATIN-1 character set. Unicode is a way to use charaters with ordinal values beyond 255. The good thing about it is that the UTF8 encoding in perl works silently in the backdrop and you can intermix characters with any ordinal value. This ofcourse does not mean that you will be able to use character with any ordinal values for display. The font file must contains the corresponding glyph.The way to extract the glyph for a character is done by looking into "cmap" table of the font. This table contains the character ordinal number and a correspoding index. This index is used to look into the "glyf" table to extract the shape of the character. Thar means if you just substitute another index for a particular ordinal number you can actually display a different character, a mechanism known as "glyph substitution". As you can guess there is one more way to display a particular character instead of what if should display in a more font specific manner. If you just add a particular offset to a glyph ordinal value and provide the index for this added value in the "cmap" table, you can generate a completely different glyph. This mechanism works for a particular type of fonts supplied by Microsoft called symbol fonts. Example of these are symbol.ttf and wingding. Both these fonts does not supply any glyphs corresponding to LATIN-1 character sets but with ordinal values in the range of 61472-61695. But notice if you fire up your word-processor and change the font to symbol and type any character on the key board you get a display. For example, if you type A (ordinal value 65) what you get is greek capital alpha. This works this way: as soon as the word-processor find that you are using a symbol font (you can call is_symbol() method to find that) it just adds 61440 to any character you type and then queries the "cmap" table for the glyph.One more important aspect of using a TTF file is to find the width of a string. The easiest way to find this to query "htmx" table, which contains advanced width of each character, add up all the advance widths of the individual characters in the string and then go look into "kern" table, which contains the kerning value for pair of glyphs add deduct these values from the total width. You need to deduct also the left-side bearing of the first character and the right-side bearing of the last character from the total width.SYNOPSIS use Font::TTFMetrics; my $metrics = Font::TTFMetrics->new("somefont.ttf"); my $ascent = $metrics->get_ascent(); Requirements: · Perl


Font::TTFMetrics Related Software