[PHP] How to get information from TrueType font file

The following source code is the php class, for retrieve the name tables from font file.

yctin.ttf.class.php

<?php
/**
 * ycTIN - TTF class
 * Get the information tables from TrueType font file
 * 
 * @name ycTIN - TTF Info class
 * @version 0.1
 * @license GPL 3.0
 * @author Timmy Tin(ycTIN)
 * @website http://www.yctin.com
 * 
 * @history
 * v0.1		get all `name` tables
 */
class ycTIN_TTF {
	public $debug = true;
	private $error_message_tpl = "[ycTIN_TTF][ERROR] {message} <br />n";
 
	private $filename;
	private $file;
	private $position;
	private $offset;
	private $tables;
 
	public function __construct($filename = false) {
		if (false !== $filename) {
			$this->open($filename);
		}
	}
 
	public function __destruct() {
		$this->close();
	}
 
	public function open($filename) {
		$this->close();
 
		if (empty($filename)) {
			$this->printError("The filename cannot be empty");
			return false;
		}
		if (! file_exists($filename)) {
			$this->printError("The file $filename does not exist");
			return false;
		}
 
		$this->filename = $filename;
		$this->file = file_get_contents($filename);
		$this->tables = array();
 
		if (empty($this->file)) {
			$this->printError("The file $filename is empty");
			return false;
		}
		return true;
	}
 
	public function close() {
		$this->position = $this->offset = 0;
		unset($this->filename, $this->file, $this->tables);
	}
 
	public function getNameTable() {
		if (! isset($this->file) || empty($this->file)) {
			$this->printError("Please open the file before getNameTable()");
			return false;
		}
 
		$num_of_tables = $this->getUint16(4);
		for($i = 0; $i < $num_of_tables; $i ++) {
			if ("name" == $this->getTag(12 + $i * 16)) {
				$this->offset = $this->getUint32(12 + $i * 16 + 8);
				$this->position = $this->offset + 2;
				$num_of_name_tables = $this->getUint16();
				$name_tables_offset = $this->getUint16() + $this->offset;
			}
		}
 
		$name_tables = array();
		for($i = 0; $i < $num_of_name_tables; $i ++) {
			$this->position = $this->offset + 6 + $i * 12;
			$platform_id = $this->getUint16();
			$specific_id = $this->getUint16();
			$lang_id = $this->getUint16();
			$name_id = $this->getUint16();
			$string_length = $this->getUint16();
			$string_offset = $this->getUint16() + $name_tables_offset;
 
			$key = "$platform_id::$specific_id::$lang_id";
 
			if (isset($name_id) && empty($name_tables[$key][$name_id])) {
				$text = substr($this->file, $string_offset, $string_length);
				$name_tables[$key][$name_id] = str_replace(chr(0), "", $text);
			}
		}
 
		return $this->tables['name'] = $name_tables;
	}
 
	private function getTag($pt = false) {
		if (false === $pt) {
			$pt = $this->position;
			$this->position += 4;
		}
		return substr($this->file, $pt, 4);
	}
 
	private function getUint32($pt = false) {
		if (false === $pt) {
			$pt = $this->position;
			$this->position += 4;
		}
		$r = unpack("N", substr($this->file, $pt, 4));
		return $r[1];
	}
 
	private function getUint16($pt = false) {
		if (false === $pt) {
			$pt = $this->position;
			$this->position += 2;
		}
		$r = unpack("n", substr($this->file, $pt, 2));
		return $r[1];
	}
 
	private function printError($message) {
		if (true === $this->debug) {
			echo str_replace("{message}", $message, $this->error_message_tpl);
		}
	}
}
?>

This is a demo code.

yctin.ttf.demo.php

<div style="white-space: pre;">
<?php
//include libary
include ("yctin.ttf.class.php");
 
//create yctin_ttf object
$ttf = new ycTIN_TTF();
 
//open font file
if ($ttf->open("yctin.ttf")) {
 
	//get name table
	$rs = $ttf->getNameTable();
 
	//display result
	print_r($rs);
}
?>
</div>

Related posts:

  1. [PHP] Force download the files Some time you may want the user download the file...
  2. TrueType Overview TrueType is an outline font standard originally developed by Apple...
  3. [PHP] Detect and handle execution timeout error Sometime php will throw unhandled exception, e.g. over maximum execution...
  4. jQuery Plugins – Month Picker Good user experience month selector, easily to choose the report...
  5. jQuery Plugins – CheckTree(ycTIN Edition) This edition is compatible with CheckTree, fixed some bugs and...


1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Loading ... Loading ...
Tags: , , ,  

5 Comments

  1. schnurzelpurz

    Thanks for that great class! Exactly what I’m looking for…
    Maybe the following link is usefull for others to get the meaning of the array entries:
    http://developer.apple.com/fonts/TTRefMan/RM06/Chap6name.html
    (look for “Table 42: Name Identifiers”)

    ycTIN Reply:

    Yes, it is a great resources.
    I shared this link on this post.

    Reply

  2. Thank you, great script! You saved me so much trouble with this.

    Reply

  3. Great script, but can it work with opentype more? It seems it doesnt return the glyph count on otf files, but can read some other details like copyright, etc.

    ycTIN Reply:

    Hi Michael,

    This class only implemented the method for get the name table, so not return the glyph data.

    If you need more information, please visit the following link:
    http://www.microsoft.com/typography/otspec/otff.htm#otttables

    Reply

Leave a Reply

*