.
1 /*
2 Copyright (C) 2013 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 KerningList : Table {
21
22 Gee.ArrayList<UndoItem> undo_items;
23
24 public KerningList () {
25 undo_items = new Gee.ArrayList<UndoItem> ();
26 }
27
28 public override Gee.ArrayList<Row> get_rows () {
29 Gee.ArrayList<Row> rows = new Gee.ArrayList<Row> ();
30 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes ();
31 int i;
32
33 i = 0;
34
35 classes.get_classes ((left, right, kerning) => {
36 Row r = new Row.columns_3 (left, right, @"$kerning", i);
37 rows.add (r);
38 i++;
39 });
40
41 classes.get_single_position_pairs ((left, right, kerning) => {
42 Row r = new Row.columns_3 (left, right, @"$kerning", i);
43 rows.add (r);
44 i++;
45 });
46
47 rows.sort ((a, b) => {
48 Row sa, sb;
49 sa = (Row) a;
50 sb = (Row) b;
51 return strcmp (sa.column_text.get (0).text, sb.column_text.get (0).text);
52 });
53
54 rows.insert (0, new Row.headline (t_("Kerning Pairs")));
55
56 if (rows.size == 1) {
57 rows.insert (1, new Row.columns_1 (t_("No kerning pairs created."), 0, false));
58 }
59
60 return rows;
61 }
62
63 public override void selected_row (Row row, int column, bool delete_button) {
64 return_if_fail (row.column_text.size > 2);
65
66 if (delete_button) {
67 delete_kerning (row.column_text.get (0).text, row.column_text.get (1).text);
68 }
69 }
70
71 void delete_kerning (string left, string right) {
72 double kerning = 0;
73 GlyphRange glyph_range_first, glyph_range_next;
74 Font font = BirdFont.get_current_font ();
75 KerningClasses classes = font.get_kerning_classes ();
76 string l, r;
77 int class_index = -1;
78
79 l = GlyphRange.unserialize (left);
80 r = GlyphRange.unserialize (right);
81
82 glyph_range_first = new GlyphRange ();
83 glyph_range_next = new GlyphRange ();
84
85 try {
86 glyph_range_first.parse_ranges (left);
87 glyph_range_next.parse_ranges (right);
88 } catch (GLib.MarkupError e) {
89 warning (e.message);
90 return;
91 }
92
93 if (left != "" && right != "") {
94 if (glyph_range_first.is_class () || glyph_range_next.is_class ()) {
95
96 kerning = classes.get_kerning_for_range (glyph_range_first, glyph_range_next);
97 class_index = classes.get_kerning_item_index (glyph_range_first, glyph_range_next);
98
99 classes.delete_kerning_for_class (left, right);
100 } else {
101 kerning = classes.get_kerning (left, right);
102 classes.delete_kerning_for_pair (left, right);
103 }
104
105 undo_items.add (new UndoItem (left, right, kerning, class_index));
106 font.touch ();
107 }
108 }
109
110 public override string get_label () {
111 return t_("Kerning Pairs");
112 }
113
114 public override string get_name () {
115 return "Kerning Pairs";
116 }
117
118 public override void undo () {
119 UndoItem ui;
120 KerningClasses classes = BirdFont.get_current_font ().get_kerning_classes ();
121 GlyphRange glyph_range_first, glyph_range_next;
122
123 try {
124 if (undo_items.size == 0) {
125 return;
126 }
127
128 ui = undo_items.get (undo_items.size - 1);
129
130 glyph_range_first = new GlyphRange ();
131 glyph_range_next = new GlyphRange ();
132
133 glyph_range_first.parse_ranges (ui.first);
134 glyph_range_next.parse_ranges (ui.next);
135
136 if (glyph_range_first.is_class () || glyph_range_next.is_class ()) {
137 classes.set_kerning (glyph_range_first, glyph_range_next, ui.kerning, ui.class_priority);
138 } else {
139 classes.set_kerning_for_single_glyphs (ui.first, ui.next, ui.kerning);
140 }
141
142 undo_items.remove_at (undo_items.size - 1);
143 } catch (MarkupError e) {
144 warning (e.message);
145 }
146
147 update_rows ();
148 }
149
150 public override void update_rows () {
151 redraw_area (0, 0, allocation.width, allocation.height);
152 }
153
154 private class UndoItem : GLib.Object {
155 public string first;
156 public string next;
157 public double kerning;
158 public int class_priority;
159
160 public UndoItem (string first, string next, double kerning, int class_priority = -1) {
161 this.first = first;
162 this.next = next;
163 this.kerning = kerning;
164 this.class_priority = class_priority;
165 }
166 }
167 }
168
169 }
170