Module: PdfExtract::Margins

Defined in:
lib/analysis/margins.rb

Class Method Summary collapse

Class Method Details

.axis_spatials(pdf, name, axis) ⇒ Object



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
34
35
36
37
38
39
40
41
42
43
# File 'lib/analysis/margins.rb', line 6

def self.axis_spatials pdf, name, axis
  pdf.spatials name, :paged => true, :depends_on => [:regions] do |parser|
    axis_mask = MultiRange.new
    page = -1
    page_width = 0
    page_height = 0

    dimension = :width if axis == :x
    dimension = :height if axis == :y

    parser.before do
      axis_mask = MultiRange.new
      page = -1
    end

    parser.objects :regions do |region|
      if page == -1
        page = region[:page]
        page_width = region[:page_width]
        page_height = region[:page_height]
      end
      
      axis_mask.append region[axis]..(region[axis]+region[dimension])
    end

    parser.after do
      if axis_mask.count.zero?
        nil
      else
        yield axis_mask, {
          :page => page,
          :page_width => page_width,
          :page_height => page_height
        }
      end
    end
  end
end

.include_in(pdf) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/analysis/margins.rb', line 45

def self.include_in pdf
  axis_spatials pdf, :top_margins, :y do |y_mask, obj|
    obj.merge({
      :x => 0,
      :y => y_mask.max,
      :width => obj[:page_width],
      :height => obj[:page_height] - y_mask.max
    })
  end

  axis_spatials pdf, :bottom_margins, :y do |y_mask, obj|
    obj.merge({
      :x => 0,
      :y => 0,
      :width => obj[:page_width],
      :height => y_mask.min
    })
  end

  axis_spatials pdf, :left_margins, :x do |x_mask, obj|
    obj.merge({
      :x => 0,
      :y => 0,
      :width => x_mask.min,
      :height => obj[:page_height]
    })
  end

  axis_spatials pdf, :right_margins, :x do |x_mask, obj|
    obj.merge({
      :x => x_mask.max,
      :y => 0,
      :width => obj[:page_width] - x_mask.max,
      :height => obj[:page_height]
    })
  end
end