[cairo] Hairlines

Ian Britten britten at caris.com
Thu Jan 29 05:29:32 PST 2009


Hi Carl,
Many thanks for you patient explanations.  I'm coming to the realization
that I'm perhaps using Cairo (Or, at least its images) in a different
way than others, which is maybe why I keep getting tripped up on this.

For the sake of discussion, I'll reply to a couple of your points, but
as we both conclude, I can (And will have to) work around this myself...

> I think Gustavo's point is that if we add a "hairline mode" that draws a
> 1.0/300.0 device-pixel line in PDF output, but a 1.0 device-pixel line
> in image-surface output, then those hairlines can look wildly different
> in comparison to other elements. For example, the PDF hairlines could
> look much thinner than non-hairline elements, while the image hairlines
> would look much thicker than the same elements.

[ See below first, but... ]
For my case, I'd still posit that Cairo is already drawing the data
'wildly different'.  I drew a line with a non-zero thickness and a
non-zero alpha, and nothing came out.  I would (naively) think this
should always result in at least one device unit being rendered - One
image pixel, in this case.

[ Disclaimer - I'm fairly ignorant of the details of things like
anti-aliasing, sub-pixel rendering, and any other fancy stuff that
Cairo does, which probably clouds my perspective... ]

> I think the fundamental problem you are dealing with is that
> low-resolution devices are simply less capable than high-resolution
> devices. I know that I wish I could always ignore pixels, but the fact
> is that I can't yet. (I wrote a little bit of line-chart-drawing code
> this week and I was amazed at how much math I had to do to get the
> scaling set up just right so that the frame and tick marks would align
> to pixel bounaries.)

I think this is the crux of the difference in perspective.
I'm not using Cairo to render directly to the (low-resolution) screen.
I'm generating output images, many of which have resolutions far beyond
that of any monitor (eg: 40,000 x 40,000 pixels).  Downsampling and
rendering these images to a monitor is a separate problem from
actually generating them.
However, if you zoom in far enough on these images, you should
eventually be able to see the (1 image pixel thick) hairlines, which
are required to be there.


Also, the words "align to pixel boundaries" seems to recur regularly
in Cairo, but I'll confess that I don't appreciate how it's supposed
to impact me.  Other than these friggin hairlines, all my rendering is
done via Cartesian 'user units', MM thicknesses, etc.  I don't think
about 'pixels' anywhere in my drawing code, but maybe I'm supposed to
be... ?

> But, the feature you are wanting here should be something that you can
> implement on top of cairo just fine. You should be able to write a
> little function like:
> 
> 	_stroke_hairline (cairo_t *cr);
> 
> which can cairo_save(), examine the device-pixel size, set up an
> identity matrix, choose an appropriate line width based on the
> device-pixel size, cairo_stoke(), and then cairo_restore().

Ya, thanks.  I'm preparing to do something like that.
I guess the only reason I keep 'beating this dead horse' is for the
sake of the next person trying to do this, and wondering if there
couldn't/shouldn't be a more obvious/cleaner way to handle this
idiom...

Anyways, like I said, thanks for the discussion.
Ian


More information about the cairo mailing list