Class: Prawn::FontMetricCache

Inherits:
Object
  • Object
show all
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

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, options )
  f = if options[:style]
        # override style with :style => :bold
        @document.find_font(@document.font ? @document.font.name : 'Helvetica',
                  :style => options[:style])
      else
        @document.font
      end

  key = CacheEntry.new( f, options, string )

  unless length = @cache[ key ]
    length = @cache[ key ] = f.compute_width_of(string, options)
  end

  length +
    (@document.character_spacing * @document.font.character_count(string))
end