File: reader.rb

package info (click to toggle)
ruby-ttfunk 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,472 kB
  • sloc: ruby: 7,954; makefile: 7
file content (49 lines) | stat: -rw-r--r-- 949 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
44
45
46
47
48
49
# frozen_string_literal: true

module TTFunk
  # Helper methods to read form file content.
  # @api rpivate
  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(number)
      number >= 0x8000 ? -((number ^ 0xFFFF) + 1) : number
    end

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

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