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.
Bounding box for SVG rectangles
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 public override void update_boundaries (Matrix view_matrix) { 98 Matrix object_matrix = transforms.get_matrix (); 99 object_matrix.multiply (object_matrix, view_matrix); 100 101 double x0, x1, x2, x3; 102 double y0, y1, y2, y3; 103 104 double half_stroke_width = style.stroke_width / 2; 105 106 x0 = x - half_stroke_width; 107 y0 = y - half_stroke_width; 108 109 x1 = x + width + half_stroke_width; 110 y1 = y - half_stroke_width; 111 112 x2 = x + width + half_stroke_width; 113 y2 = y + height + half_stroke_width; 114 115 x3 = x - half_stroke_width; 116 y3 = y + height + half_stroke_width; 117 118 object_matrix.transform_point (ref x0, ref y0); 119 object_matrix.transform_point (ref x1, ref y1); 120 object_matrix.transform_point (ref x2, ref y2); 121 object_matrix.transform_point (ref x3, ref y3); 122 123 top = min (y0, y1, y2, y3); 124 bottom = max (y0, y1, y2, y3); 125 left = min (x0, x1, x2, x3); 126 right = max (x0, x1, x2, x3); 127 } 128 129 double min (double x0, double x1, double x2, double x3) { 130 double m = x0; 131 132 if (x1 < m) { 133 m = x1; 134 } 135 136 if (x2 < m) { 137 m = x2; 138 } 139 140 if (x3 < m) { 141 m = x3; 142 } 143 144 return m; 145 } 146 147 double max (double x0, double x1, double x2, double x3) { 148 double m = x0; 149 150 if (x1 > m) { 151 m = x1; 152 } 153 154 if (x2 > m) { 155 m = x2; 156 } 157 158 if (x3 > m) { 159 m = x3; 160 } 161 162 return m; 163 } 164 } 165 166 } 167