The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

GsubTable.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/GsubTable.vala.
Fix compile time error
1 /* 2 Copyright (C) 2012 2013 2014 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 using Math; 15 16 namespace BirdFont { 17 18 public class GsubTable : OtfTable { 19 20 GlyfTable glyf_table; 21 22 public GsubTable (GlyfTable glyf_table) { 23 this.glyf_table = glyf_table; 24 id = "GSUB"; 25 } 26 27 public override void parse (FontData dis) throws GLib.Error { 28 } 29 30 public void process () throws GLib.Error { 31 FontData fd; 32 FontData clig_subtable; 33 uint16 length; 34 35 LigatureCollection clig; 36 ContextualLigatureCollection contextual; 37 38 uint16 feature_lookups; 39 uint16 lookups_end; 40 41 Gee.ArrayList<FontData> chain_data; 42 43 fd = new FontData (); 44 clig = new LigatureCollection.clig (glyf_table); 45 contextual = new ContextualLigatureCollection (glyf_table); 46 47 fd.add_ulong (0x00010000); // table version 48 fd.add_ushort (10); // offset to script list 49 fd.add_ushort (30); // offset to feature list 50 fd.add_ushort (contextual.has_ligatures () ? 46 : 44); // offset to lookup list 51 52 // script list 53 fd.add_ushort (1); // number of items in script list 54 fd.add_tag ("DFLT"); // default script 55 fd.add_ushort (8); // offset to script table from script list 56 57 // script table 58 fd.add_ushort (4); // offset to default language system 59 fd.add_ushort (0); // number of languages 60 61 // LangSys table 62 fd.add_ushort (0); // reserved 63 fd.add_ushort (0); // required features (0xFFFF is none) 64 fd.add_ushort (1); // number of features 65 fd.add_ushort (0); // feature index 66 67 // feature table 68 fd.add_ushort (1); // number of features 69 70 fd.add_tag ("clig"); // feature tag 71 fd.add_ushort (8); // offset to feature 72 // FIXME: Should it be liga and clig? 73 74 clig = new LigatureCollection.clig (glyf_table); 75 contextual = new ContextualLigatureCollection (glyf_table); 76 77 feature_lookups = contextual.has_ligatures () ? 2 : 1; 78 79 fd.add_ushort (0); // feature prameters (null) 80 fd.add_ushort (feature_lookups); // number of lookups 81 82 if (contextual.has_ligatures ()) { 83 fd.add_ushort (1 + contextual.get_size ()); // lookup chained_context (etc.) The chained context tables are listed here but the actual ligature table is only referenced in the context table 84 fd.add_ushort (0); // lookup clig_subtable 85 } else { 86 fd.add_ushort (0); // lookup clig_subtable 87 } 88 89 clig_subtable = clig.get_font_data (glyf_table); 90 chain_data = get_chaining_contextual_substition_subtable (contextual); 91 92 // lookup table 93 uint16 lookups; 94 95 if (contextual.has_ligatures ()) { 96 lookups = 2 + (uint16) contextual.get_size (); 97 } else { 98 lookups = 1; 99 } 100 101 fd.add_ushort (lookups); // number of lookups 102 103 if (contextual.has_ligatures ()) { 104 uint16 offset_to_lookup; 105 106 offset_to_lookup = 6 + 2 * contextual.get_size (); 107 fd.add_ushort (offset_to_lookup); // offset to lookup 1, regular ligatures 108 109 for (int i = 0; i < contextual.get_size (); i++) { 110 offset_to_lookup += 8; 111 // offset to ligature lookups used in chaining substitution 112 fd.add_ushort (offset_to_lookup); 113 } 114 115 // offset to lookup for the chain table 116 offset_to_lookup += 8; 117 fd.add_ushort (offset_to_lookup); 118 } else { 119 fd.add_ushort (4); // offset to lookup 1 120 } 121 122 lookups_end = 8; // regular ligatures 123 124 if (contextual.has_ligatures ()) { 125 lookups_end += 8 * contextual.get_size (); // contextual ligatures 126 lookups_end += 6; // chaining table 127 lookups_end += 2 * (uint16) contextual.get_size (); // chaining subtables 128 } 129 130 length = 0; 131 fd.add_ushort (4); // lookup type 132 fd.add_ushort (0); // lookup flags 133 fd.add_ushort (1); // number of subtables 134 fd.add_ushort (lookups_end + length); // array of offsets to subtable 135 length += (uint16) clig_subtable.length_with_padding (); 136 lookups_end -= 8; 137 138 if (contextual.has_ligatures ()) { 139 140 for (int i = 0; i < contextual.ligatures.size; i++) { 141 fd.add_ushort (4); // lookup type 142 fd.add_ushort (0); // lookup flags 143 fd.add_ushort (1); // number of subtables 144 145 LigatureCollection ligature_set = contextual.ligatures.get (i); 146 fd.add_ushort (lookups_end + length); // array of offsets to subtable 147 length += (uint16) ligature_set.get_font_data (glyf_table).length_with_padding (); 148 149 lookups_end -= 8; 150 } 151 152 fd.add_ushort (6); // lookup type 153 fd.add_ushort (0); // lookup flags 154 fd.add_ushort (contextual.get_size ()); // number of subtables 155 156 foreach (FontData d in chain_data) { 157 fd.add_ushort (lookups_end + length); // array of offsets to subtable 158 length += (uint16) d.length_with_padding (); 159 } 160 161 lookups_end -= 6 + 2 * chain_data.size; 162 } 163 164 if (lookups_end != 0) { 165 warning (@"Wrong offset to end of lookups, $lookups_end bytes left."); 166 } 167 168 fd.append (clig_subtable); 169 170 if (contextual.has_ligatures ()) { 171 foreach (LigatureCollection s in contextual.ligatures) { 172 fd.append (s.get_font_data (glyf_table)); 173 } 174 175 foreach (FontData d in chain_data) { 176 fd.append (d); 177 } 178 } 179 180 fd.pad (); 181 182 this.font_data = fd; 183 } 184 185 // chaining contextual substitution format3 186 Gee.ArrayList<FontData> get_chaining_contextual_substition_subtable (ContextualLigatureCollection contexts) throws GLib.Error { 187 Gee.ArrayList<FontData> fd = new Gee.ArrayList<FontData> (); 188 uint16 ligature_lookup_index = 1; 189 190 foreach (ContextualLigature context in contexts.ligature_context) { 191 fd.add (context.get_font_data (glyf_table, ligature_lookup_index)); 192 ligature_lookup_index++; 193 } 194 195 return fd; 196 } 197 } 198 199 } 200