[cairo] drawing inconsistency

Carl Worth cworth at cworth.org
Tue Jan 22 06:57:03 PST 2008


On Mon, 21 Jan 2008 14:09:18 +0200, "Adi Oanca" wrote:
>     I've put my hands on cairo about a couple weeks ago. In that time,
> I did not find some information about how Cairo maps logical
> coordinates onto pixels.

We probably should write up more about this, (anytime a user can't
find an answer to a question after honestly looking suggests that
there's some documentation somewhere that could be improved).

But fortunately there's very little to have to explain here.

There are two coordinate systems in cairo, ("user space" and "device
space"). Initially they're the same, so that's what I'll talk about
here. But for completeness, the user can change the mapping from user
space to device space with things like cairo_translate, cairo_scale,
cairo_rotate, cairo_transform, and cairo_set_matrix.

So what does device space look like? It's a two-dimensional space with
the origin at the upper-left of your device. The positive X axis
extends to the right and the positive Y axis extends down.

It's useful to draw a grid showing lines at the integer coordinates of
device-space, (hopefully this ASCII (UTF-8!) art will be rendered near
square for you):

	 0   1   2   3
	0+---+---+---+...
	 |   |   |   |
	1+---+---+---+...
	 |   |   |   |
	2+---+---+---+...
	 .   .   .   .
	 .   .   .   .

Finally, we can introduce the notion of "pixels" which are one or more
sample points distributed throughout each of the visible
"squares". More specifically, for non-antialiased drawing,
(CAIRO_ANTIALIAS_NONE), there is one sample point for each pixel and
they are located at half-integer positions on the device-pixel grid,
(that is, in the center of each square in my diagram above)[*].

Even with antialiased rendering, (which is the default for cairo),
it's often desirable to cover some pixels entirely. And the key for
doing this is to make the final geometry lie exactly on integer
coordinates of the device-pixel grid, (the "lines" in the figure
above).

So, drawing with pixel-precision in cairo is actually very simple. The
easiest thing to do is to simply sketch out the desired effect on the
lines graph paper and cairo will predictably[*] sample pixels within
each square.

-Carl

[*] As Bertram mentioned earlier, we recently discovered a bug in
pixman for the sample position with A1 surfaces. This bug is hit by
CAIRO_ANTIALIAS_NONE case. If your geometry stays "on the lines" as
recommended above, the result will still be predictable and the bug
shouldn't affect you. But the results with non-aligned geometry can
currently be a bit surprising.

Bertram has recently posted a fix for this bug which we'll be
incorporating into pixman soon so it will be available in the next
release of pixman, (which will come out before cairo 1.6). So with the
next, cairo 1.6 release, you should even be able to get predictable
sampling at the half-integer positions.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.cairographics.org/archives/cairo/attachments/20080122/3b42760e/attachment.pgp 


More information about the cairo mailing list