[cairo] Any place for a stroke / fill / path hinting layer in cairo?

Peter Clifton pcjc2 at cam.ac.uk
Wed Jan 7 05:08:28 PST 2009


Hi guys,

I believe pixel aligned graphics is somewhat of a FAQ for people new to
cairo, and for our use in gEDA, a lot of the new code is devoted to
ensuring that certain pieces of graphics are aligned in some way with
the pixel grid.

I was wondering if any of the cairo developers could see a logical place
in the architecture which could handle a hinting layer, or hinting
options.
Would a hinting API involve hookable callbacks for the application to
provide cairo assistance, or would it involve teaching cairo various
methods of hinting which the app would specify?

Ultimately, I'd love to be able to use stock cairo calls everywhere,
with identical calls for printing to a ps / pdf surface, or rendering to
the screen. This would only be possible if I've already set up some
mechanism with the pixel-based surface (xlib, or image surface) to
ensure the output is on-grid where we needs it to be.



Specifically, I have the following types of drawing helpers - working in
place of gEDA's old rendering routines which took positions in pixels

lines (hinted according to width and end-cap, to give a +0.5px shift as
necessary to get on-grid, and to give a +1 to the larger X or Y
coordinate for BUTT capped horizontal or vertical lines. (For rendering
an inclusive span).

Boxes intended to align with our grid / other lines. For this, I work
out where lines forming the box would go, as drawn with the line
routines.

Boxes intended to appear centred on a line object. The centre of the box
is shifted 0.5px, as appropriate, based on the width of the line it is
to be visually centred onto. The half-width / half-height are hinted as
appropriate to bring their strokes back onto the pixel grid (this time
depending on the stroke width of the box).

Arcs. Our X and Y scales can be slightly non-uniform, so for arcs I
translate and apply a scale matrix. The centre of the arc is computed
from working out where lines touching its tangent at the extreme X and Y
points would land, assuming the arc was in fact a full circle. (This
allows arcs and line to join nicely, even with the non-uniform
scale-factor and pixel-grid hinting of lines.

Arcs intended to appear centred on a line object. (Similar to boxes
above. This time, the centre is hinted to the line we're trying to
centre on, and the radius is brought back on-grid (assuming we're above
some threshold radius).


Then we get onto stroke styles for drawing lines. This hinting isn't
very good - it mostly just works for horizontal / vertical lines. When
we're rendering a line-style which involves a pass with dots (zero width
segments with a round cap), I apply 0.5px hinting as required to land
those dots centrally on the pixel grid.

In addition, if the line-width is below a certain radius, we revert from
drawing dots to drawing squares, so anti-aliasing doesn't cause the
visual weight of the dot to fade into the background.


What has struck me, is that it would perhaps be useful to do this
hinting via some layer in cairo. As I've described, much of the hinting
I do is based on line-widths, and figuring out which pieces of graphic
are important to get aligned. This suggests to me that hinting in the
path layer alone would not help.. it has to be closer to where the
stroke / fill is applied, and for some objects, there may be different
aspects which should be hinted.. radius and/or centre, for example.

Getting access to the path inside the rendering engine may allow for
nicer hinting of the dots or dashes in a non-solid line style, however -
as a counter example, when drawing an arc, we only want to hint specific
points on it (e.g. the X/Y extremities in our case). It wouldn't
necessarily look good if we tried to hint every spline which an arc is
decomposed into.


Best regards,

-- 
Peter Clifton

Electrical Engineering Division,
Engineering Department,
University of Cambridge,
9, JJ Thomson Avenue,
Cambridge
CB3 0FA

Tel: +44 (0)7729 980173 - (No signal in the lab!)



More information about the cairo mailing list