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.
Merge branch 'master' of github.com:johanmattssonm/birdfont
1 /* 2 Copyright (C) 2014 2015 2016 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 enum Transform { 20 SLANT, 21 SIZE, 22 SVG_TO_TTF 23 } 24 25 public class OverviewTools : ToolCollection { 26 27 static LabelTool all_glyphs; 28 static LabelTool default_glyphs; 29 static LabelTool unicode; 30 31 static Gee.ArrayList<LabelTool> custom_character_sets; 32 33 public static Gee.ArrayList<Expander> expanders; 34 public ZoomBar zoom_bar; 35 36 public static Expander zoom_expander; 37 38 public static SpinButton skew; 39 public static SpinButton resize; 40 41 public static double current_master_size; 42 43 public OverviewTools () { 44 Expander font_name = new Expander (); 45 Expander character_sets = new Expander (t_("Character Sets")); 46 Expander zoom_expander = new Expander (t_("Zoom")); 47 Expander transform_slant_expander = new Expander (t_("Transform")); 48 Expander transform_size_expander = new Expander (); 49 Expander glyph_expander = new Expander (t_("Glyph")); 50 Expander multi_master = new Expander (t_("Multi-Master")); 51 52 expanders = new Gee.ArrayList<Expander> (); 53 custom_character_sets = new Gee.ArrayList<LabelTool> (); 54 55 font_name.add_tool (new FontName ()); 56 57 zoom_bar = new ZoomBar (); 58 zoom_bar.new_zoom.connect ((z) => { 59 get_overview ().set_zoom (z); 60 }); 61 zoom_expander.add_tool (zoom_bar); 62 63 all_glyphs = new LabelTool (t_("All Glyphs")); 64 all_glyphs.has_counter = true; 65 all_glyphs.select_action.connect ((self) => { 66 OverView overview = get_overview (); 67 overview.display_all_available_glyphs (); 68 update_overview_characterset (overview); 69 FontDisplay.dirty_scrollbar = true; 70 }); 71 character_sets.add_tool (all_glyphs); 72 73 default_glyphs = new LabelTool (t_("Default")); 74 default_glyphs.has_counter = true; 75 default_glyphs.select_action.connect ((self) => { 76 OverView overview = get_overview (); 77 GlyphRange gr = new GlyphRange (); 78 DefaultCharacterSet.use_default_range (gr); 79 overview.set_current_glyph_range (gr); 80 update_overview_characterset (overview); 81 FontDisplay.dirty_scrollbar = true; 82 }); 83 character_sets.add_tool (default_glyphs); 84 85 unicode = new LabelTool (t_("Unicode")); 86 unicode.has_counter = true; 87 unicode.select_action.connect ((self) => { 88 OverView overview = get_overview (); 89 GlyphRange gr = new GlyphRange (); 90 DefaultCharacterSet.use_full_unicode_range (gr); 91 overview.set_current_glyph_range (gr); 92 update_overview_characterset (overview); 93 FontDisplay.dirty_scrollbar = true; 94 }); 95 character_sets.add_tool (unicode); 96 97 character_sets.set_persistent (true); 98 character_sets.set_unique (false); 99 100 skew = new SpinButton ("skew_overview", t_("Skew")); 101 skew.set_big_number (true); 102 skew.set_int_value ("0.000"); 103 104 FontSettings settings = BirdFont.get_current_font ().settings; 105 string? skew_value = settings.get_setting ("skew_overview"); 106 107 if (skew_value != null) { 108 skew.set_int_value ((!) skew_value); 109 } 110 111 skew.set_int_step (1); 112 skew.set_min (-100); 113 skew.set_max (100); 114 skew.show_icon (true); 115 skew.set_persistent (false); 116 transform_slant_expander.add_tool (skew); 117 118 Tool transform_slant = new Tool ("transform_slant", t_("Transform")); 119 transform_slant.set_icon ("transform"); 120 transform_slant.select_action.connect ((self) => { 121 FontSettings fs = BirdFont.get_current_font ().settings; 122 fs.set_setting ("skew_overview", @"$(skew.get_value ())"); 123 transform_slant.selected = false; 124 125 process_transform (Transform.SLANT); 126 127 BirdFont.get_current_font ().touch (); 128 }); 129 transform_slant.selected = false; 130 transform_slant.set_persistent (false); 131 transform_slant_expander.add_tool (transform_slant); 132 133 resize = new SpinButton ("resize_overview", t_("Resize")); 134 resize.set_big_number (true); 135 resize.set_int_value ("100.0"); 136 137 string? resize_value = settings.get_setting ("resize_overview"); 138 139 if (resize_value != null) { 140 resize.set_int_value ((!) resize_value); 141 142 if (resize.get_value () <= 0) { 143 resize.set_int_value ("100.0"); 144 } 145 } 146 147 resize.set_int_step (1); 148 resize.set_min (0); 149 resize.set_max (300); 150 resize.show_icon (true); 151 resize.set_persistent (false); 152 transform_size_expander.add_tool (resize); 153 154 Tool transform_size = new Tool ("transform_size", t_("Transform")); 155 transform_size.set_icon ("transform"); 156 transform_size.select_action.connect ((self) => { 157 FontSettings fs = BirdFont.get_current_font ().settings; 158 fs.set_setting ("resize_overview", @"$(resize.get_value ())"); 159 transform_size.selected = false; 160 161 process_transform (Transform.SIZE); 162 163 BirdFont.get_current_font ().touch (); 164 }); 165 transform_size.selected = false; 166 transform_size.set_persistent (false); 167 transform_size_expander.add_tool (transform_size); 168 169 Tool alternate = new Tool ("alternate", t_("Create alternate")); 170 alternate.select_action.connect (add_new_alternate); 171 glyph_expander.add_tool (alternate); 172 173 Tool curve_orientation = new Tool ("curve_orientation", t_("Set curve orientation")); 174 curve_orientation.select_action.connect ((self) => { 175 Task t = new Task (fix_curve_orientation); 176 177 MainWindow.run_blocking_task (t); 178 179 IdleSource idle = new IdleSource (); 180 idle.set_callback (() => { 181 self.set_selected (false); 182 BirdFont.get_current_font ().touch (); 183 MainWindow.get_overview ().reset_thumbnails (); 184 GlyphCanvas.redraw (); 185 return false; 186 }); 187 idle.attach (null); 188 }); 189 glyph_expander.add_tool (curve_orientation); 190 191 Tool search_glyph = new Tool ("search", t_("Search")); 192 search_glyph.select_action.connect (search_for_glyph); 193 glyph_expander.add_tool (search_glyph); 194 195 Tool convert_to_ttf = new Tool ("svg_to_birdfont_overview", t_("Convert SVG file to monochrome glyph")); 196 convert_to_ttf.set_icon ("svg_to_birdfont"); 197 198 convert_to_ttf.select_action.connect ((self) => { 199 process_transform (Transform.SVG_TO_TTF); 200 self.set_selected (false); 201 BirdFont.get_current_font ().touch (); 202 }); 203 204 convert_to_ttf.selected = false; 205 convert_to_ttf.set_persistent (false); 206 glyph_expander.add_tool (convert_to_ttf); 207 208 SpinButton master_size; 209 current_master_size = 0; 210 master_size = new SpinButton ("master_size", t_("Master Size")); /// Master refers to a glyph master in a multi-master font. 211 master_size.set_big_number (false); 212 master_size.set_int_value ("0.000"); 213 master_size.set_int_step (1); 214 master_size.set_min (-1); 215 master_size.set_max (1); 216 master_size.show_icon (true); 217 master_size.set_persistent (false); 218 master_size.new_value_action.connect ((self) => { 219 current_master_size = self.get_value (); 220 MainWindow.get_overview ().update_item_list (); 221 GlyphCanvas.redraw (); 222 }); 223 multi_master.add_tool (master_size); 224 225 Tool create_new_master = new Tool ("new_master", t_("Create new master font")); /// Master is a master in a multi-master font. 226 create_new_master.select_action.connect (create_master); 227 multi_master.add_tool (create_new_master); 228 229 expanders.add (font_name); 230 expanders.add (zoom_expander); 231 expanders.add (character_sets); 232 expanders.add (transform_slant_expander); 233 expanders.add (transform_size_expander); 234 expanders.add (glyph_expander); 235 236 if (BirdFont.has_argument ("--test")) { 237 expanders.add (multi_master); 238 } 239 } 240 241 void create_master () { 242 Font font = BirdFont.get_current_font (); 243 int i = 0; 244 GlyphCollection glyph_collection; 245 GlyphCollection? gc = font.get_glyph_collection_index (i); 246 Glyph g; 247 248 while (gc != null) { 249 glyph_collection = (!) gc; 250 251 // FIXME: MASTER NAME 252 GlyphMaster master = new GlyphMaster.for_id("Master 2"); 253 g = glyph_collection.get_interpolated (current_master_size); 254 master.add_glyph(g); 255 glyph_collection.add_master (master); 256 glyph_collection.set_selected_master (master); 257 258 i++; 259 gc = font.get_glyph_collection_index (i); 260 } 261 262 MainWindow.get_overview ().update_item_list (); 263 GlyphCanvas.redraw (); 264 } 265 266 void fix_curve_orientation () { 267 OverView o; 268 Glyph g; 269 OverView.OverViewUndoItem ui; 270 271 o = get_overview (); 272 ui = new OverView.OverViewUndoItem (); 273 274 Font f = BirdFont.get_current_font (); 275 ui.alternate_sets = f.alternates.copy (); 276 277 foreach (GlyphCollection gc in o.selected_items) { 278 if (gc.length () > 0) { 279 g = gc.get_current (); 280 ui.glyphs.add (gc.copy_deep ()); 281 g.add_help_lines (); 282 g.fix_curve_orientation (); 283 } 284 } 285 286 o.undo_items.add (ui); 287 GlyphCanvas.redraw (); 288 } 289 290 public void search_for_glyph (Tool tool) { 291 tool.set_selected (false); 292 OverView.search (); 293 } 294 295 public void add_new_alternate (Tool tool) { 296 OverView o = MainWindow.get_overview (); 297 OverViewItem oi = o.selected_item; 298 GlyphCollection? gc; 299 300 tool.set_selected (false); 301 302 gc = (!) oi.glyphs; 303 304 if (oi.glyphs != null && ((!) oi.glyphs).is_unassigned ()) { 305 gc = null; 306 } 307 308 MainWindow.tabs.add_tab (new OtfFeatureTable (gc)); 309 } 310 311 public void process_transform (Transform transform) { 312 TransformTask task = new TransformTask (transform); 313 MainWindow.run_blocking_task (task); 314 } 315 316 public static OverView get_overview () { 317 FontDisplay fd = MainWindow.get_current_display (); 318 319 if (fd is OverView || fd is GlyphSelection) { 320 return (OverView) fd; 321 } 322 323 warning ("Current tab is not overview."); 324 325 return new OverView (); 326 } 327 328 public static void show_all_available_characters () { 329 all_glyphs.select_action (all_glyphs); 330 } 331 332 public static void update_overview_characterset (OverView? tab = null) { 333 GlyphRange gr; 334 uint size; 335 OverView overview; 336 Font font; 337 338 // All characters including .notdef 339 font = BirdFont.get_current_font (); 340 size = font.length (); 341 342 if (!font.has_glyph (".notdef")) { 343 size++; 344 } 345 346 all_glyphs.number = get_display_value (size); 347 348 // Default 349 gr = new GlyphRange (); 350 DefaultCharacterSet.use_default_range (gr); 351 size = gr.get_length (); 352 default_glyphs.number = get_display_value (size); 353 354 // Unicode 355 gr = new GlyphRange (); 356 DefaultCharacterSet.use_full_unicode_range (gr); 357 size = gr.get_length (); 358 unicode.number = get_display_value (size); 359 360 overview = (tab == null) ? MainWindow.get_overview () : (!) tab; 361 362 // set selected item 363 all_glyphs.set_selected (false); 364 default_glyphs.set_selected (false); 365 unicode.set_selected (false); 366 367 if (overview.all_available) { 368 all_glyphs.set_selected (true); 369 } else if (overview.glyph_range.name == "Default") { 370 default_glyphs.set_selected (true); 371 } else if (overview.glyph_range.name == "Unicode") { 372 unicode.set_selected (true); 373 } 374 375 Toolbox.redraw_tool_box (); 376 } 377 378 static string get_display_value (uint size) { 379 double k; 380 string display_size; 381 382 if (size >= 1000) { 383 k = size / 1000.0; 384 size = (uint) Math.rint (k); 385 display_size = @"$(size)k"; 386 } else { 387 display_size = @"$(size)"; 388 } 389 390 return display_size; 391 } 392 393 public override Gee.ArrayList<Expander> get_expanders () { 394 return expanders; 395 } 396 397 public override Gee.ArrayList<string> get_displays () { 398 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 399 d.add ("Overview"); 400 return d; 401 } 402 } 403 404 } 405