Getting the Name of a Font from a TTF, OTF File

Font files such as TTF and OTF have metadata embedded in them with information such as the font’s name, what font family it belongs to and so forth. I wrote a PHP class that provides the ability to grab this information from any TTF and OTF file. I’ve tested it on many different fonts and it’s working surprisingly well.

Example Usage

1
2
$objFontInfo = new FontInfo( "path/to/font.ttf" );
print $objFontInfo->getFontName();

Here is the code for the class. A lot of credit goes to this answer provided on StackOverflow. I rewrote the majority of the class, simplifying it and using Hungarian notation (the way to go in PHP!). If you’re not a fan of Hungarian notation, then just grab code from the StackOverflow answer.

FontInfo.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
 
  /**
   * @class
   * Retrieves information such as the proper name from a font TTF or OTF font file.
   */
  class FontInfo {
 
    /**
     * Contains information in the TTF 'name' table.
     */
    private $_arrInfo = null;
 
    /**
     * Constructor.
     * @param strFontPath Path to the font file.
     */
    function __construct( $strFontPath ) {
      $this->_arrInfo = $this->getFontInfo( $strFontPath );
    }
 
    /**
     * Gets the information about the font at the specified path.
     * @param strFontPath
     * @return
     */
    private function getFontInfo( $strFontPath ) {
      // Open the file and read its contents.
      $obintJFile = fopen( $strFontPath, "r" );
      $strText = fread( $obintJFile, filesize( $strFontPath ) );
      fclose( $obintJFile );
 
      // Grab information.
      $intNumberOfTables = hexdec( $this->dec2ord( $strText[4] ) . $this->dec2ord( $strText[5] ) );
      for( $intI = 0; $intI < $intNumberOfTables; $intI++ ) {
        $strTag = $strText[ 12 + $intI * 16 ] . $strText[ 12 + $intI * 16 + 1 ] . $strText[ 12 + $intI * 16 + 2 ] . $strText[ 12 + $intI * 16 + 3 ];
 
        if( $strTag == "name" ) {
          $intOffset = hexdec(
            $this->dec2ord( $strText[ 12 + $intI * 16 + 8 ] ) . $this->dec2ord( $strText[ 12 + $intI * 16 + 8 + 1 ] ) .
            $this->dec2ord( $strText[ 12 + $intI * 16 + 8 + 2 ] ) . $this->dec2ord( $strText[ 12 + $intI * 16 + 8 + 3 ] )
          );
          $intOffsetStorage = hexdec( $this->dec2ord( $strText[ $intOffset + 4 ] ) . $this->dec2ord( $strText[ $intOffset + 5 ] ) );
          $intNumberOfNameRecords = hexdec( $this->dec2ord( $strText[ $intOffset + 2 ] ) . $this->dec2ord( $strText[ $intOffset + 3 ] ) );
        }
      }
 
      $intStorageDecimal = $intOffsetStorage + $intOffset;
      $strStorageHexadecimal = strtoupper( dechex( $intStorageDecimal ) );
 
      for( $intJ = 0; $intJ < $intNumberOfNameRecords; $intJ++ ) {
        //$platform_id_dec = hexdec( $this->dec2ord( $this->text[$this->ntOffset+6+$intJ*12+0]).$this->dec2ord( $this->text[$this->ntOffset+6+$intJ*12+1]));
        $intNameId = hexdec( $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 6 ] ) . $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 7 ] ) );
        $intStringLength = hexdec( $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 8 ] ) . $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 9 ] ) );
        $intStringOffset = hexdec( $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 10 ] ) . $this->dec2ord( $strText[ $intOffset + 6 + $intJ * 12 + 11 ] ) );
 
        if( !empty( $intNameId ) && empty( $arrFontTags[ $intNameId ] ) ) {
          for( $intL = 0; $intL < $intStringLength; $intL++ ) {
            if( ord( $strText[ $intStorageDecimal + $intStringOffset + $intL ] ) == "0") {
              continue;
            } else {
              $arrFontTags[ $intNameId ] .= $strText[ $intStorageDecimal + $intStringOffset + $intL ];
            }
          }
        }
      }
 
      return $arrFontTags;
    }
 
    /**
     * Converts decimal to hex using the ascii value.
     * @param intDecimal
     * @return
     */
    protected function dec2ord( $intDecimal ) {
      return $this->dec2hex( ord( $intDecimal ) );
    }
 
    /**
     * Performs hexadecimal to decimal conversion with proper padding.
     * @param intDecimal
     * @return
     */
    protected function dec2hex( $intDecimal ) {
      return str_repeat( "0", 2 - strlen( ( $strHexadecimal = strtoupper( dechex( $intDecimal ) ) ) ) ) . $strHexadecimal;
    }
 
    /**
     * Gets the copyright.
     * @return
     */
    public function getCopyright() {
      return $this->_arrInfo[0];
    }
 
    /**
     * Gets the font family.
     * @return
     */
    public function getFontFamily() {
      return $this->_arrInfo[1];
    }
 
    /**
     * Gets the sub font family.
     * @return
     */
    public function getFontSubFamily() {
      return $this->_arrInfo[2];
    }
 
    /**
     * Gets the font id.
     * @return
     */
    public function getFontId() {
      return $this->_arrInfo[3];
    }
 
    /**
     * Gets the font name.
     * @return
     */
    public function getFontName() {
      return $this->_arrInfo[4];
    }
 
  }

Good luck!

5 thoughts on “Getting the Name of a Font from a TTF, OTF File

Leave a Reply to chirag Cancel reply

Your email address will not be published. Required fields are marked *