The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

AlternateFeature.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/AlternateFeature.vala.
Merge branch 'master' of github.com:johanmattssonm/birdfont
1 /* 2 Copyright (C) 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 15 namespace BirdFont { 16 17 public class AlternateFeature : GLib.Object { 18 GlyfTable glyf_table; 19 Gee.ArrayList<Alternate> alternates; 20 string tag; 21 22 public AlternateFeature (GlyfTable glyf_table, string tag) { 23 Font font = OpenFontFormatWriter.get_current_font (); 24 25 this.tag = tag; 26 this.glyf_table = glyf_table; 27 alternates = font.alternates.get_alt (tag); 28 29 alternates.sort ((a, b) => { 30 Alternate alt1 = (Alternate) a; 31 Alternate alt2 = (Alternate) b; 32 return strcmp ((!) alt1.character.to_string (), (!) alt2.character.to_string ()); 33 }); 34 } 35 36 public bool has_alternates () { 37 return alternates.size > 0; 38 } 39 40 public Lookups get_lookups () throws GLib.Error { 41 Lookups lookups = new Lookups (); 42 Lookup lookup = new Lookup (3, 0, tag); 43 FontData fd = new FontData (); 44 45 fd.add_ushort (1); // format identifier 46 47 // offset to coverage 48 int coverage_offset = 6; 49 coverage_offset += 2 * alternates.size; 50 51 foreach (Alternate a in alternates) { 52 coverage_offset += 2; 53 coverage_offset += 2 * a.alternates.size; 54 } 55 56 fd.add_ushort ((uint16) coverage_offset); 57 58 // number of alternate sets 59 fd.add_ushort ((uint16) alternates.size); 60 61 int offset = 6 + 2 * alternates.size; 62 for (int i = 0; i < alternates.size; i++) { 63 // offset to each alternate set 64 fd.add_ushort ((uint16) offset); 65 offset += 2; 66 offset += 2 * alternates.get (i).alternates.size; 67 } 68 69 // alternates 70 foreach (Alternate alternate in alternates) { 71 fd.add_ushort ((uint16) alternate.alternates.size); 72 73 alternate.alternates.sort ((a, b) => { 74 string alt1 = (string) a; 75 string alt2 = (string) b; 76 return strcmp (alt1, alt2); 77 }); 78 79 foreach (string alt in alternate.alternates) { 80 fd.add_ushort ((uint16) glyf_table.get_gid (alt)); 81 } 82 } 83 84 if (fd.length_with_padding () != coverage_offset) { 85 warning (@"Bad coverage offset. $(fd.length_with_padding ()) != $coverage_offset"); 86 } 87 88 // coverage 89 fd.add_ushort (1); // format 90 fd.add_ushort ((uint16) alternates.size); // coverage array length 91 foreach (Alternate alternate in alternates) { 92 string glyph_name = (!) alternate.character.to_string (); 93 fd.add_ushort ((uint16) glyf_table.get_gid (glyph_name)); 94 } 95 96 lookup.add_subtable (fd); 97 lookups.add_lookup (lookup); 98 99 return lookups; 100 } 101 } 102 103 } 104