Sha256: 25767f7705e023a616621e533aa5d2f46b94a6fdaa165d89f1faf4bc92f8a741

Contents?: true

Size: 1.07 KB

Versions: 4

Compression:

Stored size: 1.07 KB

Contents

# encoding: utf-8
#
# font_metric_cache.rb : The Prawn font class
#
# Copyright Dec 2012, Kenneth Kalmer. All Rights Reserved.
#
# This is free software. Please see the LICENSE and COPYING files for details.
#

module Prawn

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

    CacheEntry = Struct.new( :font, :options, :string )

    def initialize( document )
      @document = document

      @cache = {}
    end

    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

  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
prawn-0.14.0 lib/prawn/font_metric_cache.rb
prawn-0.13.2 lib/prawn/font_metric_cache.rb
prawn-0.13.1 lib/prawn/font_metric_cache.rb
prawn-0.13.0 lib/prawn/font_metric_cache.rb