Sha256: 7c4ce7391b7bf253b03ea5cb192b466283a1a6a81196afbf7eea2c3837ec869a

Contents?: true

Size: 957 Bytes

Versions: 20

Compression:

Stored size: 957 Bytes

Contents

module Prawn
  # Cache used internally by Prawn::Document instances to calculate the width
  # of various strings for layout purposes.
  #
  # @private
  class FontMetricCache
    CacheEntryMonkeyPatch = Struct.new(:font, :font_size, :options, :string)

    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 = CacheEntryMonkeyPatch.new(f, @document.font_size, 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
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
br_danfe-1.0.0 lib/prawn/font_metric_cache.rb
br_danfe-0.20.0 lib/prawn/font_metric_cache.rb
br_danfe-0.19.0 lib/prawn/font_metric_cache.rb
br_danfe-0.18.0 lib/prawn/font_metric_cache.rb
br_danfe-0.17.8 lib/prawn/font_metric_cache.rb
br_danfe-0.17.7 lib/prawn/font_metric_cache.rb
br_danfe-0.17.6 lib/prawn/font_metric_cache.rb
br_danfe-0.17.5 lib/prawn/font_metric_cache.rb
br_danfe-0.17.4 lib/prawn/font_metric_cache.rb
br_danfe-0.17.3 lib/prawn/font_metric_cache.rb
br_danfe-0.17.2 lib/prawn/font_metric_cache.rb
br_danfe-0.17.1 lib/prawn/font_metric_cache.rb
br_danfe-0.17.0 lib/prawn/font_metric_cache.rb
br_danfe-0.16.0 lib/prawn/font_metric_cache.rb
br_danfe-0.15.1 lib/prawn/font_metric_cache.rb
br_danfe-0.15.0 lib/prawn/font_metric_cache.rb
br_danfe-0.14.5 lib/prawn/font_metric_cache.rb
br_danfe-0.14.4 lib/prawn/font_metric_cache.rb
br_danfe-0.14.3 lib/prawn/font_metric_cache.rb
br_danfe-0.14.2 lib/prawn/font_metric_cache.rb