Class: TTFunk::File

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ File

Returns a new instance of File.



39
40
41
42
# File 'lib/ttfunk.rb', line 39

def initialize(contents)
  @contents = StringIO.new(contents)
  @directory = Directory.new(@contents)
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



9
10
11
# File 'lib/ttfunk.rb', line 9

def contents
  @contents
end

#directoryObject (readonly)

Returns the value of attribute directory.



10
11
12
# File 'lib/ttfunk.rb', line 10

def directory
  @directory
end

Class Method Details

.from_dfont(file, which = 0) ⇒ Object



16
17
18
# File 'lib/ttfunk.rb', line 16

def self.from_dfont(file, which=0)
  new(ResourceFile.open(file) { |dfont| dfont["sfnt", which] })
end

.open(io_or_path) ⇒ Object



12
13
14
# File 'lib/ttfunk.rb', line 12

def self.open(io_or_path)
  new verify_and_open(io_or_path).read
end

.verify_and_open(io_or_path) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ttfunk.rb', line 20

def self.verify_and_open(io_or_path)
  # File or IO
  if io_or_path.respond_to?(:rewind)
    io = io_or_path
    # Rewind if the object we're passed is an IO, so that multiple embeds of
    # the same IO object will work
    io.rewind
    # read the file as binary so the size is calculated correctly
    # guard binmode because some objects acting io-like don't implement it
    io.binmode if io.respond_to?(:binmode)
    return io
  end
  # String or Pathname
  io_or_path = Pathname.new(io_or_path)
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
  io = io_or_path.open('rb')
  io
end

Instance Method Details

#ascentObject



45
46
47
# File 'lib/ttfunk.rb', line 45

def ascent
  @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) || horizontal_header.ascent
end

#bboxObject



57
58
59
# File 'lib/ttfunk.rb', line 57

def bbox
  [header.x_min, header.y_min, header.x_max, header.y_max]
end

#cmapObject



70
71
72
# File 'lib/ttfunk.rb', line 70

def cmap
  @cmap ||= TTFunk::Table::Cmap.new(self)
end

#descentObject



49
50
51
# File 'lib/ttfunk.rb', line 49

def descent
  @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) || horizontal_header.descent
end

#directory_info(tag) ⇒ Object



62
63
64
# File 'lib/ttfunk.rb', line 62

def directory_info(tag)
  directory.tables[tag.to_s]
end

#glyph_locationsObject



102
103
104
# File 'lib/ttfunk.rb', line 102

def glyph_locations
  @glyph_locations ||= TTFunk::Table::Loca.new(self)
end

#glyph_outlinesObject



106
107
108
# File 'lib/ttfunk.rb', line 106

def glyph_outlines
  @glyph_outlines ||= TTFunk::Table::Glyf.new(self)
end

#headerObject



66
67
68
# File 'lib/ttfunk.rb', line 66

def header
  @header ||= TTFunk::Table::Head.new(self)
end

#horizontal_headerObject



74
75
76
# File 'lib/ttfunk.rb', line 74

def horizontal_header
  @horizontal_header ||= TTFunk::Table::Hhea.new(self)
end

#horizontal_metricsObject



78
79
80
# File 'lib/ttfunk.rb', line 78

def horizontal_metrics
  @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)
end

#kerningObject



86
87
88
# File 'lib/ttfunk.rb', line 86

def kerning
  @kerning ||= TTFunk::Table::Kern.new(self)
end

#line_gapObject



53
54
55
# File 'lib/ttfunk.rb', line 53

def line_gap
  @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) || horizontal_header.line_gap
end

#maximum_profileObject



82
83
84
# File 'lib/ttfunk.rb', line 82

def maximum_profile
  @maximum_profile ||= TTFunk::Table::Maxp.new(self)
end

#nameObject



90
91
92
# File 'lib/ttfunk.rb', line 90

def name
  @name ||= TTFunk::Table::Name.new(self)
end

#os2Object



94
95
96
# File 'lib/ttfunk.rb', line 94

def os2
  @os2 ||= TTFunk::Table::OS2.new(self)
end

#postscriptObject



98
99
100
# File 'lib/ttfunk.rb', line 98

def postscript
  @postscript ||= TTFunk::Table::Post.new(self)
end