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 / hmtx.rb
100644 98 lines (81 sloc) 2.749 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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
 
# Mtx is a base class for Hmtx and Vmtx.
class Mtx < Font::TTF::FontChunk
 
    def initialize(*args)
        super(*args)
 
        if self.tag == :hmtx
            @hea = :hhea
        else
            @hea = :vhea
        end
 
        if exists_in_file?
            @font.at_offset(@offset) do
                @metrics = []
                
                num_metrics = @font.get_table(@hea).number_of_metrics
                num_metrics.times do
                    @metrics << [@font.read_ufword, @font.read_fword]
                end
               
                @side_bearings = []
 
                (@font.get_table(:maxp).num_glyphs - num_metrics).times do
                    @side_bearings << @font.read_fword
                end
            end
        end
    end
 
    # Returns an Array of [advance_width, left/top_side_bearing] pairs.
    def metrics
        last_aw = @metrics.last[0]
        @metrics + @side_bearings.collect { |sb| [last_aw, sb] }
    end
 
    # Sets metrics.
    def metrics=(mtrx)
        len = mtrx.length
        raise "Number of (h/v)metrics must be equal to number of glyphs" \
            if len != @font.get_table(:maxp).num_glyphs
        @font.get_table(@hea).number_of_metrics = len
        @side_bearings = []
        @metrics = mtrx
    end
 
    # Dumps the (h/v)mtx table in binary raw format as may be found in a font
    # file.
    def dump
        raw = ""
        (@metrics || []).each do |advanced_width, side_bearing|
            raw += advanced_width.to_ufword
            raw += side_bearing.to_fword
        end
        (@side_bearings || []).each do |side_bearing|
            raw += side_bearing.to_fword
        end
        raw
    end
    
end
 
# Hmtx is the horizontal metrics table.
class Hmtx < Mtx
 
    alias :hmetrics :metrics
    alias :hmetrics= :metrics=
 
    def initialize(*args)
        super(*args)
    end
    
end
 
end
end
end