Commit 5bc1ea7
Fix memory leaks in GlyphRange
Changed files (7)
modifiedlibbirdfont/BirdFontFile.valamodifiedlibbirdfont/DrawingTools.valamodifiedlibbirdfont/GlyphRange.valamodifiedlibbirdfont/GridTool.valamodifiedlibbirdfont/OpenFontFormat/Cmap.valamodifiedlibbirdfont/StrokeTool.valamodifiedlibbirdfont/TestCases.vala
Diff
@@ -273,7 +273,7 @@ class BirdFontFile : GLib.Object { os.put_string (@"<grid width=\"$(gv)\"/>\n"); } - if (GridTool.sizes.length () > 0) {+ if (GridTool.sizes.size > 0) { os.put_string ("\n"); } @@ -1020,9 +1020,7 @@ public class DrawingTools : ToolCollection { grid_expander.tool.remove_link (grid_expander.tool.last ()); } - while (GridTool.sizes.length () > 0) {- GridTool.sizes.remove_link (GridTool.sizes.first ());- }+ GridTool.sizes.clear (); grid_expander.set_open (true); MainWindow.get_toolbox ().update_expanders ();@@ -1051,7 +1049,7 @@ public class DrawingTools : ToolCollection { grid_expander.add_tool (grid_width); - GridTool.sizes.append (grid_width);+ GridTool.sizes.add (grid_width); grid_expander.set_open (true); tb.update_expanders ();@@ -16,17 +16,18 @@ namespace BirdFont { public class GlyphRange { - public List<UniRange> ranges;+ public Gee.ArrayList<UniRange> ranges; /** Glyphs without a corresponding unicode value (ligatures). */- public List<string> unassigned;+ public Gee.ArrayList<string> unassigned; uint32 len = 0; bool range_is_class = false; public GlyphRange () {- unassigned = new List<string> ();+ ranges = new Gee.ArrayList<UniRange> ();+ unassigned = new Gee.ArrayList<string> (); } public bool is_class () {@@ -42,18 +43,12 @@ public class GlyphRange { } public void empty () {- while (unassigned.length () > 0) {- unassigned.remove_link (unassigned.first ());- }-- while (ranges.length () > 0) {- ranges.remove_link (ranges.first ());- }- + unassigned.clear ();+ ranges.clear (); len = 0; } - public unowned List<UniRange> get_ranges () {+ public unowned Gee.ArrayList<UniRange> get_ranges () { return ranges; } @@ -72,7 +67,7 @@ public class GlyphRange { public uint32 get_length () { uint32 l = len;- l += unassigned.length ();+ l += unassigned.size; return l; } @@ -346,23 +341,43 @@ public class GlyphRange { } private void merge_range (UniRange r) {+ Gee.ArrayList<UniRange> deleted = new Gee.ArrayList<UniRange> ();+ Gee.ArrayList<UniRange> merged = new Gee.ArrayList<UniRange> ();+ bool updated = false;+ foreach (UniRange u in ranges) { if (u == r) { continue;- } + } if (u.start == r.stop + 1) { u.start = r.start;- ranges.remove_all (r);- merge_range (u);+ deleted.add (r);+ merged.add (u);+ break; } if (u.stop == r.start - 1) { u.stop = r.stop;- ranges.remove_all (r);- merge_range (u);+ deleted.add (r);+ merged.add (u);+ break; } }++ updated = merged.size > 0;++ foreach (UniRange m in deleted) {+ while (ranges.remove (m));+ }+ + foreach (UniRange m in merged) {+ merge_range (m);+ }+ + if (updated) {+ merge_range (r);+ } } public string get_char (uint32 index) {@@ -372,20 +387,20 @@ public class GlyphRange { StringBuilder sb; unichar c; - if (index > len + unassigned.length ()) {+ if (index > len + unassigned.size) { return "\0".dup(); } if (index >= len) {- if (index - len >= unassigned.length ()) {+ if (index - len >= unassigned.size) { return "\0".dup(); } - chr = ((!) unassigned.nth (index - len)).data;+ chr = unassigned.get ((int) (index - len)); return chr; } - r = ranges.first ().data;+ r = ranges.get (0); ti = index; foreach (UniRange u in ranges) {@@ -458,7 +473,7 @@ public class GlyphRange { UniRange ur = new UniRange (start, stop); len += ur.length ();- ranges.append (ur);+ ranges.add (ur); return ur; }@@ -19,8 +19,8 @@ namespace BirdFont { class GridTool : Tool { - static List<Line> horizontal;- static List<Line> vertical;+ static Gee.ArrayList<Line> horizontal;+ static Gee.ArrayList<Line> vertical; static bool visible = false; public static bool ttf_units = false;@@ -28,7 +28,7 @@ class GridTool : Tool { public static double size_x; public static double size_y; - public static List<SpinButton> sizes;+ public static Gee.ArrayList<SpinButton> sizes; public GridTool (string n) { string units;@@ -39,10 +39,10 @@ class GridTool : Tool { ttf_units = true; } - horizontal = new List <Line> ();- vertical = new List <Line> ();+ horizontal = new Gee.ArrayList <Line> ();+ vertical = new Gee.ArrayList <Line> (); - sizes = new List <SpinButton> ();+ sizes = new Gee.ArrayList <SpinButton> (); size_x = 2; size_y = 2;@@ -122,20 +122,15 @@ class GridTool : Tool { Line left = g.get_line ("left"); Line right = g.get_line ("right"); - while (vertical.length () > 0) {- vertical.delete_link (vertical.first ());- }- - while (horizontal.length () > 0) {- horizontal.delete_link (horizontal.first ());- }+ vertical.clear ();+ horizontal.clear (); n = 0; for (i = left.get_pos () - 7 * step; i <= right.get_pos () + 7 * step; i += step) { l = new Line ("grid", i, Line.VERTICAL); l.set_moveable (false); l.set_color (0.2, 0.6, 0.2, 0.2);- vertical.append (l);+ vertical.add (l); if (++n >= max_lines) { break;@@ -147,7 +142,7 @@ class GridTool : Tool { t = new Line ("grid", i, Line.HORIZONTAL); t.set_moveable (false); t.set_color (0.2, 0.6, 0.2, 0.2);- horizontal.prepend (t); + horizontal.insert (0, t); if (++n >= max_lines) { break;@@ -159,7 +154,7 @@ class GridTool : Tool { u = new Line ("grid", i, Line.HORIZONTAL); u.set_moveable (false); u.set_color (0.2, 0.6, 0.2, 0.2);- horizontal.append (u); + horizontal.add (u); if (++n >= max_lines) { break;@@ -178,13 +173,13 @@ class GridTool : Tool { } public static double tie_point_x (double x, bool coordinate)- requires (vertical.length () >= 2)+ requires (vertical.size >= 2) { double d, m;- Line xmin = vertical.first ().data;+ Line xmin = vertical.get (0); Line xpos;- Line startx = vertical.first ().data;- Line stopx = vertical.last ().data;+ Line startx = vertical.get (0);+ Line stopx = vertical.get (vertical.size - 1); // outside of the grid if (!coordinate) {@@ -225,13 +220,13 @@ class GridTool : Tool { } public static double tie_point_y (double y, bool coordinate)- requires (horizontal.length () >= 2)+ requires (horizontal.size >= 2) { double d, m;- Line ymin = horizontal.first ().data;+ Line ymin = horizontal.get (0); Line ypos;- Line starty = horizontal.first ().data;- Line stopy = horizontal.last ().data;+ Line starty = horizontal.get (0);+ Line stopy = horizontal.get (horizontal.size - 1); // outside of the grid if (!coordinate) {@@ -279,11 +274,11 @@ class GridTool : Tool { return visible; } - public static unowned List<Line> get_horizontal_lines () {+ public static Gee.ArrayList<Line> get_horizontal_lines () { return horizontal; } - public static unowned List<Line> get_vertical_lines () {+ public static Gee.ArrayList<Line> get_vertical_lines () { return vertical; } @@ -205,7 +205,7 @@ public class CmapSubtableWindowsUnicode : CmapSubtable { public void process (FontData fd, GlyfTable glyf_table) throws GLib.Error { GlyphRange glyph_range = new GlyphRange ();- unowned List<UniRange> ranges;+ Gee.ArrayList<UniRange> ranges; uint16 seg_count_2; uint16 seg_count;@@ -224,10 +224,8 @@ public class CmapSubtableWindowsUnicode : CmapSubtable { } } - // glyph_range.print_all ();- ranges = glyph_range.get_ranges ();- seg_count = (uint16) ranges.length () + 1;+ seg_count = (uint16) ranges.size + 1; seg_count_2 = seg_count * 2; search_range = 2 * largest_pow2 (seg_count); entry_selector = largest_pow2_exponent (seg_count);@@ -206,7 +206,7 @@ public class StrokeTool : Tool { static Path add_tangent_points (Path p) { Path path; int steps = 2;- List<EditPoint> new_points = new List<EditPoint> ();+ Gee.ArrayList<EditPoint> new_points = new Gee.ArrayList<EditPoint> (); EditPoint nep; EditPoint? previous = null;@@ -249,15 +249,15 @@ public class StrokeTool : Tool { return false; } - new_points.append (ep);+ new_points.add (ep); } if (step <= 0) { prev = start;- new_points.append (start);+ new_points.add (start); } else if (step >= 1) { prev = stop;- new_points.append (stop);+ new_points.add (stop); } else { prev = ep; }@@ -269,8 +269,8 @@ public class StrokeTool : Tool { return true; }); - while (new_points.length () > 0) {- nep = new_points.first ().data;+ while (new_points.size > 0) {+ nep = new_points.get (0); if (previous != null) { nep.prev = ((!) previous).get_link_item ();@@ -283,7 +283,7 @@ public class StrokeTool : Tool { previous = nep; - new_points.remove_link (new_points.first ());+ new_points.remove_at (0); path.create_list (); } @@ -836,10 +836,12 @@ class TestCases { gr.add_range ('a', 'd'); gr.add_range ('f', 'z'); gr.add_range ('b', 'd');- + + gr.print_all ();+ return_if_fail (gr.length () == 'z' - 'a' + 1);- return_if_fail (gr.get_ranges ().length () == 1);- return_if_fail (gr.get_ranges ().first ().data.length () == 'z' - 'a' + 1);+ return_if_fail (gr.get_ranges ().size == 1);+ return_if_fail (gr.get_ranges ().get (0).length () == 'z' - 'a' + 1); for (unichar i = 'a'; i <= 'z'; i++) { uint index = i - 'a';