Skip to content

Commit f74ed50

Browse files
committed
Added an SVG path export function
1 parent a9edfe2 commit f74ed50

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

classes/font_glyph_outline.cls.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ function __construct(Font_Table_glyf $table, $offset = null, $size = null) {
7373
function parse() {
7474
$font = $this->getFont();
7575
$font->seek($this->offset);
76+
77+
if (!$this->size) {
78+
return;
79+
}
7680

7781
$data = $font->unpack(array(
7882
"numberOfContours" => self::int16,

classes/font_glyph_outline_simple.cls.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class Font_Glyph_Outline_Simple extends Font_Glyph_Outline {
2222

2323
function parse(){
2424
$data = parent::parse();
25+
26+
if (!$this->size) {
27+
return;
28+
}
29+
2530
$font = $this->getFont();
2631

2732
$noc = $data["numberOfContours"];
@@ -108,4 +113,73 @@ function parse(){
108113
$this->table = null;
109114
return $this->data = $data;
110115
}
116+
117+
public function getSVGPath(){
118+
$path = "";
119+
120+
$points = $this->data["points"];
121+
$length = count($points);
122+
$firstIndex = 0;
123+
$count = 0;
124+
125+
for($i = 0; $i < $length; $i++) {
126+
$count++;
127+
128+
if ($points[$i]["endOfContour"]) {
129+
$path .= $this->addContourToPath($points, $firstIndex, $count);
130+
$firstIndex = $i + 1;
131+
$count = 0;
132+
}
133+
}
134+
135+
return $path;
136+
}
137+
138+
protected function addContourToPath($points, $startIndex, $count) {
139+
$offset = 0;
140+
$path = "";
141+
142+
while($offset < $count) {
143+
$point_m1 = $points[ ($offset == 0) ? ($startIndex+$count-1) : $startIndex+($offset-1)%$count ];
144+
$point = $points[ $startIndex + $offset%$count ];
145+
$point_p1 = $points[ $startIndex + ($offset+1)%$count ];
146+
$point_p2 = $points[ $startIndex + ($offset+2)%$count ];
147+
148+
if($offset == 0) {
149+
$path .= "M{$point['x']},{$point['y']} ";
150+
}
151+
152+
if ($point["onCurve"] && $point_p1["onCurve"]) {
153+
$path .= "Q{$point_p1['x']},{$point_p1['y']} ";
154+
$offset++;
155+
}
156+
else if ($point["onCurve"] && !$point_p1["onCurve"] && $point_p2["onCurve"]){
157+
$path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
158+
$offset += 2;
159+
}
160+
else if ($point["onCurve"] && !$point_p1["onCurve"] && !$point_p2["onCurve"]){
161+
$path .= "Q{$point_p1['x']},{$point_p1['y']},".$this->midValue($point_p1['x'], $point_p2['x']).",".$this->midValue($point_p1['y'], $point_p2['y'])." ";
162+
$offset += 2;
163+
}
164+
else if (!$point["onCurve"] && !$point_p1["onCurve"]) {
165+
$path .= "Q{$point['x']},{$point['y']},".$this->midValue($point['x'], $point_p1['x']).",".$this->midValue($point['y'], $point_p1['y'])." ";
166+
$offset++;
167+
}
168+
else if (!$point["onCurve"] && $point_p1["onCurve"]) {
169+
$path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
170+
$offset++;
171+
}
172+
else {
173+
break;
174+
}
175+
}
176+
177+
$path .= "z ";
178+
179+
return $path;
180+
}
181+
182+
function midValue($a, $b){
183+
return $a + ($b - $a)/2;
184+
}
111185
}

www/js/glyph.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var Glyph = {
88
return a + (b - a)/2;
99
},
1010

11+
splitPath: function(path) {
12+
return path.match(/([a-z])|(-?\d+(?:\.\d+)?)/ig);
13+
},
14+
1115
addContourToPath: function(ctx, points, startIndex, count) {
1216
ctx.beginPath();
1317

@@ -120,6 +124,10 @@ var Glyph = {
120124
ctx.stroke();
121125
ctx.restore();
122126

127+
if (!shape) {
128+
return;
129+
}
130+
123131
// glyph bounding box
124132
ctx.beginPath();
125133
ctx.strokeStyle = "rgba(0,0,0,0.3)";

0 commit comments

Comments
 (0)