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

Revisions

View the latest version of libsvgbird/Rectangle.vala.
Click to object matrix transformation
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 return this.x <= x <= this.x + width && this.y <= y <= this.y + height; 36 } 37 38 public override void draw_outline (Context cr) { 39 if (rx == 0 && ry == 0) { 40 cr.rectangle (x, y, width, height); 41 } else { 42 draw_rounded_corners (cr); 43 } 44 } 45 46 public void draw_rounded_corners (Context cr) { 47 cr.save (); 48 cr.new_path (); 49 cr.translate (-rx, -ry); 50 elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); 51 elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); 52 elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); 53 elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); 54 cr.close_path (); 55 cr.restore (); 56 } 57 58 public void elliptical_arc (Context cr, double x, double y, double angle_start, double angle_stop) { 59 cr.save (); 60 cr.translate (x + rx, y + ry); 61 cr.scale (rx, ry); 62 cr.arc (0, 0, 1, angle_start, angle_stop); 63 cr.restore (); 64 } 65 66 public override void move (double dx, double dy) { 67 x += dx; 68 y += dy; 69 } 70 71 public override bool is_empty () { 72 return false; 73 } 74 75 public override Object copy () { 76 Rectangle r = new Rectangle (); 77 78 r.x = x; 79 r.y = y; 80 r.rx = rx; 81 r.ry = ry; 82 r.width = width; 83 r.height = height; 84 85 Object.copy_attributes (this, r); 86 87 return r; 88 } 89 90 public override string to_string () { 91 return "Rectangle"; 92 } 93 94 } 95 96 } 97