editierte Völlig braucht noch etwas Arbeit, aber diese
ausprobieren
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
Dann können Sie es wie folgt
nennen
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
Die einzigen Optionen, die erforderlich sind, sind position
, height
und width
(bounding_box
kein height
erfordert aber da Sie es innerhalb eines Rechtecks geben Sie eine height
angeben müssen.
Obwohl es empfohlen wird, die text_color
oder revert_before_block
sind auch anders gesetzt, der Text innerhalb des Blocks wird unsichtbar sein.
options
enthält alle bounding_box-Optionen einschließlich der Verwendung eines Blocks sowie der folgenden neuen Optionen stroke:{:color,:width}
, mit der Sie den äußeren Strich für das Rechteck festlegen können. fill_color:
Legen Sie die Farbe des Rechtecks fest. text_color
die Farbe des Textes innerhalb des Blocks. revert_before_block
setzt die Farben zurück, bevor der Block ausgeführt wird, da fill_color
die text_color in Prawn steuert. Sie können diese Option anstelle von text_color
verwenden, wenn die richtige Farbe bereits festgelegt ist. skip_revert
Dies verwirft die Optionen fill_color
, stroke_color
und self.line_width
, die vor dem Aufruf dieser Methode gesetzt wurden. Diese Erweiterung wird auch , wenn die text_color
ist die gleiche wie die fill_color
.
Hoffe das hilft jemand aus.
Haben Sie eine funktionierende Lösung gefunden? – inquisitive