Created
December 18, 2012 10:35
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Thank you 👍
Good Job! Thank you
Can't find reset_font_color
in the prawn api. Are you sure?
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
great gist! saved me a lot of time!