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.glyph.get_name (), alt2.glyph.get_name ()); 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 (GlyphCollection g in alternate.alternates) { 64 string name = g.get_name (); 65 fd.add_ushort ((uint16) glyf_table.get_gid (name)); 66 } 67 } 68 69 // coverage 70 fd.add_ushort (1); // format 71 fd.add_ushort ((uint16) font.alternates.size); // coverage array length 72 foreach (Alternate alternate in font.alternates) { 73 string glyph_name = alternate.glyph.get_name (); 74 fd.add_ushort ((uint16) glyf_table.get_gid (glyph_name)); 75 } 76 77 lookup.add_subtable (fd); 78 lookups.add_lookup (lookup); 79 80 return lookups; 81 } 82 83 int get_number_of_alternates () { 84 int n = 0; 85 Font font = OpenFontFormatWriter.get_current_font (); 86 87 foreach (Alternate alternate in font.alternates) { 88 n += alternate.alternates.size; 89 } 90 91 return n; 92 } 93 } 94 95 } 96