6
6
* @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
7
7
*/
8
8
9
- require_once dirname (__FILE__ ) . "/Encoding_Map.php " ;
9
+ namespace FontLib ;
10
+
11
+ use FontLib \Table \Type \name ;
12
+ use FontLib \TrueType \File ;
10
13
11
14
/**
12
15
* Adobe Font Metrics file creation utility class.
13
- *
16
+ *
14
17
* @package php-font-lib
15
18
*/
16
19
class Adobe_Font_Metrics {
17
20
private $ f ;
18
-
21
+
19
22
/**
20
- * @var Font_TrueType
23
+ * @var File
21
24
*/
22
25
private $ font ;
23
-
24
- function __construct (Font_TrueType $ font ) {
26
+
27
+ function __construct (File $ font ) {
25
28
$ this ->font = $ font ;
26
29
}
27
-
28
- function write ($ file , $ encoding = null ){
30
+
31
+ function write ($ file , $ encoding = null ) {
29
32
$ map_data = array ();
30
33
31
34
if ($ encoding ) {
32
35
$ encoding = preg_replace ("/[^a-z0-9-_]/ " , "" , $ encoding );
33
- $ map_file = dirname (__FILE__ ). "/../maps/ $ encoding.map " ;
36
+ $ map_file = dirname (__FILE__ ) . "/../maps/ $ encoding.map " ;
34
37
if (!file_exists ($ map_file )) {
35
- throw new Exception ("Unkown encoding ( $ encoding) " );
38
+ throw new \ Exception ("Unkown encoding ( $ encoding) " );
36
39
}
37
-
38
- $ map = new Encoding_Map ($ map_file );
40
+
41
+ $ map = new Encoding_Map ($ map_file );
39
42
$ map_data = $ map ->parse ();
40
43
}
41
-
44
+
42
45
$ this ->f = fopen ($ file , "w+ " );
43
-
46
+
44
47
$ font = $ this ->font ;
45
-
48
+
46
49
$ this ->startSection ("FontMetrics " , 4.1 );
47
50
$ this ->addPair ("Notice " , "Converted by PHP-font-lib " );
48
51
$ this ->addPair ("Comment " , "https://github.com/PhenX/php-font-lib " );
49
-
52
+
50
53
$ encoding_scheme = ($ encoding ? $ encoding : "FontSpecific " );
51
54
$ this ->addPair ("EncodingScheme " , $ encoding_scheme );
52
-
55
+
53
56
$ 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 )) {
56
59
continue ;
57
60
}
58
-
59
- $ this ->addPair (Font_Table_name ::$ nameIdCodes [$ id ], $ record ->string );
61
+
62
+ $ this ->addPair (name ::$ nameIdCodes [$ id ], $ record ->string );
60
63
}
61
-
64
+
62
65
$ os2 = $ font ->getData ("OS/2 " );
63
66
$ this ->addPair ("Weight " , ($ os2 ["usWeightClass " ] > 400 ? "Bold " : "Medium " ));
64
-
67
+
65
68
$ 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 " ));
68
71
$ 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
+
71
74
$ hhea = $ font ->getData ("hhea " );
72
-
75
+
73
76
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 " ]));
76
79
$ this ->addPair ("Descender " , $ font ->normalizeFUnit ($ hhea ["descent " ]));
77
80
}
78
81
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 " ]));
81
84
$ this ->addPair ("Descender " , -abs ($ font ->normalizeFUnit ($ os2 ["typoDescender " ])));
82
85
}
83
-
86
+
84
87
$ head = $ font ->getData ("head " );
85
88
$ this ->addArray ("FontBBox " , array (
86
89
$ font ->normalizeFUnit ($ head ["xMin " ]),
87
90
$ font ->normalizeFUnit ($ head ["yMin " ]),
88
91
$ font ->normalizeFUnit ($ head ["xMax " ]),
89
92
$ font ->normalizeFUnit ($ head ["yMax " ]),
90
93
));
91
-
94
+
92
95
$ glyphIndexArray = $ font ->getUnicodeCharMap ();
93
-
96
+
94
97
if ($ glyphIndexArray ) {
95
- $ hmtx = $ font ->getData ("hmtx " );
98
+ $ hmtx = $ font ->getData ("hmtx " );
96
99
$ names = $ font ->getData ("post " , "names " );
97
-
100
+
98
101
$ 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 ) {
102
105
list ($ c , $ name ) = $ value ;
103
-
104
- if (!isset ($ glyphIndexArray [$ c ])) continue ;
105
-
106
+
107
+ if (!isset ($ glyphIndexArray [$ c ])) {
108
+ continue ;
109
+ }
110
+
106
111
$ g = $ glyphIndexArray [$ c ];
107
-
112
+
108
113
if (!isset ($ hmtx [$ g ])) {
109
114
$ hmtx [$ g ] = $ hmtx [0 ];
110
115
}
111
-
116
+
112
117
$ this ->addMetric (array (
113
118
"C " => ($ code > 255 ? -1 : $ code ),
114
119
"WX " => $ font ->normalizeFUnit ($ hmtx [$ g ][0 ]),
@@ -117,76 +122,86 @@ function write($file, $encoding = null){
117
122
}
118
123
}
119
124
else {
120
- foreach ($ glyphIndexArray as $ c => $ g ) {
125
+ foreach ($ glyphIndexArray as $ c => $ g ) {
121
126
if (!isset ($ hmtx [$ g ])) {
122
127
$ hmtx [$ g ] = $ hmtx [0 ];
123
128
}
124
-
129
+
125
130
$ this ->addMetric (array (
126
- "U " => $ c ,
131
+ "U " => $ c ,
127
132
"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 ,
130
135
));
131
136
}
132
137
}
133
-
138
+
134
139
$ this ->endSection ("CharMetrics " );
135
-
140
+
136
141
$ kern = $ font ->getData ("kern " , "subtable " );
137
142
$ tree = $ kern ["tree " ];
138
-
143
+
139
144
if (!$ encoding && is_array ($ tree )) {
140
145
$ 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 ;
164
169
}
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 " );
165
179
}
166
-
167
- $ this ->endSection ("KernPairs " );
180
+ }
181
+
182
+ $ this ->endSection ("KernPairs " );
168
183
$ this ->endSection ("KernData " );
169
184
}
170
185
}
171
-
186
+
172
187
$ this ->endSection ("FontMetrics " );
173
188
}
174
-
189
+
175
190
function addLine ($ line ) {
176
191
fwrite ($ this ->f , "$ line \n" );
177
192
}
178
-
193
+
179
194
function addPair ($ key , $ value ) {
180
195
$ this ->addLine ("$ key $ value " );
181
196
}
182
-
197
+
183
198
function addArray ($ key , $ array ) {
184
- $ this ->addLine ("$ key " . implode (" " , $ array ));
199
+ $ this ->addLine ("$ key " . implode (" " , $ array ));
185
200
}
186
-
201
+
187
202
function addMetric ($ data ) {
188
203
$ array = array ();
189
- foreach ($ data as $ key => $ value ) {
204
+ foreach ($ data as $ key => $ value ) {
190
205
$ array [] = "$ key $ value " ;
191
206
}
192
207
$ this ->addLine (implode (" ; " , $ array ));
@@ -195,7 +210,7 @@ function addMetric($data) {
195
210
function startSection ($ name , $ value = "" ) {
196
211
$ this ->addLine ("Start $ name $ value " );
197
212
}
198
-
213
+
199
214
function endSection ($ name ) {
200
215
$ this ->addLine ("End $ name " );
201
216
}
0 commit comments