@@ -113,11 +113,139 @@ function parse(){
113
113
$ this ->table = null ;
114
114
return $ this ->data = $ data ;
115
115
}
116
+
117
+ public function splitSVGPath ($ path ) {
118
+ preg_match_all ('/([a-z])|(-?\d+(?:\.\d+)?)/i ' , $ path , $ matches , PREG_PATTERN_ORDER );
119
+ return $ matches [0 ];
120
+ }
121
+
122
+ public function makePoints ($ path ) {
123
+ $ path = $ this ->splitSVGPath ($ path );
124
+ $ l = count ($ path );
125
+ $ i = 0 ;
126
+
127
+ $ points = array ();
128
+
129
+ while ($ i < $ l ) {
130
+ switch ($ path [$ i ]) {
131
+ // moveTo
132
+ case "M " :
133
+ //if ($i == 0) {
134
+ $ points [] = array (
135
+ "onCurve " => true ,
136
+ "x " => $ path [++$ i ],
137
+ "y " => $ path [++$ i ],
138
+ "endOfContour " => ($ path [$ i ] === "z " ),
139
+ );
140
+ //}
141
+ break ;
142
+
143
+ // lineTo
144
+ case "L " :
145
+ $ points [] = array (
146
+ "onCurve " => true ,
147
+ "x " => $ path [++$ i ],
148
+ "y " => $ path [++$ i ],
149
+ "endOfContour " => ($ path [$ i ] === "z " ),
150
+ );
151
+ break ;
152
+
153
+ // quadraticCurveTo
154
+ case "Q " :
155
+ $ points [] = array (
156
+ "onCurve " => false ,
157
+ "x " => $ path [++$ i ],
158
+ "y " => $ path [++$ i ],
159
+ "endOfContour " => false ,
160
+ );
161
+ $ points [] = array (
162
+ "onCurve " => true ,
163
+ "x " => $ path [++$ i ],
164
+ "y " => $ path [++$ i ],
165
+ "endOfContour " => ($ path [$ i ] === "z " ),
166
+ );
167
+ break ;
168
+
169
+ // closePath
170
+ default :
171
+ case "z " :
172
+ $ i ++;
173
+ break ;
174
+ }
175
+ }
176
+
177
+ return $ points ;
178
+ }
179
+
180
+ function encode (){
181
+ parent ::encode ();
182
+
183
+ return $ this ->encodePoints ($ this ->data ["points " ]);
184
+ }
116
185
117
- public function getSVGContours (){
186
+ public function encodePoints ($ points ) {
187
+ $ endPtsOfContours = array ();
188
+ $ flags = array ();
189
+ $ coords_x = array ();
190
+ $ coords_y = array ();
191
+
192
+ $ last_x = 10e10 ;
193
+ $ last_y = 10e10 ;
194
+ foreach ($ points as $ i => $ point ) {
195
+ $ flag = 0 ;
196
+ if ($ point ["onCurve " ]) {
197
+ $ flag |= self ::ON_CURVE ;
198
+ }
199
+
200
+ if ($ point ["endOfContour " ]) {
201
+ $ endPtsOfContours [] = $ i ;
202
+ }
203
+
204
+ // Simplified, we could do some optimizations
205
+ if ($ point ["x " ] == $ last_x ) {
206
+ $ flag |= self ::THIS_X_IS_SAME ;
207
+ }
208
+ else {
209
+ $ coords_x [] = intval ($ point ["x " ]); // int16
210
+ }
211
+
212
+ // Simplified, we could do some optimizations
213
+ if ($ point ["y " ] == $ last_y ) {
214
+ $ flag |= self ::THIS_Y_IS_SAME ;
215
+ }
216
+ else {
217
+ $ coords_y [] = intval ($ point ["y " ]); // int16
218
+ }
219
+
220
+ $ flags [] = $ flag ;
221
+ $ last_x = $ point ["x " ];
222
+ $ last_y = $ point ["y " ];
223
+ }
224
+
225
+ $ font = $ this ->getFont ();
226
+
227
+ $ l = 0 ;
228
+ $ l += $ font ->writeInt16 (count ($ endPtsOfContours )); // endPtsOfContours
229
+ $ l += $ font ->writeInt16 (min ($ coords_x )); // xMin
230
+ $ l += $ font ->writeInt16 (min ($ coords_y )); // yMin
231
+ $ l += $ font ->writeInt16 (max ($ coords_x )); // xMax
232
+ $ l += $ font ->writeInt16 (max ($ coords_y )); // yMax
233
+ $ l += $ font ->w (array (self ::uint16, count ($ endPtsOfContours )), $ endPtsOfContours ); // endPtsOfContours
234
+ $ l += $ font ->writeInt16 (0 ); // instructionLength
235
+ $ l += $ font ->w (array (self ::uint8, count ($ flags )), $ flags ); // flags
236
+ $ l += $ font ->w (array (self ::int16, count ($ coords_x )), $ coords_x ); // xCoordinates
237
+ $ l += $ font ->w (array (self ::int16, count ($ coords_y )), $ coords_y ); // yCoordinates
238
+
239
+ return $ l ;
240
+ }
241
+
242
+ public function getSVGContours ($ points = null ){
118
243
$ path = "" ;
119
244
120
- $ points = $ this ->data ["points " ];
245
+ if (!$ points ) {
246
+ $ points = $ this ->data ["points " ];
247
+ }
248
+
121
249
$ length = count ($ points );
122
250
$ firstIndex = 0 ;
123
251
$ count = 0 ;
@@ -135,11 +263,6 @@ public function getSVGContours(){
135
263
return $ path ;
136
264
}
137
265
138
- public function parseSVGPath ($ path ) {
139
- preg_match_all ('/([a-z])|(-?\d+(?:\.\d+)?)/i ' , $ path , $ matches , PREG_PATTERN_ORDER );
140
- return $ matches [0 ];
141
- }
142
-
143
266
protected function getSVGPath ($ points , $ startIndex , $ count ) {
144
267
$ offset = 0 ;
145
268
$ path = "" ;
0 commit comments