class Prawn::FontMetricCache

Cache used internally by Prawn::Document instances to calculate the width of various strings for layout purposes.

@private

Constants

CacheEntry

Public Class Methods

new(document) click to toggle source
# File lib/prawn/font_metric_cache.rb, line 17
def initialize(document)
  @document = document

  @cache = {}
end

Public Instance Methods

width_of(string, options) click to toggle source
# File lib/prawn/font_metric_cache.rb, line 23
def width_of(string, options)
  f = if options[:style]
        # override style with :style => :bold
        @document.find_font(@document.font.family, :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