Commit 9740edc
SVG rectangle
Changed files (4)
modifiedlibbirdfont/Object.valaaddedlibbirdfont/Svg/Rectangle.valamodifiedlibbirdfont/Svg/SvgFile.valamodifiedlibbirdfont/Svg/SvgPath.vala
Diff
@@ -24,20 +24,20 @@ 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; } // 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 () { }@@ -84,6 +84,27 @@ public abstract class Object : GLib.Object { 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 ();+ }+ } } }new file mode 100644@@ -0,0 +1,103 @@+/*+ 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";+ }+}++}@@ -132,7 +132,41 @@ public class SvgFile : GLib.Object { } 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 @@ public class SvgFile : GLib.Object { 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 @@ public class SvgFile : GLib.Object { } 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 ()); }@@ -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 @@ using Cairo; 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 class SvgPath : Object { } public override void draw (Context cr, Color? c = null) {- Color fill, stroke;- cr.save (); foreach (Points p in points) {@@ -54,23 +38,8 @@ public class SvgPath : Object { 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 (); }