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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document, parent, point, options = {}) ⇒ BoundingBox

:nodoc:



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/prawn/document/bounding_box.rb', line 211

def initialize(document, parent, point, options={}) #:nodoc:
  unless options[:width]
    raise ArgumentError, "BoundingBox needs the :width option to be set"
  end

  @document = document
  @parent = parent
  @x, @y = point
  @width, @height = options[:width], options[:height]
  @total_left_padding = 0
  @total_right_padding = 0
  @stretched_height = nil
end

Instance Attribute Details

#documentObject (readonly)

Returns the value of attribute document.



225
226
227
# File 'lib/prawn/document/bounding_box.rb', line 225

def document
  @document
end

#parentObject (readonly)

Returns the value of attribute parent.



225
226
227
# File 'lib/prawn/document/bounding_box.rb', line 225

def parent
  @parent
end

#total_left_paddingObject (readonly)

The current indentation of the left side of the bounding box.



227
228
229
# File 'lib/prawn/document/bounding_box.rb', line 227

def total_left_padding
  @total_left_padding
end

#total_right_paddingObject (readonly)

The current indentation of the right side of the bounding box.



229
230
231
# File 'lib/prawn/document/bounding_box.rb', line 229

def total_right_padding
  @total_right_padding
end

Class Method Details

.restore_deep_copy(bounds, document) ⇒ Object

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.



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

def self.restore_deep_copy(bounds, document)
  bounds.instance_variable_set("@document", document)
  bounds
end

Instance Method Details

#absolute_bottomObject

Absolute bottom y-coordinate of the bottom box



399
400
401
# File 'lib/prawn/document/bounding_box.rb', line 399

def absolute_bottom
  @y - height
end

#absolute_bottom_leftObject

Absolute bottom-left point of the bounding box



417
418
419
# File 'lib/prawn/document/bounding_box.rb', line 417

def absolute_bottom_left
  [absolute_left, absolute_bottom]
end

#absolute_bottom_rightObject

Absolute bottom-left point of the bounding box



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

def absolute_bottom_right
  [absolute_right, absolute_bottom]
end

#absolute_leftObject

Absolute left x-coordinate of the bounding box



381
382
383
# File 'lib/prawn/document/bounding_box.rb', line 381

def absolute_left
  @x
end

#absolute_rightObject

Absolute right x-coordinate of the bounding box



387
388
389
# File 'lib/prawn/document/bounding_box.rb', line 387

def absolute_right
  @x + width
end

#absolute_topObject

Absolute top y-coordinate of the bounding box



393
394
395
# File 'lib/prawn/document/bounding_box.rb', line 393

def absolute_top
  @y
end

#absolute_top_leftObject

Absolute top-left point of the bounding box



405
406
407
# File 'lib/prawn/document/bounding_box.rb', line 405

def absolute_top_left
  [absolute_left, absolute_top]
end

#absolute_top_rightObject

Absolute top-right point of the bounding box



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

def absolute_top_right
  [absolute_right, absolute_top]
end

#add_left_padding(left_padding) ⇒ Object

Increase the left padding of the bounding box.



274
275
276
277
278
# File 'lib/prawn/document/bounding_box.rb', line 274

def add_left_padding(left_padding)
  @total_left_padding += left_padding
  @x += left_padding
  @width -= left_padding
end

#add_right_padding(right_padding) ⇒ Object

Increase the right padding of the bounding box.



288
289
290
291
# File 'lib/prawn/document/bounding_box.rb', line 288

def add_right_padding(right_padding)
  @total_right_padding += right_padding
  @width -= right_padding
end

#anchorObject

The translated origin (x,y-height) which describes the location of the bottom left corner of the bounding box



234
235
236
# File 'lib/prawn/document/bounding_box.rb', line 234

def anchor
  [@x, @y - height]
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)])


325
326
327
# File 'lib/prawn/document/bounding_box.rb', line 325

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


375
376
377
# File 'lib/prawn/document/bounding_box.rb', line 375

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


363
364
365
# File 'lib/prawn/document/bounding_box.rb', line 363

def bottom_right
  [right,bottom]
end

#deep_copyObject

Returns a deep copy of these bounds (including all parent bounds but not copying the reference to the Document).



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

def deep_copy
  copy = dup
  # Deep-copy the parent bounds
  copy.instance_variable_set("@parent", if BoundingBox === @parent
                                          @parent.deep_copy
                                        end)
  copy.instance_variable_set("@document", nil)
  copy
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.



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

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

#indent(left_padding, right_padding = 0, &block) ⇒ Object

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


264
265
266
267
268
269
270
271
# File 'lib/prawn/document/bounding_box.rb', line 264

def indent(left_padding, right_padding = 0, &block)
  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

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


244
245
246
# File 'lib/prawn/document/bounding_box.rb', line 244

def left
  0
end

#left_sideObject

an alias for absolute_left



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

def left_side
  absolute_left
end

#move_past_bottomObject

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



456
457
458
459
460
461
462
# File 'lib/prawn/document/bounding_box.rb', line 456

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.



476
477
478
479
480
481
482
483
# File 'lib/prawn/document/bounding_box.rb', line 476

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


305
306
307
# File 'lib/prawn/document/bounding_box.rb', line 305

def right
  @width
end

#right_sideObject

an alias for absolute_right



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

def right_side
  absolute_right
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)


470
471
472
# File 'lib/prawn/document/bounding_box.rb', line 470

def stretchy?
  !@height
end

#subtract_left_padding(left_padding) ⇒ Object

Decrease the left padding of the bounding box.



281
282
283
284
285
# File 'lib/prawn/document/bounding_box.rb', line 281

def subtract_left_padding(left_padding)
  @total_left_padding -= left_padding
  @x -= left_padding
  @width += left_padding
end

#subtract_right_padding(right_padding) ⇒ Object

Decrease the right padding of the bounding box.



294
295
296
297
# File 'lib/prawn/document/bounding_box.rb', line 294

def subtract_right_padding(right_padding)
  @total_right_padding -= right_padding
  @width += right_padding
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)])


315
316
317
# File 'lib/prawn/document/bounding_box.rb', line 315

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


338
339
340
# File 'lib/prawn/document/bounding_box.rb', line 338

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


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

def top_right
  [right,top]
end

#widthObject

Width of the bounding box



429
430
431
# File 'lib/prawn/document/bounding_box.rb', line 429

def width
  @width
end