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.
Save stylistic alternatives to bf fonts and make gsub features optional
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 20 public AlternateFeature (GlyfTable glyf_table) { 21 this.glyf_table = glyf_table; 22 } 23 24 public bool has_alternates () { 25 Font font = OpenFontFormatWriter.get_current_font (); 26 return font.alternates.size > 0; 27 } 28 29 public Lookups get_lookups () throws GLib.Error { 30 Lookups lookups = new Lookups (); 31 Lookup lookup = new Lookup (3, 0, Lookups.ALTERNATES); 32 FontData fd = new FontData (); 33 Font font = OpenFontFormatWriter.get_current_font (); 34 35 font.alternates.sort ((a, b) => { 36 Alternate alt1 = (Alternate) a; 37 Alternate alt2 = (Alternate) b; 38 return strcmp ((!) alt1.character.to_string (), (!) alt2.character.to_string ()); 39 }); 40 41 fd.add_ushort (1); // format identifier 42 43 // offset to coverage 44 int coverage_offset = 6; 45 coverage_offset += 2 * font.alternates.size; 46 coverage_offset += 2 + 2 * get_number_of_alternates (); 47 fd.add_ushort ((uint16) coverage_offset); 48 49 // number of alternate sets 50 fd.add_ushort ((uint16) font.alternates.size); 51 52 int offset = 6 + 2 * font.alternates.size; 53 for (int i = 0; i < font.alternates.size; i++) { 54 // offset to each alternate set 55 fd.add_ushort ((uint16) offset); 56 offset += 2; 57 offset += 2 * font.alternates.size; 58 } 59 60 // alternates 61 fd.add_ushort ((uint16) font.alternates.size); 62 foreach (Alternate alternate in font.alternates) { 63 foreach (string alt in alternate.alternates) { 64 fd.add_ushort ((uint16) glyf_table.get_gid (alt)); 65 } 66 } 67 68 // coverage 69 fd.add_ushort (1); // format 70 fd.add_ushort ((uint16) font.alternates.size); // coverage array length 71 foreach (Alternate alternate in font.alternates) { 72 string glyph_name = (!) alternate.character.to_string (); 73 fd.add_ushort ((uint16) glyf_table.get_gid (glyph_name)); 74 } 75 76 lookup.add_subtable (fd); 77 lookups.add_lookup (lookup); 78 79 return lookups; 80 } 81 82 int get_number_of_alternates () { 83 int n = 0; 84 Font font = OpenFontFormatWriter.get_current_font (); 85 86 foreach (Alternate alternate in font.alternates) { 87 n += alternate.alternates.size; 88 } 89 90 return n; 91 } 92 } 93 94 } 95