Class: TTFunk::File

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

Overview

File represents an individual font. It can represents both TrueType and OpenType fonts.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents, offset = 0) ⇒ File

Returns a new instance of File.

Parameters:

  • contents (String)

    binary string containg the font data

  • offset (Integer) (defaults to: 0)

    offset at which the font data starts



148
149
150
151
# File 'lib/ttfunk.rb', line 148

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

Instance Attribute Details

#contentsString (readonly)

Raw content of the font.

Returns:

  • (String)


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

def contents
  @contents
end

#directoryTTFunk::Directory (readonly)

Font tables directory.

Returns:



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

def directory
  @directory
end

Class Method Details

.from_dfont(file, which = 0) ⇒ TTFunk::File

Load a font from a resource file.

Parameters:

  • file (String, Pathname)

    Path to the resource file.

  • which (Integer, String) (defaults to: 0)

    index or name of the font to load

Returns:



68
69
70
# File 'lib/ttfunk.rb', line 68

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

.from_ttc(io, which = 0) ⇒ TTFunk::File .from_ttc(file_path, which = 0) ⇒ TTFunk::File

Load a font from a TrueType collection.

Overloads:

  • .from_ttc(io, which = 0) ⇒ TTFunk::File

    Parameters:

    • file (IO)

      IO to read the collection from.

    • which (Integer) (defaults to: 0)

      index of the font to load

    Returns:

  • .from_ttc(file_path, which = 0) ⇒ TTFunk::File

    Parameters:

    • file_path (String, Pathname)

      Path to the resource file.

    • which (Integer) (defaults to: 0)

      index of the font to load

    Returns:



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

def self.from_ttc(file, which = 0)
  Collection.open(file) { |ttc| ttc[which] }
end

.open(io) ⇒ TTFunk::File .open(path) ⇒ TTFunk::File

Open font file

Overloads:

  • .open(io) ⇒ TTFunk::File

    Parameters:

    • io (IO)

      IO to read font content from. IO position and binmode might change.

    Returns:

  • .open(path) ⇒ TTFunk::File

    Parameters:

    • path (String, Pathname)

      Path to file to read the font from.

    Returns:



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

def self.open(io_or_path)
  new(verify_and_read(io_or_path))
end

.verify_and_open(io) ⇒ io .verify_and_open(path) ⇒ IO

Deprecated.

This method might retain open files for longer than necessary.

Turn a path or IO into an IO convenient for TTFunk. The resulting IO is going to be in bin mode and its position set to the beginning.

Overloads:

  • .verify_and_open(io) ⇒ io

    Parameters:

    • io (IO)

      IO to prepare. Its position and binmode might change.

    Returns:

    • (io)
  • .verify_and_open(path) ⇒ IO

    Returns newly opened IO for the path.

    Parameters:

    • path (String, Pathname)

      path of the file to turn into an IO.

    Returns:

    • (IO)

      newly opened IO for the path

Raises:

  • (ArgumentError)

See Also:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ttfunk.rb', line 98

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_or_path.open('rb')
end

.verify_and_read(io) ⇒ String .verify_and_read(path) ⇒ String

Read contents of a path or IO.

Overloads:

  • .verify_and_read(io) ⇒ String

    Parameters:

    • io (IO)

      IO to read from. Its position and binmode might change. IO is read from the beginning regardless of its initial position.

    Returns:

    • (String)
  • .verify_and_read(path) ⇒ String

    Parameters:

    • path (String, Pathname)

      path of the file to read.

    Returns:

    • (String)

Raises:

  • (ArgumentError)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ttfunk.rb', line 127

