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.
Merge ../birdfont-2.x
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 false; 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.new_path (); 48 elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); 49 elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); 50 elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); 51 elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); 52 cr.close_path (); 53 } 54 55 public void elliptical_arc (Context cr, double x, double y, double angle_start, double angle_stop) { 56 cr.save (); 57 cr.translate (x + rx, y + ry); 58 cr.scale (rx, ry); 59 cr.arc (0, 0, 1, angle_start, angle_stop); 60 cr.restore (); 61 } 62 63 public override void move (double dx, double dy) { 64 x += dx; 65 y += dy; 66 } 67 68 public override void rotate (double theta, double xc, double yc) { 69 } 70 71 public override bool is_empty () { 72 return false; 73 } 74 75 public override void resize (double ratio_x, double ratio_y) { 76 } 77 78 public override Object copy () { 79 Rectangle r = new Rectangle (); 80 81 r.x = x; 82 r.y = y; 83 r.rx = rx; 84 r.ry = ry; 85 r.width = width; 86 r.height = height; 87 88 Object.copy_attributes (this, r); 89 90 return r; 91 } 92 93 public override string to_string () { 94 return "Rectangle"; 95 } 96 97 } 98 99 } 100