# [cairo] scale with text

Tobias Ceglarek dev at ceglarek.org
Sun Feb 18 02:27:27 PST 2007

```Wow! Thank you very much for your detailed answer!

Tomorrow i will have an exam. After this I will work with the cairo
thing again. I will make a report as soon as possible!

Prinz Igor

On Sat, 2007-02-17 at 14:01 -0800, Carl Worth wrote:
> On Sat, 17 Feb 2007 21:03:47 +0100, Tobias Ceglarek wrote:
> > How can I zoom the cairo surface with all its elements (text, paths, ..)
> > keeping the ratios of sizes?
>
> Ah, yes. This is a great question, and like many great questions has
>
> First, text is indeed special. The non-linearity you are seeing is due
> to the fact that there is hinting going on with your font. Hinting is
> a process of adjusting the glyph paths and metrics so that they align
> nicely with device-pixel boundaries.
>
> For example, look closely (real close!) at your images and notice that
> the pixels of the H glyphs are entirely black---there aren't any
> shades of gray[1] that would happen if the glyph path didn't like
> exactly "on pixel boundaries"[2].
>
> But then do the math. In the original image, you've got an H that is
> 11 pixels wide with 4 pixels between each. So 15 total pixels per
> glyph. In the zoomed image, you've got an H that is 12 pixels wide
> with 5 pixels between each for 17 total pixels per glyph. But with
> your scale factor, (which appears to be 1.1), you would expect the
> zoomed glyph to occupy 15 * 1.1 = 16.5 pixels. And sure enough, an
> error of 0.5 pixels per glyph * 20 glyph accounts for the ~ 10 pixels
> of overlap you are seeing in the zoomed image.
>
> OK, so that's what's happening. Now, how to fix this problem? It
> still depends on the details of what you want. So here are a few
> different things that could be part of the answer:
>
> 1. Turn off the hinting
>
>    You can do this with cairo_font_options_set_hint_style and
>    cairo_font_options_set_hint_metrics. See the example I attached
>    below for details. (And note that I also explicitly set
>    ANTIALIAS_GRAY since you really don't want ANTIALIAS_SUBPIXEL
>    without hinting).
>
>    This is the right answer if you want to be able to scale without
>    any change in shape, (for example, if you're animating a zoom with
>    text, the changes in shape and position can be very
>    undesirable). But, without hinting, text can also be much less
>    legible, so it's not necessarily always the right thing.
>
> 2. Measure your text, and base other elements sizes on that
>
>    For the most legible, static text, you really do want to leave the
>    hinting on. So one way to get good results is to first choose a
>    font size, then measure the text, and base the size of constrained
>    related items based on the results.
>
>    Measuring the text first is often necessary anyway, since many
>    applications will need to be able to support an arbitrarily supplied
>    font at run-time, and the metrics will vary from one font to the
>    other.
>
> 3. Stop using the toy API (use cairo_show_glyphs instead of cairo_show_text)
>
>    If you really want to enforce constraints on the positioning of
>    your glyphs, then you really don't want to be using cairo_show_text
>    anyway. Instead, you can use cairo_show_glyphs to individually
>    position each glyph.
>
>    It's a fair amount of extra work to use cairo_show_glyphs instead,
>    (you have to do your own mapping from characters to font glyph IDs,
>    for example), so it's often wise to adopt a library such as pango
>    that does those tasks for you.
>
> I hope that helps,
>
> -Carl
>
> [1] Nor rainbow colors if you might have sub-pixel rendering going on.
>
> [2] "half-way between pixel sample-points" if you can't accept that
> pixels are little squares with edges.
>

```