Class: DynamicPDFApi::FullNameTable

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_client/FullNameTable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, table_directory, position) ⇒ FullNameTable

Returns a new instance of FullNameTable.



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
# File 'lib/ruby_client/FullNameTable.rb', line 6

def initialize(io, table_directory, position)

  if table_directory != nil
    int_offset = read_ulong(table_directory, position + 8)
    int_length = read_ulong(table_directory, position + 12)

    io.seek(int_offset, IO::SEEK_SET)
    @data = io.read(int_length).b
  end

  @_name = ""

  dataStart = read_ushort(4)
  headerStart = 6
  headerEnd = (read_ushort(2)* 12)

  (headerStart...headerEnd).step(12) do |i|
    if read_ushort(i + 6) == 4  # 4 is the Name ID for Full font name
      if read_ushort(i) == 3 && read_ushort(i + 2) == 1 && read_ushort(i + 4) == 0x0409 # 3 for PlatForm ID, 1 for Encoding ID and 0x0409 Language ID for English(United States)
        offset = read_ushort(i + 10)
        length = read_ushort(i + 8)
        raw_bytes = @data[dataStart + offset, length]
        if raw_bytes
          decoded = raw_bytes.dup.force_encoding("UTF-16BE").encode("UTF-8", invalid: :replace, undef: :replace).strip
          @_name = decoded
          break unless @_name.empty?
        end
      end
    end
  end

  if @_name.empty?
    (headerStart...headerEnd).step(12) do |i|
      if read_ushort(i + 6) == 4  # 4 is the Name ID for Full font name
        if read_ushort(i) == 3 # 3 for PlatForm ID, 0 for Encoding ID and 0x0409 Language ID for English(United States)
          offset = read_ushort(i + 10)
          length = read_ushort(i + 8)
          raw_bytes = @data[dataStart + offset, length]
          if raw_bytes
            decoded = raw_bytes.dup.force_encoding("UTF-16BE").encode("UTF-8", invalid: :replace, undef: :replace).strip
            @_name = decoded
            break
          end
        end
      end
    end
  end

  # Remove spaces and hyphens
  @_name.gsub!(/[ -]/, "") unless @_name.nil?
end

Instance Attribute Details

#_nameObject (readonly)

Returns the value of attribute _name.



5
6
7
# File 'lib/ruby_client/FullNameTable.rb', line 5

def _name
  @_name
end

Instance Method Details

#read_ulong(array, index) ⇒ Object



63
64
65
# File 'lib/ruby_client/FullNameTable.rb', line 63

def read_ulong(array, index)
  array[index, 4].unpack1("N")
end

#read_ushort(index) ⇒ Object



59
60
61
# File 'lib/ruby_client/FullNameTable.rb', line 59

def read_ushort(index)
  @data[index, 2]&.unpack1("n") || 0
end