Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add background/fill color to a bounding_box in Prawn

Is it possible to add a background color to a bounding_box in Prawn?

 bounding_box([100, cursor], width: 80, height: 20) do
    pad_top(7) { text "THIS IS TEXT", size: 8, align: :center }
    stroke_bounds
 end 

Ive tried adding this to the bounding_box block

     background_color: "CCCCCC"

Ive tried adding this inside the block

    fill_color "CCCCCC"
    background_color "CCCCCC"

Nothing seems to work with a bounding_box

like image 733
hellion Avatar asked Jul 20 '13 00:07

hellion


2 Answers

Here the code

    bounding_box([200,cursor], :width => 350, :height => 80) do
        stroke_color 'FFFFFF'
        stroke_bounds
        stroke do
            stroke_color 'FFFF00'
            fill_color '36DA5C'
            fill_and_stroke_rounded_rectangle [cursor-80,cursor], 350, 80, 10
            fill_color '000000'
        end
    end
like image 57
Willing Avatar answered Sep 21 '22 16:09

Willing


This was discussed 2008 [1] (apparently didn't lead anywhere though), i don't see it mentioned anywhere in the manual [2] either.

As to how it's done: After putting all content into your bounding box, you can obtain the resulting width and height from the bounding box. With that information you can use primitve rectangle drawing to fill it. Afterwards you will have to redraw the content, because by now you will have painted right over it with your rectangle fill.

Hopefully (probably?) there's a better way too, one where you don't need to draw your actual content twice; Make sure to share it with everyone when you come across it!

All the stuff I mentioned in the quick how-to above is beautifully documented in the manual [2]; Good luck!

[1] https://groups.google.com/forum/#!msg/prawn-ruby/6XW54cGy0GA/RdXwL0Zo_Z8J

[2] http://prawn.majesticseacreature.com/manual.pdf

like image 40
Simon Repp Avatar answered Sep 18 '22 16:09

Simon Repp