PyCairo – 如何获得 SVG 单元?
在本文中,我们将看到如何使用Python在 pycairo 中获取 SVG 单元。 SVG unit用于描述SVG规范中坐标和长度有效的单位
Pycairo是一个Python模块,为 cairo 图形库提供绑定。这个库用于在Python中创建SVG即矢量文件。打开 SVG 文件以查看它(只读)的最简单快捷的方法是使用现代网络浏览器,如 Chrome、Firefox、Edge 或 Internet Explorer——几乎所有这些都应该为 SVG 格式提供某种渲染支持.
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 the 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
# 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(100, 100, 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.stroke()
# getting the svg unit
value = surface.get_document_unit()
# printing the SVG unit
print("SVG unit= " + str(value))
输出:
SVG unit= 7