public
Description: Fast, Nimble PDF Writer for Ruby
Homepage: http://prawn.majesticseacreature.com
Clone URL: git://github.com/sandal/prawn.git
prawn / vendor / font_ttf / ttf / fontchunk.rb
100644 77 lines (67 sloc) 2.473 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# TTF/Ruby, a library to read and write TrueType fonts in Ruby.
# Copyright (C) 2006 Mathieu Blondel
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 
module Font
module TTF
 
# A FontChunk is a portion of font. It starts at an offset and has a given
# length. It is useful to handle tables that have not been implemented
# or to quickly get a dump for a table that has not been modified.
class FontChunk
 
    attr_reader :font
    attr_accessor :offset, :len
 
    def initialize(font, offset=nil, len=nil)
        @font = font
        # When a FontChunk is modified by user,
        # offset and len are not true anymore
        @offset = offset
        @len = len
    end
 
    # Returns the end of the class name as a Symbol.
    # Will be useful for tables, which are subclasses of FontChunk.
    # For example, calling tag on Font::TTF:Table::Loca object will return
    # :loca.
    def tag
        t = self.class.name.split("::").last.downcase.to_sym
        t = :"OS/2" if t == :os2
        t
    end
 
    # Basically each table is a FontChunk and tables may be created by hand
    # (i.e. not exist in file yet). This method returns whether the FontChunk
    # already exists in file or not.
    def exists_in_file?
        not @offset.nil?
    end
 
    # Returns raw binary data of the FontChunk.
    def dump
        @font.at_offset(@offset) do
            @font.read(@len)
        end
    end
 
    # Returns a checksum of dump.
    def self.checksum(dump)
        # FIXME: this methods seems to be buggy
        len = ((raw.length + 3) & ~3) / IO::SIZEOF_ULONG
        sum = 0
        (len - 1).times do |i|
            ulong_str = raw.slice(i * IO::SIZEOF_ULONG, IO::SIZEOF_ULONG)
            ulong = ulong_str.unpack("N")[0]
            sum += ulong
        end
        sum
    end
 
end
 
end
end