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 merge bug
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 106 public FontData get_font_data (GlyfTable glyf_table) throws GLib.Error { 107 FontData set_data; 108 uint16 ligature_pos; 109 uint16 table_start; 110 FontData fd; 111 112 fd = new FontData (); 113 114 // ligature substitution subtable 115 table_start = (uint16) fd.length_with_padding (); 116 117 fd.add_ushort (1); // format identifier 118 fd.add_ushort (6 + (uint16) 2 * ligature_sets.size); // offset to coverage 119 fd.add_ushort ((uint16) ligature_sets.size); // number of ligature set tables 120 121 // array of offsets to ligature sets 122 uint16 size = 0; 123 foreach (LigatureSet l in ligature_sets) { 124 ligature_pos = 10 + (uint16) ligature_sets.size * 4 + size; 125 fd.add_ushort (ligature_pos); 126 size += (uint16) l.get_set_data ().length_with_padding (); 127 } 128 129 // coverage 130 fd.add_ushort (1); // format 131 fd.add_ushort ((uint16) ligature_sets.size); 132 133 // coverage gid: 134 foreach (LigatureSet l in ligature_sets) { 135 fd.add_ushort ((uint16) glyf_table.get_gid (l.get_coverage_char ())); 136 } 137 138 foreach (LigatureSet l in ligature_sets) { 139 set_data = l.get_set_data (); 140 fd.append (set_data); 141 } 142 143 return fd; 144 } 145 } 146 147 } 148