[cairo] thin antialiased lines

Carl Worth cworth at cworth.org
Sun Jul 15 10:00:42 PDT 2007

On Sun, 15 Jul 2007 13:12:54 +0200, "control H" wrote:
> An other solution I tried was disabling the whole anti alias thing.
> But that results in ugly lines; their width is inconsistent. It's a
> bit hard to describe, I can send example charts to clarify it.

I know exactly what you mean. And the effect is exactly what is
expected given the current implementation in cairo---which was that we
implemented everything for high-quality anti-aliased rendering and
then only added the non-anti-aliased rendering under duress by simply
flipping a bit, (or an enum at least).

> Let's put it this way: lines drawn with cairo (without AA) compared to the
> old fashioned gdk_draw_line() are far inferior, having pixels at
> places where I don't want them to be.

Yes, cairo's current non-antialiased lines will look worse than
GDK's. But even GDK doesn't draw nice non-antialiased lines I think,
(particularly at larger widths).

There _are_ known techniques for drawing very nice non-antialiased
lines, (much better than what GDK does I believe). The key idea is to
use a specially-created polygon for the pen called a Pen
Polygon. This a technique developed by John Hobby for his Ph.D. and
then also described much more succinctly here:

	[Hobby89] John D. Hobby, Rasterizing Curves of Constant Width,
	JACM 36(2), 1


A very interesting thing is that cairo's stroke algorithm already
works exactly as Hobby's algorithm requires, (we really liked the
performance and robustness aspects of using a polygonal approximation
of the pen). We didn't originally implement the Pen Polygon part
since we didn't care about (or even make available) non-antialiased
line stroking.

So, augmenting cairo to do this well is really as simple as writing a
single function that accepts a desired line width and computes a Pen
Polygon, (several Pen Polygons for small, common sizes could even be
pre-computed and just returned from a table).

I think that the mailing list archives even have some messages from
someone who experimented with doing this in cairo quite some time ago,
(I don't recall now who or when or how the results were).

> PS I'm new to this list. Of course I've first been searching the
> archives but couldn't find this problem/solution. If this has been
> discussed before I'm sorry.

No problem at all. Welcome to the list!

And repeating an old issue is just a reminder to us that we haven't
addressed it yet. So please don't feel bad about that!


PS. You did say once:

> [Is there a way] to draw small anti aliased lines of 1
> pixel thick and in the correct color?

I'd encourage you to think deeply about what you're actually asking
for here. The anti-aliasing that cairo does involves computing
coverage for partially-covered pixels. So the results you are getting
now _are_ in the correct color. When a line has a width of 0.5 and is
black, you shouldn't expect to get a pixel darker than "half black"

Now, there are some issues about apparent line width that could be
related to doing "gamma-correct" compositing, (which cairo currently
doesn't do). And fixing that, (while not trivial), _might_ be a way to
give you a solution you like without dropping down to using
non-antialiased rendering.
-------------- 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/20070715/b62f14ad/attachment.pgp 

More information about the cairo mailing list