Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

PyCairo – How to set SVG unit ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will see how we can set SVG unit in pycairo using python. SVG unit is used to describe the units valid for coordinates and lengths in the SVG specification

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.

These are the following SVG units in Pycairo

  1. USER User unit, a value in the current coordinate system. If used in the root element for the initial coordinate systems it corresponds to pixels
  2. EM The size of the element’s font
  3. EX The x-height of the element’s font
  4. PX Pixels (1px = 1/96th of 1in)
  5. IN Inches (1in = 2.54cm = 96px)
  6. CM Centimeters (1cm = 96px/2.54)
  7. MM Millimeters (1mm = 1/10th of 1cm)
  8. PT Points (1pt = 1/72th of 1in)
  9. PC Picas (1pc = 1/6th of 1in)
  10. PERCENT Percent, a value that is some fraction of another reference value.

In order to use the we will use set_document_unit with SVG surface object

Syntax : set_document_unit()

Argument : It takes SVU unit i.e value associated with it.

Return : It returns non

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
    context = cairo.Context(surface)
  
    # creating a rectangle(square)
    context.rectangle(10, 10, 100, 100)
  
    # setting color of the context
    context.set_source_rgba(0.4, 1, 0.4, 1)
  
    # stroke out the color and width property
    context.fill()
  
    # Setting SVG unit
    surface.set_document_unit(7)
  
# printing
print("SVG unit Changed")

Output:

SVG unit Changed
My Personal Notes arrow_drop_up
Last Updated : 12 Nov, 2020
Like Article
Save Article
Similar Reads
Related Tutorials