class Prawn::Document::BoundingBox
Low level layout helper that simplifies coordinate math.
See {Prawn::Document#bounding_box} for a description of what this class is used for.
Attributes
Owning document.
@private @return [Prawn::Document]
Parent bounding box.
@private @return [BoundingBox?]
The current indentation of the left side of the bounding box.
@private @return [Number]
The current indentation of the right side of the bounding box.
@private @return [Number]
Width of the bounding box.
@return [Number]
Public Class Methods
Source
# File lib/prawn/document/bounding_box.rb, line 258 def initialize(document, parent, point, options = {}) unless options[:width] raise ArgumentError, 'BoundingBox needs the :width option to be set' end @document = document @parent = parent @x, @y = point @width = options[:width] @height = options[:height] @total_left_padding = 0 @total_right_padding = 0 @stretched_height = nil end
@private @param document [Prawn::Document] ownding document @param parent [BoundingBox?] parent bounding box @param point [Array(Number, Number)] coordinates of the top left corner @param options [Hash{Symbol => any}] @option options :width [Number] width @option options :height [Number] optional height
Source
# File lib/prawn/document/bounding_box.rb, line 619 def self.restore_deep_copy(bounds, document) bounds.instance_variable_set(:@document, document) bounds end
Restores a copy of the bounds taken by {BoundingBox#deep_copy} in the context of the given ‘document`. Does not set the bounds of the document to the resulting {BoundingBox}, only returns it.
@private @param bounds [BoundingBox] @param document [Prawn::Document] @return [BoundingBox]
Public Instance Methods
Source
# File lib/prawn/document/bounding_box.rb, line 487 def absolute_bottom @y - height end
Absolute bottom y-coordinate of the bottom box.
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 508 def absolute_bottom_left [absolute_left, absolute_bottom] end
Absolute bottom-left point of the bounding box.
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 515 def absolute_bottom_right [absolute_right, absolute_bottom] end
Absolute bottom-left point of the bounding box.
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 466 def absolute_left @x end
Absolute left x-coordinate of the bounding box.
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 473 def absolute_right @x + width end
Absolute right x-coordinate of the bounding box.
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 480 def absolute_top @y end
Absolute top y-coordinate of the bounding box.
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 494 def absolute_top_left [absolute_left, absolute_top] end
Absolute top-left point of the bounding box.
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 501 def absolute_top_right [absolute_right, absolute_top] end
Absolute top-right point of the bounding box.
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 348 def add_left_padding(left_padding) @total_left_padding += left_padding @x += left_padding @width -= left_padding end
Increase the left padding of the bounding box.
@private @param left_padding [Number] @return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 370 def add_right_padding(right_padding) @total_right_padding += right_padding @width -= right_padding end
Increase the right padding of the bounding box.
@private @param right_padding [Number] @return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 302 def anchor [@x, @y - height] end
The translated origin (x, y - height) which describes the location of the bottom left corner of the bounding box.
@private @return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 411 def bottom 0 end
Relative bottom y-coordinate of the bounding box. Always 0.
@example Position some text 3 pts from the bottom of the containing box
draw_text('hello', at: [0, (bounds.bottom + 3)])
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 459 def bottom_left [left, bottom] end
Relative bottom-left point of the bounding box.
@example Draw a line along the left hand side of the page
stroke do line(bounds.bottom_left, bounds.top_left) end
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 447 def bottom_right [right, bottom] end
Relative bottom-right point of the bounding box.
@example Draw a line along the right hand side of the page
stroke do line(bounds.bottom_right, bounds.top_right) end
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 598 def deep_copy copy = dup # Deep-copy the parent bounds copy.instance_variable_set( :@parent, if @parent.is_a?(BoundingBox) @parent.deep_copy end, ) copy.instance_variable_set(:@document, nil) copy end
Returns a deep copy of these bounds (including all parent bounds but not copying the reference to the Document).
@private @return [BoundingBox]
Source
# File lib/prawn/document/bounding_box.rb, line 529 def height return @height if @height @stretched_height = [ (absolute_top - @document.y), Float(@stretched_height || 0.0), ].max end
Height of the bounding box. If the box is ‘stretchy’ (unspecified height attribute), height is calculated as the distance from the top of the box to the current drawing position.
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 334 def indent(left_padding, right_padding = 0) add_left_padding(left_padding) add_right_padding(right_padding) yield ensure @document.bounds.subtract_left_padding(left_padding) @document.bounds.subtract_right_padding(right_padding) end
Temporarily adjust the x coordinate to allow for left padding
@example
indent 20 do text "20 points in" indent 30 do text "50 points in" end end indent 20, 20 do text "indented on both sides" end
@private @param left_padding [Number] @param right_padding [Number] @return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 312 def left 0 end
Relative left x-coordinate of the bounding box. Always 0.
@example Position some text 3 pts from the left of the containing box
draw_text('hello', at: [(bounds.left + 3), 0])
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 542 def left_side absolute_left end
An alias for {absolute_left}.
@private @return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 560 def move_past_bottom if @document.page_number == @document.page_count @document.start_new_page else @document.go_to_page(@document.page_number + 1) end end
Moves to the top of the next page of the document, starting a new page if necessary.
@return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 581 def reference_bounds if stretchy? raise NoReferenceBounds unless @parent @parent.reference_bounds else self end end
Returns the innermost non-stretchy bounding box.
@return [BoundingBox] @raise [NoReferenceBounds]
Source
# File lib/prawn/document/bounding_box.rb, line 391 def right @width end
Relative right x-coordinate of the bounding box. Equal to the box width.
@example Position some text 3 pts from the right of the containing box
draw_text('hello', at: [(bounds.right - 3), 0])
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 550 def right_side absolute_right end
An alias for {absolute_right}.
@private @return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 573 def stretchy? !@height end
Returns ‘false` when the box has a defined height, `true` when the height is being calculated on the fly based on the current vertical position.
@return [Boolean]
Source
# File lib/prawn/document/bounding_box.rb, line 359 def subtract_left_padding(left_padding) @total_left_padding -= left_padding @x -= left_padding @width += left_padding end
Decrease the left padding of the bounding box.
@private @param left_padding [Number] @return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 380 def subtract_right_padding(right_padding) @total_right_padding -= right_padding @width += right_padding end
Decrease the right padding of the bounding box.
@private @param right_padding [Number] @return [void]
Source
# File lib/prawn/document/bounding_box.rb, line 401 def top height end
Relative top y-coordinate of the bounding box. Equal to the box height.
@example Position some text 3 pts from the top of the containing box
draw_text('hello', at: [0, (bounds.top - 3)])
@return [Number]
Source
# File lib/prawn/document/bounding_box.rb, line 423 def top_left [left, top] end
Relative top-left point of the bounding_box.
@example Draw a line from the top left of the box diagonally to the bottom right
stroke do line(bounds.top_left, bounds.bottom_right) end
@return [Array(Number, Number)]
Source
# File lib/prawn/document/bounding_box.rb, line 435 def top_right [right, top] end
Relative top-right point of the bounding box.
@example Draw a line from the top_right of the box diagonally to the bottom left
stroke do line(bounds.top_right, bounds.bottom_left) end
@return [Array(Number, Number)]