The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

Lookups.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/Lookups.vala.
Cache overview items
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 using Math; 15 16 namespace BirdFont { 17 18 // Contextual substitutions uses this table for chained substitutions, 19 // the order is not arbitrary. 20 21 public class Lookups : GLib.Object { 22 public Gee.ArrayList<Lookup> tables = new Gee.ArrayList<Lookup> (); 23 24 public Lookups () { 25 } 26 27 /** Subtables added in the proirity order for the substitution or 28 * spacing transformation. The first table will have highest 29 * priority. 30 */ 31 public void add_lookup (Lookup lookup) { 32 tables.add (lookup); 33 } 34 35 /** Find the lookup index for a particular lookup. */ 36 public uint16 find (string token) { 37 uint16 index = 0; 38 foreach (Lookup lookup in tables) { 39 if (lookup.token == token) { 40 return index; 41 } 42 43 index++; 44 } 45 46 warning (@"No lookup has been created with token $(token)"); 47 return 0; 48 } 49 50 public void append (Lookups lookups) { 51 foreach (Lookup lookup in lookups.tables) { 52 tables.add (lookup); 53 } 54 } 55 56 public FontData generate_lookup_list () throws GLib.Error { 57 FontData fd = new FontData (); 58 FontData entry; 59 uint lookup_offset; 60 61 fd.add_ushort ((uint16) tables.size); // number of lookups 62 lookup_offset = 2 + 2 * tables.size; 63 64 foreach (Lookup lookup in tables) { 65 fd.add_ushort ((uint16) lookup_offset); 66 lookup_offset += lookup.get_lookup_entry_size (); 67 } 68 69 // add empty lookups and step back when tables are written 70 foreach (Lookup lookup in tables) { 71 entry = lookup.get_lookup_entry (0); 72 lookup.entry_offset = fd.length_with_padding (); 73 fd.append (entry); 74 } 75 76 if (lookup_offset != fd.length_with_padding ()) { 77 warning ("Wrong lookup offset."); 78 warning (@"$lookup_offset != $(fd.length_with_padding ())"); 79 } 80 81 foreach (Lookup lookup in tables) { 82 uint offset_pos = lookup.entry_offset + 6; 83 fd.seek (offset_pos); 84 85 uint offset = (fd.length_with_padding () - (lookup.entry_offset)); 86 foreach (FontData f in lookup.subtables) { 87 fd.add_ushort ((uint16) offset); 88 offset += f.length_with_padding (); 89 } 90 fd.seek_end (); 91 92 foreach (FontData subtable in lookup.subtables) { 93 fd.append (subtable); 94 } 95 } 96 97 return fd; 98 } 99 } 100 101 } 102