PHP Sample Method for Getting Font Names of ttf Format Files

  • 2021-11-29 06:32:11
  • OfStack

In this paper, an example is given to describe the method of obtaining the font name of ttf format file by PHP. Share it for your reference, as follows:


<?php
$names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
foreach ($names as $name) {
  if ($name['language'] == 1033)
    $code = 'utf-16le';
  elseif ($name['language'] == 2052) $code = 'utf-16be';
  var_dump(mb_convert_encoding($name['name'],'utf-8',$code));
}
function GetFontName($FilePath) {
  $fp = fopen($FilePath, 'r');
  if ($fp) {
    //TT_OFFSET_TABLE
    $meta = unpack('n6', fread($fp, 12));
    // Check whether it is 1 A true type Whether the font file and version number are 1.0
    if ($meta[1] != 1 || $meta[2] != 0)
      return FALSE;
    $Found = FALSE;
    for ($i = 0; $i < $meta[3]; $i++) {
      //TT_TABLE_DIRECTORY
      $tablemeta = unpack('N4', $data = fread($fp, 16));
      if (substr($data, 0, 4) == 'name') {
        $Found = TRUE;
        break;
      }
    }
    if ($Found) {
      fseek($fp, $tablemeta[3]);
      //TT_NAME_TABLE_HEADER
      $tablecount = unpack('n3', fread($fp, 6));
      $Found = FALSE;
      for ($i = 0; $i < $tablecount[2]; $i++) {
        //TT_NAME_RECORD
        $table = unpack('n6', fread($fp, 12));
        if ($table[4] == 1) {
          $npos = ftell($fp);
          fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
          $fontname = trim($x = fread($fp, $table[5]));
          if (strlen($fontname) > 0) {
            $names[] = array (
                'platform' => $table[1], // Platform (operating system) 
    'language' => $table[3], // Language of font name 
    'encoding' => $table[2], // Encoding of font names 
    'name' => $fontname // Font name 
            );
            //break;
          }
          fseek($fp, $npos, SEEK_SET);
        }
      }
    }
    fclose($fp);
  }
  return $names;
}
?>

Run results:

string(6) "SimHei"
string (5) "SimHe"//BUG with UTF-16LE code missing words
string (6) [bold]

Note: If you only need to get the font name here, you can improve the above code as follows:


<?php
$names = GetFontName('c:/windows/fonts/FZHPJW.TTF');
$newnames = array();
foreach ($names as $name) {
  if ($name['language'] == 1033)
    $code = 'utf-16le';
  elseif ($name['language'] == 2052) $code = 'utf-16be';
  array_push($newnames,@mb_convert_encoding($name['name'], 'utf-8', $code));
}
$font_name=array_pop($newnames);
echo $font_name;
function GetFontName($FilePath) {
  $fp = fopen($FilePath, 'r');
  if ($fp) {
    //TT_OFFSET_TABLE
    $meta = unpack('n6', fread($fp, 12));
    // Check whether it is 1 A true type Whether the font file and version number are 1.0
    if ($meta[1] != 1 || $meta[2] != 0)
      return FALSE;
    $Found = FALSE;
    for ($i = 0; $i < $meta[3]; $i++) {
      //TT_TABLE_DIRECTORY
      $tablemeta = unpack('N4', $data = fread($fp, 16));
      if (substr($data, 0, 4) == 'name') {
        $Found = TRUE;
        break;
      }
    }
    if ($Found) {
      fseek($fp, $tablemeta[3]);
      //TT_NAME_TABLE_HEADER
      $tablecount = unpack('n3', fread($fp, 6));
      $Found = FALSE;
      for ($i = 0; $i < $tablecount[2]; $i++) {
        //TT_NAME_RECORD
        $table = unpack('n6', fread($fp, 12));
        if ($table[4] == 1) {
          $npos = ftell($fp);
          fseek($fp, $n = $tablemeta[3] + $tablecount[3] + $table[6], SEEK_SET);
          $fontname = trim($x = fread($fp, $table[5]));
          if (strlen($fontname) > 0) {
            $names[] = array (
                'platform' => $table[1], // Platform (operating system) 
    'language' => $table[3], // Language of font name 
    'encoding' => $table[2], // Encoding of font names 
    'name' => $fontname // Font name 
            );
            //break;
          }
          fseek($fp, $npos, SEEK_SET);
        }
      }
    }
    fclose($fp);
  }
  return $names;
}
?>

You can output directly at this time:

Blackbody

For more readers interested in PHP related contents, please check the topics on this site: "php File Operation Summary", "PHP Operation and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introduction Course", "php Object-Oriented Programming Introduction Course", "php String Usage Summary", "php+mysql Database Operation Introduction Course" and "php Common Database Operation Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: