Sha256: 8df633294e919035ac3f657c3924c2b28fbdcb64aae5bd17bab4836c5e4c8f2c

Contents?: true

Size: 1.26 KB

Versions: 1

Compression:

Stored size: 1.26 KB

Contents

# coding: utf-8

require 'afm'
require 'pdf/reader/synchronized_cache'

module AFM
  # this is a monkey patch for the AFM gem. hopefully my patch will be accepted
  # upstream and I can drop this
  class Font
    def metrics_for_name(name)
      @char_metrics[name.to_s]
    end
  end
end

class PDF::Reader
  module WidthCalculator

    # Type1 fonts can be one of 14 "built in" standard fonts. In these cases,
    # the reader is expected to have it's own copy of the font metrics.
    # see Section 9.6.2.2, PDF 32000-1:2008, pp 256
    class BuiltIn

      def initialize(font)
        @font = font
        @@all_metrics ||= PDF::Reader::SynchronizedCache.new

        metrics_path = File.join(File.dirname(__FILE__), "..","afm","#{font.basefont}.afm")

        if File.file?(metrics_path)
          @metrics = @@all_metrics[metrics_path] ||= AFM::Font.new(metrics_path)
        else
          raise ArgumentError, "No built-in metrics for #{font.basefont}"
        end
      end

      def glyph_width(code_point)
        return 0 if code_point.nil? || code_point < 0

        m = @metrics.metrics_for(code_point)
        if m.nil?
          name = @font.encoding.int_to_name(code_point)
          m = @metrics.metrics_for_name(name)
        end
        m[:wx]
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pdf-reader-1.3.0 lib/pdf/reader/width_calculator/built_in.rb