The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

GlyphCollection.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/GlyphCollection.vala.
Ignore case in key bindings
1 /* 2 Copyright (C) 2012, 2014 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 namespace BirdFont { 19 20 public class GlyphCollection : GLib.Object { 21 unichar unicode_character; 22 string name; 23 bool unassigned = false; 24 public Gee.ArrayList<Glyph> glyphs = new Gee.ArrayList<Glyph> (); 25 public int selected; 26 27 public GlyphCollection (unichar unicode_character, string name) { 28 this.unicode_character = unicode_character; 29 this.name = name; 30 } 31 32 public GlyphCollection.with_glyph (unichar unicode_character, string name) { 33 Glyph g; 34 35 this.unicode_character = unicode_character; 36 this.name = name; 37 38 g = new Glyph (name, unicode_character); 39 glyphs.add (g); 40 } 41 42 public void remove (int index) { 43 return_if_fail (0 <= index < glyphs.size); 44 45 if (selected >= index) { 46 selected--; 47 } 48 49 glyphs.remove_at (index); 50 } 51 52 public void set_selected (Glyph g) { 53 int i = 0; 54 55 foreach (Glyph gl in glyphs) { 56 if (gl == g) { 57 selected = i; 58 return; 59 } 60 i++; 61 } 62 63 selected = 0; 64 warning ("Glyph is not a part of the collection."); 65 } 66 67 public void set_unassigned (bool a) { 68 unassigned = a; 69 } 70 71 public bool is_unassigned () { 72 return unassigned; 73 } 74 75 public void add_glyph (Glyph g) { 76 glyphs.add (g); 77 } 78 79 public Glyph get_current () { 80 if (likely (0 <= selected < glyphs.size)) { 81 return glyphs.get (selected); 82 } 83 84 warning (@"No glyph selected for $(name): $selected glyphs.size: $(glyphs.size)"); 85 86 return new Glyph ("", '\0'); 87 } 88 89 public void insert_glyph (Glyph g, bool selected_glyph) { 90 glyphs.add (g); 91 92 if (selected_glyph) { 93 selected = glyphs.size - 1; 94 } 95 } 96 97 public uint length () { 98 return glyphs.size; 99 } 100 101 public string get_unicode () { 102 StringBuilder unicode = new StringBuilder (); 103 unicode.append_unichar (unicode_character); 104 return unicode.str; 105 } 106 107 public void set_unicode_character (unichar c) { 108 unicode_character = c; 109 } 110 111 public unichar get_unicode_character () { 112 return unicode_character; 113 } 114 115 public string get_name () { 116 return name; 117 } 118 119 public void set_name (string n) { 120 name = n; 121 } 122 123 public void set_selected_version (int version_id) { 124 int i = 0; 125 foreach (Glyph g in glyphs) { 126 if (g.version_id == version_id) { 127 selected = i; 128 break; 129 } 130 i++; 131 } 132 } 133 134 /** Create a copy of this list. This method will copy the list data but 135 * keep pointers to the original glyphs. 136 * @return a new list with copies of pointers to the glyphs 137 */ 138 public GlyphCollection copy () { 139 GlyphCollection n = new GlyphCollection (unicode_character, name); 140 141 foreach (Glyph g in glyphs) { 142 n.insert_glyph (g, false); 143 } 144 145 n.selected = selected; 146 n.unassigned = unassigned; 147 148 return n; 149 } 150 151 public GlyphCollection copy_deep () { 152 GlyphCollection n = new GlyphCollection (unicode_character, name); 153 154 foreach (Glyph g in glyphs) { 155 n.insert_glyph (g.copy (), false); 156 } 157 158 n.selected = selected; 159 n.unassigned = unassigned; 160 161 return n; 162 } 163 164 public int get_last_id () { 165 return_val_if_fail (glyphs.size > 0, 0); 166 return glyphs.get (glyphs.size - 1).version_id; 167 } 168 } 169 170 } 171