[cairo] How to get extents of transformed graphics

Andreas Falkenhahn andreas at falkenhahn.com
Fri Sep 9 20:51:53 UTC 2022


On 09.09.2022 at 21:36 Michal Sudolsky wrote:
    
> 1.16.0_5

My version is 1.16.0.

> Will this produce the same result for you as for me?

No, when I run that code the "C" shape visibly exceeds the bounding rectangle and there's blank space at the top. But only when transformation is active. With no transformation the "C" shape fits the bounding rectangle perfectly. See attached screenshot.

Is this a Cairo bug or could it be related to the font? The "C" shape from your system looks like a different font than the one that is used on my system.

> cairo_surface_t *surface =
> cairo_image_surface_create(CAIRO_FORMAT_A8, 1000, 500);
>         cairo_t *cr = cairo_create(surface);
>         double x1, y1, x2, y2;
>         {
>   cairo_new_path(cr);
>   cairo_move_to(cr, 100, 100);
>   cairo_set_font_size(cr, 100);
>   cairo_text_path(cr, "C");
>   cairo_fill_extents(cr, &x1, &y1, &x2, &y2);
>   cairo_fill(cr);
>   cairo_rectangle(cr, x1, y1, x2 - x1, y2 - y1);
>   cairo_set_line_width(cr, 0.3);
>   cairo_stroke(cr);
>         }
>         {
>   cairo_rectangle(cr, x1 * 4.6, y1 * 4.6, (x2 - x1) * 4.6, (y2 - y1) * 4.6);
>   cairo_set_line_width(cr, 0.3);
>   cairo_stroke(cr);
>   cairo_matrix_t cm;
>   cairo_matrix_init(&cm, 4.6, 0, 0, 4.6, 0, 0);
>   cairo_set_matrix(cr, &cm);
>   cairo_new_path(cr);
>   cairo_move_to(cr, 100, 100);
>   cairo_set_font_size(cr, 100);
>   cairo_text_path(cr, "C");
>   cairo_fill(cr);
>         }
>         cairo_surface_flush(surface);
>         cairo_surface_write_to_png(surface, "tmp.png");
>         return 0;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: tmp.png
Type: image/png
Size: 7477 bytes
Desc: not available
URL: <https://lists.cairographics.org/archives/cairo/attachments/20220909/d74dbc16/attachment.png>


More information about the cairo mailing list