def self.verify_and_read(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.read
  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_or_path.binread
end

Instance Method Details

#ascentInteger

Glyphs ascent as defined for in the font.

Returns:

  • (Integer)


156
157
158
159
# File 'lib/ttfunk.rb', line 156

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

#bboxArray(Integer, Integer, Integer, Integer)

Glyps bounding box as defined in the font.

Returns:

  • (Array(Integer, Integer, Integer, Integer))


180
181
182
# File 'lib/ttfunk.rb', line 180

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

#cffTable::Table::Cff?

Compact Font Format (‘CFF `) table

Returns:

  • (Table::Table::Cff, nil)


279
280
281
# File 'lib/ttfunk.rb', line 279

def cff
  @cff ||= TTFunk::Table::Cff.new(self)
end

#cmapTTFunk::Tbale::Cmap?

Character to Glyph Index Mapping (‘cmap`) table

Returns:

  • (TTFunk::Tbale::Cmap, nil)


202
203
204
# File 'lib/ttfunk.rb', line 202

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

#descentInteger

Glyphs descent as defined in the font.

Returns:

  • (Integer)


164
165
166
167
# File 'lib/ttfunk.rb', line 164

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

#digital_signatureTTFunk::Table::Dsig?

Digital Signature (‘DSIG`) table

Returns:



296
297
298
299
300
301
# File 'lib/ttfunk.rb', line 296

def digital_signature
  @digital_signature ||=
    if directory.tables.include?(TTFunk::Table::Dsig::TAG)
      TTFunk::Table::Dsig.new(self)
    end
end

#directory_info(tag) ⇒ Hash?

Font directory entry for the table with the provided tag.

Parameters:

  • tag (String)

    table tab

Returns:

  • (Hash, nil)


188
189
190
# File 'lib/ttfunk.rb', line 188

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

#find_glyph(glyph_id) ⇒ TTFunk::Table::Cff::Charstring, ...

Find glyph by its index.

Returns:



308
309
310
311
312
313
314
# File 'lib/ttfunk.rb', line 308

def find_glyph(glyph_id)
  if cff.exists?
    cff.top_index[0].charstrings_index[glyph_id].glyph
  else
    glyph_outlines.for(glyph_id)
  end
end

#glyph_locationsTTFunk::Table::Loca?

Index to Location (‘loca`) table

Returns:



258
259
260
# File 'lib/ttfunk.rb', line 258

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

#glyph_outlinesTTFunk::Table::Glyf?

Glyph Data (‘glyf`) table

Returns:



265
266
267
# File 'lib/ttfunk.rb', line 265

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

#headerTTFunk::Table::Head?

Font Header (‘head`) table

Returns:



195
196
197
# File 'lib/ttfunk.rb', line 195

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

#horizontal_headerTTFunk::Table::Hhea?

Horizontal Header (‘hhea`) table

Returns:



209
210
211
# File 'lib/ttfunk.rb', line 209

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

#horizontal_metricsTTFunk::Table::Hmtx?

Horizontal Metrics (‘hmtx`) table

Returns:



216
217
218
# File 'lib/ttfunk.rb', line 216

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

#kerningTTFunk::Table::Kern?

Kerning (‘kern`) table

Returns:



230
231
232
# File 'lib/ttfunk.rb', line 230

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

#line_gapInteger

Line gap as defined in the font.

Returns:

  • (Integer)


172
173
174
175
# File 'lib/ttfunk.rb', line 172

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

#maximum_profileTTFunk::Table::Maxp?

Maximum Profile (‘maxp`) table

Returns:



223
224
225
# File 'lib/ttfunk.rb', line 223

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

#nameTTFunk::Table::Name?

Naming (‘name`) table

Returns:



237
238
239
# File 'lib/ttfunk.rb', line 237

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

#os2TTFunk::Table:OS2?

OS/2 and Windows Metrics (‘OS/2`) table

Returns:

  • (TTFunk::Table:OS2, nil)


244
245
246
# File 'lib/ttfunk.rb', line 244

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

#postscriptTTFunk::Table::Post?

PostScript (‘post`) table

Returns:



251
252
253
# File 'lib/ttfunk.rb', line 251

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

#sbixTTFunk::Table::Sbix?

Standard Bitmap Graphics (‘sbix`) table

Returns:



272
273
274
# File 'lib/ttfunk.rb', line 272

def sbix
  @sbix ||= TTFunk::Table::Sbix.new(self)
end

#vertical_originsTTFunk::Table::Vorg?

Vertical Origin (‘VORG`) table

Returns:



286
287
288
289
290
291
# File 'lib/ttfunk.rb', line 286

def vertical_origins
  @vertical_origins ||=
    if directory.tables.include?(TTFunk::Table::Vorg::TAG)
      TTFunk::Table::Vorg.new(self)
    end
end