The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Use libsvgbird for parsing path transformations

These changes was commited to the Birdfont repository Fri, 07 Oct 2016 09:44:48 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 07 Oct 2016 09:44:48 +0000 (11:44 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Fri, 07 Oct 2016 09:50:38 +0000 (11:50 +0200)
commit 24be6eb5628ad0f5cf439068c9a8f1d13d2941d5
tree f9b3828535b1934035b3c5bd22fee559d813d458
parent 25e177c76c41b34730539202917992569600dd3a
Use libsvgbird for parsing path transformations

libbirdfont/SvgParser.vala
libsvgbird/Doubles.vala
libsvgbird/Object.vala
libsvgbird/Points.vala
libsvgbird/SvgFile.vala
--- a/libbirdfont/SvgParser.vala +++ b/libbirdfont/SvgParser.vala @@ -15,6 +15,7 @@ using B; using Math; using SvgBird; + using Cairo; namespace BirdFont { @@ -276,7 +277,9 @@ Cairo.Matrix view_box_matrix = view_box.get_matrix (width, height); view_box_matrix.multiply (view_box_matrix, matrix); SvgTransform t = new SvgTransform.for_matrix (view_box_matrix); - transform (t.get_xml (), pl); + SvgTransforms transforms = new SvgTransforms (); + transforms.add (t); + transform_paths (transforms, paths); } return paths; @@ -343,64 +346,70 @@ parse_line (t, pl); } } - - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform (attr.get_content (), pl); - } - } - } - - private void transform (string transform_functions, Layer layer) { - PathList path_list = new PathList (); - foreach (SvgBird.Object o in layer.objects) { - if (o is PathObject) { - path_list.add (((PathObject) o).get_path ()); + PathList paths = new PathList (); + + foreach (SvgBird.Object object in pl.objects) { + if (object is Path) { + paths.add ((Path) object); } } - transform_paths (transform_functions, path_list); - transform_subgroups (transform_functions, layer); + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, paths); } - private void transform_subgroups (string transform_functions, Layer layer) { - foreach (Layer subgroup in layer.get_sublayers ()) { - transform (transform_functions, subgroup); + private void transform_paths (SvgTransforms transforms, PathList pl) { + foreach (Path p in pl.paths) { + transform_path (transforms, p); } } - - private void transform_paths (string transform_functions, PathList pl) { - string data = transform_functions.dup (); - string[] functions; - - // use only a single space as separator - while (data.index_of (" ") > -1) { - data = data.replace (" ", " "); + + private void transform_path (SvgTransforms transforms, Path path) { + Font font = BirdFont.get_current_font (); + Glyph glyph = MainWindow.get_current_glyph (); + + foreach (EditPoint ep in path.points) { + ep.tie_handles = false; + ep.reflective_point = false; } - return_if_fail (data.index_of (")") > -1); - - // add separator - data = data.replace (") ", "|"); - data = data.replace (")", "|"); - functions = data.split ("|"); - - for (int i = functions.length - 1; i >= 0; i--) { - if (functions[i].has_prefix ("translate")) { - translate (functions[i], pl); - } - - if (functions[i].has_prefix ("scale")) { - scale (functions[i], pl); - } + Matrix matrix = transforms.get_matrix (); - if (functions[i].has_prefix ("matrix")) { - matrix (functions[i], pl); - } + foreach (EditPoint ep in path.points) { + double x, y; - // TODO: rotate etc. + x = ep.independent_x - glyph.left_limit; + y = font.top_limit - ep.independent_y; + + matrix.transform_point (ref x, ref y); + + ep.independent_x = glyph.left_limit + x; + ep.independent_y = font.top_limit - y; + + x = ep.get_right_handle ().x - glyph.left_limit; + y = font.top_limit - ep.get_right_handle ().y; + + matrix.transform_point (ref x, ref y); + + ep.get_right_handle ().x = glyph.left_limit + x; + ep.get_right_handle ().y = font.top_limit - y; + + x = ep.get_left_handle ().x - glyph.left_limit; + y = font.top_limit - ep.get_left_handle ().y; + + matrix.transform_point (ref x, ref y); + + ep.get_left_handle ().x = glyph.left_limit + x; + ep.get_left_handle ().y = font.top_limit - y; } + + double stroke_x = path.stroke; + double stroke_y = path.stroke; + + matrix.transform_distance (ref stroke_x, ref stroke_y); + + path.stroke = stroke_x; } /** @param path a path in the cartesian coordinate system @@ -595,11 +604,8 @@ npl.add (p); - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), npl); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, npl); npl.apply_style (style); append_paths (pl, npl); @@ -663,11 +669,8 @@ npl.add (p); - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), npl); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, npl); npl.apply_style (style); append_paths (pl, npl); @@ -740,11 +743,8 @@ npl.add (p); - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), npl); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, npl); npl.apply_style (style); append_paths (pl, npl); @@ -841,11 +841,8 @@ npl.add (p); // FIXME: right layer for other transforms - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), npl); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, npl); npl.apply_style (style); append_paths (layer, npl); @@ -894,11 +891,8 @@ path_list.add (p); path_list.apply_style (style); - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), path_list); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, path_list); return path_list; } @@ -974,11 +968,8 @@ } } - foreach (Attribute attr in tag.get_attributes ()) { - if (attr.get_name () == "transform") { - transform_paths (attr.get_content (), path_list); - } - } + SvgTransforms transforms = SvgFile.get_transform (tag.get_attributes ()); + transform_paths (transforms, path_list); } public static void create_lines_for_segment (Path path, EditPoint start, EditPoint end, double tolerance) {
--- a/libsvgbird/Doubles.vala +++ b/libsvgbird/Doubles.vala @@ -117,7 +117,7 @@ public void remove (int offset, int length) { if (unlikely (offset < 0 || offset + length > size)) { - warning ("Invalid offset."); + warning (@"Invalid offset: $offset, length: $length, size: $size"); return; }
--- a/libsvgbird/Object.vala +++ b/libsvgbird/Object.vala @@ -145,8 +145,6 @@ public abstract bool is_empty (); public virtual void move (double dx, double dy) { - Gradient g; - left += dx; right += dx; top += dy;
--- a/libsvgbird/Points.vala +++ b/libsvgbird/Points.vala @@ -58,24 +58,20 @@ return insert_cubic (size, handle_x, handle_y, next_handle_x, next_handle_y, x, y); + } + + public int add_line (double x, double y) { + return insert_line (size, x, y); } public int insert_cubic (int position, double handle_x, double handle_y, double next_handle_x, double next_handle_y, double x, double y) { - int index = position; + int index; if (size == 0) { - index = 0; - insert_type (index, POINT_LINE); - insert (index + 1, x); - insert (index + 2, y); - insert (index + 3, 0); - insert (index + 4, 0); - insert (index + 5, 0); - insert (index + 6, 0); - insert (index + 7, 0); + insert_line (0, x, y); } index = position; @@ -86,6 +82,21 @@ insert (index + 4, next_handle_y); insert (index + 5, x); insert (index + 6, y); + insert (index + 7, 0); + + return index; + } + + public int insert_line (int position, double x, double y) { + int index = position; + + insert_type (index, POINT_LINE); + insert (index + 1, x); + insert (index + 2, y); + insert (index + 3, 0); + insert (index + 4, 0); + insert (index + 5, 0); + insert (index + 6, 0); insert (index + 7, 0); return index;
--- a/libsvgbird/SvgFile.vala +++ b/libsvgbird/SvgFile.vala @@ -828,7 +828,7 @@ return !hidden; } - private SvgTransforms get_transform (Attributes attributes) { + public static SvgTransforms get_transform (Attributes attributes) { foreach (Attribute attr in attributes) { if (attr.get_name () == "transform") { return parse_transform (attr.get_content ());