Commit 40ef66e
remove empty paths from ttf files
Changed files (4)
modifiedlibbirdfont/Glyph.valamodifiedlibbirdfont/OpenFontFormatWriter.valamodifiedlibbirdfont/Path.valamodifiedlibbirdfont/Svg.vala
Diff
@@ -446,16 +446,32 @@ public class Glyph : FontDisplay { return margin_boundries_visible; } + /** @return true if a path was removed. */ public void remove_empty_paths () {- foreach (var p in path_list) {- if (p.points.length () < 2) {+ foreach (Path p in path_list) {+ if (p.points.length () <= 2) { delete_path (p);+ remove_empty_paths ();+ return; } }++ foreach (Path p in path_list) {+ assert (p.points.length () > 2);+ } } - public void delete_path (Path p) {- path_list.remove_all (p);+ public void delete_path (Path p) requires (path_list.length () > 0) {+ for (unowned List<Path> pl = path_list.first (); pl != path_list.last (); pl = pl.next) {+ if (pl.data == p) {+ path_list.remove_link (pl);+ return;+ }+ }+ + if (path_list.last ().data == p) {+ path_list.remove_link (path_list.last ());+ } } public string get_svg_data () {@@ -1381,6 +1381,8 @@ class GlyfTable : Table { uint len; uint coordinate_length; + Path quadratic; + PointType pt; fd.seek_end (); // append glyph @@ -1405,27 +1407,38 @@ class GlyfTable : Table { tymax = int16.MIN; // bounding box will be set again after coordinate arrays have been created - fd.add_16 (10); - fd.add_16 (20); - fd.add_16 (30); - fd.add_16 (40); + fd.add_16 (0); + fd.add_16 (0); + fd.add_16 (0); + fd.add_16 (0); // end points end_point = 0; last_end_point = 0; foreach (Path p in g.path_list) { - p = p.get_quadratic_points (); - foreach (EditPoint e in p.points) { + quadratic = p.get_quadratic_points (); + + if (quadratic.points.length () == 0) { + warning (@"No points in path (quadratic points $(quadratic.points.length ())) (before conversion $(p.points.length ()))"); + continue; + } + + foreach (EditPoint e in quadratic.points) { end_point++; + pt = e.get_right_handle ().type; - if (e.get_right_handle ().type == PointType.CUBIC) { + if (pt == PointType.CUBIC || pt == PointType.QUADRATIC) { end_point++; } + + if (end_point == 0xFFFF) { + warning ("Too many points"); + } } fd.add_u16 (end_point - 1); if (end_point - 1 < last_end_point) { - warning (@"Next endpoint has bad value. (end_point - 1 < last_end_point) ($(end_point - 1) < $last_end_point"); + warning (@"Next endpoint has bad value. (end_point - 1 < last_end_point) ($(end_point - 1) < $last_end_point)"); } last_end_point = end_point - 1; @@ -123,7 +123,7 @@ public class Path { foreach (EditPoint e in ep) { if (n != null) { en = (!) n;- draw_next (e, en, cr);+ draw_next (en, e, cr); } n = e;@@ -134,7 +134,7 @@ public class Path { en = (!) n; em = ep.first ().data; - draw_next (em, en, cr);+ draw_next (en, em, cr); } cr.stroke ();@@ -170,7 +170,7 @@ public class Path { foreach (EditPoint e in ep) { if (n != null) { en = (!) n;- draw_next (e, en, cr);+ draw_next (en, e, cr); } n = e;@@ -180,7 +180,7 @@ public class Path { if (!is_open () && ep.length () >= 2 && n != null) { en = (!) n; em = ep.first ().data;- draw_next (em, en, cr);+ draw_next (en, em, cr); } // fill path@@ -196,7 +196,13 @@ public class Path { } private void draw_next (EditPoint e, EditPoint en, Context cr) {- draw_curve (e, en, cr);+ PointType t = e.get_right_handle ().type;+ + if (t == PointType.DOUBLE_CURVE) {+ + } else {+ draw_curve (e, en, cr);+ } } private static void draw_curve (EditPoint e, EditPoint en, Context cr, double alpha = 1) {@@ -227,32 +233,32 @@ public class Path { center_x = g.allocation.width / 2.0; center_y = g.allocation.height / 2.0; - xa = center_x + en.x;- ya = center_y - en.y;+ xa = center_x + e.x;+ ya = center_y - e.y; - xb = center_x + en.get_right_handle ().x ();- yb = center_y - en.get_right_handle ().y ();+ xb = center_x + e.get_right_handle ().x ();+ yb = center_y - e.get_right_handle ().y (); - xc = center_x + e.get_left_handle ().x ();- yc = center_y - e.get_left_handle ().y ();+ xc = center_x + en.get_left_handle ().x ();+ yc = center_y - en.get_left_handle ().y (); - xd = center_x + e.x;- yd = center_y - e.y; + xd = center_x + en.x;+ yd = center_y - en.y; } /** Curve absolute glyph data. */ public static void get_abs_bezier_points (EditPoint e, EditPoint en, out double xa, out double ya, out double xb, out double yb, out double xc, out double yc, out double xd, out double yd) {- xa = + en.x;- ya = - en.y;+ xa = + e.x;+ ya = - e.y; - xb = + en.get_right_handle ().x ();- yb = - en.get_right_handle ().y ();+ xb = + e.get_right_handle ().x ();+ yb = - e.get_right_handle ().y (); - xc = + e.get_left_handle ().x ();- yc = - e.get_left_handle ().y ();+ xc = + en.get_left_handle ().x ();+ yc = - en.get_left_handle ().y (); - xd = + e.x;- yd = - e.y; + xd = + en.x;+ yd = - en.y; } /** Line points relative to center. */@@ -260,11 +266,11 @@ public class Path { double xc = Glyph.xc (); double yc = Glyph.yc (); - xa = xc + en.x;- ya = yc - en.y;+ xa = xc + e.x;+ ya = yc - e.y; - xb = xc + e.x;- yb = yc - e.y;+ xb = xc + en.x;+ yb = yc - en.y; } public void draw_line (EditPoint e, EditPoint en, Context cr, double alpha = 1) { @@ -79,13 +79,13 @@ class Svg { private static void add_abs_next (EditPoint start, EditPoint end, StringBuilder svg, Glyph g, bool do_glyph) { if (start.right_handle.type == PointType.LINE_QUADRATIC) {- add_abs_line_to (end, start, svg, g, do_glyph);+ add_abs_line_to (start, end, svg, g, do_glyph); } else if (start.right_handle.type == PointType.LINE_CUBIC && end.left_handle.type == PointType.LINE_CUBIC) {- add_abs_line_to (end, start, svg, g, do_glyph);+ add_abs_line_to (start, end, svg, g, do_glyph); } else if (end.left_handle.type == PointType.QUADRATIC || start.right_handle.type == PointType.QUADRATIC) {- add_quadratic_abs_path (end, start, svg, g, do_glyph);+ add_quadratic_abs_path (start, end, svg, g, do_glyph); } else {- add_cubic_abs_path (end, start, svg, g, do_glyph);+ add_cubic_abs_path (start, end, svg, g, do_glyph); } }