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.
Store font size in kerning display
1 /* 2 Copyright (C) 2013 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 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 static Tool previous_kerning_string; 29 public static Tool next_kerning_string; 30 31 public KerningTools () { 32 init (); 33 } 34 35 public static void init () { 36 Expander kerning_tools = new Expander (t_("Kerning Tools")); 37 classes = new Expander (); 38 expanders = new Gee.ArrayList<Expander> (); 39 40 Expander font_name = new Expander (); 41 font_name.add_tool (new FontName ()); 42 43 Expander zoom_expander = new Expander (t_("Font Size")); 44 45 zoom_bar = new ZoomBar (); 46 zoom_bar.new_zoom.connect ((z) => { 47 Font f; 48 49 font_size = 3 * z; 50 51 if (font_size < 0.1) { 52 font_size = 0.1; 53 } 54 55 f = BirdFont.get_current_font (); 56 f.settings.set_setting ("kerning_zoom", @"$z"); 57 58 GlyphCanvas.redraw (); 59 }); 60 zoom_expander.add_tool (zoom_bar); 61 62 Tool new_kerning_class = new Tool ("kerning_class", t_("Create new kerning class.")); 63 new_kerning_class.select_action.connect ((self) => { 64 Font font = BirdFont.get_current_font (); 65 string label = t_("Kerning class"); 66 KerningRange kr = new KerningRange (font, @"$label $(++next_class)"); 67 classes.add_tool (kr); 68 classes.redraw (); 69 }); 70 kerning_tools.add_tool (new_kerning_class); 71 72 Tool text_kerning = new Tool ("kerning_text_input", t_("Use text input to enter kerning values.")); 73 text_kerning.select_action.connect ((self) => { 74 KerningDisplay d = MainWindow.get_kerning_display (); 75 d.set_kerning_by_text (); 76 }); 77 kerning_tools.add_tool (text_kerning); 78 79 Tool insert_last = new Tool ("insert_glyph_from_overview", t_("Insert glyph from overview")); 80 insert_last.select_action.connect ((self) => { 81 KerningDisplay d = MainWindow.get_kerning_display (); 82 GlyphSelection gs = new GlyphSelection (); 83 84 gs.selected_glyph.connect ((gc) => { 85 d.inser_glyph (gc.get_current ()); 86 MainWindow.get_tab_bar ().select_tab_name ("Kerning"); 87 }); 88 89 GlyphCanvas.set_display (gs); 90 }); 91 kerning_tools.add_tool (insert_last); 92 93 Tool insert_unicode = new Tool ("insert_unichar", t_("Insert character by unicode value")); 94 insert_unicode.select_action.connect ((self) => { 95 KerningDisplay d = MainWindow.get_kerning_display (); 96 d.insert_unichar (); 97 }); 98 kerning_tools.add_tool (insert_unicode); 99 100 string empty_kerning_text = t_("Open a text file with kerning strings first."); 101 102 previous_kerning_string = new Tool ("previous_kerning_string", t_("Previous kerning string")); 103 previous_kerning_string.select_action.connect ((self) => { 104 FontDisplay fd = MainWindow.get_current_display (); 105 KerningDisplay d = (KerningDisplay) fd; 106 Font f = BirdFont.get_current_font (); 107 string w = f.kerning_strings.previous (); 108 109 if (f.kerning_strings.is_empty ()) { 110 MainWindow.show_message (empty_kerning_text); 111 } else if (w == "") { 112 MainWindow.show_message (t_("You have reached the beginning of the list.")); 113 } else { 114 d.new_line (); 115 d.add_text (w); 116 } 117 }); 118 kerning_tools.add_tool (previous_kerning_string); 119 120 next_kerning_string = new Tool ("next_kerning_string", t_("Next kerning string")); 121 next_kerning_string.select_action.connect ((self) => { 122 FontDisplay fd = MainWindow.get_current_display (); 123 KerningDisplay d = (KerningDisplay) fd; 124 Font f = BirdFont.get_current_font (); 125 string w = f.kerning_strings.next (); 126 127 if (f.kerning_strings.is_empty ()) { 128 MainWindow.show_message (empty_kerning_text); 129 } else if (w == "") { 130 MainWindow.show_message (t_("You have reached the end of the list.")); 131 } else { 132 d.new_line (); 133 d.add_text (w); 134 } 135 }); 136 kerning_tools.add_tool (next_kerning_string); 137 138 kerning_tools.set_persistent (false); 139 kerning_tools.set_unique (false); 140 141 classes.set_persistent (true); 142 classes.set_unique (true); 143 144 expanders.add (font_name); 145 expanders.add (zoom_expander); 146 expanders.add (kerning_tools); 147 expanders.add (classes); 148 } 149 150 public static void add_unique_class (KerningRange kerning_class) { 151 KerningRange k; 152 153 if (is_null (classes)) { // FIXME: export without tools 154 init (); 155 } 156 157 foreach (Tool t in classes.tool) { 158 if (!(t is KerningRange)) { 159 warning ("Tool is not kerning range"); 160 return; 161 } 162 163 k = (KerningRange) t; 164 if (k.glyph_range.get_all_ranges () == kerning_class.glyph_range.get_all_ranges ()) { 165 return; 166 } 167 } 168 169 classes.add_tool (kerning_class); 170 } 171 172 public static GlyphRange get_kerning_class (int index) { 173 if (likely (0 <= index < classes.tool.size)) { 174 return ((KerningRange) classes.tool.get (index)).glyph_range; 175 } else { 176 warning ("Index out of bounds."); 177 return new GlyphRange (); 178 } 179 } 180 181 public static void update_kerning_classes () { 182 Font font = BirdFont.get_current_font (); 183 KerningClasses k = 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 (font); 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 (font); 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 public override Gee.ArrayList<string> get_displays () { 263 Gee.ArrayList<string> d = new Gee.ArrayList<string> (); 264 d.add ("Kerning"); 265 d.add ("Spacing"); 266 return d; 267 } 268 } 269 270 } 271