[cairo] Getting started

Vladimir Vukicevic vladimirv at gmail.com
Sun Jul 24 03:22:25 PDT 2005

On 7/23/05, Behdad Esfahbod <behdad at cs.toronto.edu> wrote:
> Hi,
> After Carl's good tutorial yesterday at OLS, I finally got
> started using Cairo.  As an example, I added Cairo support to
> dasher, the predictive text input widget.  I found it quite
> straightforward to convert from Gdk drawing to Cairo.  But I've
> got a big problem:  Currently I get the two-pixel-wide blurred
> lines, so I thought I can cairo_translate(cr, -0.5, -0.5) to fix
> that.  But doing that (looks like fixing the problem) makes the
> rendering /extremely/ slow, so much that it's unusable.
> Any way to get around it?  Why is a translate so slow?  My
> graphics consists of a bunch of rectangles...

Note that in the FAQ entry about smeared lines it says "adjust the
endpoints by 0.5 in the appropriate direction"  For drawing lines, you
don't want to offset both the x and the y coordinates by 0.5 -- just
one, depending on the orientation of your line.  The line is converted
into a solid region that is then filled, by extending the path by half
of the line-width to either side.  So, for a horizontal line that you
want to extend from pixel 2 to pixel 5, you'd want to leave the x
coordinate as integer values, but add 0.5 to the y coordinate to push
the line down into the middle of the pixels.  But horizontally, you
want the line to take up an integer width of pixels.  Note that a line
going from 2 to 5 will only cover pixels 2,3,4 -- the line itself will
end at the boundary of pixel 5, and won't actually cover pixel 5. 
You'll have to add 1 to the end of the horizontal line in the x
direction to make it fill pixels 2,3,4,5.

Reading that over, I realize that the explanation is probably pretty
confusing -- but grab a piece of graph paper, pretend each square is a
pixel. Then draw a one-pixel-wide region, with your path running down
the middle of it.  It should be pretty clear which coordinates need to
be offset. :)

The reason you're getting a massive slowdown is that because with a
.5,.5 offset you need to blend the first and last pixel, you end up
going down a very slow codepath that's otherwise not needed.  (I
followed this path for a while looking for a similar bug in mozilla;
there may be a cairo bug involved that's causing additional slowdown,
but I didn't get that far.)

    - Vlad

More information about the cairo mailing list