The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

BackgroundSelectionTool.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) 2014 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 BackgroundSelectionTool : CutBackgroundTool { 21 22 public BackgroundSelectionTool () { 23 base ("select_background", t_("Select background")); 24 editor_events = true; 25 persistent = true; 26 self_destination = false; 27 28 new_image.connect (add_new_image); 29 30 select_action.connect ((t) => { 31 DrawingTools.move_canvas.set_selected (false); 32 GlyphCanvas.redraw (); 33 }); 34 35 draw_action.connect ((self, cr, glyph) => { 36 double x, y, w, h; 37 BackgroundImage bg; 38 Text label = new Text (); 39 double tx, ty, font_height; 40 41 if (glyph.get_background_image () == null) { 42 // No image to draw. 43 return; 44 } 45 46 // draw a border around each selection 47 bg = (!) glyph.get_background_image (); 48 foreach (BackgroundSelection bs in bg.selections) { 49 x = Glyph.reverse_path_coordinate_x (bs.x); 50 y = Glyph.reverse_path_coordinate_y (bs.y); 51 w = Glyph.reverse_path_coordinate_x (bs.x + bs.w) - x; 52 h = Glyph.reverse_path_coordinate_y (bs.y + bs.h) - y; 53 54 cr.save (); 55 cr.set_line_width (2.0); 56 57 if (bs.assigned_glyph != null) { 58 cr.set_source_rgba (237 / 255.0, 67 / 255.0, 0, 1); 59 } else { 60 cr.set_source_rgba (132 / 255.0, 132 / 255.0, 132 / 255.0, 1); 61 } 62 63 cr.rectangle (x, y, w, h); 64 cr.stroke (); 65 66 cr.arc (x + w, y + h, 9.0, 0, 2 * PI); 67 cr.fill (); 68 69 if (bs.assigned_glyph != null) { 70 font_height = 18; 71 label.set_text ((!) bs.assigned_glyph); 72 label.set_font_size (font_height); 73 tx = x + w - label.get_width () / 2.0; 74 ty = y + h + label.get_acender () / 2.0 - label.get_decender (); 75 label.set_font_size (font_height); 76 label.draw_at_baseline (cr, tx, ty); 77 } 78 79 cr.restore (); 80 } 81 }); 82 } 83 84 public void add_new_image (BackgroundImage image, 85 double x, double y, double w, double h) { 86 87 BackgroundImage bg; 88 BackgroundSelection selection; 89 90 if (MainWindow.get_current_glyph ().get_background_image () == null) { 91 warning ("No image"); 92 return; 93 } 94 95 bg = (!) MainWindow.get_current_glyph ().get_background_image (); 96 selection = new BackgroundSelection (image, bg, x, y, w, h); 97 98 bg.add_selection (selection); 99 Toolbox.background_tools.add_part (selection); 100 } 101 } 102 103 } 104