The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

KerningRange.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
Circle boundaries heads/master
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 17 namespace BirdFont { 18 19 /** Representation of a kerning class. */ 20 public class KerningRange : Tool { 21 22 public string ranges = ""; 23 public GlyphRange glyph_range; 24 bool malformed = false; 25 Font font; 26 27 public KerningRange (Font f, string? name = null, string tip = "") { 28 base (null , tip); 29 30 font = f; 31 32 glyph_range = new GlyphRange (); 33 34 if (name != null) { 35 base.name = (!) name; 36 } 37 38 panel_press_action.connect ((selected, button, tx, ty) => { 39 KerningDisplay kerning_display = MainWindow.get_kerning_display (); 40 41 if (button == 3 || KeyBindings.modifier == CTRL) { 42 update_kerning_classes (); 43 } else if (malformed) { 44 kerning_display.show_parse_error (); 45 } else if (button == 1 && !glyph_range.is_empty ()) { 46 kerning_display.add_range (glyph_range); 47 } 48 }); 49 50 panel_move_action.connect ((selected, button, tx, ty) => { 51 return false; 52 }); 53 54 panel_release_action.connect ((selected, button, tx, ty) => { 55 }); 56 } 57 58 public void update_spacing_class () { 59 set_ranges (ranges); 60 } 61 62 public void set_ranges (string r) { 63 GlyphRange glyph_range = new GlyphRange (); 64 SpacingData spacing = font.get_spacing (); 65 string new_range; 66 string ch; 67 68 try { 69 glyph_range.parse_ranges (r); 70 new_range = glyph_range.get_all_ranges (); 71 72 malformed = false; 73 74 for (int i = 0; i < glyph_range.get_length (); i++) { 75 ch = glyph_range.get_char (i); 76 77 foreach (string c in spacing.get_all_connections (ch)) { 78 if (!glyph_range.has_character (c) && c != "" && c != "?") { 79 new_range += " " + GlyphRange.serialize (c); 80 } 81 } 82 83 if (!font.has_glyph (ch)) { 84 malformed = true; 85 } 86 } 87 88 set_one_range (new_range); 89 } catch (GLib.MarkupError e) { 90 warning (e.message); 91 malformed = true; 92 } 93 } 94 95 private void set_one_range (string r) throws MarkupError { 96 GlyphRange old = new GlyphRange (); 97 98 old.parse_ranges (ranges); 99 old.set_class (true); 100 101 ranges = r; 102 name = r; 103 104 glyph_range.empty (); 105 glyph_range.parse_ranges (r); 106 glyph_range.set_class (true); 107 108 font.spacing.kerning_classes.update_range (old, glyph_range); 109 } 110 111 public void update_kerning_classes () { 112 KerningDisplay kerning_display = MainWindow.get_kerning_display (); 113 TextListener listener = new TextListener (t_("Kerning class"), ranges, t_("Set")); 114 listener.signal_text_input.connect ((text) => { 115 set_ranges (text); 116 KerningTools.classes.clear_cache (); 117 KerningTools.classes.redraw (); 118 }); 119 120 listener.signal_submit.connect (() => { 121 MainWindow.get_kerning_display ().suppress_input = false; 122 TabContent.hide_text_input (); 123 124 // remove all empty classes 125 if (ranges == "") { 126 glyph_range.empty (); 127 KerningTools.remove_empty_classes (); 128 } 129 }); 130 131 kerning_display.suppress_input = true; 132 133 TabContent.show_text_input (listener); 134 } 135 136 public override void draw_tool (Context cr, double px, double py) { 137 double xt, yt; 138 Text label_text; 139 140 double x = this.x - px; 141 double y = this.y - py; 142 143 xt = x + 5; 144 yt = y + 10; 145 146 cr.save (); 147 148 label_text = new Text (); 149 label_text.set_text (name); 150 Theme.text_color (label_text, "Text Tool Box"); 151 label_text.set_font_size (18); 152 label_text.widget_x = x; 153 label_text.widget_y = y; 154 label_text.draw (cr); 155 156 cr.restore (); 157 } 158 } 159 160 } 161