Class: Arrolio::Flowables::ListFlowable

Inherits:
Arrolio::Flowable show all
Defined in:
lib/arrolio/flowables/list_flowable.rb

Overview

Hanging-indent list. Each item has a marker column (fixed width) and a body column (wraps under body, not marker).

Direct Known Subclasses

NoteFlowable

Instance Attribute Summary collapse

Attributes inherited from Arrolio::Flowable

#style

Instance Method Summary collapse

Methods inherited from Arrolio::Flowable

#keep_together?, #keep_with_next?, #page_break_after?, #page_break_before?, #space_after, #space_before, #split

Constructor Details

#initialize(items, kind: :bullet, style: Style::Definition.new, marker_width: 18.0, body_indent: 6.0) ⇒ ListFlowable

Returns a new instance of ListFlowable.



10
11
12
13
14
15
16
17
18
# File 'lib/arrolio/flowables/list_flowable.rb', line 10

def initialize(items, kind: :bullet, style: Style::Definition.new,
               marker_width: 18.0, body_indent: 6.0)
  super(style: style)
  @kind = kind.to_sym
  @marker_width = marker_width.to_f
  @body_indent = body_indent.to_f
  @items = items
  freeze
end

Instance Attribute Details

#body_indentObject (readonly)

Returns the value of attribute body_indent.



8
9
10
# File 'lib/arrolio/flowables/list_flowable.rb', line 8

def body_indent
  @body_indent
end

#itemsObject (readonly)

Returns the value of attribute items.



8
9
10
# File 'lib/arrolio/flowables/list_flowable.rb', line 8

def items
  @items
end

#kindObject (readonly)

Returns the value of attribute kind.



8
9
10
# File 'lib/arrolio/flowables/list_flowable.rb', line 8

def kind
  @kind
end

#marker_widthObject (readonly)

Returns the value of attribute marker_width.



8
9
10
# File 'lib/arrolio/flowables/list_flowable.rb', line 8

def marker_width
  @marker_width
end

Instance Method Details

#do_split(width, remaining_height, context = nil) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/arrolio/flowables/list_flowable.rb', line 68

def do_split(width, remaining_height, context = nil)
  head_items = []
  tail_items = []
  used = 0.0

  @items.each do |item|
    _, body_flowables = item
    item_h = body_flowables.sum { |f| f.height(width - @marker_width - @body_indent, context) }
    if used + item_h <= remaining_height || head_items.empty?
      head_items << item
      used += item_h
    else
      tail_items << item
    end
  end

  head = if head_items.empty?
  nil
else
  self.class.new(head_items, kind: @kind, style: @style,
                       marker_width: @marker_width,
                       body_indent: @body_indent)
end
  tail = if tail_items.empty?
  nil
else
  self.class.new(tail_items, kind: @kind, style: @style,
                       marker_width: @marker_width,
                       body_indent: @body_indent)
end
  [head, tail]
end

#emit(x, y, width, context = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/arrolio/flowables/list_flowable.rb', line 28

def emit(x, y, width, context = nil)
  boxes = []
  consumed = 0.0
  cursor = y
  body_x = x + @marker_width + @body_indent
  body_width = width - @marker_width - @body_indent

  @items.each_with_index do |item, idx|
    marker_text, body_flowables = item
    marker = marker_text || default_marker(idx)

    if marker.is_a?(String)
      if marker.empty?
        # no marker to draw
      else
        mboxes, = marker_flowable(marker, x, cursor, width, context).emit(x, cursor, width, context)
        boxes.concat(mboxes)
      end
    elsif marker
      # marker is a Flowable (e.g. NoteFlowable passes a TextFlowable)
      mboxes, = marker.emit(x, cursor, width, context)
      boxes.concat(mboxes)
    end

    body_flowables.each do |f|
      fh = f.height(body_width, context)
      fboxes, = f.emit(body_x, cursor, body_width, context)
      boxes.concat(fboxes)
      cursor -= fh
      consumed += fh
    end
  end

  [boxes, consumed]
end

#height(width, context = nil) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/arrolio/flowables/list_flowable.rb', line 20

def height(width, context = nil)
  @items.sum do |item|
    _, body_flowables = item
    body_width = width - @marker_width - @body_indent
    body_flowables.sum { |f| f.height(body_width, context) }
  end
end

#splittable?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/arrolio/flowables/list_flowable.rb', line 64

def splittable?
  true
end