Class: Prawn::FontMetricCache
- Inherits:
-
Object
- Object
- Prawn::FontMetricCache
- Defined in:
- lib/prawn/font_metric_cache.rb
Overview
Cache used internally by Prawn::Document instances to calculate the width of various strings for layout purposes.
Defined Under Namespace
Classes: CacheEntry
Instance Method Summary collapse
-
#initialize(document) ⇒ FontMetricCache
constructor
A new instance of FontMetricCache.
- #width_of(string, options) ⇒ Object
Constructor Details
#initialize(document) ⇒ FontMetricCache
Returns a new instance of FontMetricCache.
18 19 20 21 22 |
# File 'lib/prawn/font_metric_cache.rb', line 18 def initialize( document ) @document = document @cache = {} end |
Instance Method Details
#width_of(string, options) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/prawn/font_metric_cache.rb', line 24 def width_of( string, ) f = if [:style] # override style with :style => :bold @document.find_font(@document.font ? @document.font.name : 'Helvetica', :style => [:style]) else @document.font end key = CacheEntry.new( f, , string ) unless length = @cache[ key ] length = @cache[ key ] = f.compute_width_of(string, ) end length + (@document.character_spacing * @document.font.character_count(string)) end |