The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LigatureCollection.vala in libbirdfont/OpenFontFormat

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/OpenFontFormat/LigatureCollection.vala.
Fix ligature substitutions with space
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 namespace BirdFont { 16 17 public class LigatureCollection : GLib.Object { 18 19 public Gee.ArrayList<LigatureSet> ligature_sets; 20 21 LigatureSet lig_set; 22 LigatureSet last_set; 23 24 public LigatureCollection.clig (GlyfTable glyf_table) { 25 ligature_sets = new Gee.ArrayList<LigatureSet> (); 26 lig_set = new LigatureSet (glyf_table); 27 last_set = new LigatureSet (glyf_table); 28 29 add_clig_ligatures (glyf_table); 30 } 31 32 public LigatureCollection.contextual (GlyfTable glyf_table, ContextualLigature ligature) { 33 ligature_sets = new Gee.ArrayList<LigatureSet> (); 34 lig_set = new LigatureSet (glyf_table); 35 last_set = new LigatureSet (glyf_table); 36 37 add_contextual_ligatures (glyf_table, ligature); 38 } 39 40 void add_clig_ligatures (GlyfTable glyf_table) { 41 Font font = BirdFont.get_current_font (); 42 Ligatures ligatures = font.get_ligatures (); 43 44 ligatures.get_ligatures ((parts, ligature) => { 45 add_ligatures (glyf_table, parts, ligature); 46 }); 47 } 48 49 void add_contextual_ligatures (GlyfTable glyf_table, ContextualLigature cl) { 50 foreach (string l in cl.ligatures.strip ().split (" ")) { 51 add_ligatures (glyf_table, cl.input, l); 52 } 53 } 54 55 // multiple ligatures in non-contextual substitution 56 public void add_ligatures (GlyfTable glyf_table, string characters, string ligatures) 57 requires (!is_null (lig_set) && !is_null (last_set)) { 58 59 Font font = BirdFont.get_current_font (); 60 string[] parts = characters.strip ().split (" "); 61 string l = ligatures; 62 bool has_set = false; 63 64 if (l.has_prefix ("U+") || l.has_prefix ("u+")) { 65 l = (!) Font.to_unichar (l).to_string (); 66 } 67 68 if (!font.has_glyph (l)) { 69 warning (@"Ligature $l does not correspond to a glyph in this font."); 70 return; 71 } 72 73 foreach (string p in parts) { 74 if (p.has_prefix ("U+") || p.has_prefix ("u+")) { 75 p = (!) Font.to_unichar (p).to_string (); 76 } 77 78 if (!font.has_glyph (p)) { 79 warning (@"Ligature substitution of $p is not possible, the character does have a glyph."); 80 return; 81 } 82 } 83 84 if (parts.length == 0) { 85 warning ("No parts."); 86 return; 87 } 88 89 foreach (LigatureSet s in ligature_sets) { 90 if (s.starts_with (parts[0])) { 91 has_set = true; 92 last_set = s; 93 } 94 } 95 96 if (has_set) { 97 last_set.add (new Ligature (l, characters)); 98 } else { 99 lig_set = new LigatureSet (glyf_table); 100 lig_set.add (new Ligature (l, characters)); 101 ligature_sets.add (lig_set); 102 } 103 104 // make sure coverage table is sorted otherwise will substitution not work 105 ligature_sets.sort ((a, b) => { 106 LigatureSet la = (LigatureSet) a; 107 LigatureSet lb = (LigatureSet) b; 108 return (int) (la.get_coverage_char ().get_char () - lb.get_coverage_char ().get_char ()); 109 }); 110 } 111 112 public FontData get_font_data (GlyfTable glyf_table) throws GLib.Error { 113 FontData set_data; 114 uint16 ligature_pos; 115 uint16 table_start; 116 int coverage_offset; 117 FontData fd; 118 119 fd = new FontData (); 120 121 // ligature substitution subtable 122 table_start = (uint16) fd.length_with_padding (); 123 124 fd.add_ushort (1); // format identifier 125 126 coverage_offset = 6 + 2 * ligature_sets.size; 127 fd.add_ushort ((uint16) coverage_offset); // offset to coverage 128 fd.add_ushort ((uint16) ligature_sets.size); // number of ligature set tables 129 130 // array of offsets to ligature sets 131 uint16 size = 0; 132 foreach (LigatureSet l in ligature_sets) { 133 ligature_pos = 10 + (uint16) ligature_sets.size * 4 + size; 134 fd.add_ushort (ligature_pos); 135 size += (uint16) l.get_set_data ().length_with_padding (); 136 } 137 138 // coverage 139 fd.add_ushort (1); // format 140 fd.add_ushort ((uint16) ligature_sets.size); 141 142 // coverage gid: 143 foreach (LigatureSet l in ligature_sets) { 144 fd.add_ushort ((uint16) glyf_table.get_gid (l.get_coverage_char ())); 145 } 146 147 foreach (LigatureSet l in ligature_sets) { 148 set_data = l.get_set_data (); 149 fd.append (set_data); 150 } 151 152 return fd; 153 } 154 } 155 156 } 157