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, all on one line. The title is left-aligned, the page number is right-aligned, and the gap between is filled with dot leaders.

Emits three PlacedBox values:

1. text box for the title at x=0
2. text box for the page number, right-aligned at x=width
3. (optional) line box for the dot leader between them

Constant Summary collapse

LEADER_CHAR =
'.'
LEADER_GAP =

pt between title/page and the leader run

4.0

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?, #min_keep_height, #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.



19
20
21
22
23
24
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 19

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.



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

def level
  @level
end

#page_numberObject (readonly)

Returns the value of attribute page_number.



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

def page_number
  @page_number
end

#titleObject (readonly)

Returns the value of attribute title.



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

def title
  @title
end

Instance Method Details

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 32

def emit(x, y, width, context = nil)
  measurer = GlyphMeasurer.new(font_name: @style.font_name)
  line_h = height(width, context)
  # Apply the style's margin_left as an indent (ToC sub-entries
  # use this to nest visually under their parent entry).
  indent = @style.margin_left.to_f
  x += indent
  width -= indent
  page_str = @page_number.to_s
  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)
  page_x = x + width - page_w
  leader_w = page_x - (x + title_w + LEADER_GAP)

  boxes = []
  boxes.concat(emit_title(x, y, width, line_h, context))
  boxes.concat(emit_page_number(page_x, y, page_w, line_h, context))
  boxes.concat(emit_leader(x + title_w + LEADER_GAP, y, leader_w, line_h, measurer)) if leader_w.positive?
  [boxes, line_h]
end

#height(_width, _context = nil) ⇒ Object



26
27
28
29
30
# File 'lib/arrolio/flowables/toc_line_flowable.rb', line 26

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