The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

KernList.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/KernList.vala.
Fix compile time error
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 KernList : GLib.Object { 18 public Gee.ArrayList<PairFormat1> pairs; 19 20 public delegate void KernIterator (Kern k); 21 public delegate void PairFormat1Iterator (PairFormat1 k); 22 23 GlyfTable glyf_table; 24 uint num_pairs; 25 26 public KernList (GlyfTable glyf_table) { 27 this.glyf_table = glyf_table; 28 num_pairs = 0; 29 pairs = new Gee.ArrayList<PairFormat1> (); 30 } 31 32 /** @return number of pairs. */ 33 public uint fetch_all_pairs () { 34 PairFormat1 current_pairs = new PairFormat1 (); 35 KerningClasses classes; 36 37 if (pairs.size > 0 || num_pairs > 0) { 38 warning ("Pairs already loaded."); 39 } 40 41 num_pairs = 0; 42 pairs.clear (); 43 44 classes = BirdFont.get_current_font ().get_kerning_classes (); 45 classes.all_pairs ((kp) => { 46 uint16 gid_left, gid_right; 47 KerningPair kerning_pair = kp; 48 int i; 49 50 if (unlikely (kerning_pair.character.get_name () == "")) { 51 warning ("No name for glyph"); 52 } 53 54 current_pairs = new PairFormat1 (); 55 gid_left = (uint16) glyf_table.get_gid (kerning_pair.character.get_name ()); 56 current_pairs.left = gid_left; 57 pairs.add (current_pairs); 58 59 if (unlikely (kerning_pair.kerning.size == 0)) { 60 warning (@"No kerning pairs for character: $((kerning_pair.character.get_name ()))"); 61 } 62 63 i = 0; 64 num_pairs += kerning_pair.kerning.size; 65 foreach (Kerning k in kerning_pair.kerning) { 66 gid_right = (uint16) glyf_table.get_gid (k.get_glyph ().get_name ()); 67 current_pairs.pairs.add (new Kern (gid_left, gid_right, (int16) Math.rint (k.val * HeadTable.UNITS))); 68 } 69 70 current_pairs.pairs.sort ((a, b) => { 71 Kern first = (Kern) a; 72 Kern next = (Kern) b; 73 return first.right - next.right; 74 }); 75 }); 76 77 pairs.sort ((a, b) => { 78 PairFormat1 first = (PairFormat1) a; 79 PairFormat1 next = (PairFormat1) b; 80 return first.left - next.left; 81 }); 82 83 return num_pairs; 84 } 85 86 /** @return the number of glyphs that is kerned on the left side in 87 * pair pos format 1. 88 */ 89 public uint get_length_left () { 90 return pairs.size; 91 } 92 93 /** @return the total number of pairs. */ 94 public uint get_length () { 95 return num_pairs; 96 } 97 98 public void all_kern (KernIterator iter, int limit) { 99 int i = 0; 100 foreach (PairFormat1 p in pairs) { 101 foreach (Kern k in p.pairs) { 102 if (unlikely (i >= limit)) { 103 warning (@"Too many pairs. Limit: $limit"); 104 return; 105 } 106 107 iter (k); 108 i++; 109 } 110 } 111 } 112 113 public void all_pairs_format1 (PairFormat1Iterator iter, int limit = -1) { 114 uint i = 0; 115 116 foreach (PairFormat1 p in pairs) { 117 if (unlikely (i >= limit && limit != -1)) { 118 warning (@"Too many pairs. Limit: $limit"); 119 return; 120 } 121 122 iter (p); 123 124 i++; 125 } 126 } 127 } 128 129 } 130