User Space and Device Space

In this lesson, we'll cover user and device space in Pycairo.

We'll cover the following

Introduction to spaces

Pycairo has two coordinate spaces: user space and device space. By default, these two spaces are identical, which is why we treat them as one space. However, we can modify user space and get useful results.

User space is the coordinate system that we, as programmers, use to draw things in Pycairo. When we draw a rectangle that is 200 units by 100 units, those units are user space units.

Device space is the coordinate system Pycairo uses internally to draw images. If we use Pycairo to create a PNG image, 1 unit in device space will correspond to 1 pixel of the output image.

If we use Pycairo to create a vector output (such as an SVG file), 1 unit in device space will correspond to 1 “unit” in the vector file. However, since vector formats like SVG are scalable, this unit can be anything we want it to be.

Once we define a Pycairo Surface, the device space is fixed and user space can be changed as per our requirements.

Here is a diagram of user space and device space for a surface that was defined as 550 units by 550 units:

Get hands-on with 1200+ tech skills courses.