Class: Prawn::FontMetricCache

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn_ext/font_metric_cache.rb

Instance Method Summary collapse

Instance Method Details

#width_of(string, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/prawn_ext/font_metric_cache.rb', line 5

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

  encoded_string = f.normalize_encoding(string)

  key = CacheEntry.new(f.hash, options, encoded_string)

  @cache[key] ||= f.compute_width_of(encoded_string, options)

  length = @cache[key]

  character_count = @document.font.character_count(encoded_string)
  if character_count.positive?
    length += @document.character_spacing * (character_count - 1)
  end

  length
end