Class: Pdfrb::Layout::BoxFitter

Inherits:
Object
  • Object
show all
Defined in:
lib/pdfrb/layout/box_fitter.rb

Overview

Fits an ordered list of boxes into a Frame, flowing to a new Frame (provided by a block) when one fills up.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boxes:, frame:) ⇒ BoxFitter

Returns a new instance of BoxFitter.



10
11
12
13
14
15
# File 'lib/pdfrb/layout/box_fitter.rb', line 10

def initialize(boxes:, frame:)
  @boxes = boxes
  @frame = frame
  @result = []
  @overflow = []
end

Instance Attribute Details

#boxesObject (readonly)

Returns the value of attribute boxes.



8
9
10
# File 'lib/pdfrb/layout/box_fitter.rb', line 8

def boxes
  @boxes
end

#frameObject (readonly)

Returns the value of attribute frame.



8
9
10
# File 'lib/pdfrb/layout/box_fitter.rb', line 8

def frame
  @frame
end

#overflowObject (readonly)

Returns the value of attribute overflow.



8
9
10
# File 'lib/pdfrb/layout/box_fitter.rb', line 8

def overflow
  @overflow
end

#resultObject (readonly)

Returns the value of attribute result.



8
9
10
# File 'lib/pdfrb/layout/box_fitter.rb', line 8

def result
  @result
end

Instance Method Details

#draw(canvas) ⇒ Object



43
44
45
46
47
# File 'lib/pdfrb/layout/box_fitter.rb', line 43

def draw(canvas)
  @result.each do |box, pos|
    box.draw(canvas, pos[0], pos[1])
  end
end

#fitObject



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
# File 'lib/pdfrb/layout/box_fitter.rb', line 17

def fit
  current_frame = @frame
  cursor_y = current_frame.top
  @boxes.each do |box|
    loop do
      position = current_frame.find_available_area(box.width || current_frame.width, box.height || (cursor_y - current_frame.bottom))
      if position && box.fit?(position[2] || current_frame.width, position[3] || (cursor_y - current_frame.bottom))
        @result << [box, position]
        current_frame.remove_area(position[0], position[1] - box.height, box.width, box.height)
        cursor_y = position[1] - box.height
        break
      else
        next_frame = yield if block_given?
        if next_frame
          current_frame = next_frame
          cursor_y = current_frame.top
        else
          @overflow << box
          break
        end
      end
    end
  end
  self
end

#fit_complete?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/pdfrb/layout/box_fitter.rb', line 49

def fit_complete?
  @overflow.empty?
end