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
Add *nix as OS tag heads/master
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 if (ligatures.size == 0) { 39 warning ("No ligatures in set."); 40 return ""; 41 } 42 43 return Ligature.get_coverage (ligatures.get (0).substitution); 44 } 45 46 public FontData get_set_data () throws GLib.Error { 47 FontData fd, ligature_data; 48 uint16 o, pos; 49 50 fd = new FontData (); 51 52 // number of ligatures in this set 53 fd.add_ushort ((uint16) ligatures.size); 54 55 ligature_data = new FontData (); 56 foreach (Ligature l in ligatures) { 57 // offset to ligatures 58 o = 2 + 2 * ligatures.size; 59 60 pos = (uint16) (o + ligature_data.length_with_padding ()); 61 62 fd.add_ushort (pos); 63 add_ligature (ligature_data, l); 64 } 65 66 fd.append (ligature_data); 67 68 return fd; 69 } 70 71 /** Add ligature to fd */ 72 void add_ligature (FontData fd, Ligature ligature) throws GLib.Error { 73 string[] parts = ligature.substitution.split (" "); 74 bool first = true; 75 int gid; 76 string l; 77 78 l = ligature.ligature.strip (); 79 if (l.has_prefix ("U+") || l.has_prefix ("u+")) { 80 l = (!) Font.to_unichar (l).to_string (); 81 } 82 83 if (l == "space") { 84 l = " "; 85 } 86 87 gid = glyf_table.get_gid (l); 88 89 if (gid == -1) { 90 warning (@"No glyph ID for ligature $(ligature.ligature)."); 91 gid = 0; 92 } 93 94 fd.add_ushort ((uint16) gid); 95 96 // number of components including the coverage glyph 97 fd.add_ushort ((uint16) parts.length); 98 99 foreach (string p in parts) { 100 if (p.has_prefix ("U+") || p.has_prefix ("u+")) { 101 p = (!) Font.to_unichar (p).to_string (); 102 } 103 104 if (p == "space") { 105 p = " "; 106 } 107 108 gid = (uint16) glyf_table.get_gid (p); 109 110 if (gid == -1) { 111 warning (@"No glyph ID for ligature $(ligature.ligature)."); 112 gid = 0; 113 } 114 115 if (!first) { 116 fd.add_ushort ((uint16) gid); 117 } 118 119 first = false; 120 } 121 } 122 } 123 124 } 125