Class: Pdfrb::Layout::ListBox

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

Overview

A list of items, each rendered with a marker (bullet, decimal, alpha, roman, etc.). Children are boxes (typically TextBox).

Constant Summary collapse

MARKERS =
{
  bullet: "",
  disc: "",
  circle: "",
  square: "",
  decimal: ->(i) { "#{i + 1}." },
  lower_alpha: ->(i) { "#{('a'.ord + i).chr}." },
  upper_alpha: ->(i) { "#{('A'.ord + i).chr}." },
  lower_roman: ->(i) { "#{RomanNumeral.convert(i + 1).downcase}." },
  upper_roman: ->(i) { "#{RomanNumeral.convert(i + 1)}." },
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Box

#height, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#draw, #supports_position_flow?

Constructor Details

#initialize(items:, marker_type: :bullet) ⇒ ListBox

Returns a new instance of ListBox.



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

def initialize(items:, marker_type: :bullet, **)
  super(**)
  @items = items
  @marker_type = marker_type
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



29
30
31
# File 'lib/pdfrb/layout/list_box.rb', line 29

def items
  @items
end

#marker_typeObject (readonly)

Returns the value of attribute marker_type.



29
30
31
# File 'lib/pdfrb/layout/list_box.rb', line 29

def marker_type
  @marker_type
end

Instance Method Details

#draw_content(canvas, x, y) ⇒ Object



63
64
65
66
67
68
69
70
71
# File 'lib/pdfrb/layout/list_box.rb', line 63

def draw_content(canvas, x, y)
  marker_w = 20.0
  offset_y = y
  @items.each_with_index do |item, i|
    marker_text(canvas, x, offset_y, i)
    item.draw(canvas, x + marker_w, offset_y)
    offset_y -= item.height
  end
end

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/pdfrb/layout/list_box.rb', line 73

def empty?
  @items.empty?
end

#fit?(available_width, available_height) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/pdfrb/layout/list_box.rb', line 49

def fit?(available_width, available_height)
  marker_w = 20.0
  content_w = available_width - marker_w
  remaining = available_height
  @items.each do |item|
    return false unless item.fit?(content_w, remaining)

    remaining -= item.height
  end
  @width = available_width
  @height = available_height - remaining
  true
end