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.

The standard usage is through the helper methods Document::Layout#text and Document::Layout#formatted_text.

This class uses TextLayouter behind the scenes to do the hard work.

Used Box Properties

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.

Limitations

When setting the style property ‘position’ to :flow, padding and border to the left and right as well as a predefined fixed width are not respected and the result will look wrong.

Examples

Showing some text:

#>pdf-composer
composer.box(:text, items: layout.text_fragments("This is some text."))
# Or easier with the provided convenience method
composer.text("This is also some text")

It is possible to flow the text around other objects by using the style property ‘position’ with the value :flow:

#>pdf-composer
composer.box(:base, width: 30, height: 30,
             style: {margin: 5, position: :float, background_color: "hp-blue-light"})
composer.text("This is some text. " * 20, position: :flow)

While top and bottom padding and border can be used with flow positioning, left and right padding and border are not supported and the result will look wrong:

#>pdf-composer
composer.box(:base, width: 30, height: 30,
             style: {margin: 5, position: :float, background_color: "hp-blue-light"})
composer.text("This is some text. " * 20, padding: 10, position: :flow,
              text_align: :justify)

Constant Summary

Constants included from Utils

Utils::EPSILON

Instance Attribute Summary

Attributes inherited from Box

#fit_result, #height, #properties, #style, #width

Instance Method Summary collapse

Methods inherited from Box

#content_height, #content_width, create, #draw, #fit, #split, #split_box?

Constructor Details

#initialize(items:, **kwargs) ⇒ TextBox

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



89
90
91
92
93
94
95
# File 'lib/hexapdf/layout/text_box.rb', line 89

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

Instance Method Details

#empty?Boolean

:nodoc:

Returns:

  • (Boolean)


110
111
112
# File 'lib/hexapdf/layout/text_box.rb', line 110

def empty?
  super && (!@result || @result.lines.empty?)
end

#supports_position_flow?Boolean

Returns true as the ‘position’ style property value :flow is supported.

Returns:

  • (Boolean)


105
106
107
# File 'lib/hexapdf/layout/text_box.rb', line 105

def supports_position_flow?
  true
end

#textObject

Returns the text that will be drawn.

This will ignore any inline boxes or kerning values.



100
101
102
# File 'lib/hexapdf/layout/text_box.rb', line 100

def text
  @items.map {|item| item.kind_of?(TextFragment) ? item.text : '' }.join
end