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