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.
Move the SVG parser and the SVG rendering code to libsvgbird
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 public double rx = 0; 27 public double ry = 0; 28 29 public Rectangle () { 30 } 31 32 public override bool is_over (double x, double y) { 33 return false; 34 } 35 36 public override void draw (Context cr) { 37 cr.save (); 38 apply_transform (cr); 39 40 if (rx == 0 && ry == 0) { 41 cr.rectangle (x, y, width, height); 42 } else { 43 draw_rounded_corners (cr); 44 } 45 46 paint (cr); 47 cr.restore (); 48 } 49 50 public void draw_rounded_corners (Context cr) { 51 cr.new_path (); 52 elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); 53 elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); 54 elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); 55 elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); 56 cr.close_path (); 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 void move (double dx, double dy) { 68 x += dx; 69 y += dy; 70 } 71 72 public override void update_region_boundaries () { 73 } 74 75 public override void rotate (double theta, double xc, double yc) { 76 } 77 78 public override bool is_empty () { 79 return false; 80 } 81 82 public override void resize (double ratio_x, double ratio_y) { 83 } 84 85 public override Object copy () { 86 Rectangle r = new Rectangle (); 87 88 r.x = x; 89 r.y = y; 90 r.width = width; 91 r.height = height; 92 93 Object.copy_attributes (this, r); 94 95 return r; 96 } 97 98 public override string to_string () { 99 return "Rectangle"; 100 } 101 } 102 103 } 104