The Birdfont Source Code


All Repositories / birdfont.git / commitdiff – RSS feed

Parse SVG circles

These changes was commited to the Birdfont repository Tue, 13 Oct 2015 14:35:35 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
[Tue, 13 Oct 2015 14:35:35 +0000]

Updated Files

birdfont-import-debug.sh
libbirdfont/CircleTool.vala
libbirdfont/Path.vala
libbirdfont/SvgParser.vala
libbirdfont/SvgStyle.vala
diff --git birdfont-import-debug.sh(new)
--- /dev/null +++ b/birdfont-import-debug.sh @@ -1,1 +1,8 @@ + #!/bin/sh + export LD_LIBRARY_PATH="./build/bin:$LD_LIBRARY_PATH" + PKG_PATH=$(dirname "$(readlink -f "$0")") + cd "${PKG_PATH}" + + gdb ./build/bin/birdfont-import -ex "run "$@"" +
--- a/libbirdfont/CircleTool.vala +++ b/libbirdfont/CircleTool.vala @@ -112,12 +112,37 @@ ymin = p.y; } } + } + + public static Path create_circle (double x, double y, + double r, 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 = 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; } void press (int button, double x, double y) { Glyph glyph = MainWindow.get_current_glyph (); - double radius = 2; - double px, py, steps; Path path = new Path (); press_x = x; @@ -132,30 +157,16 @@ glyph.store_undo_state (); - steps = (DrawingTools.point_type == PointType.QUADRATIC) ? PI / 8 : PI / 4; + double px = Glyph.path_coordinate_x (x); + double py = Glyph.path_coordinate_y (y); - for (double angle = 0; angle < 2 * PI; angle += steps) { - px = radius * cos (angle) + Glyph.path_coordinate_x (x); - py = radius * sin (angle) + Glyph.path_coordinate_y (y); - path.add (px, py); - } - - path.init_point_type (); - path.close (); - path.recalculate_linear_handles (); + path = create_circle (px, py, 2, DrawingTools.point_type); if (StrokeTool.add_stroke) { path.stroke = StrokeTool.stroke_width; path.line_cap = StrokeTool.line_cap; } - - for (int i = 0; i < 3; i++) { - foreach (EditPoint ep in path.points) { - ep.set_tie_handle (true); - ep.process_tied_handle (); - } - } - + glyph.add_path (path); if (!PenTool.is_counter_path (circle)) {
--- a/libbirdfont/Path.vala +++ b/libbirdfont/Path.vala @@ -2101,10 +2101,14 @@ update_region_boundaries (); } - public void init_point_type () { + public void init_point_type (PointType pt = PointType.NONE) { PointType type; - switch (DrawingTools.point_type) { + if (pt == PointType.NONE) { + pt = DrawingTools.point_type; + } + + switch (pt) { case PointType.QUADRATIC: type = PointType.LINE_QUADRATIC; break;
--- a/libbirdfont/SvgParser.vala +++ b/libbirdfont/SvgParser.vala @@ -189,6 +189,10 @@ if (t.get_name () == "polygon") { parse_polygon (t, pl); + } + + if (t.get_name () == "circle") { + parse_circle (t, pl); } } @@ -232,6 +236,10 @@ if (t.get_name () == "rect") { parse_rect (t, pl); + } + + if (t.get_name () == "circle") { + parse_circle (t, pl); } } @@ -417,6 +425,71 @@ } return param.strip(); + } + + private void parse_circle (Tag tag, Layer pl) { + Path p; + double x, y, r; + Glyph g; + PathList npl; + BezierPoints[] bezier_points; + SvgStyle style = new SvgStyle (); + bool hidden = false; + + npl = new PathList (); + + x = 0; + y = 0; + r = 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 () == "r") { + r = 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_circle (bezier_points[0].x0, + bezier_points[0].y0, r, 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) { @@ -541,18 +614,7 @@ } pl.paths.append (path_list); - - foreach (Path p in path_list.paths) { - if (style.has_stroke ()) { - p.stroke = style.get_stroke_width (); - } else { - p.stroke = 0; - } - - p.line_cap = style.get_line_cap (); - p.reset_stroke (); - p.update_region_boundaries (); - } + style.apply (path_list); // assume the even odd rule is applied and convert the path // to a path using the non-zero rule
--- a/libbirdfont/SvgStyle.vala +++ b/libbirdfont/SvgStyle.vala @@ -82,8 +82,22 @@ } return s; + } + + public void apply (PathList path_list) { + foreach (Path p in path_list.paths) { + if (has_stroke ()) { + p.stroke = get_stroke_width (); + } else { + p.stroke = 0; + } + + p.line_cap = get_line_cap (); + p.reset_stroke (); + p.update_region_boundaries (); + } } } }