Class: FontProcessor::FontFile
- Inherits:
-
Object
- Object
- FontProcessor::FontFile
- Defined in:
- lib/fontprocessor/font_file.rb
Overview
Inspects an OpenType font file for various tidbits of information.
Instance Method Summary collapse
-
#features ⇒ Object
Public: Gets a list of GSUB and GPOS features, in a simpler version than that returned by skytype.
-
#glyph_count ⇒ Object
Public: Gets the total number of glyphs in the font.
-
#has_postscript_outlines? ⇒ Boolean
Returns true when the font contains PostScript (sometimes referred to as CFF) outlines.
-
#has_truetype_outlines? ⇒ Boolean
Returns true when the font contains TrueType (sometimes referred to as TT) outlines.
-
#initialize(filename) ⇒ FontFile
constructor
A new instance of FontFile.
-
#metrics ⇒ Object
Public: Inspects the font for various pieces of metadata (mostly metrics) about the font.
-
#names ⇒ Object
Public: Inspects the name table of the font, which contains various metadata from the foundry/designer.
-
#optical_size ⇒ Object
Public: Gets any Optical Size feature from the font.
-
#unicode ⇒ Object
Public: Inspects which Unicode values are contained within the font.
-
#unicode_ranges ⇒ Object
Public: similar to .unicode, it returns the same data but in a compact form where contigious integers are represented as a Range.
Constructor Details
#initialize(filename) ⇒ FontFile
Returns a new instance of FontFile.
4 5 6 7 8 9 10 |
# File 'lib/fontprocessor/font_file.rb', line 4 def initialize(filename) @filename = filename raise "Font file not found: #{@filename}" unless File.exists? @filename @font_manipulator = Skytype::FontManipulator.new(File.binread(@filename)) raise "Failed to create Skytype::FontManipulator" if @font_manipulator.nil? @unicode = nil end |
Instance Method Details
#features ⇒ Object
Public: Gets a list of GSUB and GPOS features, in a simpler version than that returned by skytype.
Returns a JSON formatted string of GSUB and GPOS features
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fontprocessor/font_file.rb', line 67 def features feature_hash = {} features = JSON.parse @font_manipulator.extract_features features['tables'].each do |table| feature_hash["#{table['tag']}"] = [] table['features'].each do |feature| feature_hash["#{table['tag']}"].push feature['tag'] end end JSON.generate feature_hash end |
#glyph_count ⇒ Object
Public: Gets the total number of glyphs in the font.
Returns an unsigned 16-bit integer
52 53 54 |
# File 'lib/fontprocessor/font_file.rb', line 52 def glyph_count @font_manipulator.get_num_glyphs end |
#has_postscript_outlines? ⇒ Boolean
Returns true when the font contains PostScript (sometimes referred to as CFF) outlines
100 101 102 |
# File 'lib/fontprocessor/font_file.rb', line 100 def has_postscript_outlines? @font_manipulator.is_post_script_font? end |
#has_truetype_outlines? ⇒ Boolean
Returns true when the font contains TrueType (sometimes referred to as TT) outlines
106 107 108 |
# File 'lib/fontprocessor/font_file.rb', line 106 def has_truetype_outlines? @font_manipulator.has_table("glyf") end |
#metrics ⇒ Object
Public: Inspects the font for various pieces of metadata (mostly metrics) about the font.
Returns Hash including the following keys:
em, max_ascent, max_descent, os2_panose, upos,
optical_size, os2_winascent, os2_windescent,
os2_typoascent, os2_typodescent, os2_typolinegap,
hhea_ascent, hhea_descent, hhea_linegap
os2_weight, os2_width
43 44 45 46 47 |
# File 'lib/fontprocessor/font_file.rb', line 43 def metrics metrics ||= Skytype::FontUtils.get_metrics_as_hash(@font_manipulator) metrics.merge!({ "optical_size" => optical_size }) unless metrics.has_key?("optical_size") metrics end |
#names ⇒ Object
Public: Inspects the name table of the font, which contains various metadata from the foundry/designer.
In reality these table contains localized versions of these strings for multiple operating systems. To simplify things, we have chosen to use only use the English versions of these names for the Windows Platform.
Most of the time this isn’t an issue however occasionally foundries/designers place different values in different platforms/languages.
Returns a Hash of name record labels and their values. Possible keys include:
'Copyright', 'Family', 'Subfamily', 'Unique ID', 'Full name',
'Version', 'PostScript name', 'Trademark', 'Manufacturer', 'Designer',
'Description', 'Vendor URL', 'Designer URL', 'License Description',
'License URL', 'Reserved', 'Preferred Family', 'Preferred Subfamily',
'Compatible Full', 'Sample text', 'PostScript CID', 'WWS Family Name',
'WWS Subfamily Name'
30 31 32 |
# File 'lib/fontprocessor/font_file.rb', line 30 def names Skytype::FontUtils.get_names_as_hash(@font_manipulator) end |
#optical_size ⇒ Object
Public: Gets any Optical Size feature from the font.
Returns an unsigned 32-bit integer or nil if no Optical Size feature exists
59 60 61 |
# File 'lib/fontprocessor/font_file.rb', line 59 def optical_size @font_manipulator.get_optical_size end |
#unicode ⇒ Object
Public: Inspects which Unicode values are contained within the font.
Returns an Array of Unicode ids represented as Fixnums contained within the font.
83 84 85 86 |
# File 'lib/fontprocessor/font_file.rb', line 83 def unicode @unicode ||= @font_manipulator.unicode_as_binary_entries @unicode end |
#unicode_ranges ⇒ Object
Public: similar to .unicode, it returns the same data but in a compact form where contigious integers are represented as a Range.
Returns an Array of Unicode ids represented as Fixnums (or Ranges if contigious) contained within the font. Characters which in the cmap table are not returned.
94 95 96 |
# File 'lib/fontprocessor/font_file.rb', line 94 def unicode_ranges compact_ranges(unicode) end |