Clone in a terminal:
Commit 4c87ac8
Johan Mattsson - 2016-01-06T18:48:19+00:00
4c87ac892737dba3bd306c661cbd465103ac827ejohan.mattsson.m@gmail.com

Parse SVG colors

Changed files (11)

Diff

diff --git a/libbirdfont/Color.vala b/libbirdfont/Color.valaindex 0924b59..a6e872d 100644--- a/libbirdfont/Color.vala+++ b/libbirdfont/Color.vala@@ -102,6 +102,62 @@ public class Color { 		} 	} +	public static Color? parse (string? svg_color) {+		if (svg_color == null) {+			return null;+		}+		+		string color = ((!) svg_color).replace ("#", "");+		uint32 c;+		string[] arguments;+		Color parsed = black ();+		+		if (color.char_count () == 6) {+			color.scanf ("%x", out c);+			parsed.r = (uint8)((c & 0xFF0000) >> 16) / 254.0;+			parsed.g = (uint8)((c & 0x00FF00) >> 8)/ 254.0;+			parsed.b = (uint8)(c & 0x0000FF) / 254.0;+		} else if (color.char_count () == 3) {+			color.scanf ("%x", out c);+			parsed.r = (uint8)(((c & 0xF00) >> 4) | ((c & 0xF00) >> 8)) / 254.0;+			parsed.g = (uint8)((c & 0x0F0) | ((c & 0x0F0) >> 4)) / 254.0;+			parsed.b = (uint8)(((c & 0x00F) << 4) | (c & 0x00F)) / 254.0;+		} else if (color.index_of ("%") > -1) {+			color = color.replace ("rgb", "");+			color = color.replace (" ", "");+			color = color.replace ("\t", "");+			color = color.replace ("%", "");+			arguments = color.split (",");+			+			return_val_if_fail (arguments.length == 3, parsed);+			arguments[0].scanf ("%lf", out parsed.r);+			arguments[1].scanf ("%lf", out parsed.g);+			arguments[2].scanf ("%lf", out parsed.b);+		} else if (color.index_of ("rgb") > -1) {+			color = color.replace ("rgb", "");+			color = color.replace (" ", "");+			color = color.replace ("\t", "");+			arguments = color.split (",");+			+			return_val_if_fail (arguments.length == 3, parsed);+			+			int r, g, b;+			arguments[0].scanf ("%d", out r);+			parsed.r = r / 254.0;+			+			arguments[1].scanf ("%d", out g);+			parsed.g = g / 254.0;+			+			arguments[2].scanf ("%d", out b);+			parsed.b = b / 254.0;+		} else {+			warning ("Unknown color type: " + color);+		}+		+		+		return parsed;+ 	}+ 	public void to_hsva (out double h, out double s, out double v, out double a) { 		double red, green, blue; 		double min, max;diff --git a/libbirdfont/Doubles.vala b/libbirdfont/Doubles.valaindex 50264d5..9a8f9c9 100644--- a/libbirdfont/Doubles.vala+++ b/libbirdfont/Doubles.vala@@ -18,7 +18,7 @@ namespace BirdFont {  public class Doubles : GLib.Object { 	public double* data;-	public int size = 10;+	public int size = 0; 	int capacity = 10; 	 	public Doubles () {diff --git a/libbirdfont/Glyph.vala b/libbirdfont/Glyph.valaindex 15a2b17..f967ac7 100644--- a/libbirdfont/Glyph.vala+++ b/libbirdfont/Glyph.vala@@ -1793,10 +1793,7 @@ public class Glyph : FontDisplay { 			cr.save (); 			cr.new_path (); 			-			get_current_layer ().print();-			 			foreach (Object o in get_visible_objects ()) {-				print("visible obj\n"); 				if (o is PathObject) { 					Path p = ((PathObject) o).get_path (); @@ -1808,6 +1805,7 @@ public class Glyph : FontDisplay { 					o.draw (cr); 				} 			}+			 			cr.close_path (); 			cr.fill (); 			cr.restore ();diff --git a/libbirdfont/MenuTab.vala b/libbirdfont/MenuTab.valaindex 829c544..63963d4 100644--- a/libbirdfont/MenuTab.vala+++ b/libbirdfont/MenuTab.vala@@ -122,8 +122,7 @@ public class MenuTab : FontDisplay { 		Font current_font = BirdFont.get_current_font (); 		string ttf_name = ExportSettings.get_file_name (current_font) + ".ttf"; 		string ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + ".ttf";-		-		print (@"$ttf_name == $ttf_name_mac");+ 		if (ttf_name == ttf_name_mac) { 			MainWindow.show_message (t_("You need to choose a different name for the TTF file with Mac adjustmets.")); 			ttf_name_mac = ExportSettings.get_file_name_mac (current_font) + " Mac.ttf";diff --git a/libbirdfont/Object.vala b/libbirdfont/Object.valaindex 8863e53..b306672 100644--- a/libbirdfont/Object.vala+++ b/libbirdfont/Object.vala@@ -24,7 +24,7 @@ public abstract class Object : GLib.Object { 	public SvgStyle style = new SvgStyle (); 	public Gee.ArrayList<SvgTransform> transforms = new Gee.ArrayList<SvgTransform> (); 	-	public abstract Color? color { get; set; }+	public abstract Color? color { get; set; } // FIXME: keep this in svg style 	public abstract Color? stroke_color { get; set; } 	public abstract Gradient? gradient { get; set; } diff --git a/libbirdfont/OpenFontFormat/GlyfTable.vala b/libbirdfont/OpenFontFormat/GlyfTable.valaindex 1816af2..6d8fbf8 100644--- a/libbirdfont/OpenFontFormat/GlyfTable.vala+++ b/libbirdfont/OpenFontFormat/GlyfTable.vala@@ -266,7 +266,6 @@ public class GlyfTable : OtfTable { 		// flags		 		nflags = glyf_data.get_nflags (); 		if (unlikely (nflags != npoints)) {-			print ("glyf table data:\n"); 			fd.dump (); 			warning (@"(nflags != npoints)  ($nflags != $npoints) in glyph $(g.name). ncontours: $ncontours"); 		}diff --git a/libbirdfont/Svg/Points.vala b/libbirdfont/Svg/Points.valaindex 3253257..7e93574 100644--- a/libbirdfont/Svg/Points.vala+++ b/libbirdfont/Svg/Points.vala@@ -1,5 +1,5 @@ /*-	Copyright (C) 2015 Johan Mattsson+	Copyright (C) 2016 Johan Mattsson  	This library is free software; you can redistribute it and/or modify  	it under the terms of the GNU Lesser General Public License as diff --git a/libbirdfont/Svg/SvgFile.vala b/libbirdfont/Svg/SvgFile.valaindex 18e0655..0b8f646 100644--- a/libbirdfont/Svg/SvgFile.vala+++ b/libbirdfont/Svg/SvgFile.vala@@ -1,5 +1,5 @@ /*-	Copyright (C) 2015 Johan Mattsson+	Copyright (C) 2016 Johan Mattsson  	This library is free software; you can redistribute it and/or modify  	it under the terms of the GNU Lesser General Public License as @@ -160,7 +160,7 @@ public class SvgFile : GLib.Object { 			transform = transform.replace ("  ", " "); 		} 		-		if (unlikely (transform.index_of (")") > -1)) {+		if (unlikely (transform.index_of (")") == -1)) { 			warning ("No parenthesis in transform function."); 			return transform_functions; 		}@@ -308,6 +308,7 @@ public class SvgFile : GLib.Object { 		SvgParser.get_bezier_points (data, out bezier_points, out points_size, true);  		for (int i = 0; i < points_size; i++) {+			// FIXME: add more types 			if (bezier_points[i].type == 'M') { 				points.x = bezier_points[i].x0; 				points.y = bezier_points[i].y0;diff --git a/libbirdfont/Svg/SvgPath.vala b/libbirdfont/Svg/SvgPath.valaindex eabe861..48e81b1 100644--- a/libbirdfont/Svg/SvgPath.vala+++ b/libbirdfont/Svg/SvgPath.vala@@ -45,58 +45,46 @@ public class SvgPath : Object { 	} 			 	public override void draw (Context cr, Color? c = null) {+		Color fill, stroke;+ 		cr.save (); 		 		foreach (Points p in points) { 			cr.new_path ();-			cr.move_to (p.x, p.y);-			+			cr.move_to (p.x, p.y);			 			draw_points (cr, p); 		} 		-		cr.fill (); // FIXME: stroke etc.+		if (style.fill != null) {+			fill = (!) style.fill;+			cr.set_source_rgba (fill.r, fill.g, fill.b, fill.a);+			+			if (style.stroke != null) {+				cr.fill_preserve ();+			} else {+				cr.fill ();+			}+		}+		+		if (style.stroke != null) {+			stroke = (!) style.stroke;+			cr.set_source_rgba (stroke.r, stroke.g, stroke.b, stroke.a);+			cr.stroke ();+		}+ 		cr.restore (); 	}  	public void draw_points (Context cr, Points points) { 		Doubles p = points.point_data;--		print(@"draw $(p.size)\n"); 		-		return_if_fail (p.size % 6 != 0);+		return_if_fail (p.size % 6 == 0); 		 		for (int i = 0; i < p.size; i += 6) { 			cr.curve_to (p.data[i], p.data[i + 1],  				p.data[i + 2], p.data[i + 3], 				p.data[i + 4], p.data[i + 5]);-				-			print(@"$(p.data[i])");-		}-		-		cr.set_source_rgba (0, 0.5, 0, 1);-		cr.fill ();-		-		cr.close_path ();-		cr.new_path ();-		cr.move_to (points.x, points.y);-		cr.set_source_rgba (0, 0, 0.5, 0.5);-		-		for (int i = 0; i < p.size; i += 2) {-			cr.line_to (p.data[i], p.data[i + 1]);-		}--		cr.fill ();--		cr.close_path ();-		cr.new_path ();-		cr.move_to (points.x, points.y);-		cr.set_source_rgba (0.5, 0, 0, 0.5);-		-		for (int i = 0; i < p.size; i += 6) {-			cr.line_to (p.data[i + 4], p.data[i + 5]);-		}--		cr.fill ();		+		}		 	}  	public override void move (double dx, double dy) {diff --git a/libbirdfont/Svg/SvgStyle.vala b/libbirdfont/Svg/SvgStyle.valaindex 33a6cdc..8a1be19 100644--- a/libbirdfont/Svg/SvgStyle.vala+++ b/libbirdfont/Svg/SvgStyle.vala@@ -21,6 +21,9 @@ public class SvgStyle { 	 	Gee.HashMap<string, string> style; 	+	public Color? stroke = null;+	public Color? fill = null;+	 	public SvgStyle () { 		style = new Gee.HashMap<string, string> (); 	}@@ -82,6 +85,9 @@ public class SvgStyle { 				s.style.set ("fill", a.get_content ()); 			} 		}+	+		s.stroke = Color.parse (s.style.get ("stroke"));+		s.fill = Color.parse (s.style.get ("fill")); 		 		return s; 	}diff --git a/libbirdfont/Svg/SvgTransform.vala b/libbirdfont/Svg/SvgTransform.valaindex a4bb027..632b527 100644--- a/libbirdfont/Svg/SvgTransform.vala+++ b/libbirdfont/Svg/SvgTransform.vala@@ -1,5 +1,5 @@ /*-	Copyright (C) 2015 Johan Mattsson+	Copyright (C) 2016 Johan Mattsson  	This library is free software; you can redistribute it and/or modify  	it under the terms of the GNU Lesser General Public License as 

Contact

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