Class: Pdfrb::Font::TrueType::File

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/font/true_type/file.rb

Overview

Minimal TrueType / OpenType font file reader. Parses the sfnt header + table directory; lazy-loads individual tables on access.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ File

Returns a new instance of File.



12
13
14
15
16
# File 'lib/pdfrb/font/true_type/file.rb', line 12

def initialize(data)
  @data = data.b
  @tables = {}
  parse_directory
end

Instance Attribute Details

#num_tablesObject (readonly)

Returns the value of attribute num_tables.



10
11
12
# File 'lib/pdfrb/font/true_type/file.rb', line 10

def num_tables
  @num_tables
end

#tablesObject (readonly)

Returns the value of attribute tables.



10
11
12
# File 'lib/pdfrb/font/true_type/file.rb', line 10

def tables
  @tables
end

Class Method Details

.read(path) ⇒ Object



18
19
20
# File 'lib/pdfrb/font/true_type/file.rb', line 18

def self.read(path)
  new(::File.binread(path))
end

Instance Method Details

#cmapObject



48
# File 'lib/pdfrb/font/true_type/file.rb', line 48

def cmap; @cmap ||= Cmap.new(cmap_table); end

#cmap_tableObject



33
# File 'lib/pdfrb/font/true_type/file.rb', line 33

def cmap_table; table("cmap"); end

#glyf_tableObject



41
# File 'lib/pdfrb/font/true_type/file.rb', line 41

def glyf_table; table("glyf"); end

#headObject

Parsed-table accessors. Lazy and memoised on the instance.



45
# File 'lib/pdfrb/font/true_type/file.rb', line 45

def head; @head ||= Head.new(head_table); end

#head_tableObject



34
# File 'lib/pdfrb/font/true_type/file.rb', line 34

def head_table; table("head"); end

#hheaObject



46
# File 'lib/pdfrb/font/true_type/file.rb', line 46

def hhea; @hhea ||= Hhea.new(hhea_table); end

#hhea_tableObject



35
# File 'lib/pdfrb/font/true_type/file.rb', line 35

def hhea_table; table("hhea"); end

#hmtxObject



50
51
52
# File 'lib/pdfrb/font/true_type/file.rb', line 50

def hmtx
  @hmtx ||= Hmtx.new(hmtx_table, number_of_hmetrics: hhea.number_of_hmetrics || 0)
end

#hmtx_tableObject



36
# File 'lib/pdfrb/font/true_type/file.rb', line 36

def hmtx_table; table("hmtx"); end

#loca_tableObject



42
# File 'lib/pdfrb/font/true_type/file.rb', line 42

def loca_table; table("loca"); end

#maxp_tableObject



38
# File 'lib/pdfrb/font/true_type/file.rb', line 38

def maxp_table; table("maxp"); end

#name_tableObject



37
# File 'lib/pdfrb/font/true_type/file.rb', line 37

def name_table; table("name"); end

#num_glyphsObject



54
55
56
57
58
# File 'lib/pdfrb/font/true_type/file.rb', line 54

def num_glyphs
  return 0 unless maxp_table

  maxp_table.getbyte(4) * 256 + maxp_table.getbyte(5)
end

#os2Object



47
# File 'lib/pdfrb/font/true_type/file.rb', line 47

def os2; @os2 ||= OS2.new(os2_table); end

#os2_tableObject



40
# File 'lib/pdfrb/font/true_type/file.rb', line 40

def os2_table; table("OS/2"); end

#post_tableObject



39
# File 'lib/pdfrb/font/true_type/file.rb', line 39

def post_table; table("post"); end

#table(tag) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/pdfrb/font/true_type/file.rb', line 22

def table(tag)
  return @tables[tag][:data] if @tables[tag]

  entry = find_table_entry(tag)
  return nil unless entry

  offset = entry[:offset]
  length = entry[:length]
  @data.byteslice(offset, length).force_encoding(::Encoding::BINARY)
end