Class: Arrolio::Flowables::TextFlowable

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

Overview

A paragraph of flowing text. Selects a line-breaking strategy based on the style's line_break attribute (:greedy or :knuth_plass). Both strategies produce TextLayout::Line with the same shape, so the rest of the pipeline is agnostic.

Direct Known Subclasses

HeadingFlowable

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(runs, style: Style::Definition.new, measurer: nil) ⇒ TextFlowable

Returns a new instance of TextFlowable.



12
13
14
15
16
# File 'lib/arrolio/flowables/text_flowable.rb', line 12

def initialize(runs, style: Style::Definition.new, measurer: nil)
  super(style: style)
  @runs = Array(runs)
  @measurer = measurer || GlyphMeasurer.new(font_name: style.font_name)
end

Instance Attribute Details

#measurerObject (readonly)

Returns the value of attribute measurer.



10
11
12
# File 'lib/arrolio/flowables/text_flowable.rb', line 10

def measurer
  @measurer
end

#runsObject (readonly)

Returns the value of attribute runs.



10
11
12
# File 'lib/arrolio/flowables/text_flowable.rb', line 10

def runs
  @runs
end

Instance Method Details

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/arrolio/flowables/text_flowable.rb', line 38

def do_split(width, remaining_height, _context = nil)
  lines = laid_out(width)
  line_h = line_height
  head_count = (remaining_height.to_f / line_h).floor
  head_count = [head_count, lines.length].min
  head_count = 1 if head_count < 1 && lines.any?

  head_lines = lines[0...head_count]
  tail_lines = lines[head_count..] || []

  head_runs = collect_runs(head_lines)
  tail_runs = collect_runs(tail_lines)

  head = head_runs.empty? ? nil : self.class.new(head_runs, style: @style, measurer: @measurer)
  tail = tail_runs.empty? ? nil : self.class.new(tail_runs, style: @style, measurer: @measurer)
  [head, tail]
end

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



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

def emit(x, y, width, _context = nil)
  lines = laid_out(width)
  return [[], 0.0] if lines.empty?

  box = Output::PlacedBox.text(
    x: x, y: y - (line_height * lines.length),
    width: width, height: line_height * lines.length,
    lines: lines, line_height: line_height, style: @style
  )
  [[box], box.height]
end

#height(width, _context = nil) ⇒ Object



18
19
20
# File 'lib/arrolio/flowables/text_flowable.rb', line 18

def height(width, _context = nil)
  laid_out(width).length * line_height
end

#splittable?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/arrolio/flowables/text_flowable.rb', line 34

def splittable?
  true
end