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.
Merge ../birdfont-2.x
1 /* 2 Copyright (C) 2012, 2013 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 OtfTable : Object { 18 19 public string id = "NO_ID"; 20 21 public uint32 checksum = 0; 22 public uint32 offset = 0; 23 public uint32 length = 0; 24 25 protected FontData? font_data = null; 26 27 public virtual string get_id () { 28 return id; 29 } 30 31 public FontData get_font_data () { 32 if (font_data == null) { 33 warning (@"No font data for $(id)."); 34 font_data = new FontData (); 35 } 36 37 return (!) font_data; 38 } 39 40 public bool has_data () { 41 return length > 0; 42 } 43 44 public virtual void parse (FontData dis) throws GLib.Error { 45 warning (@"Parse is not implemented for $(id)."); 46 } 47 48 /** Validate table checksum. */ 49 public bool validate (FontData dis) { 50 bool valid; 51 52 if (length == 0) { 53 stderr.printf (@"OtfTable $id is of zero length.\n"); 54 valid = false; 55 } else { 56 valid = OtfTable.validate_table (dis, checksum, offset, length, id); 57 } 58 59 if (!valid) { 60 stderr.printf (@"OtfTable $id is invalid.\n"); 61 } 62 63 return valid; 64 } 65 66 public static bool validate_table (FontData dis, uint32 checksum, uint32 offset, uint32 length, string name) { 67 uint32 ch = calculate_checksum (dis, offset, length, name); 68 bool c; 69 70 c = (ch == checksum); 71 72 if (!c) { 73 stderr.printf(@"Checksum does not match data for $(name).\n"); 74 stderr.printf(@"name: $name, checksum: $checksum, offset: $offset, length: $length\n"); 75 stderr.printf(@"calculated checksum $(ch)\n"); 76 } 77 78 return c; 79 } 80 81 public static uint32 calculate_checksum (FontData dis, uint32 offset, uint32 length, string name) { 82 uint32 checksum = 0; 83 uint32 l; 84 85 dis.seek (offset); 86 87 l = (length % 4 > 0) ? length / 4 + 1 : length / 4; 88 89 for (uint32 i = 0; i < l; i++) { 90 checksum += dis.read_ulong (); 91 } 92 93 return checksum; 94 } 95 96 public static uint16 max_pow_2_less_than_i (uint16 ind) { 97 uint16 last = 0; 98 uint16 i = 1; 99 100 while ((i <<= 1) < ind) { 101 last = i; 102 } 103 104 return last; 105 } 106 107 public static uint16 max_log_2_less_than_i (uint16 ind) { 108 return (uint16) (Math.log (ind) / Math.log (2)); 109 } 110 } 111 112 } 113