Class: Arrolio::Flowables::TocLineFlowable

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

Overview

One ToC line: title + dot-leader + page number. One line tall.

Instance Attribute Summary collapse

Attributes inherited from Arrolio::Flowable

#style

Instance Method Summary collapse

Methods inherited from Arrolio::Flowable

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

Constructor Details

#initialize(title, page_number, level: 1, style: Style::Definition.new) ⇒ TocLineFlowable

Returns a new instance of TocLineFlowable.



9
10
11
12
13
14
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 9

def initialize(title, page_number, level: 1, style: Style::Definition.new)
  super(style: style)
  @title = title.to_s
  @page_number = page_number.to_i
  @level = level.to_i
end

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



7
8
9
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 7

def level
  @level
end

#page_numberObject (readonly)

Returns the value of attribute page_number.



7
8
9
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 7

def page_number
  @page_number
end

#titleObject (readonly)

Returns the value of attribute title.



7
8
9
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 7

def title
  @title
end

Instance Method Details

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



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 22

def emit(x, y, width, context = nil)
  page_str = @page_number.to_s
  measurer = GlyphMeasurer.new(font_name: @style.font_name)
  title_w = measurer.width_of_string(@title, font_size: @style.font_size)
  page_w = measurer.width_of_string(page_str, font_size: @style.font_size)
  gap = width - title_w - page_w - 10
  dots = gap.positive? ? ('.' * (gap / measurer.space_width(font_size: @style.font_size)).floor) : ' '
  text = "#{@title} #{dots} #{page_str}"
  tf = TextFlowable.new([InlineRun.new(text, style: @style)], style: @style)
  tf.emit(x, y, width, context)
end

#height(_width, _context = nil) ⇒ Object



16
17
18
19
20
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 16

def height(_width, _context = nil)
  GlyphMeasurer.new(font_name: @style.font_name)
               .line_height(font_size: @style.font_size,
                            line_spacing: @style.line_spacing)
end