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(io, which = 0)  ⇒ TTFunk::File
      
        .from_ttc(file_path, which = 0)  ⇒ TTFunk::File
      
      
    
    
  
  
  
  
  
  
  
  
  
    
Load a font from a TrueType collection.
 - 
  
    
      
      
        .open(io)  ⇒ TTFunk::File
      
        .open(path)  ⇒ TTFunk::File
      
      
    
    
  
  
  
  
  
  
  
  
  
    
Open font file.
 - 
  
    
      
      
        .verify_and_open(io)  ⇒ io
      
        .verify_and_open(path)  ⇒ IO
      
      
    
    
  
  
  
  
  
  
  deprecated
  
  
    Deprecated. 
This method might retain open files for longer than necessary.
 - 
  
    
      
      
        .verify_and_read(io)  ⇒ String
      
        .verify_and_read(path)  ⇒ String
      
      
    
    
  
  
  
  
  
  
  
  
  
    
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
#initialize(contents, offset = 0) ⇒ File
Returns a new instance of File.
Source Code
148  | def initialize(contents, offset = 0)  | 
149  | @contents = StringIO.new(contents)  | 
150  | @directory = Directory.new(@contents, offset)  | 
151  | end
 | 
Instance Attribute Details
#contents ⇒ String (readonly)
Raw content of the font.
Source Code
44  | def contents  | 
45  |   @contents
 | 
46  | end
 | 
#directory ⇒ TTFunk::Directory (readonly)
Font tables directory.
Source Code
48  | def directory  | 
49  |   @directory
 | 
50  | end
 | 
Class Method Details
.from_dfont(file, which = 0) ⇒ TTFunk::File
Load a font from a resource file.
Source Code
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.
Source Code
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
Source Code
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
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.
Source Code
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.
Source Code
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
#ascent ⇒ Integer
Glyphs ascent as defined for in the font.
Source Code
156  | def ascent  | 
157  | @ascent ||= (os2.exists? && os2.ascent && os2.ascent.nonzero?) ||  | 
158  | horizontal_header.ascent  | 
159  | end
 | 
#bbox ⇒ Array(Integer, Integer, Integer, Integer)
Glyps bounding box as defined in the font.
Source Code
180  | def bbox  | 
181  | [header.x_min, header.y_min, header.x_max, header.y_max]  | 
182  | end
 | 
#cff ⇒ Table::Table::Cff?
Compact Font Format (CFF ) table
Source Code
279  | def cff  | 
280  | @cff ||= TTFunk::Table::Cff.new(self)  | 
281  | end
 | 
#cmap ⇒ TTFunk::Tbale::Cmap?
Character to Glyph Index Mapping (cmap) table
Source Code
202  | def cmap  | 
203  | @cmap ||= TTFunk::Table::Cmap.new(self)  | 
204  | end
 | 
#descent ⇒ Integer
Glyphs descent as defined in the font.
Source Code
164  | def descent  | 
165  | @descent ||= (os2.exists? && os2.descent && os2.descent.nonzero?) ||  | 
166  | horizontal_header.descent  | 
167  | end
 | 
#digital_signature ⇒ TTFunk::Table::Dsig?
Digital Signature (DSIG) table
Source Code
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.
Source Code
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.
Source Code
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_locations ⇒ TTFunk::Table::Loca?
Index to Location (loca) table
Source Code
258  | def glyph_locations  | 
259  | @glyph_locations ||= TTFunk::Table::Loca.new(self)  | 
260  | end
 | 
#glyph_outlines ⇒ TTFunk::Table::Glyf?
Glyph Data (glyf) table
Source Code
265  | def glyph_outlines  | 
266  | @glyph_outlines ||= TTFunk::Table::Glyf.new(self)  | 
267  | end
 | 
#header ⇒ TTFunk::Table::Head?
Font Header (head) table
Source Code
195  | def header  | 
196  | @header ||= TTFunk::Table::Head.new(self)  | 
197  | end
 | 
#horizontal_header ⇒ TTFunk::Table::Hhea?
Horizontal Header (hhea) table
Source Code
209  | def horizontal_header  | 
210  | @horizontal_header ||= TTFunk::Table::Hhea.new(self)  | 
211  | end
 | 
#horizontal_metrics ⇒ TTFunk::Table::Hmtx?
Horizontal Metrics (hmtx) table
Source Code
216  | def horizontal_metrics  | 
217  | @horizontal_metrics ||= TTFunk::Table::Hmtx.new(self)  | 
218  | end
 | 
#kerning ⇒ TTFunk::Table::Kern?
Kerning (kern) table
Source Code
230  | def kerning  | 
231  | @kerning ||= TTFunk::Table::Kern.new(self)  | 
232  | end
 | 
#line_gap ⇒ Integer
Line gap as defined in the font.
Source Code
172  | def line_gap  | 
173  | @line_gap ||= (os2.exists? && os2.line_gap && os2.line_gap.nonzero?) ||  | 
174  | horizontal_header.line_gap  | 
175  | end
 | 
#maximum_profile ⇒ TTFunk::Table::Maxp?
Maximum Profile (maxp) table
Source Code
223  | def maximum_profile  | 
224  | @maximum_profile ||= TTFunk::Table::Maxp.new(self)  | 
225  | end
 | 
#name ⇒ TTFunk::Table::Name?
Naming (name) table
Source Code
237  | def name  | 
238  | @name ||= TTFunk::Table::Name.new(self)  | 
239  | end
 | 
#os2 ⇒ TTFunk::Table:OS2?
OS/2 and Windows Metrics (OS/2) table
Source Code
244  | def os2  | 
245  | @os2 ||= TTFunk::Table::OS2.new(self)  | 
246  | end
 | 
#postscript ⇒ TTFunk::Table::Post?
PostScript (post) table
Source Code
251  | def postscript  | 
252  | @postscript ||= TTFunk::Table::Post.new(self)  | 
253  | end
 | 
#sbix ⇒ TTFunk::Table::Sbix?
Standard Bitmap Graphics (sbix) table
Source Code
272  | def sbix  | 
273  | @sbix ||= TTFunk::Table::Sbix.new(self)  | 
274  | end
 | 
#vertical_origins ⇒ TTFunk::Table::Vorg?
Vertical Origin (VORG) table
Source Code
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
 |