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

Revisions

View the latest version of libbirdfont/BackgroundSelectionTool.vala.
Merge branch '2.14' of github.com:johanmattssonm/birdfont into 2.14
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 GlyphCanvas.redraw (); 32 }); 33 34 draw_action.connect ((self, cr, glyph) => { 35 double x, y, w, h; 36 BackgroundImage bg; 37 Text label = new Text (); 38 double tx, ty, font_height; 39 40 if (glyph.get_background_image () == null) { 41 // No image to draw. 42 return; 43 } 44 45 // draw a border around each selection 46 bg = (!) glyph.get_background_image (); 47 foreach (BackgroundSelection bs in bg.selections) { 48 x = Glyph.reverse_path_coordinate_x (bs.x); 49 y = Glyph.reverse_path_coordinate_y (bs.y); 50 w = Glyph.reverse_path_coordinate_x (bs.x + bs.w) - x; 51 h = Glyph.reverse_path_coordinate_y (bs.y + bs.h) - y; 52 53 cr.save (); 54 cr.set_line_width (2.0); 55 56 if (bs.assigned_glyph != null) { 57 cr.set_source_rgba (237 / 255.0, 67 / 255.0, 0, 1); 58 } else { 59 cr.set_source_rgba (132 / 255.0, 132 / 255.0, 132 / 255.0, 1); 60 } 61 62 cr.rectangle (x, y, w, h); 63 cr.stroke (); 64 65 cr.arc (x + w, y + h, 9.0, 0, 2 * PI); 66 cr.fill (); 67 68 if (bs.assigned_glyph != null) { 69 font_height = 18; 70 label.set_text ((!) bs.assigned_glyph); 71 label.set_font_size (font_height); 72 tx = x + w - label.get_width () / 2.0; 73 ty = y + h + label.get_acender () / 2.0 - label.get_decender (); 74 label.set_font_size (font_height); 75 label.draw_at_baseline (cr, tx, ty); 76 } 77 78 cr.restore (); 79 } 80 }); 81 } 82 83 public void add_new_image (BackgroundImage image, 84 double x, double y, double w, double h) { 85 86 BackgroundImage bg; 87 BackgroundSelection selection; 88 89 if (MainWindow.get_current_glyph ().get_background_image () == null) { 90 warning ("No image"); 91 return; 92 } 93 94 bg = (!) MainWindow.get_current_glyph ().get_background_image (); 95 selection = new BackgroundSelection (image, bg, x, y, w, h); 96 97 bg.add_selection (selection); 98 Toolbox.background_tools.add_part (selection); 99 } 100 } 101 102 } 103