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, #empty?

Constructor Details

#initialize(items, **kwargs) ⇒ TextBox

Creates a new TextBox object with the given inline items (e.g. TextFragment and InlineBox objects).



47
48
49
50
51
52
# File 'lib/hexapdf/layout/text_box.rb', line 47

def initialize(items, **kwargs)
  super(kwargs)
  @tl = TextLayouter.new(style)
  @items = items
  @result = nil
end

Instance Method Details

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



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/hexapdf/layout/text_box.rb', line 59

def fit(available_width, available_height, frame)
  @result = if style.position == :flow
              @tl.fit(@items, frame.width_specification, frame.contour_line.bbox.height)
            else
              @tl.fit(@items, available_width, available_height)
            end
  @height = @result.height
  @width = @result.lines.max_by(&:width)&.width || 0

  success = (@result.status == :success)
  @draw_block = success ? method(:draw_text) : nil
  success
end