[cairo] Locking font / pen space

Bill Spitzak spitzak at gmail.com
Sat Aug 28 09:05:15 PDT 2010


On Aug 28, 2010, at 5:30 AM, Arjen Nienhuis wrote:

> Help me try to understand this please.
>
> A use case for cairo_lock_font_space() is to draw text of the same  
> size while using a different transform:
>
> // All captions should be the same size
> cairo_lock_font_space(cr);
>
> // draw as tall widget at [0,0] and add a caption
> cairo_save(cr);
> cairo_scale(cr, 2, 0.5);
> my_draw_widget(cr);
> cairo_show_text (cr, "My Tall Widget");
> cairo_restore(cr);
>
> // draw as narrow widget at [100,0] and add a caption
> cairo_save(cr);
> cairo_translate(100, 0);
> cairo_scale(0.5, 2);
> my_draw_widget(cr);
> cairo_show_text (cr, "My Narrow Widget");
> cairo_restore(cr);
>
> Right?

Yes that will draw text the same size as it was being drawn before the  
lock_font_space.

> Now what if I want the text in my_draw_widget() to scale? How would  
> that work?
>
> void my_draw_widget(cr)
> {
>     // draw a /!\ symbol
>     cairo_save(cr);
>     my_draw_triangle(30,0, 60,50, 0,50);
>     cairo_translate(25, 40);
>     cairo_set_font_size(cr, 30);
>     cairo_show_text (cr, "!");
>     cairo_restore(cr);
> }

That will draw text *exactly* the same whether font-lock is on or off.  
I think this may be the confusion.

Font-lock will only make a difference if there is a transform (rotate,  
scale, etc) between when you set the font and when you use it.

In fact I very much believe almost every current Cairo program will  
work correctly whether or not font-lock is on. That is because nobody  
ever does transforms between setting the font and printing, because  
the current behavior is never what is wanted. There may be some  
programs that set the font, don't draw, then transform, then draw,  
these would have to be fixed by swapping the transform and font  
setting. But just looking at the examples it seems very rare that  
programmers write it this way.

> I think I'd rather save the font space in a variable and restore it  
> when needed:
>
> my_label_font_space = cairo_save_font_space(cr);
>
> void my_draw_label(cr, text)
> {
>     cairo_save(cr);
>     cairo_restore_font_space(cr, my_label_font_space);
>     cairo_show_text (cr, text);
>     cairo_restore(cr);
> }

I can assure you this is NOT what is wanted. This sort of hack can be  
done now in Cairo (by building paths and then restoring the transform,  
or by loading the identity transform). The purpose of this call is to  
get the desired behavior with *fewer* calls!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100828/23716b07/attachment-0001.htm>


More information about the cairo mailing list