The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Ligatures.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/Ligatures.vala.
Add contextual ligatures to the GUI and fix directory privileges
1 /* 2 Copyright (C) 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 Ligatures : GLib.Object { 21 22 Gee.ArrayList<Ligature> ligatures = new Gee.ArrayList<Ligature> (); 23 24 public delegate void LigatureIterator (string substitution, string ligature); 25 public delegate void SingleLigatureIterator (GlyphSequence substitution, GlyphCollection ligature); 26 27 public Ligatures () { 28 } 29 30 public void get_ligatures (LigatureIterator iter) { 31 foreach (Ligature l in ligatures) { 32 iter (l.substitution, l.ligature); 33 } 34 } 35 36 public void get_single_substitution_ligatures (SingleLigatureIterator iter) { 37 get_ligatures ((substitution, ligature) => { 38 Font font = BirdFont.get_current_font (); 39 GlyphCollection? gc; 40 GlyphCollection li; 41 GlyphSequence gs; 42 string[] subst_names = substitution.split (" "); 43 44 gc = font.get_glyph_collection_by_name (ligature); 45 46 if (gc == null) { 47 return; 48 } 49 50 li = (!) gc; 51 52 gs = new GlyphSequence (); 53 foreach (string s in subst_names) { 54 gc = font.get_glyph_collection_by_name (s); 55 56 if (gc == null) { 57 return; 58 } 59 60 gs.glyph.add (((!) gc).get_current ()); 61 } 62 63 iter (gs, li); 64 }); 65 } 66 67 public int count () { 68 return ligatures.size; 69 } 70 71 public void remove_at (int i) { 72 return_if_fail (0 <= i < ligatures.size); 73 ligatures.remove_at (i); 74 } 75 76 public void set_ligature (int index) { 77 Ligature lig; 78 TextListener listener; 79 80 return_if_fail (0 <= index < ligatures.size); 81 82 lig = ligatures.get (index); 83 listener = new TextListener (t_("Ligature"), "", t_("Set")); 84 85 listener.signal_text_input.connect ((text) => { 86 lig.ligature = text; 87 }); 88 89 listener.signal_submit.connect (() => { 90 MainWindow.native_window.hide_text_input (); 91 MainWindow.get_ligature_display ().update_rows (); 92 }); 93 94 MainWindow.native_window.set_text_listener (listener); 95 } 96 97 public void set_substitution (int index) { 98 Ligature lig; 99 TextListener listener; 100 101 return_if_fail (0 <= index < ligatures.size); 102 103 lig = ligatures.get (index); 104 listener = new TextListener (t_("Text"), "", t_("Set")); 105 106 listener.signal_text_input.connect ((text) => { 107 lig.substitution = text; 108 sort_ligatures (); 109 }); 110 111 listener.signal_submit.connect (() => { 112 MainWindow.native_window.hide_text_input (); 113 MainWindow.get_ligature_display ().update_rows (); 114 }); 115 116 MainWindow.native_window.set_text_listener (listener); 117 } 118 119 public void add_ligature (string subst, string liga) { 120 ligatures.insert (0, new Ligature (liga, subst)); 121 sort_ligatures (); 122 } 123 124 void sort_ligatures () { 125 print (@"\n"); 126 ligatures.sort ((a, b) => { 127 Ligature first, next; 128 bool r; 129 int chars_first, chars_next; 130 131 first = (Ligature) a; 132 next = (Ligature) b; 133 134 chars_first = first.substitution.split (" ").length; 135 chars_next = next.substitution.split (" ").length; 136 137 return chars_next - chars_first; 138 }); 139 } 140 } 141 142 } 143