Class: Prawn::Document::BoundingBox

Inherits:
Object
  • Object
show all
Defined in:
lib/prawn/document/bounding_box.rb

Overview

Low level layout helper that simplifies coordinate math.

See #bounding_box for a description of what this class is used for.

Direct Known Subclasses

ColumnBox

Defined Under Namespace

Classes: NoReferenceBounds

Stable API collapse

Stable API collapse

Extension API collapse

Instance Attribute Details

#widthNumber (readonly)

Width of the bounding box.

Returns:

  • (Number)


522
523
524
# File 'lib/prawn/document/bounding_box.rb', line 522

def width
  @width
end

Instance Method Details

#absolute_bottomNumber

Absolute bottom y-coordinate of the bottom box.

Returns:

  • (Number)


487
488
489
# File 'lib/prawn/document/bounding_box.rb', line 487

def absolute_bottom
  @y - height
end

#absolute_bottom_leftArray(Number, Number)

Absolute bottom-left point of the bounding box.

Returns:

  • (Array(Number, Number))


508
509
510
# File 'lib/prawn/document/bounding_box.rb', line 508

def absolute_bottom_left
  [absolute_left, absolute_bottom]
end

#absolute_bottom_rightArray(Number, Number)

Absolute bottom-left point of the bounding box.

Returns:

  • (Array(Number, Number))


515
516
517
# File 'lib/prawn/document/bounding_box.rb', line 515

def absolute_bottom_right
  [absolute_right, absolute_bottom]
end

#absolute_leftNumber

Absolute left x-coordinate of the bounding box.

Returns:

  • (Number)


466
467
468
# File 'lib/prawn/document/bounding_box.rb', line 466

def absolute_left
  @x
end

#absolute_rightNumber

Absolute right x-coordinate of the bounding box.

Returns:

  • (Number)


473
474
475
# File 'lib/prawn/document/bounding_box.rb', line 473

def absolute_right
  @x + width
end

#absolute_topNumber

Absolute top y-coordinate of the bounding box.

Returns:

  • (Number)


480
481
482
# File 'lib/prawn/document/bounding_box.rb', line 480

def absolute_top
  @y
end

#absolute_top_leftArray(Number, Number)

Absolute top-left point of the bounding box.

Returns:

  • (Array(Number, Number))


494
495
496
# File 'lib/prawn/document/bounding_box.rb', line 494

def absolute_top_left
  [absolute_left, absolute_top]
end

#absolute_top_rightArray(Number, Number)

Absolute top-right point of the bounding box.

Returns:

  • (Array(Number, Number))


501
502
503
# File 'lib/prawn/document/bounding_box.rb', line 501

def absolute_top_right
  [absolute_right, absolute_top]
end

#bottomNumber

Relative bottom y-coordinate of the bounding box. Always 0.

Examples:

Position some text 3 pts from the bottom of the containing box

draw_text('hello', at: [0, (bounds.bottom + 3)])

Returns:

  • (Number)


411
412
413
# File 'lib/prawn/document/bounding_box.rb', line 411

def bottom
  0
end

#bottom_leftArray(Number, Number)

Relative bottom-left point of the bounding box.

Examples:

Draw a line along the left hand side of the page

stroke do
  line(bounds.bottom_left, bounds.top_left)
end

Returns:

  • (Array(Number, Number))


459
460
461
# File 'lib/prawn/document/bounding_box.rb', line 459

def bottom_left
  [left, bottom]
end

#bottom_rightArray(Number, Number)

Relative bottom-right point of the bounding box.

Examples:

Draw a line along the right hand side of the page

stroke do
  line(bounds.bottom_right, bounds.top_right)
end

Returns:

  • (Array(Number, Number))


447
448
449
# File 'lib/prawn/document/bounding_box.rb', line 447

def bottom_right
  [right, bottom]
end

#heightNumber Also known as: update_height

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.

Returns:

  • (Number)


529
530
531
532
533
534
535
536
# 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

#leftNumber

Relative left x-coordinate of the bounding box. Always 0.

Examples:

Position some text 3 pts from the left of the containing box

draw_text('hello', at: [(bounds.left + 3), 0])

Returns:

  • (Number)


312
313
314
# File 'lib/prawn/document/bounding_box.rb', line 312

def left
  0
end

#move_past_bottomvoid

This method returns an undefined value.

Moves to the top of the next page of the document, starting a new page if necessary.



560
561
562
563
564
565
566
# 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

#reference_boundsBoundingBox

Returns the innermost non-stretchy bounding box.

Returns:

Raises:



581
582
583
584
585
586
587
588
589
# 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

#rightNumber

Relative right x-coordinate of the bounding box. Equal to the box width.

Examples:

Position some text 3 pts from the right of the containing box

draw_text('hello', at: [(bounds.right - 3), 0])

Returns:

  • (Number)


391
392
393
# File 'lib/prawn/document/bounding_box.rb', line 391

def right
  @width
end

#stretchy?Boolean

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.

Returns:

  • (Boolean)


573
574
575
# File 'lib/prawn/document/bounding_box.rb', line 573

def stretchy?
  !@height
end

#topNumber

Relative top y-coordinate of the bounding box. Equal to the box height.

Examples:

Position some text 3 pts from the top of the containing box

draw_text('hello', at: [0, (bounds.top - 3)])

Returns:

  • (Number)


401
402
403
# File 'lib/prawn/document/bounding_box.rb', line 401

def top
  height
end

#top_leftArray(Number, Number)

Relative top-left point of the bounding_box.

Examples:

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

Returns:

  • (Array(Number, Number))


423
424
425
# File 'lib/prawn/document/bounding_box.rb', line 423

def top_left
  [left, top]
end

#top_rightArray(Number, Number)

Relative top-right point of the bounding box.

Examples:

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

Returns:

  • (Array(Number, Number))


435
436
437
# File 'lib/prawn/document/bounding_box.rb', line 435

def top_right
  [right, top]
end