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.



586
587
588
589
590
591
# File 'lib/hexapdf/layout/text_layouter.rb', line 586

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



580
581
582
# File 'lib/hexapdf/layout/text_layouter.rb', line 580

def height
  @height
end

#linesObject (readonly)

Array of layed out lines.



576
577
578
# File 'lib/hexapdf/layout/text_layouter.rb', line 576

def lines
  @lines
end

#remaining_itemsObject (readonly)

The remaining items that couldn’t be layed out.



583
584
585
# File 'lib/hexapdf/layout/text_layouter.rb', line 583

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.



573
574
575
# File 'lib/hexapdf/layout/text_layouter.rb', line 573

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



594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
# File 'lib/hexapdf/layout/text_layouter.rb', line 594

def draw(canvas, x, y)
  last_item = 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_with_index do |line, index|
      y -= @lines[index].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_item&.style == item.style)
          last_item = item
        elsif !item.empty?
          canvas.restore_graphics_state
          item.draw(canvas, line_x + item_x, y + item_y)
          canvas.save_graphics_state
          last_item = nil
        end
      end
    end
  end
end