In this article, we will learn how we can draw different line caps types using PyCairo in python. The line caps are endpoints of lines.
Steps of Implementation :
- Import the Pycairo module.
- Create a SVG surface object and add context to it.
- Setting color of the context & line width
- Setting of line cap style using set_line_cap( )
- Creating a line.
There are three different types line cap styles in PyCairo.
1. set_line_cap ( cairo.LINE_CAP_BUTT )
Example :
# importing pycairo import cairo
# creating a SVG surface # here geek94 is file name & 700, 700 is dimension with cairo.SVGSurface( "geek94.svg" , 700 , 700 ) as surface:
# creating a cairo context object for SVG surface
# using Context method
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 12 )
context.set_line_cap(cairo.LINE_CAP_BUTT)
context.move_to( 30 , 50 )
context.line_to( 150 , 50 )
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print ( "File Saved" )
|
Output :
2. set_line_cap ( cairo.LINE_CAP_ROUND )
Example :
# importing pycairo import cairo
# creating a SVG surface # here geek94 is file name & 700, 700 is dimension with cairo.SVGSurface( "geek94.svg" , 700 , 700 ) as surface:
# creating a cairo context object for SVG surface
# using Context method
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 12 )
context.set_line_cap(cairo.LINE_CAP_ROUND)
context.move_to( 30 , 50 )
context.line_to( 150 , 50 )
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print ( "File Saved" )
|
Output :
3. set_line_cap ( cairo.LINE_CAP_SQUARE )
Example :
# importing pycairo import cairo
# creating a SVG surface # here geek94 is file name & 700, 700 is dimension with cairo.SVGSurface( "geek94.svg" , 700 , 700 ) as surface:
# creating a cairo context object for SVG surface
# using Context method
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 12 )
context.set_line_cap(cairo.LINE_CAP_SQUARE)
context.move_to( 30 , 50 )
context.line_to( 150 , 50 )
# stroke out the color and width property
context.stroke()
# printing message when file is saved
print ( "File Saved" )
|
Output:
Note: A line with cairo.LINE_CAP_SQUARE cap used has a different size than a line with cairo.LINE_CAP_BUTT cap. If the line is z units wide, the line with cairo.LINE_CAP_SQUARE cap will be exactly z units greater in size; z/2 units at the beginning and z/2 units at the end.
All three type of line caps can be seen in the below python Example, and the output of each line cap can also be compared
# importing pycairo import cairo
# creating a SVG surface # here geek94 is file name & 700, 700 is dimension with cairo.SVGSurface( "geek94.svg" , 700 , 700 ) as surface:
# creating a cairo context object for SVG surface
# using Context method
context = cairo.Context(surface)
context.set_source_rgba( 0 , 0 , 0 , 1 )
context.set_line_width( 12 )
# Setting line cap style
context.set_line_cap(cairo.LINE_CAP_SQUARE)
context.move_to( 30 , 50 )
context.line_to( 150 , 50 )
# stroke out the color and width property
context.stroke()
# Setting line cap style
context.set_line_cap(cairo.LINE_CAP_ROUND)
context.move_to( 30 , 90 )
context.line_to( 150 , 90 )
context.stroke()
# Setting line cap style
context.set_line_cap(cairo.LINE_CAP_SQUARE)
context.move_to( 30 , 130 )
context.line_to( 150 , 130 )
context.stroke()
# printing message when file is saved
print ( "File Saved" )
|
Output :