The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

KerningStrings.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) 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 namespace BirdFont { 16 17 public class KerningStrings : GLib.Object { 18 19 Gee.ArrayList<string> kerning_strings = new Gee.ArrayList<string> (); 20 int current_position = 0; 21 22 public KerningStrings () { 23 } 24 25 public bool is_empty () { 26 return kerning_strings.size == 0; 27 } 28 29 public string next () { 30 string w = ""; 31 Font font = BirdFont.get_current_font (); 32 33 if (0 <= current_position < kerning_strings.size) { 34 w = kerning_strings.get (current_position); 35 current_position++; 36 font.settings.set_setting ("kerning_string_position", @"$current_position"); 37 } 38 39 return w; 40 } 41 42 public string previous () { 43 string w = ""; 44 Font font = BirdFont.get_current_font (); 45 46 if (0 <= current_position - 1 < kerning_strings.size) { 47 current_position--; 48 w = kerning_strings.get (current_position); 49 font.settings.set_setting ("kerning_string_position", @"$current_position"); 50 } 51 52 return w; 53 54 } 55 56 public void load_file () { 57 FileChooser fc = new FileChooser (); 58 fc.file_selected.connect ((f) => { 59 Font font = BirdFont.get_current_font (); 60 if (f != null) { 61 load_new_string (font, (!) f); 62 } 63 }); 64 65 MainWindow.file_chooser (t_("Load kerning strings"), fc, FileChooser.LOAD); 66 } 67 68 public void load (Font font) { 69 string path; 70 71 path = font.settings.get_setting ("kerning_string_file"); 72 73 if (path != "") { 74 load_new_string (font, path); 75 current_position = int.parse (font.settings.get_setting ("kerning_string_position")); 76 } 77 } 78 79 public void load_new_string (Font font, string kerning_strings_file) { 80 string data; 81 string[] strings; 82 string w; 83 84 try { 85 kerning_strings.clear (); 86 87 FileUtils.get_contents(kerning_strings_file, out data); 88 strings = data.replace ("\n", " ").split (" "); 89 90 foreach (string s in strings) { 91 w = s.replace ("\r", ""); 92 if (w != "") { 93 kerning_strings.add (s); 94 } 95 } 96 97 current_position = 0; 98 99 font.settings.set_setting ("kerning_string_file", kerning_strings_file); 100 } catch (GLib.Error e) { 101 warning (e.message); 102 } 103 } 104 } 105 106 } 107