Class: Prawn::SVG::TTF

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/svg/ttf.rb

Constant Summary collapse

SFNT_VERSION_STRINGS =
["\x00\x01\x00\x00", 'true', 'typ1'].freeze
LANGUAGE_IDS =

English, US English

[0, 0x409].freeze
UTF_16BE_PLATFORM_IDS =

Unicode, Microsoft

[0, 3].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ TTF

Returns a new instance of TTF.



8
9
10
# File 'lib/prawn/svg/ttf.rb', line 8

def initialize(filename)
  load_data_from_file(filename)
end

Instance Attribute Details

#familyObject (readonly)

Returns the value of attribute family.



6
7
8
# File 'lib/prawn/svg/ttf.rb', line 6

def family
  @family
end

#subfamilyObject (readonly)

Returns the value of attribute subfamily.



6
7
8
# File 'lib/prawn/svg/ttf.rb', line 6

def subfamily
  @subfamily
end

#weight_classObject (readonly)

Returns the value of attribute weight_class.



6
7
8
# File 'lib/prawn/svg/ttf.rb', line 6

def weight_class
  @weight_class
end

Class Method Details

.read_name_table(io, font_offset) ⇒ Object



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
# File 'lib/prawn/svg/ttf.rb', line 34

def self.read_name_table(io, font_offset)
  io.seek(font_offset)
  offset_table = io.read(12)
  return unless offset_table && offset_table.length == 12 && SFNT_VERSION_STRINGS.include?(offset_table[0..3])

  table_count = (offset_table[4].ord * 256) + offset_table[5].ord
  tables = io.read(table_count * 16)
  return unless tables && tables.length == table_count * 16

  offset, length = table_count.times do |index|
    start = index * 16
    break tables[start + 8..start + 15].unpack('NNN') if tables[start..start + 3] == 'name'
  end

  return unless length

  io.seek(offset)
  data = io.read(length)
  return unless data && data.length == length

  _, name_count, string_offset = data[0..5].unpack('nnn')

  names = {}
  name_count.times do |index|
    start = 6 + (index * 12)
    platform_id, _, language_id, name_id, length, offset = data[start..start + 11].unpack('nnnnnn')
    next unless offset
    next unless LANGUAGE_IDS.include?(language_id)
    next unless [1, 2, 16, 17].include?(name_id)

    offset += string_offset
    field = data[offset..offset + length - 1]
    next unless field && field.length == length

    names[name_id] =
      if UTF_16BE_PLATFORM_IDS.include?(platform_id)
        begin
          field.force_encoding(Encoding::UTF_16BE).encode(Encoding::UTF_8)
        rescue StandardError
          field
        end
      else
        field
      end
  end

  family = names[16] || names[1]
  subfamily = names[17] || names[2]
  [family, subfamily]
end

.read_weight_class(io, font_offset) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/prawn/svg/ttf.rb', line 12

def self.read_weight_class(io, font_offset)
  io.seek(font_offset)
  offset_table = io.read(12)
  return unless offset_table&.length == 12 && SFNT_VERSION_STRINGS.include?(offset_table[0..3])

  table_count = (offset_table[4].ord * 256) + offset_table[5].ord
  tables = io.read(table_count * 16)
  return unless tables&.length == table_count * 16

  table_count.times do |index|
    start = index * 16
    next unless tables[start..start + 3] == 'OS/2'

    offset, = tables[start + 8..start + 15].unpack('NN')
    io.seek(offset)
    data = io.read(6)
    return data[4..5].unpack1('n') if data&.length == 6
  end

  nil
end