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

Source Code
lib/ttfunk.rb, line 148
148
def initialize(contents, offset = 0)
149
  @contents = StringIO.new(contents)
150
  @directory = Directory.new(@contents, offset)
151
end

Instance Attribute Details

#contentsString (readonly)

Raw content of the font.

Returns:

  • (String)
Source Code
lib/ttfunk.rb, line 44
44
def contents
45
  @contents
46
end

#directoryTTFunk::Directory (readonly)

Font tables directory.

Returns:

Source Code
lib/ttfunk.rb, line 48
48
def directory
49
  @directory
50
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:

Source Code
lib/ttfunk.rb, line 68
68
def self.from_dfont(file, which = 0)
69
  new(ResourceFile.open(file) { |dfont| dfont['sfnt', which] })
70
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:

Source Code
lib/ttfunk.rb, line 82
82
def self.from_ttc(file, which = 0)
83
  Collection.open(file) { |ttc| ttc[which] }
84
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:

Source Code
lib/ttfunk.rb, line 59
59
def self.open(io_or_path)
60
  new(verify_and_read(io_or_path))
61
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:

Source Code
lib/ttfunk.rb, line 98
98
def self.verify_and_open(io_or_path)
99
  # File or IO
100
  if io_or_path.respond_to?(:rewind)
101
    io = io_or_path
102
    # Rewind if the object we're passed is an IO, so that multiple embeds of
103
    # the same IO object will work
104
    io.rewind
105
    # read the file as binary so the size is calculated correctly
106
    # guard binmode because some objects acting io-like don't implement it
107
    io.binmode if io.respond_to?(:binmode)
108
    return io
109
  end
110
  # String or Pathname
111
  io_or_path = Pathname.new(io_or_path)
112
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
113
114
  io_or_path.open('rb')
115
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)
Source Code
lib/ttfunk.rb, line 127
127
def self.verify_and_read(io_or_path)
128
  # File or IO
129
  if io_or_path.respond_to?(:rewind)
130
    io = io_or_path
131
    # Rewind if the object we're passed is an IO, so that multiple embeds of
132
    # the same IO object will work
133
    io.rewind
134
    # read the file as binary so the size is calculated correctly
135
    # guard binmode because some objects acting io-like don't implement it
136
    io.binmode if io.respond_to?(:binmode)
137
    return io.read
138
  end
139
  # String or Pathname
140
  io_or_path = Pathname.new(io_or_path)
141
  raise ArgumentError, "#{io_or_path} not found" unless io_or_path.file?
142
143
  io_or_path.binread
144
end

Instance Method Details

#ascentInteger

Glyphs ascent as defined for in the font.

Returns:

  • (Integer)
Source Code
lib/ttfunk.rb, line 156
156
def ascent
157
  @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) ||
158
    horizontal_header.ascent
159
end

#bboxArray(Integer, Integer, Integer, Integer)

Glyps bounding box as defined in the font.

Returns:

  • (Array(Integer, Integer, Integer, Integer))
Source Code
lib/ttfunk.rb, line 180
180
def bbox
181
  [header.x_min, header.y_min, header.x_max, header.y_max]
182
end

#cffTable::Table::Cff?

Compact Font Format (CFF ) table

Returns:

  • (Table::Table::Cff, nil)
Source Code
lib/ttfunk.rb, line 279
279
def cff
280
  @cff ||= TTFunk::Table::Cff.new(self)
281
end

#cmapTTFunk::Tbale::Cmap?

Character to Glyph Index Mapping (cmap) table

Returns:

  • (TTFunk::Tbale::Cmap, nil)
Source Code
lib/ttfunk.rb, line 202
202
def cmap
203
  @cmap ||= TTFunk::Table::Cmap.new(self)
204
end

#descentInteger

Glyphs descent as defined in the font.

Returns:

  • (Integer)
Source Code
lib/ttfunk.rb, line 164
164
def descent
165
  @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) ||
166
    horizontal_header.descent
167
end

#digital_signatureTTFunk::Table::Dsig?

Digital Signature (DSIG) table

Returns:

Source Code
lib/ttfunk.rb, line 296
296
def digital_signature
297
  @digital_signature ||=
