The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

Radial gradients

These changes was commited to the Birdfont repository Sat, 18 Jun 2016 22:52:56 +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>
Sat, 18 Jun 2016 22:52:56 +0000 (00:52 +0200)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Sat, 18 Jun 2016 22:52:56 +0000 (00:52 +0200)
commit b25e690afb3eb42074e25200bb7cf3659d767da1
tree 9ac353369259a3db9817ee2ba5c002a2738c90e0
parent d179b3a9dbf27733892f74e010ebb2165bb3af37
Radial gradients

libbirdfont/ColorPicker.vala
libbirdfont/Path.vala
libsvgbird/Defs.vala
libsvgbird/Gradient.vala [deleted ]
libsvgbird/LinearGradient.vala [new ]
libsvgbird/Object.vala
libsvgbird/RadialGradient.vala [new ]
libsvgbird/SvgFile.vala
--- a/libbirdfont/ColorPicker.vala +++ b/libbirdfont/ColorPicker.vala @@ -39,7 +39,7 @@ public Color stroke_color = new Color (0, 0, 0, 1); public Color fill_color = new Color (0, 0, 0, 1); - public Gradient gradient = new Gradient (); + public LinearGradient gradient = new LinearGradient (); bool update_gradient = false; int bars; Stop current_stop = new Stop (); @@ -77,7 +77,7 @@ }); } - public void set_gradient (Gradient g, Stop stop, bool update_gradient) { + public void set_gradient (LinearGradient g, Stop stop, bool update_gradient) { gradient = g; this.update_gradient = update_gradient; current_stop = stop;
--- a/libbirdfont/Path.vala +++ b/libbirdfont/Path.vala @@ -99,7 +99,7 @@ public Color? color = null; public Color? stroke_color = null; - public Gradient? gradient = null; + public LinearGradient? gradient = null; public Path () { } @@ -757,7 +757,7 @@ } if (gradient != null) { - new_path.gradient = ((!) gradient).copy (); + new_path.gradient = (LinearGradient)((!) gradient).copy (); } if (color != null) { @@ -998,7 +998,7 @@ } if (gradient != null) { - Gradient g = (!) gradient; + LinearGradient g = (!) gradient; g.x1 += delta_x; g.x2 += delta_x; g.y1 += delta_y;
--- a/libsvgbird/Defs.vala +++ b/libsvgbird/Defs.vala @@ -20,10 +20,18 @@ public class Defs { public Gee.ArrayList<ClipPath> clip_paths = new Gee.ArrayList<ClipPath> (); public Gee.ArrayList<Gradient> gradients = new Gee.ArrayList<Gradient> (); + public Gee.ArrayList<RadialGradient> radial_gradients = new Gee.ArrayList<RadialGradient> (); + public Gee.ArrayList<LinearGradient> linear_gradients = new Gee.ArrayList<LinearGradient> (); public StyleSheet style_sheet = new StyleSheet (); - public void add (Gradient g) { + public void add_linear_gradient (LinearGradient g) { gradients.add (g); + linear_gradients.add (g); + } + + public void add_radial_gradient (RadialGradient g) { + gradients.add (g); + radial_gradients.add (g); } public ClipPath? get_clip_path_for_url (string? url) { @@ -116,9 +124,17 @@ Defs d = new Defs (); foreach (Gradient g in gradients) { - d.add (g); + d.gradients.add (g); } - + + foreach (RadialGradient g in radial_gradients) { + d.radial_gradients.add (g); + } + + foreach (LinearGradient g in linear_gradients) { + d.linear_gradients.add (g); + } + d.style_sheet = style_sheet.shallow_copy (); return d; @@ -128,7 +144,16 @@ Defs d = new Defs (); foreach (Gradient g in gradients) { - d.add (g.copy ()); + Gradient gradient_copy = g.copy (); + d.gradients.add (gradient_copy); + + if (gradient_copy is LinearGradient) { + d.linear_gradients.add ((LinearGradient) gradient_copy); + } + + if (gradient_copy is RadialGradient) { + d.radial_gradients.add ((RadialGradient) gradient_copy); + } } d.style_sheet = style_sheet.copy ();
diff --git libsvgbird/Gradient.vala(deleted)
--- a/libsvgbird/Gradient.vala +++ /dev/null @@ -1,84 +1,1 @@ - /* - Copyright (C) 2015 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 - published by the Free Software Foundation; either version 3 of the - License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - */ - - using Cairo; - using Math; - - namespace SvgBird { - - public class Gradient : GLib.Object { - public double x1; - public double y1; - public double x2; - public double y2; - - public Gee.ArrayList<Stop> stops; - - public string id = ""; - public string? href = null; - public SvgTransforms transforms; - - public Gradient () { - x1 = 0; - y1 = 0; - x2 = 0; - y2 = 0; - stops = new Gee.ArrayList<Stop> (); - transforms = new SvgTransforms (); - } - - public Gradient copy () { - Gradient g = new Gradient (); - g.x1 = x1; - g.y1 = y1; - g.x2 = x2; - g.y2 = y2; - - foreach (Stop s in stops) { - g.stops.add (s.copy ()); - } - - g.id = id; - g.href = href; - transforms = transforms.copy (); - - return g; - } - - public void copy_stops (Gradient g) { - foreach (Stop stop in g.stops) { - stops.add (stop.copy ()); - } - } - - public string to_string () { - StringBuilder description = new StringBuilder (); - description.append (@"Gradient $(id): "); - description.append (@"x1=$x1, y1=$y1, x2=$x2, y2=$y2"); - - foreach (Stop stop in stops) { - description.append (" "); - description.append (stop.to_string ()); - } - - return description.str; - } - - public Matrix get_matrix () { - return transforms.get_matrix (); - } - } - - }
--- /dev/null +++ b/libsvgbird/LinearGradient.vala @@ -1,1 +1,66 @@ + /* + Copyright (C) 2015 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 + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + */ + + using Cairo; + using Math; + + namespace SvgBird { + + public class LinearGradient : Gradient { + public double x1; + public double y1; + public double x2; + public double y2; + + public LinearGradient () { + base (); + x1 = 0; + y1 = 0; + x2 = 0; + y2 = 0; + } + + public override Gradient copy () { + LinearGradient g = new LinearGradient (); + + g.x1 = x1; + g.y1 = y1; + g.x2 = x2; + g.y2 = y2; + + foreach (Stop s in stops) { + g.stops.add (s.copy ()); + } + + copy_gradient (this, g); + + return g; + } + + public override string to_string () { + StringBuilder description = new StringBuilder (); + description.append (@"Gradient $(id): "); + description.append (@"x1=$x1, y1=$y1, x2=$x2, y2=$y2"); + + foreach (Stop stop in stops) { + description.append (" "); + description.append (stop.to_string ()); + } + + return description.str; + } + } + + }
--- a/libsvgbird/Object.vala +++ b/libsvgbird/Object.vala @@ -210,12 +210,23 @@ public void apply_gradient (Context cr, Gradient? gradient) { Cairo.Pattern pattern; Gradient g; + LinearGradient linear; + RadialGradient radial; if (gradient != null) { g = (!) gradient; - - pattern = new Cairo.Pattern.linear (g.x1, g.y1, g.x2, g.y2); + if (g is LinearGradient) { + linear = (LinearGradient) g; + pattern = new Cairo.Pattern.linear (linear.x1, linear.y1, linear.x2, linear.y2); + } else if (g is RadialGradient) { + radial = (RadialGradient) g; + pattern = new Cairo.Pattern.radial (radial.cx, radial.cy, 0, radial.cx, radial.cy, radial.r); + } else { + warning ("Unknown gradient."); + pattern = new Cairo.Pattern.linear (0, 0, 0, 0); + } + Matrix gradient_matrix = g.get_matrix (); gradient_matrix.invert (); pattern.set_matrix (gradient_matrix);
--- /dev/null +++ b/libsvgbird/RadialGradient.vala @@ -1,1 +1,61 @@ + /* + 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 + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + */ + + using Cairo; + using Math; + + namespace SvgBird { + + public class RadialGradient : Gradient { + public double cx; + public double cy; + public double fx; + public double fy; + public double r; + + public RadialGradient () { + base (); + } + + public override Gradient copy () { + RadialGradient g = new RadialGradient (); + + g.cx = cx; + g.cy = cy; + g.fx = fx; + g.fy = fy; + g.r = r; + + copy_gradient (this, g); + + return g; + } + + public override string to_string () { + StringBuilder description = new StringBuilder (); + description.append (@"Radial gradient $(id): "); + description.append (@"cx=$cx, cy=$cy, fx=$fx, fy=$fy, r=$r"); + + foreach (Stop stop in stops) { + description.append (" "); + description.append (stop.to_string ()); + } + + return description.str; + } + + } + + }
--- a/libsvgbird/SvgFile.vala +++ b/libsvgbird/SvgFile.vala @@ -101,11 +101,12 @@ void parse_defs (SvgDrawing drawing, XmlElement tag) { foreach (XmlElement t in tag) { - // FIXME: radial string name = t.get_name (); if (name == "linearGradient") { parse_linear_gradient (drawing, t); + } else if (name == "radialGradient") { + parse_radial_gradient (drawing, t); } else if (name == "clipPath") { parse_clip_path (drawing, t); } @@ -133,12 +134,64 @@ } } + } + + void parse_radial_gradient (SvgDrawing drawing, XmlElement tag) { + RadialGradient gradient = new RadialGradient (); + + drawing.defs.add_radial_gradient (gradient); + + foreach (Attribute attr in tag.get_attributes ()) { + string name = attr.get_name (); + + // FIXME: gradientUnits + + if (name == "gradientTransform") { + gradient.transforms = parse_transform (attr.get_content ()); + } + + if (name == "href") { + gradient.href = attr.get_content (); + } + + if (name == "cx") { + gradient.cx = parse_number (attr.get_content ()); + } + + if (name == "cy") { + gradient.cy = parse_number (attr.get_content ()); + } + + if (name == "fx") { + gradient.fx = parse_number (attr.get_content ()); + } + + if (name == "fy") { + gradient.fy = parse_number (attr.get_content ()); + } + + if (name == "r") { + gradient.r = parse_number (attr.get_content ()); + } + + if (name == "id") { + gradient.id = attr.get_content (); + } + } + + foreach (XmlElement t in tag) { + string name = t.get_name (); + + if (name == "stop") { + parse_stop (gradient, t); + } + } } void parse_linear_gradient (SvgDrawing drawing, XmlElement tag) { - Gradient gradient = new Gradient (); + LinearGradient gradient = new LinearGradient (); - drawing.defs.add (gradient); + drawing.defs.add_linear_gradient (gradient); foreach (Attribute attr in tag.get_attributes ()) { string name = attr.get_name (); @@ -175,7 +228,6 @@ } foreach (XmlElement t in tag) { - // FIXME: radial string name = t.get_name (); if (name == "stop") {