Clone in a terminal:
Commit e4a78e6
Johan Mattsson - 2014-07-05T15:51:58+00:00
e4a78e6e15300c450c8effe6e22c45cf0c67ed05johan.mattsson.m@gmail.com

Undo for deleting items in the kerning list

Changed files (4)

Diff

diff --git a/libbirdfont/GlyphRange.vala b/libbirdfont/GlyphRange.valaindex 3678bc0..df3d8fc 100644--- a/libbirdfont/GlyphRange.vala+++ b/libbirdfont/GlyphRange.vala@@ -126,10 +126,6 @@ public class GlyphRange { 		parse_range_string (ranges, true); 	} 	-	public void parse_ranges_async (string ranges) throws MarkupError {-		parse_range_string (ranges, false);-	}	-	 	private void parse_range_string (string ranges, bool sync) throws MarkupError { 		string[] r; 				diff --git a/libbirdfont/KerningClasses.vala b/libbirdfont/KerningClasses.valaindex e4dbae6..8e09a5e 100644--- a/libbirdfont/KerningClasses.vala+++ b/libbirdfont/KerningClasses.vala@@ -80,7 +80,7 @@ public class KerningClasses : GLib.Object { 		single_kerning.set (@"$left - $right", k); 	}  -	public void set_kerning (GlyphRange left_range, GlyphRange right_range, double k) {+	public void set_kerning (GlyphRange left_range, GlyphRange right_range, double k, int class_index = -1) { 		int index; 		 		if (left_range.get_length () == 0 || right_range.get_length () == 0) {@@ -102,11 +102,17 @@ public class KerningClasses : GLib.Object { 		 		// keep the list sorted (classes first then single glyphs) 		if (index == -1) {-			classes_first.add (left_range);-			classes_last.add (right_range);-			classes_kerning.add (new Kerning (k));+			if (class_index < 0) {+				classes_first.add (left_range);+				classes_last.add (right_range);+				classes_kerning.add (new Kerning (k));+			} else {+				classes_first.insert (class_index, left_range);+				classes_last.insert (class_index, right_range);+				classes_kerning.insert (class_index, new Kerning (k));+			} 		} else {-			return_if_fail (0 <= index <= classes_first.size);+			return_if_fail (0 <= index < classes_first.size); 			classes_kerning.get (index).val = k; 		} 	}diff --git a/libbirdfont/KerningList.vala b/libbirdfont/KerningList.valaindex f146687..7847fa6 100644--- a/libbirdfont/KerningList.vala+++ b/libbirdfont/KerningList.vala@@ -22,9 +22,25 @@ public class KerningList : FontDisplay { 	int scroll = 0; 	int visible_rows = 0; 	WidgetAllocation allocation;+	Gee.ArrayList<UndoItem> undo_items;+	Gee.ArrayList<string> single_pairs; 	 	public KerningList () { 		allocation = new WidgetAllocation ();+		undo_items = new Gee.ArrayList<UndoItem> ();+		single_pairs = new Gee.ArrayList<string> ();+	}++	private void update_single_pair_list () {+		KerningClasses classes = KerningClasses.get_instance ();+		+		single_pairs.clear ();+		+		classes.get_single_position_pairs ((left, right, kerning) => {+			single_pairs.add (@"$left - $right");+		});+		+		single_pairs.sort (); 	}  	public override void draw (WidgetAllocation allocation, Context cr) {@@ -32,6 +48,9 @@ public class KerningList : FontDisplay { 		int y = 20; 		int s = 0; 		bool color = (scroll % 2) == 0;+		string l, r;+		string[] p;+		double k; 		 		this.allocation = allocation; 		@@ -54,14 +73,21 @@ public class KerningList : FontDisplay { 				color = !color; 			} 		});-		-		classes.get_single_position_pairs ((left, right, kerning) => {+	+		foreach (string pair in single_pairs) { 			if (s++ >= scroll) {-				draw_row (allocation, cr, left, right, @"$kerning", y, color);+				p = pair.split (" - ");+				return_if_fail (p.length == 2);+								+				l = p[0];+				r = p[1];+				k = classes.get_kerning (l, r);+				+				draw_row (allocation, cr, l, r, @"$k", y, color); 				y += 18; 				color = !color; 			}-		});+		} 		 		cr.restore (); 	}	@@ -97,11 +123,12 @@ public class KerningList : FontDisplay {  	public override void button_release (int button, double ex, double ey) { 		KerningClasses classes = KerningClasses.get_instance ();-		string l, r; 		int s = 0; 		int y = 0;-		Font font = BirdFont.get_current_font ();-	+		string l, r;+		string[] p;+		double k;+		 		l = ""; 		r = ""; 		@@ -116,33 +143,65 @@ public class KerningList : FontDisplay { 					}				 				} 			});-			-			if (l != "" && r != "") {-				classes.delete_kerning_for_class (l, r);-				font.touch ();-			}-			-			classes.get_single_position_pairs ((left, right, kerning) => {++			foreach (string pair in single_pairs) { 				if (s++ >= scroll) { 					y += 18;+					p = pair.split (" - ");+					return_if_fail (p.length == 2); 					-					if (y - 10 <= ey <= y + 5) {-						l = left;-						r = right;-					}				+					if (y - 10 <= ey <= y + 5) {			+						l = p[0];+						r = p[1];+					} 				}-			});-			-			if (l != "" && r != "") {-				classes.delete_kerning_for_pair (l, r);-				font.touch ();-			}+			}	+			delete_kerning (l, r); 			+			update_single_pair_list (); 			update_scrollbar (); 			redraw_area (0, 0, allocation.width, allocation.height); 		} 	} +	private void delete_kerning (string left, string right) {+		double kerning = 0;+		GlyphRange glyph_range_first, glyph_range_next;+		KerningClasses classes = KerningClasses.get_instance ();+		Font font = BirdFont.get_current_font ();+		string l, r;+		int class_index = -1;+		+		l = GlyphRange.unserialize (left);+		r = GlyphRange.unserialize (right);+		+		try {+			if (left != "" && right != "") {+				+				if (l.char_count () > 1 || r.char_count () > 1) {+					glyph_range_first = new GlyphRange ();+					glyph_range_next = new GlyphRange ();+					+					glyph_range_first.parse_ranges (left);+					glyph_range_next.parse_ranges (right);+					+					kerning = classes.get_kerning_for_range (glyph_range_first, glyph_range_next);+					class_index = classes.get_kerning_item_index (glyph_range_first, glyph_range_next);+					+					classes.delete_kerning_for_class (left, right);+				} else {+					kerning = classes.get_kerning (left, right);+					classes.delete_kerning_for_pair (left, right);+				}+				+				undo_items.add (new UndoItem (left, right, kerning, class_index));+				font.touch ();+			}+		} catch (MarkupError e) {+			warning (e.message);+		}+	}+ 	public override string get_label () { 		return t_("Kerning Pairs"); 	}@@ -183,6 +242,7 @@ public class KerningList : FontDisplay { 	}  	public override void selected_canvas () {+		update_single_pair_list (); 		update_scrollbar (); 		redraw_area (0, 0, allocation.width, allocation.height); 	}@@ -209,6 +269,54 @@ public class KerningList : FontDisplay { 		 		redraw_area (0, 0, allocation.width, allocation.height); 	}+	+	public override void undo () {+		UndoItem ui;+		KerningClasses classes = KerningClasses.get_instance ();+		GlyphRange glyph_range_first, glyph_range_next;+		+		try {+			if (undo_items.size == 0) {+				return;+			}+			+			ui = undo_items.get (undo_items.size - 1);+			+			if (ui.first.char_count () > 1 || ui.next.char_count () > 1) {+				glyph_range_first = new GlyphRange ();+				glyph_range_next = new GlyphRange ();+				+				glyph_range_first.parse_ranges (ui.first);+				glyph_range_next.parse_ranges (ui.next);+				+				classes.set_kerning (glyph_range_first, glyph_range_next, ui.kerning, ui.class_priority);+			} else {+				classes.set_kerning_for_single_glyphs (ui.first, ui.next, ui.kerning);+			}+			+			undo_items.remove_at (undo_items.size - 1);+		} catch (MarkupError e) {+			warning (e.message);+		}+		+		update_single_pair_list ();+		update_scrollbar ();+		redraw_area (0, 0, allocation.width, allocation.height);+	}+	+	private class UndoItem : GLib.Object {+		public string first;+		public string next;+		public double kerning;+		public int class_priority;+		+		public UndoItem (string first, string next, double kerning, int class_priority = -1) {+			this.first = first;+			this.next = next;+			this.kerning = kerning;+			this.class_priority = class_priority;+		}+	} }  }diff --git a/libbirdfont/OpenFontFormat/CodePageBits.vala b/libbirdfont/OpenFontFormat/CodePageBits.valaindex 9596f37..249696d 100644--- a/libbirdfont/OpenFontFormat/CodePageBits.vala+++ b/libbirdfont/OpenFontFormat/CodePageBits.vala@@ -163,7 +163,7 @@ public class CodePageBits : GLib.Object { 			ranges = new GlyphRange (); 			 			try {-				ranges.parse_ranges_async (unicode_ranges);+				ranges.parse_ranges (unicode_ranges); 			} catch (GLib.Error e) { 				warning (e.message); 			}

Contact

Email: johan.mattsson.m@gmail.com
Mastodon: typo.social/@birdfont
GitHub: johanmattssonm
LinkedIn: johan-mattsson-2ab324112