The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

LigatureSet.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/LigatureSet.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 LigatureSet : GLib.Object { 18 public Gee.ArrayList<Ligature> ligatures = new Gee.ArrayList<Ligature> (); 19 GlyfTable glyf_table; 20 21 public LigatureSet (GlyfTable gt) { 22 glyf_table = gt; 23 } 24 25 public void add (Ligature lig) { 26 ligatures.add (lig); 27 } 28 29 public bool starts_with (string s) { 30 if (ligatures.size == 0) { 31 return false; 32 } 33 34 return ligatures.get (0).substitution.has_prefix (s); 35 } 36 37 public string get_coverage_char () { 38 string s; 39 string[] sp; 40 41 if (ligatures.size == 0) { 42 warning ("No ligatures in set."); 43 return ""; 44 } 45 46 s = ligatures.get (0).substitution; 47 48 if (s.has_prefix ("U+") || s.has_prefix ("u+")) { 49 sp = s.split (" "); 50 return_val_if_fail (sp.length > 0, ""); 51 s = (!) Font.to_unichar (sp[0]).to_string (); 52 } 53 54 return (!) s.get (0).to_string (); 55 } 56 57 public FontData get_set_data () throws GLib.Error { 58 FontData fd, ligature_data; 59 uint16 o, pos; 60 61 fd = new FontData (); 62 63 // number of ligatures in this set 64 fd.add_ushort ((uint16) ligatures.size); 65 66 ligature_data = new FontData (); 67 foreach (Ligature l in ligatures) { 68 // offset to ligatures 69 o = 2 + 2 * ligatures.size; 70 71 pos = (uint16) (o + ligature_data.length_with_padding ()); 72 73 fd.add_ushort (pos); 74 add_ligature (ligature_data, l); 75 } 76 77 fd.append (ligature_data); 78 79 return fd; 80 } 81 82 /** Add ligature to fd */ 83 void add_ligature (FontData fd, Ligature ligature) throws GLib.Error { 84 string[] parts = ligature.substitution.split (" "); 85 bool first = true; 86 int gid; 87 string l; 88 89 l = ligature.ligature.strip (); 90 if (l.has_prefix ("U+") || l.has_prefix ("u+")) { 91 l = (!) Font.to_unichar (l).to_string (); 92 } 93 94 gid = glyf_table.get_gid (l); 95 96 if (gid == -1) { 97 warning (@"No glyph ID for ligature $(ligature.ligature)."); 98 gid = 0; 99 } 100 101 fd.add_ushort ((uint16) gid); 102 103 // number of components including the coverage glyph 104 fd.add_ushort ((uint16) parts.length); 105 106 foreach (string p in parts) { 107 if (p.has_prefix ("U+") || p.has_prefix ("u+")) { 108 p = (!) Font.to_unichar (p).to_string (); 109 } 110 111 gid = (uint16) glyf_table.get_gid (p); 112 113 if (gid == -1) { 114 warning (@"No glyph ID for ligature $(ligature.ligature)."); 115 gid = 0; 116 } 117 118 if (!first) { 119 fd.add_ushort ((uint16) gid); 120 } 121 122 first = false; 123 } 124 } 125 } 126 127 } 128