File: loca.rb

package info (click to toggle)
ruby-ttfunk 1.0.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 916 kB
  • sloc: ruby: 1,719; makefile: 16
file content (43 lines) | stat: -rw-r--r-- 1,077 bytes parent folder | download
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
require 'ttfunk/table'

module TTFunk
  class Table
    class Loca < Table
      attr_reader :offsets

      # Accepts an array of offsets, with each index corresponding to the
      # glyph id with that index.
      #
      # Returns a hash containing:
      #
      # * :table - the string representing the table's contents
      # * :type  - the type of offset (to be encoded in the 'head' table)
      def self.encode(offsets)
        if offsets.any? { |ofs| ofs > 0xFFFF }
          { :type => 1, :table => offsets.pack("N*") }
        else
          { :type => 0, :table => offsets.map { |o| o/2 }.pack("n*") }
        end
      end

      def index_of(glyph_id)
        @offsets[glyph_id]
      end

      def size_of(glyph_id)
        @offsets[glyph_id+1] - @offsets[glyph_id]
      end

      private

        def parse!
          type = file.header.index_to_loc_format == 0 ? "n" : "N"
          @offsets = read(length, "#{type}*")

          if file.header.index_to_loc_format == 0
            @offsets.map! { |v| v * 2 }
          end
        end
    end
  end
end