Commit 4c87ac8
Parse SVG colors
Changed files (11)
modifiedlibbirdfont/Color.valamodifiedlibbirdfont/Doubles.valamodifiedlibbirdfont/Glyph.valamodifiedlibbirdfont/MenuTab.valamodifiedlibbirdfont/Object.valamodifiedlibbirdfont/OpenFontFormat/GlyfTable.valamodifiedlibbirdfont/Svg/Points.valamodifiedlibbirdfont/Svg/SvgFile.valamodifiedlibbirdfont/Svg/SvgPath.valamodifiedlibbirdfont/Svg/SvgStyle.valamodifiedlibbirdfont/Svg/SvgTransform.vala
Diff
@@ -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;@@ -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 () {@@ -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 ();@@ -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";@@ -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; } @@ -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"); }@@ -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 @@ -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;@@ -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) {@@ -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; }@@ -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