EDITTED Completely
Still needs some work but try this out
module Prawn
module Graphics
def fill_and_stroke_bounding_box(options={},&block)
current_settings = {fill_color: fill_color,
stroke_color: stroke_color,
line_width: self.line_width }
fill_color options[:fill_color] || fill_color
stroke_color options[:stroke][:color] || stroke_color if options[:stroke]
self.line_width options[:stroke][:width] || self.line_width if options[:stroke]
rectangle options[:position], options[:width], options[:height]
options[:stroke] ? fill_and_stroke : fill
box_options = convert_box_options(options)
options[:revert_before_block] ? revert(current_settings) : check_fill_visiblity(options[:text_color])
fill_color options[:text_color] || fill_color
bounding_box(box_options[:position],box_options[:options]) do
if block_given?
block.call
end
end
revert(current_settings) unless options[:skip_revert]
end
def revert(settings={})
fill_color settings[:fill_color]
stroke_color settings[:stroke_color]
self.line_width settings[:line_width]
end
def convert_box_options(options={})
converted_options = {position: options.delete(:position)}
if options.delete(:stroke)
resize_box(options)
reposition_box(converted_options)
end
converted_options.merge({options: options})
end
def resize_box(options ={})
[:width,:height].each do |param|
options[param] -= (self.line_width * 2)
end
end
def reposition_box(options)
options[:position][0] += self.line_width
options[:position][1] -= self.line_width
end
def check_fill_visiblity(text_color)
text_color ||= fill_color
warn "[WARNING] Text Will Not be visible without text_color set or revert_before_block" if text_color == fill_color
end
end
end
Then you can call it like this
fill_and_stroke_bounding_box(position:[0,cursor],
stroke:{color: "7CFC00",width: 2.mm},
text_color: "668B8B"
fill_color:"FFFFFF",
width: 19.cm, height: 100
) do
The only options that are required are position
,height
, and width
(bounding_box
does not require a height
but since you are placing it inside a rectangle you must specify a height
.
Although it is recommended the text_color
or revert_before_block
are also set otherwise the text inside the block will be invisible.
options
includes all bounding_box options including use of a block as well as the following new options stroke:{:color,:width}
which lets you set the outside stroke for the rectangle. fill_color:
set the color of the rectangle. text_color
the color of the text inside the block. revert_before_block
will set the colors back before it executes the block because fill_color
controls the text_color in Prawn. You can use this option in place of text_color
if the correct color is already set.skip_revert
this will discard the options of fill_color
,stroke_color
, and self.line_width
that were set prior to calling this method. This extension will also warn
if the text_color
is the same as the fill_color
.
Hope this helps someone out.