Skip to content

Instantly share code, notes, and snippets.

@zinkkrysty
Created December 18, 2012 10:35
Show Gist options
  • Save zinkkrysty/4326973 to your computer and use it in GitHub Desktop.
Save zinkkrysty/4326973 to your computer and use it in GitHub Desktop.
Automatic padding of bounding box in Prawn, and allows for filling in the background color. Currently only when specifying height, but I'm open to suggestions.
require 'prawn'
module PaddedBoxHelper
#
# Fills the background of the box you're in
#
def fill_bg_color color
float do
fill_color color
fill_rectangle [0, bounds.height], bounds.width, bounds.height
reset_font_color
end
end
#
# Pads a bounding box by nesting another bounding_box inside it
# Can also take bg_color as a parameter (you have to specify height for this to work)
# Syntax:
# padded_box([x,y], <padding:Integer>, <opts...>, :bg_color => <hex:String>) do
# <your block content here>
# end
#
def padded_box *args, &block
opt = args.extract_options!
bg_color = opt.delete(:bg_color)
padding = args.delete_at(1)
bounding_box *args, opt do
fill_bg_color bg_color if bg_color
bounding_box [padding, bounds.height-padding], width: bounds.width-2*padding, height: bounds.height-2*padding do
block.call
end
end
end
end
@jschee
Copy link

jschee commented Mar 30, 2020

Bro, this gist is super clutch. Kudos and thankyou!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment