Sha256: 771212f53f371186f8f954e167a8b39964b157d06313466229cdee4921d01c88

Contents?: true

Size: 879 Bytes

Versions: 4

Compression:

Stored size: 879 Bytes

Contents

# coding: utf-8
# frozen_string_literal: true

class PDF::Reader
  # Small util class for detecting the orientation of a single PDF page. Accounts
  # for any page rotation that is in place.
  #
  #     OrientationDetector.new(:MediaBox => [0,0,612,792]).orientation
  #     => "portrait"
  #
  class OrientationDetector
    def initialize(attributes)
      @attributes = attributes
    end

    def orientation
      @orientation ||= detect_orientation
    end

    private

    def detect_orientation
      llx,lly,urx,ury = @attributes[:MediaBox]
      rotation        = @attributes[:Rotate].to_i
      width           = (urx.to_i - llx.to_i).abs
      height          = (ury.to_i - lly.to_i).abs
      if width > height
        (rotation % 180).zero? ? 'landscape' : 'portrait'
      else
        (rotation % 180).zero? ? 'portrait' : 'landscape'
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pdf-reader-2.6.0 lib/pdf/reader/orientation_detector.rb
pdf-reader-2.5.0 lib/pdf/reader/orientation_detector.rb
pdf-reader-2.4.2 lib/pdf/reader/orientation_detector.rb
pdf-reader-2.4.1 lib/pdf/reader/orientation_detector.rb