File: orientation_detector.rb

package info (click to toggle)
ruby-pdf-reader 2.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,028 kB
  • sloc: ruby: 8,673; makefile: 10
file content (33 lines) | stat: -rw-r--r-- 845 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# coding: utf-8

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
      height          = ury.to_i - lly.to_i
      if width > height
        [0,180].include?(rotation) ? 'landscape' : 'portrait'
      else
        [0,180].include?(rotation) ? 'portrait' : 'landscape'
      end
    end
  end
end