298
    if directory.tables.include?(TTFunk::Table::Dsig::TAG)
299
      TTFunk::Table::Dsig.new(self)
300
    end
301
end

#directory_info(tag) ⇒ Hash?

Font directory entry for the table with the provided tag.

Parameters:

  • tag (String)

    table tab

Returns:

  • (Hash, nil)
Source Code
lib/ttfunk.rb, line 188
188
def directory_info(tag)
189
  directory.tables[tag.to_s]
190
end

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

Find glyph by its index.

Returns:

Source Code
lib/ttfunk.rb, line 308
308
def find_glyph(glyph_id)
309
  if cff.exists?
310
    cff.top_index[0].charstrings_index[glyph_id].glyph
311
  else
312
    glyph_outlines.for(glyph_id)
313
  end
314
end

#glyph_locationsTTFunk::Table::Loca?

Index to Location (loca) table

Returns:

Source Code
lib/ttfunk.rb, line 258
258
def glyph_locations
259
  @glyph_locations ||= TTFunk::Table::Loca.new(self)
260
end

#glyph_outlinesTTFunk::Table::Glyf?

Glyph Data (glyf) table

Returns:

Source Code
lib/ttfunk.rb, line 265
265
def glyph_outlines
266
  @glyph_outlines ||= TTFunk::Table::Glyf.new(self)
267
end

#headerTTFunk::Table::Head?

Font Header (head) table

Returns:

Source Code
lib/ttfunk.rb, line 195
195
def header
196
  @header ||= TTFunk::Table::Head.new(self)
197
end

#horizontal_headerTTFunk::Table::Hhea?

Horizontal Header (hhea) table

Returns:

Source Code
lib/ttfunk.rb, line 209
209
def horizontal_header
210
  @horizontal_header ||= TTFunk::Table::Hhea.new(self)
211
end

#horizontal_metricsTTFunk::Table::Hmtx?

Horizontal Metrics (hmtx) table

Returns:

Source Code
lib/ttfunk.rb, line 216
216
def horizontal_metrics
217
  @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)
218
end

#kerningTTFunk::Table::Kern?

Kerning (kern) table

Returns:

Source Code
lib/ttfunk.rb, line 230
230
def kerning
231
  @kerning ||= TTFunk::Table::Kern.new(self)
232
end

#line_gapInteger

Line gap as defined in the font.

Returns:

  • (Integer)
Source Code
lib/ttfunk.rb, line 172
172
def line_gap
173
  @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) ||
174
    horizontal_header.line_gap
175
end

#maximum_profileTTFunk::Table::Maxp?

Maximum Profile (maxp) table

Returns:

Source Code
lib/ttfunk.rb, line 223
223
def maximum_profile
224
  @maximum_profile ||= TTFunk::Table::Maxp.new(self)
225
end

#nameTTFunk::Table::Name?

Naming (name) table

Returns:

Source Code
lib/ttfunk.rb, line 237
237
def name
238
  @name ||= TTFunk::Table::Name.new(self)
239
end

#os2TTFunk::Table:OS2?

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

Returns:

  • (TTFunk::Table:OS2, nil)
Source Code
lib/ttfunk.rb, line 244
244
def os2
245
  @os2 ||= TTFunk::Table::OS2.new(self)
246
end

#postscriptTTFunk::Table::Post?

PostScript (post) table

Returns:

Source Code
lib/ttfunk.rb, line 251
251
def postscript
252
  @postscript ||= TTFunk::Table::Post.new(self)
253
end

#sbixTTFunk::Table::Sbix?

Standard Bitmap Graphics (sbix) table

Returns:

Source Code
lib/ttfunk.rb, line 272
272
def sbix
273
  @sbix ||= TTFunk::Table::Sbix.new(self)
274
end

#vertical_originsTTFunk::Table::Vorg?

Vertical Origin (VORG) table

Returns:

Source Code
lib/ttfunk.rb, line 286
286
def vertical_origins
287
  @vertical_origins ||=
288
    if directory.tables.include?(TTFunk::Table::Vorg::TAG)
289
      TTFunk::Table::Vorg.new(self)
290
    end
291
end