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