File: reader.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 (44 lines) | stat: -rw-r--r-- 888 bytes parent folder | download | duplicates (2)
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
module TTFunk
  module Reader
    private

      def io
        @file.contents
      end

      def read(bytes, format)
        io.read(bytes).unpack(format)
      end

      def read_signed(count)
        read(count*2, "n*").map { |i| to_signed(i) }
      end

      def to_signed(n)
        (n>=0x8000) ? -((n ^ 0xFFFF) + 1) : n
      end

      def parse_from(position)
        saved, io.pos = io.pos, position
        result = yield position
        io.pos = saved
        return result
      end

      # For debugging purposes
      def hexdump(string)
        bytes = string.unpack("C*")
        bytes.each_with_index do |c, i|
          print "%02X" % c
          if (i+1) % 16 == 0
            puts
          elsif (i+1) % 8 == 0
            print "  "
          else
            print " "
          end
        end
        puts unless bytes.length % 16 == 0
      end
  end
end