Class: TTFunk::File
- Inherits:
-
Object
- Object
- TTFunk::File
- Defined in:
- lib/ttfunk.rb
Overview
File represents an individual font. It can represents both TrueType and OpenType fonts.
Instance Attribute Summary collapse
-
#contents ⇒ String
readonly
Raw content of the font.
-
#directory ⇒ TTFunk::Directory
readonly
Font tables directory.
Class Method Summary collapse
-
.from_dfont(file, which = 0) ⇒ TTFunk::File
Load a font from a resource file.
-
.from_ttc(file, which = 0) ⇒ Object
Load a font from a TrueType collection.
-
.open(io_or_path) ⇒ Object
Open font file.
-
.verify_and_open(io_or_path) ⇒ Object
deprecated
Deprecated.
This method might retain open files for longer than necessary.
-
.verify_and_read(io_or_path) ⇒ Object
Read contents of a path or IO.
Instance Method Summary collapse
-
#ascent ⇒ Integer
Glyphs ascent as defined for in the font.
-
#bbox ⇒ Array(Integer, Integer, Integer, Integer)
Glyps bounding box as defined in the font.
-
#cff ⇒ Table::Table::Cff?
Compact Font Format (‘CFF `) table.
-
#cmap ⇒ TTFunk::Tbale::Cmap?
Character to Glyph Index Mapping (‘cmap`) table.
-
#descent ⇒ Integer
Glyphs descent as defined in the font.
-
#digital_signature ⇒ TTFunk::Table::Dsig?
Digital Signature (‘DSIG`) table.
-
#directory_info(tag) ⇒ Hash?
Font directory entry for the table with the provided tag.
-
#find_glyph(glyph_id) ⇒ TTFunk::Table::Cff::Charstring, ...
Find glyph by its index.
-
#glyph_locations ⇒ TTFunk::Table::Loca?
Index to Location (‘loca`) table.
-
#glyph_outlines ⇒ TTFunk::Table::Glyf?
Glyph Data (‘glyf`) table.
-
#header ⇒ TTFunk::Table::Head?
Font Header (‘head`) table.
-
#horizontal_header ⇒ TTFunk::Table::Hhea?
Horizontal Header (‘hhea`) table.
-
#horizontal_metrics ⇒ TTFunk::Table::Hmtx?
Horizontal Metrics (‘hmtx`) table.
-
#initialize(contents, offset = 0) ⇒ File
constructor
A new instance of File.
-
#kerning ⇒ TTFunk::Table::Kern?
Kerning (‘kern`) table.
-
#line_gap ⇒ Integer
Line gap as defined in the font.
-
#maximum_profile ⇒ TTFunk::Table::Maxp?
Maximum Profile (‘maxp`) table.
-
#name ⇒ TTFunk::Table::Name?
Naming (‘name`) table.
-
#os2 ⇒ TTFunk::Table:OS2?
OS/2 and Windows Metrics (‘OS/2`) table.
-
#postscript ⇒ TTFunk::Table::Post?
PostScript (‘post`) table.
-
#sbix ⇒ TTFunk::Table::Sbix?
Standard Bitmap Graphics (‘sbix`) table.
-
#vertical_origins ⇒ TTFunk::Table::Vorg?
Vertical Origin (‘VORG`) table.
Constructor Details
Instance Attribute Details
#contents ⇒ String (readonly)
Raw content of the font.
44 45 46 |
# File 'lib/ttfunk.rb', line 44 def contents @contents end |
#directory ⇒ TTFunk::Directory (readonly)
Font tables directory.
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.
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.
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
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
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.
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.
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
#ascent ⇒ Integer
Glyphs ascent as defined for in the font.
156 157 158 159 |
# File 'lib/ttfunk.rb', line 156 def ascent @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) || horizontal_header.ascent end |
#bbox ⇒ Array(Integer, Integer, Integer, Integer)
Glyps bounding box as defined in the font.
180 181 182 |
# File 'lib/ttfunk.rb', line 180 def bbox [header.x_min, header.y_min, header.x_max, header.y_max] end |
#cff ⇒ Table::Table::Cff?
Compact Font Format (‘CFF `) table
279 280 281 |
# File 'lib/ttfunk.rb', line 279 def cff @cff ||= TTFunk::Table::Cff.new(self) end |
#cmap ⇒ TTFunk::Tbale::Cmap?
Character to Glyph Index Mapping (‘cmap`) table
202 203 204 |
# File 'lib/ttfunk.rb', line 202 def cmap @cmap ||= TTFunk::Table::Cmap.new(self) end |
#descent ⇒ Integer
Glyphs descent as defined in the font.
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_signature ⇒ TTFunk::Table::Dsig?
Digital Signature (‘DSIG`) table
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.
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.
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_locations ⇒ TTFunk::Table::Loca?
Index to Location (‘loca`) table
258 259 260 |
# File 'lib/ttfunk.rb', line 258 def glyph_locations @glyph_locations ||= TTFunk::Table::Loca.new(self) end |
#glyph_outlines ⇒ TTFunk::Table::Glyf?
Glyph Data (‘glyf`) table
265 266 267 |
# File 'lib/ttfunk.rb', line 265 def glyph_outlines @glyph_outlines ||= TTFunk::Table::Glyf.new(self) end |
#header ⇒ TTFunk::Table::Head?
Font Header (‘head`) table
195 196 197 |
# File 'lib/ttfunk.rb', line 195 def header @header ||= TTFunk::Table::Head.new(self) end |
#horizontal_header ⇒ TTFunk::Table::Hhea?
Horizontal Header (‘hhea`) table
209 210 211 |
# File 'lib/ttfunk.rb', line 209 def horizontal_header @horizontal_header ||= TTFunk::Table::Hhea.new(self) end |
#horizontal_metrics ⇒ TTFunk::Table::Hmtx?
Horizontal Metrics (‘hmtx`) table
216 217 218 |
# File 'lib/ttfunk.rb', line 216 def horizontal_metrics @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self) end |
#kerning ⇒ TTFunk::Table::Kern?
Kerning (‘kern`) table
230 231 232 |
# File 'lib/ttfunk.rb', line 230 def kerning @kerning ||= TTFunk::Table::Kern.new(self) end |
#line_gap ⇒ Integer
Line gap as defined in the font.
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_profile ⇒ TTFunk::Table::Maxp?
Maximum Profile (‘maxp`) table
223 224 225 |
# File 'lib/ttfunk.rb', line 223 def maximum_profile @maximum_profile ||= TTFunk::Table::Maxp.new(self) end |
#name ⇒ TTFunk::Table::Name?
Naming (‘name`) table
237 238 239 |
# File 'lib/ttfunk.rb', line 237 def name @name ||= TTFunk::Table::Name.new(self) end |
#os2 ⇒ TTFunk::Table:OS2?
OS/2 and Windows Metrics (‘OS/2`) table
244 245 246 |
# File 'lib/ttfunk.rb', line 244 def os2 @os2 ||= TTFunk::Table::OS2.new(self) end |
#postscript ⇒ TTFunk::Table::Post?
PostScript (‘post`) table
251 252 253 |
# File 'lib/ttfunk.rb', line 251 def postscript @postscript ||= TTFunk::Table::Post.new(self) end |
#sbix ⇒ TTFunk::Table::Sbix?
Standard Bitmap Graphics (‘sbix`) table
272 273 274 |
# File 'lib/ttfunk.rb', line 272 def sbix @sbix ||= TTFunk::Table::Sbix.new(self) end |