暫無描述

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. namespace FontLib;
  9. use FontLib\Exception\FontNotFoundException;
  10. /**
  11. * Generic font file.
  12. *
  13. * @package php-font-lib
  14. */
  15. class Font {
  16. static $debug = false;
  17. /**
  18. * @param string $file The font file
  19. *
  20. * @return TrueType\File|null $file
  21. */
  22. public static function load($file) {
  23. if(!file_exists($file)){
  24. throw new FontNotFoundException($file);
  25. }
  26. $header = file_get_contents($file, false, null, 0, 4);
  27. $class = null;
  28. switch ($header) {
  29. case "\x00\x01\x00\x00":
  30. case "true":
  31. case "typ1":
  32. $class = "TrueType\\File";
  33. break;
  34. case "OTTO":
  35. $class = "OpenType\\File";
  36. break;
  37. case "wOFF":
  38. $class = "WOFF\\File";
  39. break;
  40. case "ttcf":
  41. $class = "TrueType\\Collection";
  42. break;
  43. // Unknown type or EOT
  44. default:
  45. $magicNumber = file_get_contents($file, false, null, 34, 2);
  46. if ($magicNumber === "LP") {
  47. $class = "EOT\\File";
  48. }
  49. }
  50. if ($class) {
  51. $class = "FontLib\\$class";
  52. /** @var TrueType\File $obj */
  53. $obj = new $class;
  54. $obj->load($file);
  55. return $obj;
  56. }
  57. return null;
  58. }
  59. static function d($str) {
  60. if (!self::$debug) {
  61. return;
  62. }
  63. echo "$str\n";
  64. }
  65. static function UTF16ToUTF8($str) {
  66. return mb_convert_encoding($str, "utf-8", "utf-16");
  67. }
  68. static function UTF8ToUTF16($str) {
  69. return mb_convert_encoding($str, "utf-16", "utf-8");
  70. }
  71. }