The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Parse SVG colors

These changes was commited to the Birdfont repository Wed, 06 Jan 2016 17:47:49 +0000.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
author Johan Mattsson <johan.mattsson.m@gmail.com>
Wed, 06 Jan 2016 17:47:49 +0000 (18:47 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Wed, 06 Jan 2016 18:48:19 +0000 (19:48 +0100)
commit 4c87ac892737dba3bd306c661cbd465103ac827e
tree ebb8f56a46dbc53ed2996ba406acb5f7a6a734ed
parent 65567cf1a323aed8594a94602a8671b6764590f6
Parse SVG colors

12 files changed:
libbirdfont/Color.vala
libbirdfont/Doubles.vala
libbirdfont/Glyph.vala
libbirdfont/MenuTab.vala
libbirdfont/Object.vala
libbirdfont/OpenFontFormat/GlyfTable.vala
libbirdfont/Svg/Points.vala
libbirdfont/Svg/SvgFile.vala
libbirdfont/Svg/SvgPath.vala
libbirdfont/Svg/SvgStyle.vala
libbirdfont/Svg/SvgTransform.vala
--- a/libbirdfont/Color.vala +++ b/libbirdfont/Color.vala @@ -101,6 +101,62 @@ } } } + + 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;
--- a/libbirdfont/Doubles.vala +++ b/libbirdfont/Doubles.vala @@ -18,7 +18,7 @@ public class Doubles : GLib.Object { public double* data; - public int size = 10; + public int size = 0; int capacity = 10; public Doubles () {
--- a/libbirdfont/Glyph.vala +++ b/libbirdfont/Glyph.vala @@ -1792,11 +1792,8 @@ 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 @@ o.draw (cr); } } + cr.close_path (); cr.fill (); cr.restore ();
--- a/libbirdfont/MenuTab.vala +++ b/libbirdfont/MenuTab.vala @@ -122,8 +122,7 @@ 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";
--- a/libbirdfont/Object.vala +++ b/libbirdfont/Object.vala @@ -24,7 +24,7 @@ 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; }
--- a/libbirdfont/OpenFontFormat/GlyfTable.vala +++ b/libbirdfont/OpenFontFormat/GlyfTable.vala @@ -266,7 +266,6 @@ // 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"); }
--- 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
--- 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 @@ 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 @@ 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;
--- a/libbirdfont/Svg/SvgPath.vala +++ b/libbirdfont/Svg/SvgPath.vala @@ -45,58 +45,46 @@ } 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) {
--- a/libbirdfont/Svg/SvgStyle.vala +++ b/libbirdfont/Svg/SvgStyle.vala @@ -20,6 +20,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 @@ 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; }
--- 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