The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Rectangle.vala in /libsvgbird

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git
Circle boundaries heads/master
1 /* 2 Copyright (C) 2016 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 15 using Cairo; 16 using Math; 17 18 namespace SvgBird { 19 20 public class Rectangle : Object { 21 22 public double x = 0; 23 public double y = 0; 24 public double width = 0; 25 public double height = 0; 26 27 /** Corner radius */ 28 public double rx = 0; 29 public double ry = 0; 30 31 public Rectangle () { 32 } 33 34 public override bool is_over (double x, double y) { 35 to_object_view (ref x, ref y); 36 return this.x <= x <= this.x + width && this.y <= y <= this.y + height; 37 } 38 39 public override void draw_outline (Context cr) { 40 if (rx == 0 && ry == 0) { 41 cr.rectangle (x, y, width, height); 42 } else { 43 draw_rounded_corners (cr); 44 } 45 } 46 47 public void draw_rounded_corners (Context cr) { 48 cr.save (); 49 cr.new_path (); 50 cr.translate (-rx, -ry); 51 elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); 52 elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); 53 elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); 54 elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); 55 cr.close_path (); 56 cr.restore (); 57 } 58 59 public void elliptical_arc (Context cr, double x, double y, double angle_start, double angle_stop) { 60 cr.save (); 61 cr.translate (x + rx, y + ry); 62 cr.scale (rx, ry); 63 cr.arc (0, 0, 1, angle_start, angle_stop); 64 cr.restore (); 65 } 66 67 public override bool is_empty () { 68 return false; 69 } 70 71 public override Object copy () { 72 Rectangle r = new Rectangle (); 73 74 r.x = x; 75 r.y = y; 76 r.rx = rx; 77 r.ry = ry; 78 r.width = width; 79 r.height = height; 80 81 Object.copy_attributes (this, r); 82 83 return r; 84 } 85 86 public override string to_string () { 87 return "Rectangle"; 88 } 89 90 } 91 92 } 93