Class: HexaPDF::Layout::TextBox

Inherits:
Box
  • Object
show all
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

#height, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#content_height, #content_width, create, #draw

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:

Returns:

  • (Boolean)


115
116
117
# File 'lib/hexapdf/layout/text_box.rb', line 115

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.

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.



66
67
68
69
70
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
# File 'lib/hexapdf/layout/text_box.rb', line 66

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.contour_line.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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/hexapdf/layout/text_box.rb', line 98

def split(available_width, available_height, frame)
  fit(available_width, available_height, frame) unless @result
  if @width > available_width || @height > available_height
    [nil, self]
  elsif @result.remaining_items.empty?
    [self]
  elsif @result.lines.empty?
    [nil, self]
  else
    box = clone
    box.instance_variable_set(:@result, nil)
    box.instance_variable_set(:@items, @result.remaining_items)
    [self, box]
  end
end