Class: Pdfrb::Layout::TextBox
Overview
Renders a string of text with word-wrap into a region. Uses TextLayouter internally.
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
Attributes inherited from Box
Instance Method Summary collapse
- #draw_content(canvas, x, y) ⇒ Object
- #empty? ⇒ Boolean
- #fit?(available_width, available_height) ⇒ Boolean
-
#initialize(text:) ⇒ TextBox
constructor
A new instance of TextBox.
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
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
8 9 10 |
# File 'lib/pdfrb/layout/text_box.rb', line 8 def lines @lines end |
#text ⇒ Object (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
33 34 35 |
# File 'lib/pdfrb/layout/text_box.rb', line 33 def empty? @text.empty? end |
#fit?(available_width, available_height) ⇒ 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 |