Clone in a terminal:
Commit a98df2f
Johan Mattsson - 2014-11-11T14:32:48+00:00
a98df2fdc1c9fcdfb79f595be7412cc09d901367johan.mattsson.m@gmail.com

Store image selections in bf fonts

Changed files (6)

Diff

diff --git a/libbirdfont/BackgroundSelection.vala b/libbirdfont/BackgroundSelection.valaindex 700bf4e..348e35c 100644--- a/libbirdfont/BackgroundSelection.vala+++ b/libbirdfont/BackgroundSelection.vala@@ -16,8 +16,8 @@ namespace BirdFont {  public class BackgroundSelection : GLib.Object { 	-	public GlyphCollection? assigned_glyph;-	public BackgroundImage image;+	public string? assigned_glyph;+	public BackgroundImage? image; 	public BackgroundImage parent_image; 	 	public double x;@@ -25,7 +25,7 @@ public class BackgroundSelection : GLib.Object { 	public double w; 	public double h; 	-	public BackgroundSelection (BackgroundImage img, BackgroundImage parent_img,+	public BackgroundSelection (BackgroundImage? img, BackgroundImage parent_img, 		double x, double y, double w, double h) { 			 		assigned_glyph = null;diff --git a/libbirdfont/BackgroundSelectionTool.vala b/libbirdfont/BackgroundSelectionTool.valaindex a937020..c65b728 100644--- a/libbirdfont/BackgroundSelectionTool.vala+++ b/libbirdfont/BackgroundSelectionTool.vala@@ -35,7 +35,6 @@ public class BackgroundSelectionTool : CutBackgroundTool { 			double x, y, w, h; 			BackgroundImage bg; 			Text label = new Text ();-			GlyphCollection g; 			double tx, ty, font_height; 			 			if (glyph.get_background_image () == null) {@@ -67,12 +66,10 @@ public class BackgroundSelectionTool : CutBackgroundTool { 				cr.fill (); 				 				if (bs.assigned_glyph != null) {-					g = (!) bs.assigned_glyph;-					-					if (label.has_character (g.get_name ())) {+					if (label.has_character ((!) bs.assigned_glyph)) { 						font_height = 18; 						cr.set_source_rgba (1, 1, 1, 1);-						label.set_text (g.get_name ());+						label.set_text ((!) bs.assigned_glyph); 						tx = x  + w - label.get_width (font_height) / 2.0; 						ty = y + h; 						ty += label.get_height (font_height) / 2.0;diff --git a/libbirdfont/BackgroundTools.vala b/libbirdfont/BackgroundTools.valaindex a305744..ac51edb 100644--- a/libbirdfont/BackgroundTools.vala+++ b/libbirdfont/BackgroundTools.vala@@ -74,17 +74,22 @@ public class BackgroundTools : ToolCollection  { 			GlyphSelection gs = new GlyphSelection (); 			 			gs.selected_glyph.connect ((gc) => {-				GlyphCollection pgc;+				GlyphCollection? pgc;+				Font font = BirdFont.get_current_font (); 				-				if (bpl.selection.assigned_glyph != null) {-					pgc = (!) bpl.selection.assigned_glyph;-					pgc.get_current ().set_background_image (null);+				pgc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph);+				if (pgc != null) {+					((!) pgc).get_current ().set_background_image (null); 				}		 				-				bpl.selection.assigned_glyph = gc;+				bpl.selection.assigned_glyph = gc.get_name (); 				bpl.label = gc.get_name (); 				gc.get_current ().set_background_image (bpl.selection.image);-				bpl.selection.image.center_in_glyph (gc.get_current ());+				+				if (bpl.selection.image != null) {+					((!) bpl.selection.image).center_in_glyph (gc.get_current ());+				}+				 				set_default_canvas (); 				ZoomTool.zoom_full_background_image (); 			});@@ -97,16 +102,18 @@ public class BackgroundTools : ToolCollection  { 			// don't invalidate the toolbox iterator 			IdleSource idle = new IdleSource ();  			idle.set_callback (() => {-				GlyphCollection g;+				GlyphCollection? gc; 				BackgroundPartLabel bpl;+				Font font = BirdFont.get_current_font (); 				 				bpl = (BackgroundPartLabel) t; 				bpl.deleted = true;-				-				if (bpl.selection.assigned_glyph != null){-					g = (!) bpl.selection.assigned_glyph;-					g.get_current ().set_background_image (null);++				gc = font.get_glyph_collection_by_name (bpl.selection.assigned_glyph);+				if (gc != null) {+					((!) gc).get_current ().set_background_image (null); 				}+				 			 				parts.tool.remove (bpl); 				bpl.selection.parent_image.selections.remove (bpl.selection);diff --git a/libbirdfont/BirdFontFile.vala b/libbirdfont/BirdFontFile.valaindex 3f50503..311a71f 100644--- a/libbirdfont/BirdFontFile.vala+++ b/libbirdfont/BirdFontFile.vala@@ -188,6 +188,8 @@ class BirdFontFile : GLib.Object { 	} 	 	public void write_images (DataOutputStream os) throws GLib.Error {+		string glyph_name;+		 		if (font.background_images.size > 0) { 			os.put_string (@"<images>\n"); 			@@ -197,7 +199,32 @@ class BirdFontFile : GLib.Object { 					warning ("No name."); 				} 				-				os.put_string (@"\t<image name=\"$(b.name)\" sha1=\"$(b.get_sha1 ())\"/>\n");+				os.put_string ("\t<image ");+				os.put_string (@"name=\"$(b.name)\" ");+				os.put_string (@"sha1=\"$(b.get_sha1 ())\" ");+				os.put_string (@"x=\"$(b.img_x)\" ");+				os.put_string (@"y=\"$(b.img_y)\" ");+				os.put_string (@"scale_x=\"$(b.img_scale_x)\" ");+				os.put_string (@"scale_y=\"$(b.img_scale_y)\" ");+				os.put_string (@"rotation=\"$(b.img_rotation)\" ");+				os.put_string (">\n");+				+				foreach (BackgroundSelection selection in b.selections) {+					os.put_string ("\t\t<selection ");+					os.put_string (@"x=\"$(selection.x)\" ");+					os.put_string (@"y=\"$(selection.y)\" ");+					os.put_string (@"width=\"$(selection.w)\" ");+					os.put_string (@"height=\"$(selection.h)\" ");+					+					if (selection.assigned_glyph != null) {+						glyph_name = (!) selection.assigned_glyph;+						os.put_string (@"glyph=\"$(glyph_name)\" ");+					}+					+					os.put_string ("/>\n");+				}+				+				os.put_string (@"\t</image>\n"); 			} 		 			os.put_string (@"</images>\n");@@ -704,8 +731,10 @@ class BirdFontFile : GLib.Object { 	 	public void parse_images (Tag tag) { 		BackgroundImage? new_img;+		BackgroundImage img; 		string name; 		File img_file;+		double x, y, scale_x, scale_y, rotation; 		 		foreach (Tag t in tag) { 			if (t.get_name () == "image") {@@ -713,6 +742,12 @@ class BirdFontFile : GLib.Object { 				new_img = null; 				img_file = get_child (font.get_backgrounds_folder (), "parts"); +				x = 0;+				y = 0;+				scale_x = 0;+				scale_y = 0;+				rotation = 0;+				 				foreach (Attribute attr in t.get_attributes ()) { 					if (attr.get_name () == "sha1") { 						img_file = get_child (img_file, attr.get_content () + ".png");@@ -727,11 +762,39 @@ class BirdFontFile : GLib.Object { 					if (attr.get_name () == "name") { 						name = attr.get_content (); 					}+					+					if (attr.get_name () == "x") {+						x = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "y") {+						y = parse_double (attr.get_content ());+					}+					+					if (attr.get_name () == "scale_x") {+						scale_x = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "scale_y") {+						scale_y = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "rotation") {+						rotation = parse_double (attr.get_content ());+					} 				} 				 				if (new_img != null && name != "") {-					((!) new_img).name = name;-					Toolbox.background_tools.add_image ((!) new_img);+					img = (!) new_img;+					img.name = name;+					img.img_x = x;+					img.img_y = y;+					img.img_scale_x = scale_x;+					img.img_scale_y = scale_y;+					img.img_rotation = rotation;+					Toolbox.background_tools.add_image (img);+					+					parse_imgage_selections (img, t); 				} else { 					warning (@"No image found, name: $name");				 				}@@ -739,6 +802,50 @@ class BirdFontFile : GLib.Object { 		} 	} 	+	private void parse_imgage_selections (BackgroundImage image, Tag tag) {+		double x, y, w, h;+		string? assigned_glyph;+		BackgroundSelection s;+		+		foreach (Tag t in tag) {+			if (t.get_name () == "selection") {+				+				x = 0;+				y = 0;+				w = 0;+				h = 0;+				assigned_glyph = null;+				+				foreach (Attribute attr in t.get_attributes ()) {+					if (attr.get_name () == "x") {+						x = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "y") {+						y = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "width") {+						w = parse_double (attr.get_content ());+					}+					+					if (attr.get_name () == "height") {+						h = parse_double (attr.get_content ());+					}++					if (attr.get_name () == "glyph") {+						assigned_glyph = attr.get_content ();+					}+				}+				+				s = new BackgroundSelection (null, image, x, y, w, h);+				s.assigned_glyph = assigned_glyph;+				+				image.selections.add (s);+			}+		}+	}+	 	private void create_background_files (Tag root) { 		foreach (Tag child in root) { 			if (child.get_name () == "name") {diff --git a/libbirdfont/Font.vala b/libbirdfont/Font.valaindex 9ed5073..71cf979 100644--- a/libbirdfont/Font.vala+++ b/libbirdfont/Font.vala@@ -493,10 +493,14 @@ public class Font : GLib.Object { 	}  	/** Get glyph collection by name. */-	public GlyphCollection? get_glyph_collection_by_name (string glyph) {+	public GlyphCollection? get_glyph_collection_by_name (string? glyph) { 		// TODO: load from disk here if needed. 		GlyphCollection? gc = null;-		gc = glyph_name.get (glyph);		+		+		if (glyph != null) {+			gc = glyph_name.get ((!) glyph);+		}+		 		return gc; 	} diff --git a/libbirdfont/MenuTab.vala b/libbirdfont/MenuTab.valaindex 08b1446..c53a5aa 100644--- a/libbirdfont/MenuTab.vala+++ b/libbirdfont/MenuTab.vala@@ -512,7 +512,6 @@ public class MenuTab : FontDisplay { 	 	public static void show_all_available_characters () { 		MainWindow.get_tab_bar ().add_unique_tab (new OverView ());-		OverView o = MainWindow.get_overview (); 		 		if (!BirdFont.get_current_font ().initialised) { 			MenuTab.new_file ();

Contact

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