Class: HexaPDF::Layout::TextBox
- Defined in:
- lib/hexapdf/layout/text_box.rb
Overview
A TextBox is used for drawing text, either inside a rectangular box or by flowing it around objects of a Frame.
This class uses TextLayouter behind the scenes to do the hard work.
Instance Attribute Summary
Attributes inherited from Box
Instance Method Summary collapse
-
#empty? ⇒ Boolean
:nodoc:.
-
#fit(available_width, available_height, frame) ⇒ Object
Fits the text box into the Frame.
-
#initialize(items:, **kwargs) ⇒ TextBox
constructor
Creates a new TextBox object with the given inline items (e.g. TextFragment and InlineBox objects).
-
#split(available_width, available_height, frame) ⇒ Object
Splits the text box into two boxes if necessary and possible.
-
#supports_position_flow? ⇒ Boolean
Returns
trueas the 'position' style property value :flow is supported.
Methods inherited from Box
#content_height, #content_width, create, #draw, #split_box?
Constructor Details
#initialize(items:, **kwargs) ⇒ TextBox
Creates a new TextBox object with the given inline items (e.g. TextFragment and InlineBox objects).
50 51 52 53 54 55 |
# File 'lib/hexapdf/layout/text_box.rb', line 50 def initialize(items:, **kwargs) super(**kwargs) @tl = TextLayouter.new(style) @items = items @result = nil end |
Instance Method Details
#empty? ⇒ Boolean
:nodoc:
118 119 120 |
# File 'lib/hexapdf/layout/text_box.rb', line 118 def empty? super && (!@result || @result.lines.empty?) end |
#fit(available_width, available_height, frame) ⇒ Object
Fits the text box into the Frame.
Depending on the 'position' style property, the text is either fit into the rectangular area given by available_width and available_height, or fit to the outline of the frame starting from the top (when 'position' is set to :flow).
The spacing after the last line can be controlled via the style property last_line_gap.
Also see TextLayouter#style for other style properties taken into account.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/hexapdf/layout/text_box.rb', line 71 def fit(available_width, available_height, frame) return false if (@initial_width > 0 && @initial_width > available_width) || (@initial_height > 0 && @initial_height > available_height) @width = @height = 0 @result = if style.position == :flow @tl.fit(@items, frame.width_specification, frame.shape.bbox.height) else @width = reserved_width @height = reserved_height width = (@initial_width > 0 ? @initial_width : available_width) - @width height = (@initial_height > 0 ? @initial_height : available_height) - @height @tl.fit(@items, width, height) end @width += if @initial_width > 0 || style.align == :center || style.align == :right width else @result.lines.max_by(&:width)&.width || 0 end @height += if @initial_height > 0 || style.valign == :center || style.valign == :bottom height else @result.height end if style.last_line_gap && @result.lines.last @height += style.line_spacing.gap(@result.lines.last, @result.lines.last) end @result.status == :success end |
#split(available_width, available_height, frame) ⇒ Object
Splits the text box into two boxes if necessary and possible.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/hexapdf/layout/text_box.rb', line 103 def split(available_width, available_height, frame) fit(available_width, available_height, frame) unless @result if style.position != :flow && (@width > available_width || @height > available_height) [nil, self] elsif @result.remaining_items.empty? [self] elsif @result.lines.empty? [nil, self] else [self, create_box_for_remaining_items] end end |
#supports_position_flow? ⇒ Boolean
Returns true as the 'position' style property value :flow is supported.
58 59 60 |
# File 'lib/hexapdf/layout/text_box.rb', line 58 def supports_position_flow? true end |