The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

EditPointHandle.vala in /libbirdfont

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
Circle boundaries heads/master
1 /* 2 Copyright (C) 2012 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 Math; 16 17 namespace BirdFont { 18 19 public class EditPointHandle : GLib.Object { 20 21 public double length; 22 public unowned EditPoint parent; 23 public PointType type; 24 EditPoint? visual_handle = null; 25 static EditPoint none = new EditPoint (); 26 public bool active; 27 public bool selected; 28 29 public double angle; 30 31 public double x { 32 get { 33 double r = px (); 34 35 if (unlikely (r <= -100000)) { 36 print_position (); 37 move_to (0, 0); 38 } 39 40 return r; 41 } 42 43 set { 44 move_to_coordinate_internal (value, py ()); 45 46 if (parent.tie_handles) { 47 parent.process_tied_handle (); 48 } 49 50 if (parent.reflective_point) { 51 process_symmetrical_handle (); 52 } 53 54 process_connected_handle (); 55 } 56 } 57 58 public double y { 59 get { 60 double r = py (); 61 62 if (unlikely (r <= -100000)) { 63 print_position (); 64 move_to (0, 0); 65 } 66 67 return r; 68 } 69 70 set { 71 move_to_coordinate_internal (px (), value); 72 73 if (parent.tie_handles) { 74 parent.process_tied_handle (); 75 } 76 77 if (parent.reflective_point) { 78 process_symmetrical_handle (); 79 } 80 81 process_connected_handle (); 82 } 83 } 84 85 public double independent_x { 86 get { 87 double r = px (); 88 89 if (unlikely (r <= -100000)) { 90 print_position (); 91 move_to (0, 0); 92 } 93 94 return r; 95 } 96 97 set { 98 move_to_coordinate_internal (value, py ()); 99 } 100 } 101 102 public double independent_y { 103 get { 104 double r = py (); 105 106 if (unlikely (r <= -100000)) { 107 print_position (); 108 move_to (0, 0); 109 } 110 111 return r; 112 } 113 114 set { 115 move_to_coordinate_internal (px (), value); 116 } 117 } 118 119 public EditPointHandle.empty() { 120 this.parent = none; 121 this.angle = 0; 122 this.length = 10; 123 this.type = PointType.NONE; 124 this.active = false; 125 this.selected = false; 126 } 127 128 public EditPointHandle (EditPoint parent, double angle, double length) { 129 this.parent = parent; 130 this.angle = angle; 131 this.length = length; 132 this.type = PointType.LINE_CUBIC; 133 this.active = false; 134 this.selected = false; 135 } 136 137 public EditPointHandle copy () { 138 EditPointHandle n = new EditPointHandle.empty (); 139 n.angle = angle; 140 n.length = length; 141 n.parent = parent; 142 n.type = type; 143 n.active = active; 144 n.selected = selected; 145 return n; 146 } 147 148 public EditPoint get_parent () { 149 return parent; 150 } 151 152 public void convert_to_line () { 153 switch (type) { 154 case PointType.QUADRATIC: 155 type = PointType.LINE_QUADRATIC; 156 break; 157 case PointType.DOUBLE_CURVE: 158 type = PointType.LINE_DOUBLE_CURVE; 159 break; 160 case PointType.CUBIC: 161 type = PointType.LINE_CUBIC; 162 break; 163 default: 164 break; 165 } 166 } 167 168 public bool is_curve () { 169 switch (type) { 170 case PointType.LINE_QUADRATIC: 171 return false; 172 case PointType.LINE_DOUBLE_CURVE: 173 return false; 174 case PointType.LINE_CUBIC: 175 return false; 176 } 177 178 return true; 179 } 180 181 public void convert_to_curve () { 182 switch (type) { 183 case PointType.LINE_QUADRATIC: 184 type = PointType.QUADRATIC; 185 break; 186 case PointType.LINE_DOUBLE_CURVE: 187 type = PointType.DOUBLE_CURVE; 188 break; 189 case PointType.LINE_CUBIC: 190 type = PointType.CUBIC; 191 break; 192 case PointType.QUADRATIC: 193 break; 194 case PointType.DOUBLE_CURVE: 195 break; 196 case PointType.CUBIC: 197 break; 198 default: 199 warning (@"Type $type"); 200 break; 201 } 202 } 203 204 public void set_point_type (PointType point_type) { 205 type = point_type; 206 } 207 208 double px () { 209 assert ((EditPoint?) parent != null); 210 return cos (angle) * length + parent.x; 211 } 212 213 double py () { 214 assert ((EditPoint?) parent != null); 215 return sin (angle) * length + parent.y; 216 } 217 218 internal void print_position () { 219 warning (@"\nEdit point handle at position $(px ()),$(py ()) is not valid.\n" 220 + @"Type: $(parent.type), " 221 + @"Angle: $angle, Length: $length."); 222 } 223 224 public EditPoint get_point () { 225 EditPoint p; 226 227 if (visual_handle == null) { 228 visual_handle = new EditPoint (0, 0); 229 } 230 231 p = (!) visual_handle; 232 p.x = x; 233 p.y = y; 234 235 return p; 236 } 237 238 public bool is_left_handle () { 239 return parent.get_left_handle () == this; 240 } 241 242 public void move_to_coordinate_delta (double dx, double dy) { 243 move_to_coordinate_internal (px () + dx, py () + dy); 244 } 245 246 public void move_to_coordinate (double x, double y) { 247 move_to_coordinate_internal (x, y); 248 249 if (parent.tie_handles) { 250 tie_handle (); 251 } 252 253 if (parent.reflective_point) { 254 tie_handle (); 255 process_symmetrical_handle (); 256 } 257 258 process_connected_handle (); 259 } 260 261 public void move_to_coordinate_internal (double x, double y) { 262 double a, b, c; 263 264 a = parent.x - x; 265 b = parent.y - y; 266 c = a * a + b * b; 267 268 if (unlikely(c == 0)) { 269 angle = 0; // FIXME: this should be a different point type without line handles 270 length = 0; 271 return; 272 } 273 274 length = sqrt (fabs (c)); 275 276 if (c < 0) length = -length; 277 278 if (y < parent.y) { 279 angle = acos (a / length) + PI; 280 } else { 281 angle = -acos (a / length) + PI; 282 } 283 } 284 285 public void process_connected_handle () { 286 EditPointHandle h; 287 288 if (unlikely (type == PointType.NONE)) { 289 warning ("Invalid type."); 290 } 291 292 if (type == PointType.QUADRATIC) { 293 if (!is_left_handle ()) { 294 if (parent.next != null) { 295 h = parent.get_next ().get_left_handle (); 296 h.parent.set_reflective_handles (false); 297 h.parent.set_tie_handle (false); 298 h.type = PointType.QUADRATIC; 299 h.move_to_coordinate_internal (x, y); 300 } 301 } else { 302 if (parent.prev != null) { 303 h = parent.get_prev ().get_right_handle (); 304 h.parent.set_reflective_handles (false); 305 h.parent.set_tie_handle (false); 306 h.type = PointType.QUADRATIC; 307 h.move_to_coordinate_internal (x, y); 308 } 309 } 310 } 311 } 312 313 public void process_symmetrical_handle () { 314 if (is_left_handle ()) { 315 parent.get_right_handle ().length = length; 316 parent.get_right_handle ().process_connected_handle (); 317 } else { 318 parent.get_left_handle ().length = length; 319 parent.get_left_handle ().process_connected_handle (); 320 } 321 322 process_connected_handle (); 323 } 324 325 public void tie_handle () { 326 if (is_left_handle ()) { 327 parent.get_right_handle ().angle = angle - PI; 328 parent.get_right_handle ().process_connected_handle (); 329 } else { 330 parent.get_left_handle ().angle = angle - PI; 331 parent.get_left_handle ().process_connected_handle (); 332 } 333 334 process_connected_handle (); 335 } 336 337 public void move_delta_coordinate (double dx, double dy) { 338 double px = px () + dx; 339 double py = py () + dy; 340 move_to_coordinate (px, py); 341 } 342 343 public void move_to (double x, double y) { 344 EditPoint.to_coordinate (ref x, ref y); 345 move_to_coordinate (x, y); 346 } 347 348 public bool is_line () { 349 return PenTool.is_line (type); 350 } 351 352 public static double average_angle (double angle1, double angle2) { 353 EditPointHandle handle = new EditPointHandle (new EditPoint (0, 0, PointType.CUBIC), 0, 1); 354 double x = (cos (angle1) + cos (angle2)) / 2; 355 double y = (sin (angle1) + sin (angle2)) / 2; 356 handle.move_to_coordinate (x, y); 357 358 if (fabs (x) < 0.001 && fabs (y) < 0.001) { 359 return (angle1 + PI / 2) % (2 * PI); 360 } 361 362 return handle.angle; 363 } 364 365 } 366 367 } 368