The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Rectangle.vala in libbirdfont/Svg

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 libbirdfont/Svg/Rectangle.vala.
Use SVG transform, parse opacity and gradients
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 BirdFont { 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, Color? c = null) { 37 cr.save (); 38 39 apply_transform (cr); 40 41 if (rx == 0 && ry == 0) { 42 cr.rectangle (x, y, width, height); 43 } else { 44 draw_rounded_corners (cr); 45 } 46 47 fill_and_stroke (cr); 48 49 cr.restore (); 50 } 51 52 public void draw_rounded_corners (Context cr) { 53 cr.new_path (); 54 elliptical_arc (cr, x + width - rx, y + ry, -PI / 2, 0); 55 elliptical_arc (cr, x + width - rx, y + height - ry, 0, PI / 2); 56 elliptical_arc (cr, x + rx, y + height - ry, PI / 2, PI); 57 elliptical_arc (cr, x + rx, y + ry, PI, PI + PI / 2); 58 cr.close_path (); 59 } 60 61 public void elliptical_arc (Context cr, double x, double y, double angle_start, double angle_stop) { 62 cr.save (); 63 cr.translate (x + rx, y + ry); 64 cr.scale (rx, ry); 65 cr.arc (0, 0, 1, angle_start, angle_stop); 66 cr.restore (); 67 } 68 69 public override void move (double dx, double dy) { 70 x += dx; 71 y += dy; 72 } 73 74 public override void update_region_boundaries () { 75 } 76 77 public override void rotate (double theta, double xc, double yc) { 78 } 79 80 public override bool is_empty () { 81 return false; 82 } 83 84 public override void resize (double ratio_x, double ratio_y) { 85 } 86 87 public override Object copy () { 88 Rectangle r = new Rectangle (); 89 90 r.x = x; 91 r.y = y; 92 r.width = width; 93 r.height = height; 94 95 Object.copy_attributes (this, r); 96 97 return r; 98 } 99 100 public override string to_string () { 101 return "Rectangle"; 102 } 103 } 104 105 } 106