.
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 if (label.has_character ((!) bs.assigned_glyph)) {
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
80 cr.restore ();
81 }
82 });
83 }
84
85 public void add_new_image (BackgroundImage image,
86 double x, double y, double w, double h) {
87
88 BackgroundImage bg;
89 BackgroundSelection selection;
90
91 if (MainWindow.get_current_glyph ().get_background_image () == null) {
92 warning ("No image");
93 return;
94 }
95
96 bg = (!) MainWindow.get_current_glyph ().get_background_image ();
97 selection = new BackgroundSelection (image, bg, x, y, w, h);
98
99 bg.add_selection (selection);
100 Toolbox.background_tools.add_part (selection);
101 }
102 }
103
104 }
105