The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Line.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

Revisions

View the latest version of libbirdfont/Line.vala.
Use entire path instead of control points in glyph boundaries
1 /* 2 Copyright (C) 2012 2014 2015 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 17 namespace BirdFont { 18 19 /** Guide */ 20 public class Line : GLib.Object { 21 public static const bool VERTICAL = true; 22 public static const bool HORIZONTAL = false; 23 24 public bool dashed { get; set; } 25 26 public string label; 27 bool vertical; 28 string metrics; 29 30 public double pos; 31 32 bool active = false; 33 bool move = false; 34 35 public signal void queue_draw_area (int x, int y, int w, int h); 36 public signal void position_updated (double pos); 37 38 double r; 39 double g; 40 double b; 41 double a; 42 43 bool visible = true; 44 bool moveable = true; 45 46 public bool rsb = false; 47 public bool lsb = false; 48 49 public Line (string label = "No label set", double position = 10, bool vertical = false) { 50 this.label = label; 51 this.vertical = vertical; 52 this.pos = position; 53 54 dashed = false; 55 metrics = ""; 56 57 set_color_theme ("Guide 1"); 58 } 59 60 public Line copy () { 61 Line l = new Line (label, pos, vertical); 62 63 l.r = r; 64 l.g = g; 65 l.b = b; 66 l.a = a; 67 68 l.visible = visible; 69 l.dashed = dashed; 70 71 return l; 72 } 73 74 public void set_metrics (double m) { 75 string t = @"$m"; 76 string s = ""; 77 78 int i; 79 unichar c; 80 81 i = 0; 82 while (t.get_next_char (ref i, out c)) { 83 s = s + (!) c.to_string (); 84 85 if (i >= 5) { 86 break; 87 } 88 } 89 90 metrics = s; 91 } 92 93 public void set_visible (bool v) { 94 visible = v; 95 } 96 97 public bool is_visible () { 98 return visible; 99 } 100 101 public void set_moveable (bool m) { 102 moveable = m; 103 } 104 105 public void set_color_theme (string color) { 106 Color c = Theme.get_color (color); 107 108 r = c.r; 109 g = c.g; 110 b = c.b; 111 a = c.a; 112 } 113 114 public void set_color (double r, double g, double b, double a) { 115 this.r = r; 116 this.g = g; 117 this.b = b; 118 this.a = a; 119 } 120 121 public bool is_moving () { 122 return move; 123 } 124 125 public bool set_move (bool moving) { 126 bool r = move; 127 move = moving; 128 return (r == moving); 129 } 130 131 public bool button_press (uint button) { 132 Glyph g; 133 TextListener listener; 134 string position; 135 bool text_input = false; 136 137 if (get_active ()) { 138 if (button == 3 || KeyBindings.has_shift ()) { 139 move = false; 140 text_input = true; 141 142 g = MainWindow.get_current_glyph (); 143 if (lsb) { 144 position = @"$(g.get_left_side_bearing ())"; 145 } else if (rsb) { 146 position = @"$(g.get_right_side_bearing ())"; 147 } else { 148 position = @"$pos"; 149 } 150 151 listener = new TextListener (t_("Position"), position, t_("Move")); 152 153 listener.signal_text_input.connect ((text) => { 154 string submitted_value; 155 double parsed_value; 156 Glyph glyph; 157 double x1, y1, x2, y2; 158 159 glyph = MainWindow.get_current_glyph (); 160 submitted_value = text.replace (",", "."); 161 parsed_value = double.parse (submitted_value); 162 163 if (lsb) { 164 if (glyph.get_boundaries (out x1, out y1, out x2, out y2)) { 165 parsed_value = x1 - parsed_value; 166 } else { 167 parsed_value = glyph.right_limit - parsed_value; 168 } 169 } else if (rsb) { 170 if (glyph.get_boundaries (out x1, out y1, out x2, out y2)) { 171 parsed_value += x2; 172 } else { 173 parsed_value = glyph.left_limit - parsed_value; 174 } 175 } 176 177 pos = parsed_value; 178 position_updated (parsed_value); 179 GlyphCanvas.redraw (); 180 }); 181 182 listener.signal_submit.connect (() => { 183 TabContent.hide_text_input (); 184 }); 185 186 TabContent.show_text_input (listener); 187 } else { 188 move = true; 189 } 190 191 g = MainWindow.get_current_glyph (); 192 g.store_undo_state (); 193 } else { 194 move = false; 195 active = false; 196 } 197 198 return move || text_input; 199 } 200 201 void redraw_line () { 202 GlyphCanvas.redraw (); 203 } 204 205 public void move_line_to (int x, int y, WidgetAllocation allocation) { 206 set_move (true); 207 event_move_to (x, y, allocation); 208 } 209 210 public bool event_move_to (int x, int y, WidgetAllocation allocation) { 211 double p, c; 212 bool a = false; 213 Glyph g = MainWindow.get_current_glyph (); 214 double ivz = 1/g.view_zoom; 215 double margin = 10; 216 double none = 0; 217 218 if (!moveable) { 219 return false; 220 } 221 222 if (is_vertical ()) { // over line handle (y) 223 if (y > g.allocation.height - 10) { 224 p = pos; 225 c = Glyph.path_coordinate_x (x); 226 a = (p - margin * ivz <= c <= p + margin * ivz); 227 } 228 229 if (a != get_active ()) { 230 redraw_line (); 231 } 232 233 set_active (a); 234 235 } else { // over line handle (x) 236 if (x > g.allocation.width - 10) { 237 p = pos; 238 c = Glyph.path_coordinate_y (y); 239 a = (p - margin * ivz <= c <= p + margin * ivz); 240 } 241 242 if (a != get_active ()) { 243 redraw_line (); 244 } 245 246 set_active (a); 247 } 248 249 // move the line 250 if (move) { 251 double np = pos; 252 redraw_line (); // clear old position 253 254 if (is_vertical ()) { 255 pos = Glyph.path_coordinate_x (x); 256 257 if (GridTool.is_visible ()) { 258 GridTool.tie_coordinate (ref pos, ref none); 259 } 260 redraw_line (); // draw at new position 261 } else { 262 pos = Glyph.path_coordinate_y (y); 263 264 if (GridTool.is_visible ()) { 265 GridTool.tie_coordinate (ref none, ref pos); 266 } 267 redraw_line (); 268 } 269 270 if (Math.fabs (np - pos) > 10) { 271 queue_draw_area (0, 0, g.allocation.width, g.allocation.height); 272 } 273 274 position_updated (pos); // signal update 275 276 BirdFont.get_current_font ().touch (); 277 } 278 279 if (GridTool.is_visible ()) { 280 GridTool.update_lines (); 281 } 282 283 return move; 284 } 285 286 public bool get_active () { 287 return active; 288 } 289 290 public void set_active (bool active) { 291 this.active = active; 292 } 293 294 public string get_label () { 295 return label; 296 } 297 298 public bool is_vertical () { 299 return vertical; 300 } 301 302 public int get_position_pixel () { 303 if (is_vertical ()) { 304 return Glyph.reverse_path_coordinate_x (pos); 305 } 306 307 return Glyph.reverse_path_coordinate_y (pos) ; 308 } 309 310 public double get_pos () { 311 return pos; 312 } 313 314 public void draw (Context cr, WidgetAllocation allocation) { 315 Glyph g = MainWindow.get_current_glyph (); 316 double p, h, w; 317 double size = (active) ? 8 : 5; 318 Text glyph_metrics; 319 Text line_label; 320 321 if (!visible) { 322 return; 323 } 324 325 cr.save (); 326 cr.set_line_width (1); 327 328 if (dashed) { 329 cr.set_dash ({20, 20}, 0); 330 } 331 332 if (active) { 333 Theme.color (cr, "Highlighted Guide"); 334 } else { 335 cr.set_source_rgba (r, this.g, b, a); 336 } 337 338 // Line 339 if (is_vertical ()) { 340 p = Glyph.reverse_path_coordinate_x (pos); 341 h = g.allocation.height; 342 343 cr.move_to (p, 0); 344 cr.line_to (p, h); 345 cr.stroke (); 346 347 cr.scale (1, 1); 348 349 if (moveable) { 350 cr.new_path (); 351 cr.move_to (p - size, h); 352 cr.line_to (p, h - size); 353 cr.line_to (p + size, h); 354 cr.close_path(); 355 cr.fill (); 356 357 if (get_active ()) { 358 glyph_metrics = new Text (metrics, 17); 359 Theme.text_color (glyph_metrics, "Highlighted Guide"); 360 glyph_metrics.widget_x = p + 10; 361 glyph_metrics.widget_y = h - 25; 362 glyph_metrics.draw (cr); 363 } 364 } 365 366 } else { 367 p = Glyph.reverse_path_coordinate_y (pos); 368 w = g.allocation.width; 369 370 cr.move_to (0, p); 371 cr.line_to (w, p); 372 cr.stroke (); 373 374 if (moveable) { 375 cr.new_path (); 376 cr.move_to (w, p - size); 377 cr.line_to (w - size, p); 378 cr.line_to (w, p + size); 379 cr.close_path(); 380 cr.fill (); 381 } 382 } 383 384 // Label 385 if (get_active ()) { 386 line_label = new Text (get_label (), 19 * MainWindow.units); 387 388 if (is_vertical ()) { 389 line_label.widget_x = p + 8 * MainWindow.units; 390 line_label.widget_y = allocation.height - 55 * MainWindow.units; 391 } else { 392 line_label.widget_x = g.allocation.width 393 - 10 * MainWindow.units 394 - line_label.get_extent (); 395 396 line_label.widget_y = p + 10 * MainWindow.units; 397 } 398 399 if (active) { 400 Theme.text_color (line_label, "Highlighted Guide"); 401 } else { 402 line_label.set_source_rgba (r, this.g, b, a); 403 } 404 405 line_label.draw (cr); 406 } 407 408 cr.restore (); 409 } 410 411 412 413 } 414 415 } 416