Fill in half a shape?

2,514 views
Skip to first unread message

Robert Holak

unread,
Sep 4, 2012, 4:07:21 PM9/4/12
to Prawn
I am trying to fill in half a shape, such as the top half of a circle,
or top portion of a rounded rectangle. This is easy with straight line
based shapes - just draw a top and bottom. But I am trouble coming up
with a good way to fill in a portion of a rounded shape. Has anybody
come up with a nice trick to do this?

Daniel Nelson

unread,
Sep 4, 2012, 6:25:39 PM9/4/12
to prawn...@googlegroups.com
I started a simple prawn-shapes module a couple years ago. It can do
circles and stars, though admittedly, these are easy because it is
easy to draw half a circle.
https://github.com/Bluejade/prawn-shapes
https://github.com/downloads/Bluejade/prawn-shapes/arc.pdf

Robert Holak

unread,
Sep 5, 2012, 10:48:41 AM9/5/12
to prawn...@googlegroups.com
Thanks for the help! I had never used the add_content method - it looks like that could be a lifesaver if I want to do any other weird things not defined in Prawn. That and a PDF reference manual :)  Below is what I ended up with to make a semi-filled rounded rectangle. I'm guessing since I clip then reset, I do need to redo the round rectangle path to stroke it. Otherwise, the stroke is clipped out also. If there's a more efficient way to re-do the round rectangle path, I'm all ears though - otherwise I'm cleaning this up a tad and making it into a nice little method.  
I attached an example of what I am making - if anyone is interested I can share the final code on github - it will basically be the below with the values grabbed from the parameters passed to the method.

begin # two-part shaded round rectangle
    pdf.save_graphics_state
    # set clipping shape
    pdf.rectangle [300,300], 200,20
    pdf.set_clip_path

    pdf.rounded_rectangle [300,300], 200, 60, 10
    pdf.horizontal_line 300, 500, at: 280

    pdf.transparent 0.5,1.0 do
        pdf.fill_color "#ff0000"
        pdf.fill
    end

    pdf.restore_graphics_state
    pdf.rounded_rectangle [300,300], 200, 60, 10
    pdf.horizontal_line 300, 500, at: 280
    pdf.stroke
end

On Tuesday, September 4, 2012 5:27:39 PM UTC-5, Rob wrote:
Prawn does not (that I know of) have support for a clipping path. But it is easy to add by extending the Graphics class.

module Prawn

  module Graphics
    def set_clip_path
      add_content "W* n"
    end
  end
end

Now you can create filled shape like a rectangle or image and use a path to clip the shape, in your case the clipping shape would be a circle.

In your code: 
save_graphics_state
 # create the clipping shape here like a circle
 set_clip_path    ## from the above method
 # next create any shapes to be clipped like a rectangle with a filled color or even an image
restore_graphics_state

The order is important, the clipping shape goes on the bottom. After set_clip_path is called every subsequent item is clipped until restore_graphics_state is called.
test.pdf
Reply all
Reply to author
Forward
0 new messages