The Birdfont Source Code


All Repositories / birdfont.git / blobdiff – RSS feed

Fix memory leak

Changes made to libbirdfont/GlyphCollection.vala

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
--- libbirdfont/GlyphCollection.vala +++ libbirdfont/GlyphCollection.vala @@ -1,5 +1,5 @@ /* - Copyright (C) 2012, 2014 Johan Mattsson + Copyright (C) 2012, 2014 2015 Johan Mattsson This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as @@ -18,15 +18,15 @@ namespace BirdFont { public class GlyphCollection : GLib.Object { - public VersionList versions; unichar unicode_character; string name; bool unassigned = false; + public Gee.ArrayList<Glyph> glyphs = new Gee.ArrayList<Glyph> (); + public int selected = 0; public GlyphCollection (unichar unicode_character, string name) { this.unicode_character = unicode_character; this.name = name; - versions = new VersionList (null, this); } public GlyphCollection.with_glyph (unichar unicode_character, string name) { @@ -36,11 +36,32 @@ this.name = name; g = new Glyph (name, unicode_character); - versions = new VersionList (g, this); + glyphs.add (g); } - - ~GlyphCollection () { - versions.glyphs.clear (); + + public void remove (int index) { + return_if_fail (0 <= index < glyphs.size); + + if (selected >= index) { + selected--; + } + + glyphs.remove_at (index); + } + + public void set_selected (Glyph g) { + int i = 0; + + foreach (Glyph gl in glyphs) { + if (gl == g) { + selected = i; + return; + } + i++; + } + + selected = 0; + warning ("Glyph is not a part of the collection."); } public void set_unassigned (bool a) { @@ -52,24 +73,27 @@ } public void add_glyph (Glyph g) { - get_version_list ().add_glyph (g); - } - - public VersionList get_version_list () { - return versions; + glyphs.add (g); } public Glyph get_current () { - return versions.get_current (); + if (0 <= selected < glyphs.size) { + return glyphs.get (selected); + } + + return new Glyph ("", '\0'); } - public void insert_glyph (Glyph g, bool selected) { - versions.add_glyph (g, selected); - assert (versions.glyphs.size > 0); + public void insert_glyph (Glyph g, bool selected_glyph) { + glyphs.add (g); + + if (selected_glyph) { + selected = glyphs.size - 1; + } } public uint length () { - return versions.glyphs.size; + return glyphs.size; } public string get_unicode () { @@ -92,18 +116,17 @@ public void set_name (string n) { name = n; - } - - public int get_selected_id () { - return versions.get_current ().version_id; - } - - public int get_last_id () { - return versions.get_last_id (); } public void set_selected_version (int version_id) { - versions.set_selected_version (version_id); + int i = 0; + foreach (Glyph g in glyphs) { + if (g.version_id == version_id) { + selected = i; + break; + } + i++; + } } /** Create a copy of this list. This method will copy the list data but @@ -113,16 +136,21 @@ public GlyphCollection copy () { GlyphCollection n = new GlyphCollection (unicode_character, name); - foreach (Glyph g in versions.glyphs) { + foreach (Glyph g in glyphs) { n.insert_glyph (g, false); } - n.versions.set_selected_version (versions.current_version_id); + n.selected = selected; n.unassigned = unassigned; return n; + } + + public int get_last_id () { + return_val_if_fail (glyphs.size > 0, 0); + return glyphs.get (glyphs.size - 1).version_id; } } }