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.
Merge branch '2.10'
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 Glyph g; 292 293 if (active) { 294 g = MainWindow.get_current_glyph (); 295 296 if (lsb) { 297 set_metrics (g.get_left_side_bearing ()); 298 } else if (rsb) { 299 set_metrics (g.get_right_side_bearing ()); 300 } 301 } 302 303 this.active = active; 304 } 305 306 public string get_label () { 307 return label; 308 } 309 310 public bool is_vertical () { 311 return vertical; 312 } 313 314 public int get_position_pixel () { 315 if (is_vertical ()) { 316 return Glyph.reverse_path_coordinate_x (pos); 317 } 318 319 return Glyph.reverse_path_coordinate_y (pos) ; 320 } 321 322 public double get_pos () { 323 return pos; 324 } 325 326 public void draw (Context cr, WidgetAllocation allocation) { 327 Glyph g = MainWindow.get_current_glyph (); 328 double p, h, w; 329 double size = (active) ? 8 : 5; 330 Text glyph_metrics; 331 Text line_label; 332 333 if (!visible) { 334 return; 335 } 336 337 cr.save (); 338 cr.set_line_width (1); 339 340 if (dashed) { 341 cr.set_dash ({20, 20}, 0); 342 } 343 344 if (active) { 345 Theme.color (cr, "Highlighted Guide"); 346 } else { 347 cr.set_source_rgba (r, this.g, b, a); 348 } 349 350 // Line 351 if (is_vertical ()) { 352 p = Glyph.reverse_path_coordinate_x (pos); 353 h = g.allocation.height; 354 355 cr.move_to (p, 0); 356 cr.line_to (p, h); 357 cr.stroke (); 358 359 cr.scale (1, 1); 360 361 if (moveable) { 362 cr.new_path (); 363 cr.move_to (p - size, h); 364 cr.line_to (p, h - size); 365 cr.line_to (p + size, h); 366 cr.close_path(); 367 cr.fill (); 368 369 if (get_active ()) { 370 glyph_metrics = new Text (metrics, 17); 371 Theme.text_color (glyph_metrics, "Highlighted Guide"); 372 glyph_metrics.widget_x = p + 10; 373 glyph_metrics.widget_y = h - 25; 374 glyph_metrics.draw (cr); 375 } 376 } 377 378 } else { 379 p = Glyph.reverse_path_coordinate_y (pos); 380 w = g.allocation.width; 381 382 cr.move_to (0, p); 383 cr.line_to (w, p); 384 cr.stroke (); 385 386 if (moveable) { 387 cr.new_path (); 388 cr.move_to (w, p - size); 389 cr.line_to (w - size, p); 390 cr.line_to (w, p + size); 391 cr.close_path(); 392 cr.fill (); 393 } 394 } 395 396 // Label 397 if (get_active ()) { 398 line_label = new Text (get_label (), 19 * MainWindow.units); 399 400 if (is_vertical ()) { 401 line_label.widget_x = p + 8 * MainWindow.units; 402 line_label.widget_y = allocation.height - 55 * MainWindow.units; 403 } else { 404 line_label.widget_x = g.allocation.width 405 - 10 * MainWindow.units 406 - line_label.get_extent (); 407 408 line_label.widget_y = p + 10 * MainWindow.units; 409 } 410 411 if (active) { 412 Theme.text_color (line_label, "Highlighted Guide"); 413 } else { 414 line_label.set_source_rgba (r, this.g, b, a); 415 } 416 417 line_label.draw (cr); 418 } 419 420 cr.restore (); 421 } 422 423 424 425 } 426 427 } 428