Class: Grawlix::GlyphSizeCache
- Inherits:
-
Object
- Object
- Grawlix::GlyphSizeCache
- Defined in:
- lib/grawlix/glyph_size_cache.rb
Constant Summary collapse
- DEFAULT_FONT_PATH =
File.join(File.dirname(__FILE__), '..', '..', 'OpenSans-Regular.ttf')
Instance Method Summary collapse
- #em_size ⇒ Object
-
#initialize(font_path = DEFAULT_FONT_PATH) ⇒ GlyphSizeCache
constructor
A new instance of GlyphSizeCache.
- #width_for(character) ⇒ Object
Constructor Details
#initialize(font_path = DEFAULT_FONT_PATH) ⇒ GlyphSizeCache
Returns a new instance of GlyphSizeCache.
7 8 9 10 |
# File 'lib/grawlix/glyph_size_cache.rb', line 7 def initialize(font_path = DEFAULT_FONT_PATH) @file = TTFunk::File.open(font_path) @cache = {} end |
Instance Method Details
#em_size ⇒ Object
16 17 18 |
# File 'lib/grawlix/glyph_size_cache.rb', line 16 def em_size @em ||= @file.header.units_per_em end |
#width_for(character) ⇒ Object
12 13 14 |
# File 'lib/grawlix/glyph_size_cache.rb', line 12 def width_for(character) @cache[character] ||= @file.horizontal_metrics.for(@file.cmap.unicode.first[character.unpack("U*").first]).advance_width.to_f / em_size end |