暂无描述

name.php 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\Table\Type;
  9. use FontLib\Table\Table;
  10. use FontLib\Font;
  11. /**
  12. * `name` font table.
  13. *
  14. * @package php-font-lib
  15. */
  16. class name extends Table {
  17. private static $header_format = array(
  18. "format" => self::uint16,
  19. "count" => self::uint16,
  20. "stringOffset" => self::uint16,
  21. );
  22. const NAME_COPYRIGHT = 0;
  23. const NAME_NAME = 1;
  24. const NAME_SUBFAMILY = 2;
  25. const NAME_SUBFAMILY_ID = 3;
  26. const NAME_FULL_NAME = 4;
  27. const NAME_VERSION = 5;
  28. const NAME_POSTSCRIPT_NAME = 6;
  29. const NAME_TRADEMARK = 7;
  30. const NAME_MANUFACTURER = 8;
  31. const NAME_DESIGNER = 9;
  32. const NAME_DESCRIPTION = 10;
  33. const NAME_VENDOR_URL = 11;
  34. const NAME_DESIGNER_URL = 12;
  35. const NAME_LICENSE = 13;
  36. const NAME_LICENSE_URL = 14;
  37. const NAME_PREFERRE_FAMILY = 16;
  38. const NAME_PREFERRE_SUBFAMILY = 17;
  39. const NAME_COMPAT_FULL_NAME = 18;
  40. const NAME_SAMPLE_TEXT = 19;
  41. static $nameIdCodes = array(
  42. 0 => "Copyright",
  43. 1 => "FontName",
  44. 2 => "FontSubfamily",
  45. 3 => "UniqueID",
  46. 4 => "FullName",
  47. 5 => "Version",
  48. 6 => "PostScriptName",
  49. 7 => "Trademark",
  50. 8 => "Manufacturer",
  51. 9 => "Designer",
  52. 10 => "Description",
  53. 11 => "FontVendorURL",
  54. 12 => "FontDesignerURL",
  55. 13 => "LicenseDescription",
  56. 14 => "LicenseURL",
  57. // 15
  58. 16 => "PreferredFamily",
  59. 17 => "PreferredSubfamily",
  60. 18 => "CompatibleFullName",
  61. 19 => "SampleText",
  62. );
  63. static $platforms = array(
  64. 0 => "Unicode",
  65. 1 => "Macintosh",
  66. // 2 => Reserved
  67. 3 => "Microsoft",
  68. );
  69. static $platformSpecific = array(
  70. // Unicode
  71. 0 => array(
  72. 0 => "Default semantics",
  73. 1 => "Version 1.1 semantics",
  74. 2 => "ISO 10646 1993 semantics (deprecated)",
  75. 3 => "Unicode 2.0 or later semantics",
  76. ),
  77. // Macintosh
  78. 1 => array(
  79. 0 => "Roman",
  80. 1 => "Japanese",
  81. 2 => "Traditional Chinese",
  82. 3 => "Korean",
  83. 4 => "Arabic",
  84. 5 => "Hebrew",
  85. 6 => "Greek",
  86. 7 => "Russian",
  87. 8 => "RSymbol",
  88. 9 => "Devanagari",
  89. 10 => "Gurmukhi",
  90. 11 => "Gujarati",
  91. 12 => "Oriya",
  92. 13 => "Bengali",
  93. 14 => "Tamil",
  94. 15 => "Telugu",
  95. 16 => "Kannada",
  96. 17 => "Malayalam",
  97. 18 => "Sinhalese",
  98. 19 => "Burmese",
  99. 20 => "Khmer",
  100. 21 => "Thai",
  101. 22 => "Laotian",
  102. 23 => "Georgian",
  103. 24 => "Armenian",
  104. 25 => "Simplified Chinese",
  105. 26 => "Tibetan",
  106. 27 => "Mongolian",
  107. 28 => "Geez",
  108. 29 => "Slavic",
  109. 30 => "Vietnamese",
  110. 31 => "Sindhi",
  111. ),
  112. // Microsoft
  113. 3 => array(
  114. 0 => "Symbol",
  115. 1 => "Unicode BMP (UCS-2)",
  116. 2 => "ShiftJIS",
  117. 3 => "PRC",
  118. 4 => "Big5",
  119. 5 => "Wansung",
  120. 6 => "Johab",
  121. // 7 => Reserved
  122. // 8 => Reserved
  123. // 9 => Reserved
  124. 10 => "Unicode UCS-4",
  125. ),
  126. );
  127. protected function _parse() {
  128. $font = $this->getFont();
  129. $tableOffset = $font->pos();
  130. $data = $font->unpack(self::$header_format);
  131. $records = array();
  132. for ($i = 0; $i < $data["count"]; $i++) {
  133. $record = new nameRecord();
  134. $record_data = $font->unpack(nameRecord::$format);
  135. $record->map($record_data);
  136. $records[] = $record;
  137. }
  138. $names = array();
  139. foreach ($records as $record) {
  140. $font->seek($tableOffset + $data["stringOffset"] + $record->offset);
  141. $s = $font->read($record->length);
  142. $record->string = Font::UTF16ToUTF8($s);
  143. $names[$record->nameID] = $record;
  144. }
  145. $data["records"] = $names;
  146. $this->data = $data;
  147. }
  148. protected function _encode() {
  149. $font = $this->getFont();
  150. /** @var nameRecord[] $records */
  151. $records = $this->data["records"];
  152. $count_records = count($records);
  153. $this->data["count"] = $count_records;
  154. $this->data["stringOffset"] = 6 + $count_records * 12; // 6 => uint16 * 3, 12 => sizeof self::$record_format
  155. $length = $font->pack(self::$header_format, $this->data);
  156. $offset = 0;
  157. foreach ($records as $record) {
  158. $record->length = mb_strlen($record->getUTF16(), "8bit");
  159. $record->offset = $offset;
  160. $offset += $record->length;
  161. $length += $font->pack(nameRecord::$format, (array)$record);
  162. }
  163. foreach ($records as $record) {
  164. $str = $record->getUTF16();
  165. $length += $font->write($str, mb_strlen($str, "8bit"));
  166. }
  167. return $length;
  168. }
  169. }