The Birdfont Source Code


All Repositories / birdfont.git / commit – RSS feed

SVG rectangle

These changes was commited to the Birdfont repository Thu, 07 Jan 2016 17:22:22 +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>
Thu, 07 Jan 2016 17:22:22 +0000 (18:22 +0100)
committer Johan Mattsson <johan.mattsson.m@gmail.com>
Thu, 07 Jan 2016 17:22:22 +0000 (18:22 +0100)
commit 9740edce7f5dacbe7fe58c0e024274bafd789b34
tree 2acf70891bab0dcb131ff49864ea2641fb89cecc
parent 24fac189d4a4aebe8bf5c3ccf712344d162c4e6d
SVG rectangle

libbirdfont/Object.vala
libbirdfont/Svg/Rectangle.vala [new ]
libbirdfont/Svg/SvgFile.vala
libbirdfont/Svg/SvgPath.vala
--- a/libbirdfont/Object.vala +++ b/libbirdfont/Object.vala @@ -24,20 +24,20 @@ public SvgStyle style = new SvgStyle (); public Gee.ArrayList<SvgTransform> transforms = new Gee.ArrayList<SvgTransform> (); - 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; } + public virtual Color? color { get; set; } // FIXME: keep this in svg style + public virtual Color? stroke_color { get; set; } + public virtual Gradient? gradient { get; set; } /** Path boundaries */ - public abstract double xmax { get; set; } - public abstract double xmin { get; set; } - public abstract double ymax { get; set; } - public abstract double ymin { get; set; } + public virtual double xmax { get; set; } + public virtual double xmin { get; set; } + public virtual double ymax { get; set; } + public virtual double ymin { get; set; } - public abstract double rotation { get; set; } - public abstract double stroke { get; set; } - public abstract LineCap line_cap { get; set; default = LineCap.BUTT; } - public abstract bool fill { get; set; } + public virtual double rotation { get; set; } + public virtual double stroke { get; set; } + public virtual LineCap line_cap { get; set; default = LineCap.BUTT; } + public virtual bool fill { get; set; } public Object () { } @@ -83,8 +83,29 @@ public virtual string to_string () { return "Object"; + } + + public void fill_and_stroke (Context cr) { + Color fill, stroke; + + 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 (); + } } } }
--- /dev/null +++ b/libbirdfont/Svg/Rectangle.vala @@ -1,1 +1,104 @@ + /* + 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 BirdFont { + + public class Rectangle : Object { + + public double x = 0; + public double y = 0; + public double width = 0; + public double height = 0; + public double rx = 0; + public double ry = 0; + + public Rectangle () { + } + + public override bool is_over (double x, double y) { + return false; + } + + public override void draw (Context cr, Color? c = null) { + cr.save (); + + if (rx == 0 && ry == 0) { + cr.rectangle (x, y, width, height); + } else { + draw_rounded_corners (cr); + } + + fill_and_stroke (cr); + + cr.restore (); + } + + public void draw_rounded_corners (Context cr) { + cr.new_path (); + elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); + elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); + elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); + elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); + cr.close_path (); + } + + public void elliptical_arc (Context cr, double x, double y, double angle_start, double angle_stop) { + cr.save (); + cr.translate (x + rx, y + ry); + cr.scale (rx, ry); + cr.arc (0, 0, 1, angle_start, angle_stop); + cr.restore (); + } + + public override void move (double dx, double dy) { + x += dx; + y += dy; + } + + public override void update_region_boundaries () { + } + + public override void rotate (double theta, double xc, double yc) { + } + + public override bool is_empty () { + return false; + } + + public override void resize (double ratio_x, double ratio_y) { + } + + public override Object copy () { + Rectangle r = new Rectangle (); + + r.x = x; + r.y = y; + r.width = width; + r.height = height; + + Object.copy_attributes (this, r); + + return r; + } + + public override string to_string () { + return "Rectangle"; + } + } + + }
--- a/libbirdfont/Svg/SvgFile.vala +++ b/libbirdfont/Svg/SvgFile.vala @@ -132,7 +132,41 @@ } private void parse_rect (Layer layer, Tag tag) { + Rectangle rectangle = new Rectangle (); + foreach (Attribute attr in tag.get_attributes ()) { + string attribute = attr.get_name (); + + if (attribute == "x") { + rectangle.x = parse_number (attr.get_content ()); + } + + if (attribute == "y") { + rectangle.y = parse_number (attr.get_content ()); + } + + if (attribute == "width") { + rectangle.width = parse_number (attr.get_content ()); + } + + if (attribute == "height") { + rectangle.height = parse_number (attr.get_content ()); + } + + if (attribute == "rx") { + rectangle.rx = parse_number (attr.get_content ()); + } + + if (attribute == "ry") { + rectangle.ry = parse_number (attr.get_content ()); + } + } + + rectangle.transforms = get_transform (tag.get_attributes ()); + rectangle.style = SvgStyle.parse (tag.get_attributes ()); + rectangle.visible = is_visible (tag); + + layer.add_object (rectangle); } private void parse_circle (Layer layer, Tag tag) { @@ -209,7 +243,7 @@ return transform; } - private void remove_unit (string d) { + private string remove_unit (string d) { string s = d.replace ("pt", ""); s = s.replace ("pc", ""); s = s.replace ("mm", ""); @@ -310,7 +344,7 @@ } private Gee.ArrayList<SvgTransform> get_transform (Attributes attributes) { - foreach (Attribute attr in tag.get_attributes ()) { + foreach (Attribute attr in attributes) { if (attr.get_name () == "transform") { return parse_transform (attr.get_content ()); }
--- a/libbirdfont/Svg/SvgPath.vala +++ b/libbirdfont/Svg/SvgPath.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 @@ -16,21 +16,7 @@ namespace BirdFont { - public class SvgPath : Object { - public override double stroke { get; set; } - public override double rotation { get; set; } - public override LineCap line_cap { get; set; } - public override bool fill { get; set; } - - public override Color? color { get; set; } - public override Color? stroke_color { get; set; } - public override Gradient? gradient { get; set; } - - public override double xmin { get; set; } - public override double xmax { get; set; } - public override double ymin { get; set; } - public override double ymax { get; set; } - + public class SvgPath : Object { public Gee.ArrayList<Points> points = new Gee.ArrayList<Points> (); public SvgPath () { @@ -45,8 +31,6 @@ } public override void draw (Context cr, Color? c = null) { - Color fill, stroke; - cr.save (); foreach (Points p in points) { @@ -54,23 +38,8 @@ cr.move_to (p.x, p.y); draw_points (cr, p); } - - 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 (); - } + + fill_and_stroke (cr); cr.restore (); }