Skip to content

Commit 1de3778

Browse files
committed
Encoding of simple glyphs
1 parent 2981cb8 commit 1de3778

File tree

2 files changed

+135
-7
lines changed

2 files changed

+135
-7
lines changed

classes/font_glyph_outline.cls.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,9 @@ function parse() {
9090

9191
return $this->data = $data;
9292
}
93+
94+
function encode(){
95+
$font = $this->getFont();
96+
$font->seek($this->offset);
97+
}
9398
}

classes/font_glyph_outline_simple.cls.php

Lines changed: 130 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,139 @@ function parse(){
113113
$this->table = null;
114114
return $this->data = $data;
115115
}
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+
}
116185

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){
118243
$path = "";
119244

120-
$points = $this->data["points"];
245+
if (!$points) {
246+
$points = $this->data["points"];
247+
}
248+
121249
$length = count($points);
122250
$firstIndex = 0;
123251
$count = 0;
@@ -135,11 +263,6 @@ public function getSVGContours(){
135263
return $path;
136264
}
137265

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-
143266
protected function getSVGPath($points, $startIndex, $count) {
144267
$offset = 0;
145268
$path = "";

0 commit comments

Comments
 (0)