class HexaPDF::Layout::TextLayouter::Result

Encapsulates the result of layouting items using a TextLayouter and provides a method for drawing the result (i.e. the layed out lines) on a canvas.

Attributes

height[R]

The actual height of all layed out lines (this includes a possible offset for the first line).

lines[R]

Array of layed out lines.

remaining_items[R]

The remaining items that couldn't be layed out.

status[R]

The status after layouting the items:

:success

There are no remaining items.

:box_too_wide

A single text or inline box was too wide to fit alone on a line.

:height

There was not enough height for all items to layout.

Even if the result is not :success, the layouting may still be successful depending on the usage. For example, if we expect that there may be too many items to fit, :height is still a success.

Public Class Methods

new(status, lines, remaining_items) click to toggle source

Creates a new Result structure.

# File lib/hexapdf/layout/text_layouter.rb, line 607
def initialize(status, lines, remaining_items)
  @status = status
  @lines = lines
  @height = @lines.sum(&:y_offset) - (@lines.last&.y_min || 0)
  @remaining_items = remaining_items
end

Public Instance Methods

draw(canvas, x, y) click to toggle source

Draws the layed out lines onto the canvas with the top-left corner being at [x, y].

# File lib/hexapdf/layout/text_layouter.rb, line 615
def draw(canvas, x, y)
  last_text_fragment = nil
  canvas.save_graphics_state do
    # Best effort for leading in case we have an evenly spaced paragraph
    canvas.leading(@lines[1].y_offset) if @lines.size > 1
    @lines.each do |line|
      y -= line.y_offset
      line_x = x + line.x_offset
      line.each do |item, item_x, item_y|
        if item.kind_of?(TextFragment)
          item.draw(canvas, line_x + item_x, y + item_y,
                    ignore_text_properties: last_text_fragment&.style == item.style)
          last_text_fragment = item
        elsif !item.empty?
          canvas.restore_graphics_state
          item.draw(canvas, line_x + item_x, y + item_y)
          canvas.save_graphics_state
          last_text_fragment = nil
        end
      end
    end
  end
end