The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OverviewTools.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/OverviewTools.vala.
Fix zero length handles
1 /* 2 Copyright (C) 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 public class OverviewTools : ToolCollection { 20 21 static LabelTool all_glyphs; 22 static LabelTool default_glyphs; 23 static LabelTool unicode; 24 25 static Gee.ArrayList<LabelTool> custom_character_sets; 26 27 public static Gee.ArrayList<Expander> expanders; 28 public ZoomBar zoom_bar; 29 30 public static Expander zoom_expander; 31 32 public static SpinButton skew; 33 public static SpinButton resize; 34 35 public OverviewTools () { 36 Expander font_name = new Expander (); 37 Expander character_sets = new Expander (t_("Character Sets")); 38 Expander zoom_expander = new Expander (t_("Zoom")); 39 Expander transform_expander = new Expander (t_("Transform")); 40 Expander glyph_expander = new Expander (t_("Glyph")); 41 42 expanders = new Gee.ArrayList<Expander> (); 43 custom_character_sets = new Gee.ArrayList<LabelTool> (); 44 45 font_name.add_tool (new FontName ()); 46 47 zoom_bar = new ZoomBar (); 48 zoom_bar.new_zoom.connect ((z) => { 49 get_overview ().set_zoom (z); 50 }); 51 zoom_expander.add_tool (zoom_bar); 52 53 all_glyphs = new LabelTool (t_("All Glyphs")); 54 all_glyphs.has_counter = true; 55 all_glyphs.select_action.connect ((self) => { 56 OverView overview = get_overview (); 57 overview.display_all_available_glyphs (); 58 update_overview_characterset (overview); 59 FontDisplay.dirty_scrollbar = true; 60 }); 61 character_sets.add_tool (all_glyphs); 62 63 default_glyphs = new LabelTool (t_("Default")); 64 default_glyphs.has_counter = true; 65 default_glyphs.select_action.connect ((self) => { 66 OverView overview = get_overview (); 67 GlyphRange gr = new GlyphRange (); 68 DefaultCharacterSet.use_default_range (gr); 69 overview.set_current_glyph_range (gr); 70 update_overview_characterset (overview); 71 FontDisplay.dirty_scrollbar = true; 72 }); 73 character_sets.add_tool (default_glyphs); 74 75 unicode = new LabelTool (t_("Unicode")); 76 unicode.has_counter = true; 77 unicode.select_action.connect ((self) => { 78 OverView overview = get_overview (); 79 GlyphRange gr = new GlyphRange (); 80 DefaultCharacterSet.use_full_unicode_range (gr); 81 overview.set_current_glyph_range (gr); 82 update_overview_characterset (overview); 83 FontDisplay.dirty_scrollbar = true; 84 }); 85 character_sets.add_tool (unicode); 86 87 character_sets.set_persistent (true); 88 character_sets.set_unique (false); 89 90 skew = new SpinButton ("skew_overview", t_("Skew")); 91 skew.set_big_number (true); 92 skew.set_int_value ("0.000"); 93 skew.set_int_step (1); 94 skew.set_min (-100); 95 skew.set_max (100); 96 skew.show_icon (true); 97 skew.set_persistent (false); 98 skew.new_value_action.connect ((self) => { 99 resize.set_value_round (100, false, false); 100 }); 101 transform_expander.add_tool (skew); 102 103 resize = new SpinButton ("resize_overview", t_("Resize")); 104 resize.set_big_number (true); 105 resize.set_int_value ("0.000"); 106 resize.set_int_step (1); 107 resize.set_min (0); 108 resize.set_max (300); 109 resize.show_icon (true); 110 resize.set_persistent (false); 111 resize.new_value_action.connect ((self) => { 112 skew.set_value_round (0, false, false); 113 }); 114 transform_expander.add_tool (resize); 115 116 Tool transform = new Tool ("transform", t_("Transform")); 117 transform.select_action.connect ((self) => { 118 FontSettings fs = BirdFont.get_current_font ().settings; 119 120 fs.set_setting ("skew_overview", @"$(skew.get_value ())"); 121 transform.selected = false; 122 123 process_transform (); 124 125 BirdFont.get_current_font ().touch (); 126 }); 127 transform.selected = false; 128 transform.set_persistent (false); 129 transform_expander.add_tool (transform); 130 131 Tool alternate = new Tool ("alternate", t_("Create alternate")); 132 alternate.select_action.connect (add_new_alternate); 133 glyph_expander.add_tool (alternate); 134 135 Tool curve_orientation = new Tool ("curve_orientation", t_("Set curve orientation")); 136 curve_orientation.select_action.connect ((self) => { 137 Task t = new Task (fix_curve_orientation); 138 139 MainWindow.run_blocking_task (t); 140 141 IdleSource idle = new IdleSource (); 142 idle.set_callback (() => { 143 self.set_selected (false); 144 BirdFont.get_current_font ().touch (); 145 return false; 146 }); 147 idle.attach (null); 148 }); 149 glyph_expander.add_tool (curve_orientation); 150 151 expanders.add (font_name); 152 expanders.add (zoom_expander); 153 expanders.add (character_sets); 154 expanders.add (transform_expander); 155 expanders.add (glyph_expander); 156 } 157 158 void fix_curve_orientation () { 159 OverView o; 160 Glyph g; 161 OverView.OverViewUndoItem ui; 162 163 o = get_overview (); 164 ui = new OverView.OverViewUndoItem (); 165 166 Font f = BirdFont.get_current_font (); 167 ui.alternate_sets = f.alternates.copy (); 168 169 foreach (GlyphCollection gc in o.selected_items) { 170 if (gc.length () > 0) { 171 g = gc.get_current (); 172 ui.glyphs.add (gc.copy_deep ()); 173 g.add_help_lines (); 174 g.fix_curve_orientation (); 175 } 176 } 177 178 o.undo_items.add (ui); 179 GlyphCanvas.redraw (); 180 } 181 182 public void add_new_alternate (Tool tool) { 183 OverView o = MainWindow.get_overview (); 184 OverViewItem oi = o.selected_item; 185 GlyphCollection gc; 186 187 tool.set_selected (false); 188 189 if (oi.glyphs == null || ((!) oi.glyphs).is_unassigned ()) { 190 return; 191 } 192 193 gc = (!) oi.glyphs; 194 MainWindow.tabs.add_tab (new OtfFeatureTable (gc)); 195 } 196 197 public void process_transform () { 198 OverView o; 199 Glyph g; 200 OverView.OverViewUndoItem ui; 201 202 o = get_overview (); 203 ui = new OverView.OverViewUndoItem (); 204 205 Font f = BirdFont.get_current_font (); 206 ui.alternate_sets = f.alternates.copy (); 207 208 foreach (GlyphCollection gc in o.selected_items) { 209 if (gc.length () > 0) { 210 g = gc.get_current (); 211 ui.glyphs.add (gc.copy_deep ()); 212 g.add_help_lines (); 213 214 if (skew.get_value () != 0) { 215 DrawingTools.resize_tool.skew_glyph (g, -skew.get_value (), 0, false); 216 } 217 218 if (resize.get_value () != 100) { 219 double scale = resize.get_value () / 100; 220 DrawingTools.resize_tool.resize_glyph (g, scale, false); 221 } 222 } 223 } 224 225 o.undo_items.add (ui); 226 GlyphCanvas.redraw (); 227 } 228 229 public OverView get_overview () { 230 FontDisplay fd = MainWindow.get_current_display (); 231 232 if (fd is OverView || fd is GlyphSelection) { 233 return (OverView) fd; 234 } 235 236 warning ("Current tab is not overview."); 237 238 return new OverView (); 239 } 240 241 public static void show_all_available_characters () { 242 all_glyphs.select_action (all_glyphs); 243 } 244 245 public static void update_overview_characterset (OverView? tab = null) { 246 GlyphRange gr; 247 uint size; 248 OverView overview; 249 250 // All characters 251 size = BirdFont.get_current_font ().length (); 252 all_glyphs.number = get_display_value (size); 253 254 // Default 255 gr = new GlyphRange (); 256 DefaultCharacterSet.use_default_range (gr); 257 size = gr.get_length (); 258 default_glyphs.number = get_display_value (size); 259 260 // Unicode 261 gr = new GlyphRange (); 262 DefaultCharacterSet.use_full_unicode_range (gr); 263 size = gr.get_length (); 264 unicode.number = get_display_value (size); 265 266 overview = (tab == null) ? MainWindow.get_overview () : (!) tab; 267 268 // set selected item 269 all_glyphs.set_selected (false); 270 default_glyphs.set_selected (false); 271 unicode.set_selected (false); 272 273 if (overview.all_available) { 274 all_glyphs.set_selected (true); 275 } else if (overview.glyph_range.name == "Default") { 276 default_glyphs.set_selected (true); 277 } else if (overview.glyph_range.name == "Unicode") { 278 unicode.set_selected (true); 279 } 280 281 Toolbox.redraw_tool_box (); 282 } 283 284 static string get_display_value (uint size) { 285 double k; 286 string display_size; 287 288 if (size >= 1000) { 289 k = size / 1000.0; 290 size = (uint) Math.rint (k); 291 display_size = @"$(size)k"; 292 } else { 293 display_size = @"$(size)"; 294 } 295 296 return display_size; 297 } 298 299 public override Gee.ArrayList<Expander> get_expanders () { 300 return expanders; 301 } 302 303 public override Gee.ArrayList<string> get_displays () { 304 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 305 d.add ("Overview"); 306 return d; 307 } 308 } 309 310 } 311