Sha256: e77d445bc14d602306ba6d7e79eca8c09b2be10b6cc1c2d2b8f44378f5b589f8

Contents?: true

Size: 1.68 KB

Versions: 10

Compression:

Stored size: 1.68 KB

Contents

# coding: utf-8
# frozen_string_literal: true

#

require 'forwardable'

class PDF::Reader
  # A Hash-like object that wraps the array of glyph widths in a CID font
  # and gives us a nice way to query it for specific widths.
  #
  # there are two ways to calculate a cidfont_glyph_width, that are defined
  # in Section 9.7.4.3 PDF 32000-1:2008 pp 271, the differences are remarked
  # on below. because of these difference that may be contained within the
  # same array, it is a bit difficult to parse this array.
  class CidWidths
    extend Forwardable

    # Graphics State Operators
    def_delegators :@widths, :[], :fetch

    def initialize(default, array)
      @widths = parse_array(default, array.dup)
    end

    private

    def parse_array(default, array)
      widths  = Hash.new(default)
      params = []
      while array.size > 0
        params << array.shift

        if params.size == 2 && params.last.is_a?(Array)
          widths.merge! parse_first_form(params.first, params.last)
          params = []
        elsif params.size == 3
          widths.merge! parse_second_form(params[0], params[1], params[2])
          params = []
        end
      end
      widths
    end

    # this is the form 10 [234 63 234 346 47 234] where width of index 10 is
    # 234, index 11 is 63, etc
    def parse_first_form(first, widths)
      widths.inject({}) { |accum, glyph_width|
        accum[first + accum.size] = glyph_width
        accum
      }
    end

    # this is the form 10 20 123 where all index between 10 and 20 have width 123
    def parse_second_form(first, final, width)
      (first..final).inject({}) { |accum, index|
        accum[index] = width
        accum
      }
    end

  end
end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
pdf-reader-2.6.0 lib/pdf/reader/cid_widths.rb
pdf-reader-2.5.0 lib/pdf/reader/cid_widths.rb
pdf-reader-2.4.2 lib/pdf/reader/cid_widths.rb
pdf-reader-2.4.1 lib/pdf/reader/cid_widths.rb
pdf-reader-2.4.0 lib/pdf/reader/cid_widths.rb
pdf-reader-2.3.0 lib/pdf/reader/cid_widths.rb
pdf-reader-2.2.1 lib/pdf/reader/cid_widths.rb
embulk-input-druginfo_interview_form-0.1.0 vendor/bundle/ruby/2.5.0/gems/pdf-reader-2.2.0/lib/pdf/reader/cid_widths.rb
embulk-input-druginfo_interview_form-0.1.0 vendor/bundle/ruby/2.4.0/gems/pdf-reader-2.2.0/lib/pdf/reader/cid_widths.rb
pdf-reader-2.2.0 lib/pdf/reader/cid_widths.rb