The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

KerningTools.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/KerningTools.vala.
Remove old font size in kerning tab
1 /* 2 Copyright (C) 2013 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 KerningTools : ToolCollection { 21 public static Gee.ArrayList<Expander> expanders; 22 public static int next_class = 0; 23 public static Expander classes; 24 25 public static double font_size = 1; 26 public static ZoomBar zoom_bar; 27 28 public KerningTools () { 29 init (); 30 } 31 32 public static void init () { 33 Expander kerning_tools = new Expander (t_("Kerning Tools")); 34 classes = new Expander (); 35 expanders = new Gee.ArrayList<Expander> (); 36 37 Expander font_name = new Expander (); 38 font_name.add_tool (new FontName ()); 39 font_name.draw_separator = false; 40 41 Expander zoom_expander = new Expander (t_("Font Size")); 42 43 zoom_bar = new ZoomBar (); 44 zoom_bar.new_zoom.connect ((z) => { 45 font_size = 3 * z; 46 47 if (font_size < 0.1) { 48 font_size = 0.1; 49 } 50 51 GlyphCanvas.redraw (); 52 }); 53 zoom_expander.add_tool (zoom_bar); 54 55 Tool new_kerning_class = new Tool ("kerning_class", t_("Create new kerning class.")); 56 new_kerning_class.select_action.connect ((self) => { 57 classes.add_tool (new KerningRange (@"Kerning class $(++next_class)")); 58 Toolbox.redraw_tool_box (); 59 }); 60 kerning_tools.add_tool (new_kerning_class); 61 62 Tool text_kerning = new Tool ("kerning_text_input", t_("Use text input to enter kerning values.")); 63 text_kerning.select_action.connect ((self) => { 64 KerningDisplay d = MainWindow.get_kerning_display (); 65 d.set_kerning_by_text (); 66 }); 67 kerning_tools.add_tool (text_kerning); 68 69 Tool insert_last = new Tool ("insert_last_glyph", t_("Insert glyph from overview")); 70 insert_last.select_action.connect ((self) => { 71 KerningDisplay d = MainWindow.get_kerning_display (); 72 GlyphSelection gs = new GlyphSelection (); 73 74 gs.selected_glyph.connect ((gc) => { 75 d.inser_glyph (gc.get_current ()); 76 MainWindow.get_tab_bar ().select_tab_name ("Kerning"); 77 }); 78 79 GlyphCanvas.set_display (gs); 80 }); 81 kerning_tools.add_tool (insert_last); 82 83 Tool insert_unicode = new Tool ("insert_unichar", t_("Insert character by unicode value")); 84 insert_unicode.select_action.connect ((self) => { 85 KerningDisplay d = MainWindow.get_kerning_display (); 86 d.insert_unichar (); 87 }); 88 kerning_tools.add_tool (insert_unicode); 89 90 SpinButton font_size1 = new SpinButton ("kerning_font_size_one", t_("Font size")); 91 92 font_size1.set_max (9); 93 font_size1.set_min (0.1); 94 font_size1.set_value_round (0.5); 95 96 if (Preferences.get ("kerning_font_size_one_settings") != "") { 97 font_size1.set_value (Preferences.get ("kerning_font_size_one_settings")); 98 } 99 100 font_size1.select_action.connect ((self) => { 101 Glyph g = MainWindow.get_current_glyph (); 102 KerningTools.font_size = font_size1.get_value (); 103 g.update_view (); 104 }); 105 106 font_size1.new_value_action.connect ((self) => { 107 Glyph g = MainWindow.get_current_glyph (); 108 KerningTools.font_size = font_size1.get_value (); 109 g.update_view (); 110 Preferences.set ("kerning_font_size_one_settings", font_size1.get_display_value ()); 111 }); 112 113 kerning_tools.add_tool (font_size1); 114 115 SpinButton font_size2 = new SpinButton ("kerning_font_size_two", t_("Font size ")); 116 117 font_size2.set_max (9); 118 font_size2.set_min (0.1); 119 font_size2.set_value_round (1); 120 121 if (Preferences.get ("kerning_font_size_two_settings") != "") { 122 font_size2.set_value (Preferences.get ("kerning_font_size_two_settings")); 123 } 124 125 font_size2.new_value_action.connect ((self) => { 126 Glyph g = MainWindow.get_current_glyph (); 127 KerningTools.font_size = font_size2.get_value (); 128 g.update_view (); 129 Preferences.set ("kerning_font_size_two_settings", font_size2.get_display_value ()); 130 }); 131 132 font_size2.select_action.connect ((self) => { 133 Glyph g = MainWindow.get_current_glyph (); 134 KerningTools.font_size = font_size2.get_value (); 135 g.update_view (); 136 }); 137 kerning_tools.add_tool (font_size2); 138 139 kerning_tools.set_persistent (false); 140 kerning_tools.set_unique (false); 141 142 classes.set_persistent (true); 143 classes.set_unique (true); 144 145 expanders.add (font_name); 146 expanders.add (zoom_expander); 147 expanders.add (kerning_tools); 148 expanders.add (classes); 149 } 150 151 public static void add_unique_class (KerningRange kerning_class) { 152 KerningRange k; 153 154 if (is_null (classes)) { // FIXME: export without tools 155 init (); 156 } 157 158 foreach (Tool t in classes.tool) { 159 if (!(t is KerningRange)) { 160 warning ("Tool is not kerning range"); 161 return; 162 } 163 164 k = (KerningRange) t; 165 if (k.glyph_range.get_all_ranges () == kerning_class.glyph_range.get_all_ranges ()) { 166 return; 167 } 168 } 169 170 classes.add_tool (kerning_class); 171 } 172 173 public static GlyphRange get_kerning_class (int index) { 174 if (likely (0 <= index < classes.tool.size)) { 175 return ((KerningRange) classes.tool.get (index)).glyph_range; 176 } else { 177 warning ("Index out of bounds."); 178 return new GlyphRange (); 179 } 180 } 181 182 public static void update_kerning_classes () { 183 KerningClasses k = BirdFont.get_current_font ().get_kerning_classes (); 184 KerningRange kr; 185 GlyphRange r; 186 int i; 187 188 remove_all_kerning_classes (); 189 190 for (i = 0; i < k.classes_first.size; i++) { 191 r = k.classes_first.get (i); 192 if (r.is_class ()) { 193 kr = new KerningRange (); 194 kr.set_ranges (r.get_all_ranges ()); 195 add_unique_class (kr); 196 } 197 198 r = k.classes_last.get (i); 199 if (r.is_class ()) { 200 kr = new KerningRange (); 201 kr.set_ranges (r.get_all_ranges ()); 202 add_unique_class (kr); 203 } 204 } 205 } 206 207 private static void remove_all_kerning_classes () { 208 if (is_null (classes)) { // FIXME: export without tools 209 init (); 210 } 211 212 classes.tool.clear (); 213 214 if (!is_null (MainWindow.get_toolbox ())) { 215 MainWindow.get_toolbox ().update_expanders (); 216 } 217 } 218 219 public static void remove_empty_classes () { 220 KerningRange kr; 221 int i; 222 223 if (classes.tool.size == 0) { 224 return; 225 } 226 227 i = 0; 228 foreach (Tool t in classes.tool) { 229 return_if_fail (t is KerningRange); 230 231 kr = (KerningRange) t; 232 if (kr.glyph_range.is_empty ()) { 233 classes.tool.remove_at (i); 234 remove_empty_classes (); 235 Toolbox.redraw_tool_box (); 236 return; 237 } 238 239 i++; 240 } 241 } 242 243 public override Gee.ArrayList<Expander> get_expanders () { 244 return expanders; 245 } 246 247 public static void update_spacing_classes () { 248 KerningRange kr; 249 250 if (classes.tool.size == 0) { 251 return; 252 } 253 254 foreach (Tool t in classes.tool) { 255 return_if_fail (t is KerningRange); 256 257 kr = (KerningRange) t; 258 kr.update_spacing_class (); 259 } 260 } 261 } 262 263 } 264