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 Prawn::Document#bounding_box for a description of what this class is used for.

Direct Known Subclasses

ColumnBox

Stable API collapse

Stable API collapse

Extension API collapse

Instance Attribute Details

#widthObject (readonly)

Width of the bounding box



455
456
457
# File 'lib/prawn/document/bounding_box.rb', line 455

def width
  @width
end

Instance Method Details

#absolute_bottomObject

Absolute bottom y-coordinate of the bottom box



426
427
428
# File 'lib/prawn/document/bounding_box.rb', line 426

def absolute_bottom
  @y - height
end

#absolute_bottom_leftObject

Absolute bottom-left point of the bounding box



444
445
446
# File 'lib/prawn/document/bounding_box.rb', line 444

def absolute_bottom_left
  [absolute_left, absolute_bottom]
end

#absolute_bottom_rightObject

Absolute bottom-left point of the bounding box



450
451
452
# File 'lib/prawn/document/bounding_box.rb', line 450

def absolute_bottom_right
  [absolute_right, absolute_bottom]
end

#absolute_leftObject

Absolute left x-coordinate of the bounding box



408
409
410
# File 'lib/prawn/document/bounding_box.rb', line 408

def absolute_left
  @x
end

#absolute_rightObject

Absolute right x-coordinate of the bounding box



414
415
416
# File 'lib/prawn/document/bounding_box.rb', line 414

def absolute_right
  @x + width
end

#absolute_topObject

Absolute top y-coordinate of the bounding box



420
421
422
# File 'lib/prawn/document/bounding_box.rb', line 420

def absolute_top
  @y
end

#absolute_top_leftObject

Absolute top-left point of the bounding box



432
433
434
# File 'lib/prawn/document/bounding_box.rb', line 432

def absolute_top_left
  [absolute_left, absolute_top]
end

#absolute_top_rightObject

Absolute top-right point of the bounding box



438
439
440
# File 'lib/prawn/document/bounding_box.rb', line 438

def absolute_top_right
  [absolute_right, absolute_top]
end

#bottomObject

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


352
353
354
# File 'lib/prawn/document/bounding_box.rb', line 352

def bottom
  0
end

#bottom_leftObject

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


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

def bottom_left
  [left, bottom]
end

#bottom_rightObject

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


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

def bottom_right
  [right, bottom]
end

#heightObject 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.



461
462
463
464
465
466
467
468
# File 'lib/prawn/document/bounding_box.rb', line 461

def height
  return @height if @height

  @stretched_height = [
    (absolute_top - @document.y),
    @stretched_height.to_f
  ].max
end

#leftObject

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


266
267
268
# File 'lib/prawn/document/bounding_box.rb', line 266

def left
  0
end

#move_past_bottomObject

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



487
488
489
490
491
492
493
# File 'lib/prawn/document/bounding_box.rb', line 487

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_boundsObject

Returns the innermost non-stretchy bounding box.



505
506
507
508
509
510
511
512
513
# File 'lib/prawn/document/bounding_box.rb', line 505

def reference_bounds
  if stretchy?
    raise "Can't find reference bounds: my parent is unset" unless @parent

    @parent.reference_bounds
  else
    self
  end
end

#rightObject

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


332
333
334
# File 'lib/prawn/document/bounding_box.rb', line 332

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)


499
500
501
# File 'lib/prawn/document/bounding_box.rb', line 499

def stretchy?
  !@height
end

#topObject

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


342
343
344
# File 'lib/prawn/document/bounding_box.rb', line 342

def top
  height
end

#top_leftObject

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


365
366
367
# File 'lib/prawn/document/bounding_box.rb', line 365

def top_left
  [left, top]
end

#top_rightObject

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


378
379
380
# File 'lib/prawn/document/bounding_box.rb', line 378

def top_right
  [right, top]
end