PyCairo – How to get SVG unit?
Last Updated :
13 Feb, 2023
In this article, we will see how we can get 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
- 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
- EM The size of the element’s font
- EX The x-height of the element’s font
- PX Pixels (1px = 1/96th of 1in)
- IN Inches (1in = 2.54cm = 96px)
- CM Centimeters (1cm = 96px/2.54)
- MM Millimeters (1mm = 1/10th of 1cm)
- PT Points (1pt = 1/72th of 1in)
- PC Picas (1pc = 1/6th of 1in)
- PERCENT Percent, a value that is some fraction of another reference value.
In order to use this we will use get_document_unit with SVG surface object
Syntax : get_document_unit()
Argument : It takes no argument
Return : It returns SVG unit but when printed it shows value associated with it.
Python3
import cairo
with cairo.SVGSurface( "geek95.svg" , 700 , 700 ) as surface:
context = cairo.Context(surface)
context.rectangle( 100 , 100 , 100 , 100 )
context.set_source_rgba( 0.4 , 1 , 0.4 , 1 )
context.stroke()
value = surface.get_document_unit()
print ( "SVG unit= " + str (value))
|
Output:
SVG unit= 7
Share your thoughts in the comments
Please Login to comment...