Fitting Text To A Given Width

Lawrence D'Oliveiro <[email protected]>
Newsgroups gmane.comp.lib.cairo
Organization Geek Central
Message-ID <[email protected]>
I have been adding some code to my Qahirah Notebooks
<https://github.com/ldo/qahirah_notebooks> demonstrating how to fit
text to a given width (in the “Beginning Text” notebook). The example
uses a list of text items of wildly differing width, so no single font
size looks best for all. Instead, I apply a non-uniform scaling to the
text to make it fit.

The core of the (Python) code looks like this:

    ctx.font_matrix = uniform_font_matrix
    text_width = ctx.text_extents(item).advance.x
    if text_width > fit_width :
        ctx.font_matrix = Matrix.scale((fit_width / text_width, 1)) * uniform_font_matrix
    #end if
    ctx.show_text(item)

However, if you try the notebook, you will see that the lines frequently
come out too wide or too narrow. I tried playing with the
HINT_STYLE_xxx and HINT_METRICS_xxx font options, but these didn’t seem
to help.

What did work was to do it in a more complex way: render the text to a
separate ImageSurface with uniform scaling, then apply nonuniform
scaling to that as a pattern to render it back to the final image. This
gives me much more consistent results.

Is there some rounding going on in the text rendering?
-- 
cairo mailing list
[email protected]
https://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.