The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

OtfTable.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/OtfTable.vala.
Fix compile time error
1 2 /* 3 Copyright (C) 2012, 2013 Johan Mattsson 4 5 This library is free software; you can redistribute it and/or modify 6 it under the terms of the GNU Lesser General Public License as 7 published by the Free Software Foundation; either version 3 of the 8 License, or (at your option) any later version. 9 10 This library is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 */ 15 16 namespace BirdFont { 17 18 public class OtfTable : Object { 19 20 public string id = "NO_ID"; 21 22 public uint32 checksum = 0; 23 public uint32 offset = 0; 24 public uint32 length = 0; 25 26 protected FontData? font_data = null; 27 28 public virtual string get_id () { 29 return id; 30 } 31 32 public FontData get_font_data () { 33 if (font_data == null) { 34 warning (@"No font data for $(id)."); 35 font_data = new FontData (); 36 } 37 38 return (!) font_data; 39 } 40 41 public bool has_data () { 42 return length > 0; 43 } 44 45 public virtual void parse (FontData dis) throws GLib.Error { 46 warning (@"Parse is not implemented for $(id)."); 47 } 48 49 /** Validate table checksum. */ 50 public bool validate (FontData dis) { 51 bool valid; 52 53 if (length == 0) { 54 stderr.printf (@"OtfTable $id is of zero length.\n"); 55 valid = false; 56 } else { 57 valid = OtfTable.validate_table (dis, checksum, offset, length, id); 58 } 59 60 if (!valid) { 61 stderr.printf (@"OtfTable $id is invalid.\n"); 62 } 63 64 return valid; 65 } 66 67 public static bool validate_table (FontData dis, uint32 checksum, uint32 offset, uint32 length, string name) { 68 uint32 ch = calculate_checksum (dis, offset, length, name); 69 bool c; 70 71 c = (ch == checksum); 72 73 if (!c) { 74 stderr.printf(@"Checksum does not match data for $(name).\n"); 75 stderr.printf(@"name: $name, checksum: $checksum, offset: $offset, length: $length\n"); 76 stderr.printf(@"calculated checksum $(ch)\n"); 77 } 78 79 return c; 80 } 81 82 public static uint32 calculate_checksum (FontData dis, uint32 offset, uint32 length, string name) { 83 uint32 checksum = 0; 84 uint32 l; 85 86 dis.seek (offset); 87 88 l = (length % 4 > 0) ? length / 4 + 1 : length / 4; 89 90 for (uint32 i = 0; i < l; i++) { 91 checksum += dis.read_ulong (); 92 } 93 94 return checksum; 95 } 96 97 public static uint16 max_pow_2_less_than_i (uint16 ind) { 98 uint16 last = 0; 99 uint16 i = 1; 100 101 while ((i <<= 1) < ind) { 102 last = i; 103 } 104 105 return last; 106 } 107 108 public static uint16 max_log_2_less_than_i (uint16 ind) { 109 return (uint16) (Math.log (ind) / Math.log (2)); 110 } 111 } 112 113 } 114