1 # TTF/Ruby, a library to read and write TrueType fonts in Ruby.
2 # Copyright (C) 2006 Mathieu Blondel
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 # A FontChunk is a portion of font. It starts at an offset and has a given
22 # length. It is useful to handle tables that have not been implemented
23 # or to quickly get a dump for a table that has not been modified.
27 attr_accessor :offset, :len
29 def initialize(font, offset=nil, len=nil)
31 # When a FontChunk is modified by user,
32 # offset and len are not true anymore
37 # Returns the end of the class name as a Symbol.
38 # Will be useful for tables, which are subclasses of FontChunk.
39 # For example, calling tag on Font::TTF:Table::Loca object will return
42 t = self.class.name.split("::").last.downcase.to_sym
43 t = :"OS/2" if t == :os2
47 # Basically each table is a FontChunk and tables may be created by hand
48 # (i.e. not exist in file yet). This method returns whether the FontChunk
49 # already exists in file or not.
54 # Returns raw binary data of the FontChunk.
56 @font.at_offset(@offset) do
61 # Returns a checksum of dump.
62 def self.checksum(dump)
63 # FIXME: this methods seems to be buggy
64 len = ((raw.length + 3) & ~3) / IO::SIZEOF_ULONG
66 (len - 1).times do |i|
67 ulong_str = raw.slice(i * IO::SIZEOF_ULONG, IO::SIZEOF_ULONG)
68 ulong = ulong_str.unpack("N")[0]