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 / table / loca.rb
100644 75 lines (64 sloc) 2.675 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
# 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
module Table
 
# Loca is the Location table class. It provides the offsets of glyphs in
# the glyf table.
class Loca < Font::TTF::FontChunk
 
    # An array of glyphs offsets with number of glyphs + 1 elements.
    # The value associated with a given index of that array
    # is the offset associated with glyph with index index.
    # This offset may be used with Font::TTF::Table::Glyf#get_glyph_at_offset
    # to get the glyph object associated with offset.
    #
    # The additional offset is added so that the length of the
    # last glyph can be calculated:
    # len of last glyph = additional offs - last glyph offs
    attr_accessor :glyph_offsets
 
    # It is not recommended to create Loca objects by hand.
    # Use Font::TTF::File#get_table or Font::TTF::File#get_new_table
    # with :loca as parameter instead.
    def initialize(*args)
        super(*args)
 
        if exists_in_file?
            @font.at_offset(@offset) do
                n = @font.get_table(:maxp).num_glyphs + 1
 
                case @font.get_table(:head).index_to_loc_format
                    when Font::TTF::Table::Head::SHORT_FORMAT
                        @glyph_offsets = @font.read_ushorts(n)
                        @glyph_offsets.collect! { |o| o * 2 }
                    when Font::TTF::Table::Head::LONG_FORMAT
                        @glyph_offsets = @font.read_ulongs(n)
 
                end
            end
        end
    end
 
    # Dumps the loca table in binary raw format as may be found in a font
    # file.
    def dump
        case @font.get_table(:head).index_to_loc_format
            when Font::TTF::Table::Head::SHORT_FORMAT
                @glyph_offsets.collect { |o| o / 2 }.to_ushorts
            when Font::TTF::Table::Head::LONG_FORMAT
                @glyph_offsets.to_ulongs
 
        end
    end
    
end
 
end
end
end