The Birdfont Source Code
Parse ellipse instruction in SVG files
These changes was commited to the Birdfont repository Wed, 14 Oct 2015 08:15:51 +0000.
Contributing
Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Parse ellipse instruction in SVG files
--- a/birdfont/Main.vala
+++ b/birdfont/Main.vala
@@ -20,7 +20,6 @@
public static int main (string[] arg) {
GtkWindow native_window;
MainWindow window;
- string file;
BirdFont.BirdFont birdfont;
birdfont = new BirdFont.BirdFont ();
--- a/libbirdfont/CircleTool.vala
+++ b/libbirdfont/CircleTool.vala
@@ -124,6 +124,33 @@
for (double angle = 0; angle < 2 * PI; angle += steps) {
px = r * cos (angle) + x;
py = r * sin (angle) + y;
+ path.add (px, py);
+ }
+
+ path.init_point_type (pt);
+ path.close ();
+ path.recalculate_linear_handles ();
+
+ for (int i = 0; i < 3; i++) {
+ foreach (EditPoint ep in path.points) {
+ ep.set_tie_handle (true);
+ ep.process_tied_handle ();
+ }
+ }
+
+ return path;
+ }
+
+ public static Path create_ellipse (double x, double y,
+ double rx, double ry, PointType pt) {
+
+ double px, py;
+ Path path = new Path ();
+ double steps = (pt == PointType.QUADRATIC) ? PI / 8 : PI / 4;
+
+ for (double angle = 0; angle < 2 * PI; angle += steps) {
+ px = rx * cos (angle) + x;
+ py = ry * sin (angle) + y;
path.add (px, py);
}
--- a/libbirdfont/SvgParser.vala
+++ b/libbirdfont/SvgParser.vala
@@ -193,6 +193,10 @@
if (t.get_name () == "circle") {
parse_circle (t, pl);
+ }
+
+ if (t.get_name () == "ellipse") {
+ parse_ellipse (t, pl);
}
}
@@ -240,6 +244,10 @@
if (t.get_name () == "circle") {
parse_circle (t, pl);
+ }
+
+ if (t.get_name () == "ellipse") {
+ parse_ellipse (t, pl);
}
}
@@ -491,7 +499,77 @@
style.apply (npl);
pl.paths.append (npl);
}
+
+ private void parse_ellipse (Tag tag, Layer pl) {
+ Path p;
+ double x, y, rx, ry;
+ Glyph g;
+ PathList npl;
+ BezierPoints[] bezier_points;
+ SvgStyle style = new SvgStyle ();
+ bool hidden = false;
+
+ npl = new PathList ();
+
+ x = 0;
+ y = 0;
+ rx = 0;
+ ry = 0;
+
+ foreach (Attribute attr in tag.get_attributes ()) {
+ if (attr.get_name () == "cx") {
+ x = parse_double (attr.get_content ());
+ }
+
+ if (attr.get_name () == "cy") {
+ y = -parse_double (attr.get_content ());
+ }
+
+ if (attr.get_name () == "rx") {
+ rx = parse_double (attr.get_content ());
+ }
+
+ if (attr.get_name () == "ry") {
+ ry = parse_double (attr.get_content ());
+ }
+ if (attr.get_name () == "style") {
+ style = SvgStyle.parse (attr.get_content ());
+ }
+
+ if (attr.get_name () == "display" && attr.get_content () == "none") {
+ hidden = true;
+ }
+ }
+
+ if (hidden) {
+ return;
+ }
+
+ bezier_points = new BezierPoints[1];
+ bezier_points[0] = new BezierPoints ();
+ bezier_points[0].type == 'L';
+ bezier_points[0].x0 = x;
+ bezier_points[0].y0 = y;
+
+ g = MainWindow.get_current_glyph ();
+ move_and_resize (bezier_points, 1, false, 1, g);
+
+ p = CircleTool.create_ellipse (bezier_points[0].x0,
+ bezier_points[0].y0, rx, ry, PointType.CUBIC);
+
+ npl.add (p);
+
+ foreach (Attribute attr in tag.get_attributes ()) {
+ if (attr.get_name () == "transform") {
+ transform_paths (attr.get_content (), npl);
+ }
+ }
+
+ style.apply (npl);
+ pl.paths.append (npl);
+ }
+
private void parse_rect (Tag tag, Layer pl) {
Path p;
double x, y, x2, y2;