The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LabelTool.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/LabelTool.vala.
Parse more style attributes in SVG files
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 17 namespace BirdFont { 18 19 public class LabelTool : Tool { 20 21 public string label { 22 get { 23 return label_text.text; 24 } 25 26 set { 27 label_text.set_text (value); 28 } 29 } 30 31 public string number { get; set; } 32 public bool has_counter { get; set; } 33 public bool has_delete_button { get; set; } 34 public signal void delete_action (LabelTool self); 35 public string data = ""; 36 37 double counter_box_width = 24 * Toolbox.get_scale (); 38 double counter_box_height = 11 * Toolbox.get_scale (); 39 40 Text label_text; 41 42 public LabelTool (string label) { 43 double text_height; 44 45 base (); 46 47 label_text = new Text (); 48 label_text.set_text (label); 49 50 this.label = label; 51 this.number = "-"; 52 53 text_height = 17 * Toolbox.get_scale (); 54 label_text.set_font_size (text_height); 55 56 has_delete_button = false; 57 has_counter = false; 58 59 panel_press_action.connect ((selected, button, tx, ty) => { 60 if (has_delete_button && y <= ty <= y + h && tx >= w - 30 * Toolbox.get_scale ()) { 61 delete_action (this); 62 } 63 }); 64 } 65 66 public override void draw_tool (Context cr, double px, double py) { 67 Text glyph_count; 68 double bgx, bgy; 69 double center_x, center_y; 70 double x = this.x - px; 71 double y = this.y - py; 72 double text_height; 73 double text_width; 74 75 // background 76 if (is_selected ()) { 77 cr.save (); 78 Theme.color (cr, "Menu Background"); 79 cr.rectangle (0, y - 2 * Toolbox.get_scale (), w, h); // labels overlap with 2 pixels 80 cr.fill (); 81 cr.restore (); 82 } 83 84 // tab label 85 cr.save (); 86 87 if (is_selected ()) { 88 Theme.text_color (label_text, "Text Tool Box"); 89 } else { 90 Theme.text_color (label_text, "Text Tool Box"); 91 } 92 93 text_width = Toolbox.allocation_width; 94 95 if (has_counter) { 96 text_width -= counter_box_width - 15; 97 } 98 99 if (has_delete_button) { 100 text_width -= 30; 101 } 102 103 label_text.truncate (text_width); 104 105 label_text.draw_at_top (cr, x, y); 106 cr.restore (); 107 108 // glyph count 109 if (has_counter) { 110 cr.save (); 111 bgx = Toolbox.allocation_width - counter_box_width - 9; 112 bgy = y + 2; 113 114 if (is_selected ()) { 115 Theme.color (cr, "Glyph Count Background 1"); 116 } else { 117 Theme.color (cr, "Glyph Count Background 2"); 118 } 119 120 draw_rounded_rectangle (cr, bgx, bgy, counter_box_width, counter_box_height, 3); 121 cr.fill (); 122 cr.restore (); 123 124 glyph_count = new Text (); 125 glyph_count.set_text (@"$(this.number)"); 126 text_height = 12; 127 128 glyph_count.set_font_size (text_height); 129 center_x = bgx + (counter_box_width / 2.0 - glyph_count.get_extent () / 2.0); 130 center_y = bgy + (counter_box_height / 2.0 + 5); 131 132 if (is_selected ()) { 133 Theme.text_color (glyph_count, "Text Foreground"); 134 } else { 135 Theme.text_color (glyph_count, "Text Foreground"); 136 } 137 138 glyph_count.set_font_size (text_height); 139 glyph_count.draw_at_baseline (cr, center_x, center_y); 140 } 141 142 if (has_delete_button) { 143 cr.save (); 144 Theme.color (cr, "Text Foreground"); 145 cr.set_line_width (1); 146 cr.move_to (w - 20, y + h / 2 - 2.5 - 2); 147 cr.line_to (w - 25, y + h / 2 + 2.5 - 2); 148 cr.move_to (w - 20, y + h / 2 + 2.5 - 2); 149 cr.line_to (w - 25, y + h / 2 - 2.5 - 2); 150 cr.stroke (); 151 cr.restore (); 152 } 153 } 154 } 155 156 } 157