Skip to content

Commit 2981cb8

Browse files
committed
Simplified SVG path creation
1 parent 3e8407a commit 2981cb8

File tree

3 files changed

+36
-33
lines changed

3 files changed

+36
-33
lines changed

classes/font_glyph_outline_simple.cls.php

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function parse(){
114114
return $this->data = $data;
115115
}
116116

117-
public function getSVGPath(){
117+
public function getSVGContours(){
118118
$path = "";
119119

120120
$points = $this->data["points"];
@@ -126,7 +126,7 @@ public function getSVGPath(){
126126
$count++;
127127

128128
if ($points[$i]["endOfContour"]) {
129-
$path .= $this->addContourToPath($points, $firstIndex, $count);
129+
$path .= $this->getSVGPath($points, $firstIndex, $count);
130130
$firstIndex = $i + 1;
131131
$count = 0;
132132
}
@@ -140,42 +140,45 @@ public function parseSVGPath($path) {
140140
return $matches[0];
141141
}
142142

143-
protected function addContourToPath($points, $startIndex, $count) {
143+
protected function getSVGPath($points, $startIndex, $count) {
144144
$offset = 0;
145145
$path = "";
146146

147147
while($offset < $count) {
148-
$point_m1 = $points[ ($offset == 0) ? ($startIndex+$count-1) : $startIndex+($offset-1)%$count ];
149-
$point = $points[ $startIndex + $offset%$count ];
148+
$point = $points[ $startIndex + $offset %$count ];
150149
$point_p1 = $points[ $startIndex + ($offset+1)%$count ];
151-
$point_p2 = $points[ $startIndex + ($offset+2)%$count ];
152150

153151
if($offset == 0) {
154152
$path .= "M{$point['x']},{$point['y']} ";
155153
}
156154

157-
if ($point["onCurve"] && $point_p1["onCurve"]) {
158-
$path .= "L{$point_p1['x']},{$point_p1['y']} ";
159-
$offset++;
160-
}
161-
else if ($point["onCurve"] && !$point_p1["onCurve"] && $point_p2["onCurve"]){
162-
$path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
163-
$offset += 2;
164-
}
165-
else if ($point["onCurve"] && !$point_p1["onCurve"] && !$point_p2["onCurve"]){
166-
$path .= "Q{$point_p1['x']},{$point_p1['y']},".$this->midValue($point_p1['x'], $point_p2['x']).",".$this->midValue($point_p1['y'], $point_p2['y'])." ";
167-
$offset += 2;
168-
}
169-
else if (!$point["onCurve"] && !$point_p1["onCurve"]) {
170-
$path .= "Q{$point['x']},{$point['y']},".$this->midValue($point['x'], $point_p1['x']).",".$this->midValue($point['y'], $point_p1['y'])." ";
171-
$offset++;
172-
}
173-
else if (!$point["onCurve"] && $point_p1["onCurve"]) {
174-
$path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
175-
$offset++;
176-
}
155+
if ($point["onCurve"]) {
156+
if ($point_p1["onCurve"]) {
157+
$path .= "L{$point_p1['x']},{$point_p1['y']} ";
158+
$offset++;
159+
}
160+
else {
161+
$point_p2 = $points[ $startIndex + ($offset+2)%$count ];
162+
163+
if ($point_p2["onCurve"]){
164+
$path .= "Q{$point_p1['x']},{$point_p1['y']},{$point_p2['x']},{$point_p2['y']} ";
165+
}
166+
else {
167+
$path .= "Q{$point_p1['x']},{$point_p1['y']},".$this->midValue($point_p1['x'], $point_p2['x']).",".$this->midValue($point_p1['y'], $point_p2['y'])." ";
168+
}
169+
170+
$offset += 2;
171+
}
172+
}
177173
else {
178-
break;
174+
if ($point_p1["onCurve"]) {
175+
$path .= "Q{$point['x']},{$point['y']},{$point_p1['x']},{$point_p1['y']} ";
176+
}
177+
else {
178+
$path .= "Q{$point['x']},{$point['y']},".$this->midValue($point['x'], $point_p1['x']).",".$this->midValue($point['y'], $point_p1['y'])." ";
179+
}
180+
181+
$offset++;
179182
}
180183
}
181184

classes/font_table_glyf.cls.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function toHTML(){
7777
}
7878

7979
$shape = $glyph->getGlyphData();
80-
$shape["SVGPath"] = $glyph->getSVGPath();
80+
$shape["SVGContours"] = $glyph->getSVGContours();
8181
unset($shape["points"]);
8282
unset($shape["instructions"]);
8383
$shape_json = json_encode($shape);

www/js/glyph.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var Glyph = {
1212
return path.match(/([a-z])|(-?\d+(?:\.\d+)?)/ig);
1313
},
1414

15-
drawSVGPath: function(ctx, path) {
15+
drawSVGContours: function(ctx, path) {
1616
var p = Glyph.splitPath(path);
1717

1818
if (!p) {
@@ -28,15 +28,15 @@ var Glyph = {
2828
switch(v) {
2929
case "M":
3030
ctx.beginPath();
31-
ctx.moveTo(parseFloat(p[++i]), parseFloat(p[++i]));
31+
ctx.moveTo(p[++i], p[++i]);
3232
break;
3333

3434
case "L":
35-
ctx.lineTo(parseFloat(p[++i]), parseFloat(p[++i]));
35+
ctx.lineTo(p[++i], p[++i]);
3636
break;
3737

3838
case "Q":
39-
ctx.quadraticCurveTo(parseFloat(p[++i]), parseFloat(p[++i]), parseFloat(p[++i]), parseFloat(p[++i]));
39+
ctx.quadraticCurveTo(p[++i], p[++i], p[++i], p[++i]);
4040
break;
4141

4242
case "z":
@@ -126,6 +126,6 @@ var Glyph = {
126126
ctx.strokeStyle = "black";
127127
ctx.globalCompositeOperation = "xor";
128128

129-
Glyph.drawSVGPath(ctx, shape.SVGPath);
129+
Glyph.drawSVGContours(ctx, shape.SVGContours);
130130
}
131131
};

0 commit comments

Comments
 (0)