Class: HexaPDF::Layout::TextLayouter::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hexapdf/layout/text_layouter.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, lines, remaining_items) ⇒ Result

Creates a new Result structure.



633
634
635
636
637
638
# File 'lib/hexapdf/layout/text_layouter.rb', line 633

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

Instance Attribute Details

#heightObject (readonly)

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



627
628
629
# File 'lib/hexapdf/layout/text_layouter.rb', line 627

def height
  @height
end

#linesObject (readonly)

Array of layed out lines.



623
624
625
# File 'lib/hexapdf/layout/text_layouter.rb', line 623

def lines
  @lines
end

#remaining_itemsObject (readonly)

The remaining items that couldn’t be layed out.



630
631
632
# File 'lib/hexapdf/layout/text_layouter.rb', line 630

def remaining_items
  @remaining_items
end

#statusObject (readonly)

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.



620
621
622
# File 'lib/hexapdf/layout/text_layouter.rb', line 620

def status
  @status
end

Instance Method Details

#draw(canvas, x, y) ⇒ Object

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



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
# File 'lib/hexapdf/layout/text_layouter.rb', line 641

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