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 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 return false; 184 }); 185 idle.attach (null); 186 }); 187 glyph_expander.add_tool (curve_orientation); 188 189 Tool search_glyph = new Tool ("search", t_("Search")); 190 search_glyph.select_action.connect (search_for_glyph); 191 glyph_expander.add_tool (search_glyph); 192 193 Tool convert_to_ttf = new Tool ("svg_to_birdfont_overview", t_("Convert SVG file to monochrome glyph")); 194 convert_to_ttf.set_icon ("svg_to_birdfont"); 195 196 convert_to_ttf.select_action.connect ((self) => { 197 process_transform (Transform.SVG_TO_TTF); 198 self.set_selected (false); 199 BirdFont.get_current_font ().touch (); 200 }); 201 202 convert_to_ttf.selected = false; 203 convert_to_ttf.set_persistent (false); 204 glyph_expander.add_tool (convert_to_ttf); 205 206 SpinButton master_size; 207 current_master_size = 0; 208 master_size = new SpinButton ("master_size", t_("Master Size")); /// Master refers to a glyph master in a multi-master font. 209 master_size.set_big_number (false); 210 master_size.set_int_value ("0.000"); 211 master_size.set_int_step (1); 212 master_size.set_min (-1); 213 master_size.set_max (1); 214 master_size.show_icon (true); 215 master_size.set_persistent (false); 216 master_size.new_value_action.connect ((self) => { 217 current_master_size = self.get_value (); 218 MainWindow.get_overview ().update_item_list (); 219 GlyphCanvas.redraw (); 220 }); 221 multi_master.add_tool (master_size); 222 223 Tool create_new_master = new Tool ("new_master", t_("Create new master font")); /// Master is a master in a multi-master font. 224 create_new_master.select_action.connect (create_master); 225 multi_master.add_tool (create_new_master); 226 227 expanders.add (font_name); 228 expanders.add (zoom_expander); 229 expanders.add (character_sets); 230 expanders.add (transform_slant_expander); 231 expanders.add (transform_size_expander); 232 expanders.add (glyph_expander); 233 234 if (BirdFont.has_argument ("--test")) { 235 expanders.add (multi_master); 236 } 237 } 238 239 void create_master () { 240 Font font = BirdFont.get_current_font (); 241 int i = 0; 242 GlyphCollection glyph_collection; 243 GlyphCollection? gc = font.get_glyph_collection_index (i); 244 Glyph g; 245 246 while (gc != null) { 247 glyph_collection = (!) gc; 248 249 // FIXME: MASTER NAME 250 GlyphMaster master = new GlyphMaster.for_id("Master 2"); 251 g = glyph_collection.get_interpolated (current_master_size); 252 master.add_glyph(g); 253 glyph_collection.add_master (master); 254 glyph_collection.set_selected_master (master); 255 256 i++; 257 gc = font.get_glyph_collection_index (i); 258 } 259 260 MainWindow.get_overview ().update_item_list (); 261 GlyphCanvas.redraw (); 262 } 263 264 void fix_curve_orientation () { 265 OverView o; 266 Glyph g; 267 OverView.OverViewUndoItem ui; 268 269 o = get_overview (); 270 ui = new OverView.OverViewUndoItem (); 271 272 Font f = BirdFont.get_current_font (); 273 ui.alternate_sets = f.alternates.copy (); 274 275 foreach (GlyphCollection gc in o.selected_items) { 276 if (gc.length () > 0) { 277 g = gc.get_current (); 278 ui.glyphs.add (gc.copy_deep ()); 279 g.add_help_lines (); 280 g.fix_curve_orientation (); 281 } 282 } 283 284 o.undo_items.add (ui); 285 GlyphCanvas.redraw (); 286 } 287 288 public void search_for_glyph (Tool tool) { 289 tool.set_selected (false); 290 OverView.search (); 291 } 292 293 public void add_new_alternate (Tool tool) { 294 OverView o = MainWindow.get_overview (); 295 OverViewItem oi = o.selected_item; 296 GlyphCollection? gc; 297 298 tool.set_selected (false); 299 300 gc = (!) oi.glyphs; 301 302 if (oi.glyphs != null && ((!) oi.glyphs).is_unassigned ()) { 303 gc = null; 304 } 305 306 MainWindow.tabs.add_tab (new OtfFeatureTable (gc)); 307 } 308 309 public void process_transform (Transform transform) { 310 TransformTask task = new TransformTask (transform); 311 MainWindow.run_blocking_task (task); 312 } 313 314 public static OverView get_overview () { 315 FontDisplay fd = MainWindow.get_current_display (); 316 317 if (fd is OverView || fd is GlyphSelection) { 318 return (OverView) fd; 319 } 320 321 warning ("Current tab is not overview."); 322 323 return new OverView (); 324 } 325 326 public static void show_all_available_characters () { 327 all_glyphs.select_action (all_glyphs); 328 } 329 330 public static void update_overview_characterset (OverView? tab = null) { 331 GlyphRange gr; 332 uint size; 333 OverView overview; 334 Font font; 335 336 // All characters including .notdef 337 font = BirdFont.get_current_font (); 338 size = font.length (); 339 340 if (!font.has_glyph (".notdef")) { 341 size++; 342 } 343 344 all_glyphs.number = get_display_value (size); 345 346 // Default 347 gr = new GlyphRange (); 348 DefaultCharacterSet.use_default_range (gr); 349 size = gr.get_length (); 350 default_glyphs.number = get_display_value (size); 351 352 // Unicode 353 gr = new GlyphRange (); 354 DefaultCharacterSet.use_full_unicode_range (gr); 355 size = gr.get_length (); 356 unicode.number = get_display_value (size); 357 358 overview = (tab == null) ? MainWindow.get_overview () : (!) tab; 359 360 // set selected item 361 all_glyphs.set_selected (false); 362 default_glyphs.set_selected (false); 363 unicode.set_selected (false); 364 365 if (overview.all_available) { 366 all_glyphs.set_selected (true); 367 } else if (overview.glyph_range.name == "Default") { 368 default_glyphs.set_selected (true); 369 } else if (overview.glyph_range.name == "Unicode") { 370 unicode.set_selected (true); 371 } 372 373 Toolbox.redraw_tool_box (); 374 } 375 376 static string get_display_value (uint size) { 377 double k; 378 string display_size; 379 380 if (size >= 1000) { 381 k = size / 1000.0; 382 size = (uint) Math.rint (k); 383 display_size = @"$(size)k"; 384 } else { 385 display_size = @"$(size)"; 386 } 387 388 return display_size; 389 } 390 391 public override Gee.ArrayList<Expander> get_expanders () { 392 return expanders; 393 } 394 395 public override Gee.ArrayList<string> get_displays () { 396 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 397 d.add ("Overview"); 398 return d; 399 } 400 } 401 402 } 403