PyCairo – Creating Text Paths
In this article, we will learn how we can display text and converting text into paths using PyCairo in python. You can convert a text string to a path. Once you have the path then you can fill it (that will look similar to just displaying text the normal way), or just outline it, or both.
PyCairo : Pycairo is a Python module providing bindings for the cairo graphics library. This library is used for creating SVG i.e vector files in python. The easiest and quickest way to open an SVG file to view it (read-only) is with a modern web browser like Chrome, Firefox, Edge, or Internet Explorer—nearly all of them should provide some sort of rendering support for the SVG format.
SVG file is a graphics file that uses a two-dimensional vector graphic format created by the World Wide Web Consortium (W3C). It describes images using a text format that is based on XML. SVG files are developed as a standard format for displaying vector graphics on the web.
Steps of Implementation :
- Import the Pycairo module.
- Create a SVG surface object and add context to it.
- Setting text font size and style using set_font_size ( ) , select_font_face( )
- Display the text using show_path ( )
- Setting path color and width using set_source_rgb ( ) , set_line_width( )
Below is the implementation :
Python3
# importing pycairo import cairo # creating a SVG surface # here geek95 is file name & 700, 700 is dimension with cairo.SVGSurface( "geek95.svg" , 700 , 700 ) as surface: # creating a cairo context object for SVG surface # using Context method Context = cairo.Context(surface) # setting color of the context Context.set_source_rgb( 1 , 0 , 0 ) # approximate text height Context.set_font_size( 50 ) # Font Style Context.select_font_face( "Arial" , cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) # position for the text Context.move_to( 35 , 45 ) # displays the text Context.text_path( "GeeksforGeeks.org" ) # Width of outline Context.set_line_width( 2 ) # stroke out the color and width property Context.stroke() # printing message when file is saved print ( "File Saved" ) |
Output :
Example 2 :
Python3
# importing pycairo import cairo # Defining surface area WIDTH = 3 HEIGHT = 3 PIXEL_SCALE = 200 # creating a SVG surface # here geek95 is file name surface = cairo.SVGSurface( 'geek95.svg' , WIDTH * PIXEL_SCALE, HEIGHT * PIXEL_SCALE) # creating a cairo context object for SVG surface # using Context method context = cairo.Context(surface) # Scaling Surface context.scale(PIXEL_SCALE, PIXEL_SCALE) # Creating Rectangle For Background context.rectangle( 0 , 0 , WIDTH, HEIGHT) # Color of Rectangle For Background context.set_source_rgb( 0.3 , 0.5 , 1 ) # Filling Color in Rectangle context.fill() # defining color context.set_source_rgb( 1 , 0 , 0 ) # Font Style context.set_font_size( 0.55 ) # font style context.select_font_face( "Arial" , cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL) # Creating Rectangle context.rectangle( 0.2 , 0.8 , 2.6 , 1 ) # move to x, y percentage of surface context.move_to( 0.3 , 1.5 ) # Display Text context.text_path( "MARVEL" ) # Filling the area context.set_fill_rule(cairo.FILL_RULE_EVEN_ODD) # Filling color context.fill() # stroke out the color and width property context.stroke() # printing message when file is saved print ( "File Saved" ) |
Output:
Please Login to comment...