66 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
77 */
88
9- require_once dirname (__FILE__ ) . "/Encoding_Map.php " ;
9+ namespace FontLib ;
10+
11+ use FontLib \Table \Type \name ;
12+ use FontLib \TrueType \File ;
1013
1114/**
1215 * Adobe Font Metrics file creation utility class.
13- *
16+ *
1417 * @package php-font-lib
1518 */
1619class Adobe_Font_Metrics {
1720 private $ f ;
18-
21+
1922 /**
20- * @var Font_TrueType
23+ * @var File
2124 */
2225 private $ font ;
23-
24- function __construct (Font_TrueType $ font ) {
26+
27+ function __construct (File $ font ) {
2528 $ this ->font = $ font ;
2629 }
27-
28- function write ($ file , $ encoding = null ){
30+
31+ function write ($ file , $ encoding = null ) {
2932 $ map_data = array ();
3033
3134 if ($ encoding ) {
3235 $ encoding = preg_replace ("/[^a-z0-9-_]/ " , "" , $ encoding );
33- $ map_file = dirname (__FILE__ ). "/../maps/ $ encoding.map " ;
36+ $ map_file = dirname (__FILE__ ) . "/../maps/ $ encoding.map " ;
3437 if (!file_exists ($ map_file )) {
35- throw new Exception ("Unkown encoding ( $ encoding) " );
38+ throw new \ Exception ("Unkown encoding ( $ encoding) " );
3639 }
37-
38- $ map = new Encoding_Map ($ map_file );
40+
41+ $ map = new Encoding_Map ($ map_file );
3942 $ map_data = $ map ->parse ();
4043 }
41-
44+
4245 $ this ->f = fopen ($ file , "w+ " );
43-
46+
4447 $ font = $ this ->font ;
45-
48+
4649 $ this ->startSection ("FontMetrics " , 4.1 );
4750 $ this ->addPair ("Notice " , "Converted by PHP-font-lib " );
4851 $ this ->addPair ("Comment " , "https://github.com/PhenX/php-font-lib " );
49-
52+
5053 $ encoding_scheme = ($ encoding ? $ encoding : "FontSpecific " );
5154 $ this ->addPair ("EncodingScheme " , $ encoding_scheme );
52-
55+
5356 $ records = $ font ->getData ("name " , "records " );
54- foreach ($ records as $ id => $ record ) {
55- if (!isset (Font_Table_name ::$ nameIdCodes [$ id ]) || preg_match ("/[ \r\n]/ " , $ record ->string )) {
57+ foreach ($ records as $ id => $ record ) {
58+ if (!isset (name ::$ nameIdCodes [$ id ]) || preg_match ("/[ \r\n]/ " , $ record ->string )) {
5659 continue ;
5760 }
58-
59- $ this ->addPair (Font_Table_name ::$ nameIdCodes [$ id ], $ record ->string );
61+
62+ $ this ->addPair (name ::$ nameIdCodes [$ id ], $ record ->string );
6063 }
61-
64+
6265 $ os2 = $ font ->getData ("OS/2 " );
6366 $ this ->addPair ("Weight " , ($ os2 ["usWeightClass " ] > 400 ? "Bold " : "Medium " ));
64-
67+
6568 $ post = $ font ->getData ("post " );
66- $ this ->addPair ("ItalicAngle " , $ post ["italicAngle " ]);
67- $ this ->addPair ("IsFixedPitch " , ($ post ["isFixedPitch " ] ? "true " : "false " ));
69+ $ this ->addPair ("ItalicAngle " , $ post ["italicAngle " ]);
70+ $ this ->addPair ("IsFixedPitch " , ($ post ["isFixedPitch " ] ? "true " : "false " ));
6871 $ this ->addPair ("UnderlineThickness " , $ font ->normalizeFUnit ($ post ["underlineThickness " ]));
69- $ this ->addPair ("UnderlinePosition " , $ font ->normalizeFUnit ($ post ["underlinePosition " ]));
70-
72+ $ this ->addPair ("UnderlinePosition " , $ font ->normalizeFUnit ($ post ["underlinePosition " ]));
73+
7174 $ hhea = $ font ->getData ("hhea " );
72-
75+
7376 if (isset ($ hhea ["ascent " ])) {
74- $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ hhea ["lineGap " ]));
75- $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ hhea ["ascent " ]));
77+ $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ hhea ["lineGap " ]));
78+ $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ hhea ["ascent " ]));
7679 $ this ->addPair ("Descender " , $ font ->normalizeFUnit ($ hhea ["descent " ]));
7780 }
7881 else {
79- $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ os2 ["typoLineGap " ]));
80- $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ os2 ["typoAscender " ]));
82+ $ this ->addPair ("FontHeightOffset " , $ font ->normalizeFUnit ($ os2 ["typoLineGap " ]));
83+ $ this ->addPair ("Ascender " , $ font ->normalizeFUnit ($ os2 ["typoAscender " ]));
8184 $ this ->addPair ("Descender " , -abs ($ font ->normalizeFUnit ($ os2 ["typoDescender " ])));
8285 }
83-
86+
8487 $ head = $ font ->getData ("head " );
8588 $ this ->addArray ("FontBBox " , array (
8689 $ font ->normalizeFUnit ($ head ["xMin " ]),
8790 $ font ->normalizeFUnit ($ head ["yMin " ]),
8891 $ font ->normalizeFUnit ($ head ["xMax " ]),
8992 $ font ->normalizeFUnit ($ head ["yMax " ]),
9093 ));
91-
94+
9295 $ glyphIndexArray = $ font ->getUnicodeCharMap ();
93-
96+
9497 if ($ glyphIndexArray ) {
95- $ hmtx = $ font ->getData ("hmtx " );
98+ $ hmtx = $ font ->getData ("hmtx " );
9699 $ names = $ font ->getData ("post " , "names " );
97-
100+
98101 $ this ->startSection ("CharMetrics " , count ($ hmtx ));
99-
100- if ($ encoding ) {
101- foreach ($ map_data as $ code => $ value ) {
102+
103+ if ($ encoding ) {
104+ foreach ($ map_data as $ code => $ value ) {
102105 list ($ c , $ name ) = $ value ;
103-
104- if (!isset ($ glyphIndexArray [$ c ])) continue ;
105-
106+
107+ if (!isset ($ glyphIndexArray [$ c ])) {
108+ continue ;
109+ }
110+
106111 $ g = $ glyphIndexArray [$ c ];
107-
112+
108113 if (!isset ($ hmtx [$ g ])) {
109114 $ hmtx [$ g ] = $ hmtx [0 ];
110115 }
111-
116+
112117 $ this ->addMetric (array (
113118 "C " => ($ code > 255 ? -1 : $ code ),
114119 "WX " => $ font ->normalizeFUnit ($ hmtx [$ g ][0 ]),
@@ -117,76 +122,86 @@ function write($file, $encoding = null){
117122 }
118123 }
119124 else {
120- foreach ($ glyphIndexArray as $ c => $ g ) {
125+ foreach ($ glyphIndexArray as $ c => $ g ) {
121126 if (!isset ($ hmtx [$ g ])) {
122127 $ hmtx [$ g ] = $ hmtx [0 ];
123128 }
124-
129+
125130 $ this ->addMetric (array (
126- "U " => $ c ,
131+ "U " => $ c ,
127132 "WX " => $ font ->normalizeFUnit ($ hmtx [$ g ][0 ]),
128- "N " => (isset ($ names [$ g ]) ? $ names [$ g ] : sprintf ("uni%04x " , $ c )),
129- "G " => $ g ,
133+ "N " => (isset ($ names [$ g ]) ? $ names [$ g ] : sprintf ("uni%04x " , $ c )),
134+ "G " => $ g ,
130135 ));
131136 }
132137 }
133-
138+
134139 $ this ->endSection ("CharMetrics " );
135-
140+
136141 $ kern = $ font ->getData ("kern " , "subtable " );
137142 $ tree = $ kern ["tree " ];
138-
143+
139144 if (!$ encoding && is_array ($ tree )) {
140145 $ this ->startSection ("KernData " );
141- $ this ->startSection ("KernPairs " , count ($ tree , COUNT_RECURSIVE ) - count ($ tree ));
142-
143- foreach ($ tree as $ left => $ values ) {
144- if (!is_array ($ values )) continue ;
145- if (! isset ( $ glyphIndexArray [ $ left ])) continue ;
146-
147- $ left_gid = $ glyphIndexArray [$ left ];
148-
149- if (! isset ( $ names [ $ left_gid ])) continue ;
150-
151- $ left_name = $ names [ $ left_gid ];
152-
153- $ this -> addLine ( "" );
154-
155- foreach ( $ values as $ right => $ value ) {
156- if (! isset ( $ glyphIndexArray [ $ right ])) continue ;
157-
158- $ right_gid = $ glyphIndexArray [ $ right ];
159-
160- if (! isset ( $ names [ $ right_gid ])) continue ;
161-
162- $ right_name = $ names [ $ right_gid ];
163- $ this -> addPair ( " KPX " , " $ left_name $ right_name $ value " ) ;
146+ $ this ->startSection ("KernPairs " , count ($ tree , COUNT_RECURSIVE ) - count ($ tree ));
147+
148+ foreach ($ tree as $ left => $ values ) {
149+ if (!is_array ($ values )) {
150+ continue ;
151+ }
152+ if (! isset ( $ glyphIndexArray [$ left ])) {
153+ continue ;
154+ }
155+
156+ $ left_gid = $ glyphIndexArray [ $ left ];
157+
158+ if (! isset ( $ names [ $ left_gid ])) {
159+ continue ;
160+ }
161+
162+ $ left_name = $ names [ $ left_gid ];
163+
164+ $ this -> addLine ( "" );
165+
166+ foreach ( $ values as $ right => $ value ) {
167+ if (! isset ( $ glyphIndexArray [ $ right ])) {
168+ continue ;
164169 }
170+
171+ $ right_gid = $ glyphIndexArray [$ right ];
172+
173+ if (!isset ($ names [$ right_gid ])) {
174+ continue ;
175+ }
176+
177+ $ right_name = $ names [$ right_gid ];
178+ $ this ->addPair ("KPX " , "$ left_name $ right_name $ value " );
165179 }
166-
167- $ this ->endSection ("KernPairs " );
180+ }
181+
182+ $ this ->endSection ("KernPairs " );
168183 $ this ->endSection ("KernData " );
169184 }
170185 }
171-
186+
172187 $ this ->endSection ("FontMetrics " );
173188 }
174-
189+
175190 function addLine ($ line ) {
176191 fwrite ($ this ->f , "$ line \n" );
177192 }
178-
193+
179194 function addPair ($ key , $ value ) {
180195 $ this ->addLine ("$ key $ value " );
181196 }
182-
197+
183198 function addArray ($ key , $ array ) {
184- $ this ->addLine ("$ key " . implode (" " , $ array ));
199+ $ this ->addLine ("$ key " . implode (" " , $ array ));
185200 }
186-
201+
187202 function addMetric ($ data ) {
188203 $ array = array ();
189- foreach ($ data as $ key => $ value ) {
204+ foreach ($ data as $ key => $ value ) {
190205 $ array [] = "$ key $ value " ;
191206 }
192207 $ this ->addLine (implode (" ; " , $ array ));
@@ -195,7 +210,7 @@ function addMetric($data) {
195210 function startSection ($ name , $ value = "" ) {
196211 $ this ->addLine ("Start $ name $ value " );
197212 }
198-
213+
199214 function endSection ($ name ) {
200215 $ this ->addLine ("End $ name " );
201216 }
0 commit comments