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