.
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 set_selected (g);
41 }
42
43 public void remove (int index) {
44 return_if_fail (0 <= index < glyphs.size);
45
46 if (selected >= index) {
47 selected--;
48 }
49
50 glyphs.remove_at (index);
51 }
52
53 public void set_selected (Glyph g) {
54 int i = 0;
55
56 foreach (Glyph gl in glyphs) {
57 if (gl == g) {
58 selected = i;
59 return;
60 }
61 i++;
62 }
63
64 selected = 0;
65 warning ("Glyph is not a part of the collection.");
66 }
67
68 public void set_unassigned (bool a) {
69 unassigned = a;
70 }
71
72 public bool is_unassigned () {
73 return unassigned;
74 }
75
76 public void add_glyph (Glyph g) {
77 glyphs.add (g);
78 }
79
80 public Glyph get_current () {
81 if (likely (0 <= selected < glyphs.size)) {
82 return glyphs.get (selected);
83 }
84
85 warning (@"No glyph selected for $(name): $selected glyphs.size: $(glyphs.size)");
86
87 return new Glyph ("", '\0');
88 }
89
90 public void insert_glyph (Glyph g, bool selected_glyph) {
91 glyphs.add (g);
92
93 if (selected_glyph) {
94 selected = glyphs.size - 1;
95 }
96 }
97
98 public uint length () {
99 return glyphs.size;
100 }
101
102 public string get_unicode () {
103 StringBuilder unicode = new StringBuilder ();
104 unicode.append_unichar (unicode_character);
105 return unicode.str;
106 }
107
108 public void set_unicode_character (unichar c) {
109 unicode_character = c;
110 }
111
112 public unichar get_unicode_character () {
113 return unicode_character;
114 }
115
116 public string get_name () {
117 return name;
118 }
119
120 public void set_name (string n) {
121 name = n;
122 }
123
124 public void set_selected_version (int version_id) {
125 int i = 0;
126 foreach (Glyph g in glyphs) {
127 if (g.version_id == version_id) {
128 selected = i;
129 break;
130 }
131 i++;
132 }
133 }
134
135 /** Create a copy of this list. This method will copy the list data but
136 * keep pointers to the original glyphs.
137 * @return a new list with copies of pointers to the glyphs
138 */
139 public GlyphCollection copy () {
140 GlyphCollection n = new GlyphCollection (unicode_character, name);
141
142 foreach (Glyph g in glyphs) {
143 n.insert_glyph (g, false);
144 }
145
146 n.selected = selected;
147 n.unassigned = unassigned;
148
149 return n;
150 }
151
152 public GlyphCollection copy_deep () {
153 GlyphCollection n = new GlyphCollection (unicode_character, name);
154
155 foreach (Glyph g in glyphs) {
156 n.insert_glyph (g.copy (), false);
157 }
158
159 n.selected = selected;
160 n.unassigned = unassigned;
161
162 return n;
163 }
164
165 public int get_last_id () {
166 return_val_if_fail (glyphs.size > 0, 0);
167 return glyphs.get (glyphs.size - 1).version_id;
168 }
169 }
170
171 }
172