Class: Pdfrb::Layout::TextBox

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

Overview

Renders a string of text with word-wrap into a region. Uses TextLayouter internally.

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(text:) ⇒ TextBox

Returns a new instance of TextBox.



10
11
12
13
14
# File 'lib/pdfrb/layout/text_box.rb', line 10

def initialize(text:, **)
  super(**)
  @text = text.to_s
  @lines = []
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



8
9
10
# File 'lib/pdfrb/layout/text_box.rb', line 8

def lines
  @lines
end

#textObject (readonly)

Returns the value of attribute text.



8
9
10
# File 'lib/pdfrb/layout/text_box.rb', line 8

def text
  @text
end

Instance Method Details

#draw_content(canvas, x, y) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pdfrb/layout/text_box.rb', line 24

def draw_content(canvas, x, y)
  offset_y = y - (@lines.first&.ascender || 0)
  @lines.each do |line|
    line.draw(canvas, x, offset_y, available_width: @width,
                                  alignment: @style.text_align || :left)
    offset_y -= line.height
  end
end

#empty?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/pdfrb/layout/text_box.rb', line 33

def empty?
  @text.empty?
end

#fit?(available_width, available_height) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'lib/pdfrb/layout/text_box.rb', line 16

def fit?(available_width, available_height)
  layouter = TextLayouter.new(@style)
  @lines = layouter.layout(@text, available_width)
  @width = available_width
  @height = @lines.sum(&:height)
  @height <= available_height
end