[cairo] Bevel Effect

Steve Harrington steven.harrington at comcast.net
Sun Nov 21 07:59:18 PST 2010


On 11/20/2010 10:46 AM, Jeremy Moles wrote:

>  Hey guys--I'm looking for some help here.
>
>  I've been searching, fruitlessly, for an algorithm that will allow me to
>  perform a bevel effect in C or C++ code to simple raw image data. I've
>  looked through gimp source, ImageMagick source, and anything else I
>  could find on Google.
>
>  I was wondering if anyone here had any advice or any ideas about how I
>  could implement this? Specifically, I will be using it in Cairo to apply
>  a bevel effect to text. Is this something that could be achieved PURELY
>  in Cairo, or would I need to manipulate the image data directly?
>
>  Any help or advice would be appreciated. What I'm going for is
>  essentialy the same functionality gimp provides in it's
>  "Scripts->Decor->Apply Bevel" script.
>
>  --
>  cairo mailing list
>  cairo at cairographics.org
>  http://lists.cairographics.org/mailman/listinfo/cairo
>
>
If what you are trying to do is apply a beveled frame around a surface
then it is fairly straight forward.  Just create two drawing surfaces.
For example, suppose you have a surface of 600x600 and you want a bevel
5 units wide then you create a second surface of 590x590.  On the first
surface you draw the frame (code below).  You pass the second surface to
the drawing routine for the image you want to display.  When you want to
display the frame+image do:

   cairo_set_source_surface( Frame_cr, Frame_Surface, 0.0, 0.0 );
   cairo_paint( Frame_cr );
   cairo_stroke( Frame_cr );

   cairo_set_source_surface( Image_cr, Image_Surface, FrameWidth,
FrameWidth );
   cairo_paint( Image_cr );
   cairo_stroke( Image_cr );

May not be the best approach but it works for me.  Hope this helps.

The code to create the frame:
static void FrameIt( cairo_t  *cr, gint width, gint height, gint
FrameWidth )
{
   /* Draw my frame */
#define SQRT2 1.4132
#define Inner FrameWidth*SQRT2
   cairo_set_source_rgb( cr, 0.0, 0.0, 1.0 );
   cairo_set_line_width( cr, 1.0 );
   cairo_move_to( cr, width, 0.0 );
   cairo_line_to( cr, 0.0, 0.0 );
   cairo_line_to( cr, 0.0, height );
   cairo_line_to( cr, Inner, height-Inner );
   cairo_line_to( cr, Inner, Inner );
   cairo_line_to( cr, width-Inner, Inner );
   cairo_fill( cr );
   cairo_stroke( cr );
   cairo_set_source_rgb( cr, 0.0, 0.0, 0.7 );
   cairo_move_to( cr, 0.0, 0.0 );
   cairo_line_to( cr, Inner, Inner );
   cairo_stroke( cr );

   cairo_set_source_rgb( cr, 0.0, 0.0, 0.5 );
   cairo_set_line_width( cr, 1.0 );
   cairo_move_to( cr, width, 0.0 );
   cairo_line_to( cr, width, height );
   cairo_line_to( cr, 0.0, height );
   cairo_line_to( cr, Inner,  height-Inner );
   cairo_line_to( cr, width-Inner, height-Inner );
   cairo_line_to( cr, width-Inner, Inner );
   cairo_fill( cr );
   cairo_stroke( cr );
   cairo_set_source_rgb( cr, 0.0, 0.0, 0.3 );
   cairo_move_to( cr, width, height );
   cairo_line_to( cr, width-Inner, height-Inner );
   cairo_stroke( cr );

   return;
}





More information about the cairo mailing list