vbrtrmn has asked for the wisdom of the Perl Monks concerning the following question:

i'm either using the wrong package or i'm just too tired to figure it out.

I'm basically cataloging all of my fonts on my windows box, I have over 100, so I want to do it efficiently. I can't figure out how to get the font's name out of the font file. Below is the basic code and output, which gives me practically every piece of info, except for the most basic, the font's name.

SCRIPT

#!perl.exe -w use strict; use Font::TTF::Font; use CGI qw(:all); use Data::Dumper; print header(); my $infont = "c:/windows/fonts/D24000D5.TTF"; my $f = Font::TTF::Font->open($infont) || die "could not open font:<BR +>$!"; print "<pre>".Dumper($f)."</pre>"; $f->release; exit;

OUTPUT

$VAR1 = bless( { 'loca' => bless( { 'glyphs' => [], ' INFILE' => bless( \*Symbol::GEN0, 'IO::File' ), ' PARENT' => $VAR1, ' NAME' => 'loca', ' LENGTH' => 528, ' OFFSET' => 31404, ' CSUM' => 3174816727 }, 'Font::TTF::Loca' ), 'head' => bless( { 'flags' => 3, 'lowestRecPPEM' => 6, ' NAME' => 'head', ' CSUM' => 3212545405, 'unitsPerEm' => 2048, 'glyphDataFormat' => 0, ' read' => 1, 'magicNumber' => 1594834165, 'checkSumAdjustment' => 2521350520, 'fontRevision' => '1.00498962402344', 'xMax' => 2332, 'indexToLocFormat' => 0, ' LENGTH' => 54, 'yMax' => 1813, 'modified' => [ 0, 2883461375 ], 'macStyle' => 0, 'xMin' => '-116', 'fontDirectionHint' => 2, ' OFFSET' => 30260, 'yMin' => '-472', ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, 'version' => '1', 'created' => [ 0, 2883461375 ] }, 'Font::TTF::Head' ), 'cvt ' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'cvt ', ' LENGTH' => 122, ' OFFSET' => 1040, ' CSUM' => 769665920 }, 'Font::TTF::Cvt_' ), 'cmap' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'cmap', ' LENGTH' => 754, ' OFFSET' => 284, ' CSUM' => 590882806 }, 'Font::TTF::Cmap' ), 'maxp' => bless( { 'maxContours' => 5, 'maxComponentDepth' => 1, 'maxComponentElements' => 2, ' NAME' => 'maxp', ' CSUM' => 152502762, 'maxCompositePoints' => 110, 'maxStackElements' => 2048, ' read' => 1, 'maxFunctionDefs' => 0, ' LENGTH' => 32, 'maxStorage' => 0, ' OFFSET' => 31932, 'maxPoints' => 164, 'maxInstructionDefs' => 0, 'maxCompositeContours' => 4, 'maxTwilightPoints' => 4, ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, 'numGlyphs' => 263, 'maxSizeOfInstructions' => 213, 'version' => '1', 'maxZones' => 2 }, 'Font::TTF::Maxp' ), 'post' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'post', ' LENGTH' => 1840, ' OFFSET' => 32492, ' CSUM' => 2346503743 }, 'Font::TTF::Post' ), 'hmtx' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'hmtx', ' LENGTH' => 1052, ' OFFSET' => 30352, ' CSUM' => 1362962598 }, 'Font::TTF::Hmtx' ), 'glyf' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'glyf', ' LENGTH' => 29094, ' OFFSET' => 1164, ' CSUM' => 4161335985 }, 'Font::TTF::Glyf' ), ' OFFSET' => 0, 'name' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'name', ' LENGTH' => 528, ' OFFSET' => 31964, ' CSUM' => 3339688555 }, 'Font::TTF::Name' ), ' fname' => 'c:/windows/fonts/D24000D5.TTF', ' INFILE' => $VAR1->{'loca'}{' INFILE'}, 'prep' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'prep', ' LENGTH' => 29, ' OFFSET' => 34332, ' CSUM' => 796688338 }, 'Font::TTF::Prep' ), 'OS/2' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'OS/2', ' LENGTH' => 78, ' OFFSET' => 204, ' CSUM' => 1792378936 }, 'Font::TTF::OS_2' ), 'hhea' => bless( { ' INFILE' => $VAR1->{'loca'}{' INFILE'}, ' PARENT' => $VAR1, ' NAME' => 'hhea', ' LENGTH' => 36, ' OFFSET' => 30316, ' CSUM' => 272959505 }, 'Font::TTF::Hhea' ) }, 'Font::TTF::Font' );

--
paul

Replies are listed 'Best First'.
Re: Font::TTF Give Me the Font Name
by Cody Pendant (Prior) on Jan 06, 2003 at 03:11 UTC
    I can't help you specifically, but
    • There's a specific module in Font::TTF just for finding font names, Font::TTF::Name, which might make things less complex for this task
    • It has a method described thus:
      find_name($nid)
      Hunts down a name in all the standard places and returns the string
      which suggests to me that there's more than one place the name might be, which would explain some weirdness.

    --
    ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

      I did see Font::TTF::Name, what the heck does $nid refer to? I'm thinking name id, but the readme file is a bit vague on the subject.


      --
      paul
        This data structure:
        $f->{'name'}{'strings'}[$number][$platform_id][$encoding_id]{$language +_id}

        plus the fact that he mentions the "pid", "eid" and the "lid", makes me think that the "nid" is the $number part -- I'm only guessing though.
        --
        ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;
Solution From Another Node
by vbrtrmn (Pilgrim) on Jan 08, 2003 at 14:31 UTC

    Simplified from the TrueType font gallery node.

    #!perl.exe -w use strict; use Font::TTF::Font; use CGI qw(:all); print header(); my $f = 'arial.ttf'; my $fontname = Font::TTF::Font->open($f) || $f; $fontname = $fontname->{name} || $f if ref $fontname; $fontname = $fontname->read->find_name(4) || $f if ref $fontna +me; print $fontname; exit;

    --
    paul