class Prawn::Markup::Builders::ListBuilder

Constants

BULLET_CHAR
BULLET_MARGIN
CONTENT_MARGIN
VERTICAL_MARGIN

Attributes

column_widths[R]
list[R]

Public Class Methods

new(pdf, list, total_width, options = {}) click to toggle source
Calls superclass method
# File lib/prawn/markup/builders/list_builder.rb, line 12
def initialize(pdf, list, total_width, options = {})
  super(pdf, total_width, options)
  @list = list
  @column_widths = compute_column_widths
end

Public Instance Methods

draw() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 26
def draw
  # fix https://github.com/prawnpdf/prawn-table/issues/120
  pdf.font_size(column_cell_style(:content)[:size] || pdf.font_size) do
    make(true).draw
  end
end
make(main = false) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 18
def make(main = false)
  pdf.make_table(convert_list, list_table_options) do |t|
    t.columns(0).style(column_cell_style(:bullet))
    t.columns(1).style(column_cell_style(:content))
    set_paddings(t, main)
  end
end

Private Instance Methods

bullet(index) click to toggle source

option accessors

# File lib/prawn/markup/builders/list_builder.rb, line 127
def bullet(index)
  list.ordered ? "#{index}." : (column_cell_style(:bullet)[:char] || BULLET_CHAR)
end
bullet_font() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 119
def bullet_font
  style = column_cell_style(:bullet)
  font_name = style[:font] || pdf.font.family
  pdf.find_font(font_name, style: style[:font_style])
end
bullet_margin() click to toggle source

margin before bullet

# File lib/prawn/markup/builders/list_builder.rb, line 132
def bullet_margin
  column_cell_style(:bullet)[:margin] || BULLET_MARGIN
end
bullet_text_width() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 112
def bullet_text_width
  font = bullet_font
  font_size = column_cell_style(:bullet)[:size] || pdf.font_size
  encoded = font.normalize_encoding(bullet(list.items.size))
  font.compute_width_of(encoded, size: font_size)
end
column_cell_style(key) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 151
def column_cell_style(key)
  @column_cell_styles ||= {}
  @column_cell_styles[key] ||=
    extract_text_cell_style(options[:text] || {}).merge(list_options[key])
end
compute_column_widths() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 104
def compute_column_widths
  return [] if list.items.empty?

  bullet_width = bullet_text_width + bullet_margin
  text_width = total_width && (total_width - bullet_width)
  [bullet_width, text_width]
end
content_margin() click to toggle source

margin between bullet and content

# File lib/prawn/markup/builders/list_builder.rb, line 137
def content_margin
  column_cell_style(:content)[:margin] || CONTENT_MARGIN
end
content_width() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 100
def content_width
  column_widths.last && column_widths.last - content_margin
end
convert_list() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 59
def convert_list
  list.items.map.with_index do |item, i|
    if item.single?
      [bullet(i + 1), normalize_list_item_node(item.nodes.first)]
    else
      [bullet(i + 1), list_item_table(item)]
    end
  end
end
default_list_options() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 161
def default_list_options
  {
    content: {},
    bullet: { align: :right }
  }
end
item_node_for_hash(node) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 92
def item_node_for_hash(node)
  normalize_cell_hash(node, content_width)
end
item_node_for_list(node) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 87
def item_node_for_list(node)
  # sublist
  ListBuilder.new(pdf, node, content_width, options).make
end
item_node_for_string(node) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 96
def item_node_for_string(node)
  node
end
list_item_table(item) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 69
def list_item_table(item)
  data = item.nodes.map { |n| [normalize_list_item_node(n)] }
  style = column_cell_style(:content)
          .merge(borders: [], padding: [0, 0, padding_bottom, 0])
  pdf.make_table(data, cell_style: style, column_widths: [content_width]) do
    rows(-1).padding = [0, 0, 0, 0]
  end
end
list_options() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 157
def list_options
  @list_options ||= HashMerger.deep(default_list_options, options[:list] || {})
end
list_table_options() click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 37
def list_table_options
  {
    column_widths: column_widths,
    cell_style: { border_width: 0, inline_format: true }
  }
end
normalize_list_item_node(node) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 78
def normalize_list_item_node(node)
  normalizer = "item_node_for_#{type_key(node)}"
  if respond_to?(normalizer, true)
    send(normalizer, node)
  else
    ''
  end
end
padding_bottom() click to toggle source

vertical padding between list items

# File lib/prawn/markup/builders/list_builder.rb, line 147
def padding_bottom
  column_cell_style(:content)[:leading] || 0
end
set_paddings(table, main) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 44
def set_paddings(table, main)
  set_row_padding(table, [0, 0, padding_bottom])
  if main
    set_row_padding(table.rows(0), [vertical_margin, 0, padding_bottom])
    set_row_padding(table.rows(-1), [0, 0, padding_bottom + vertical_margin])
  else
    set_row_padding(table.rows(-1), [0, 0, 0])
  end
end
set_row_padding(row, padding) click to toggle source
# File lib/prawn/markup/builders/list_builder.rb, line 54
def set_row_padding(row, padding)
  row.columns(0).padding = [*padding, bullet_margin]
  row.columns(1).padding = [*padding, content_margin]
end
vertical_margin() click to toggle source

margin at the top and the bottom of the list

# File lib/prawn/markup/builders/list_builder.rb, line 142
def vertical_margin
  list_options[:vertical_margin] || VERTICAL_MARGIN
end