ecc216e8110bbca557bc7f576296c4f6d4bb2da4
[yaffs-website] / vendor / phenx / php-font-lib / src / FontLib / Font.php
1 <?php
2 /**
3  * @package php-font-lib
4  * @link    https://github.com/PhenX/php-font-lib
5  * @author  Fabien Ménager <fabien.menager@gmail.com>
6  * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7  */
8
9 namespace FontLib;
10
11 use FontLib\Exception\FontNotFoundException;
12
13 /**
14  * Generic font file.
15  *
16  * @package php-font-lib
17  */
18 class Font {
19   static $debug = false;
20
21   /**
22    * @param string $file The font file
23    *
24    * @return TrueType\File|null $file
25    */
26   public static function load($file) {
27       if(!file_exists($file)){
28           throw new FontNotFoundException($file);
29       }
30
31     $header = file_get_contents($file, false, null, null, 4);
32     $class  = null;
33
34     switch ($header) {
35       case "\x00\x01\x00\x00":
36       case "true":
37       case "typ1":
38         $class = "TrueType\\File";
39         break;
40
41       case "OTTO":
42         $class = "OpenType\\File";
43         break;
44
45       case "wOFF":
46         $class = "WOFF\\File";
47         break;
48
49       case "ttcf":
50         $class = "TrueType\\Collection";
51         break;
52
53       // Unknown type or EOT
54       default:
55         $magicNumber = file_get_contents($file, false, null, 34, 2);
56
57         if ($magicNumber === "LP") {
58           $class = "EOT\\File";
59         }
60     }
61
62     if ($class) {
63       $class = "FontLib\\$class";
64
65       /** @var TrueType\File $obj */
66       $obj = new $class;
67       $obj->load($file);
68
69       return $obj;
70     }
71
72     return null;
73   }
74
75   static function d($str) {
76     if (!self::$debug) {
77       return;
78     }
79     echo "$str\n";
80   }
81
82   static function UTF16ToUTF8($str) {
83     return mb_convert_encoding($str, "utf-8", "utf-16");
84   }
85
86   static function UTF8ToUTF16($str) {
87     return mb_convert_encoding($str, "utf-16", "utf-8");
88   }
89 }