<div dir="ltr">Not sure exactly what you are trying to do, but this is probably what you want (this also assumes x2>x1 and y2>y1):<div><br></div><div><font face="monospace">width = ceil(x2 * 4.6) - floor(x1 * 4.6);</font></div><div><font face="monospace">height = ceil(y2 * 4.6) - floor(y1 * 4.6);</font></div><div><br></div><div>The reason for the ceil and floor is so the fractional-covered pixels are rounded out to the result. You have to do this after you convert to pixels which is what multiplying by 4.6 is doing (?).</div><div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 9, 2022 at 7:16 AM Andreas Falkenhahn <<a href="mailto:andreas@falkenhahn.com">andreas@falkenhahn.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I can fix the issue by adding 2 to "height" like so:<br>
<br>
width = fabs(ceil(x2) - floor(x1));<br>
height = fabs(ceil(y2) - floor(y1));<br>
<br>
width = ceil(width * 4.6);<br>
height = ceil(height * 4.6) + 2;<br>
<br>
But this feels very kludgy and probably only works for my specific case and not for any arbitrary transformation values.<br>
<br>
Is there really no general solution to this problem? IMHO it's a rather common thing to do and I'm a bit puzzled why this should be so difficult to solve...<br>
<br>
On 08.09.2022 at 19:24 Andreas Falkenhahn wrote:<br>
<br>
> I've tried this:<br>
<br>
> width = fabs(x2 - floor(x1));<br>
> height = fabs(y2 - floor(y1));<br>
<br>
> width = ceil(width * 4.6);<br>
> height = ceil(height * 4.6);<br>
<br>
> Still cuts off a line of the "C" shape. I've also tried this:<br>
<br>
> width = fabs(ceil(x2) - floor(x1));<br>
> height = fabs(ceil(y2) - floor(y1));<br>
<br>
> width = ceil(width * 4.6);<br>
> height = ceil(height * 4.6);<br>
<br>
> No success either. A line is cut off.<br>
<br>
> Any more ideas? You can find my test program below...<br>
<br>
> On 08.09.2022 at 19:03 Behdad Esfahbod wrote:<br>
<br>
>> Try flooring x1,y1 before subtracting them from x2,y2 for computing the size of your surface.<br>
<br>
<br>
>> behdad<br>
>> <a href="http://behdad.org/" rel="noreferrer" target="_blank">http://behdad.org/</a><br>
<br>
>> On Thu, Sep 8, 2022 at 10:21 AM Andreas Falkenhahn <<a href="mailto:andreas@falkenhahn.com" target="_blank">andreas@falkenhahn.com</a>> wrote:<br>
<br>
>> Is there really nobody who knows how to solve this? There surely<br>
>> must be a way to find out the *exact* extents of transformed<br>
>> graphics? The idea is to find out the exact dimensions in order to<br>
>> allocate a Cairo surface that *exactly* matches the size of the path.<br>
>> <br>
>> On 28.08.2022 at 21:47 Andreas Falkenhahn wrote:<br>
>> <br>
>>> Hi,<br>
>> <br>
>>> is there any way to get the *exact* extents of transformed<br>
>>> graphics? AFAICS, cairo_fill_extents() is in user coordinates, i.e.<br>
>>> it doesn't take any transformation settings into account. So I've<br>
>>> tried to apply the transformation manually to what I get from<br>
>>> cairo_fill_extents() but I don't seem to get it 100% right. It looks<br>
>>> like one row is missing at the bottom. <br>
>> <br>
>>> This is what I've tried:<br>
>> <br>
>>> cairo_t *cr;<br>
>>> cairo_surface_t *surface;<br>
>>> cairo_matrix_t cm;<br>
>>> int k;<br>
>>> double tx = 0, ty = 0;<br>
>>> double x1, y1, x2, y2;<br>
>>> <br>
>>> surface = cairo_image_surface_create(CAIRO_FORMAT_A8, 1000, 1000);<br>
>>> cr = cairo_create(surface);<br>
>> <br>
>>> for(k = 0; k < 2; k++) {<br>
>>> <br>
>>> cairo_new_path(cr);<br>
>>> cairo_move_to(cr, tx, ty);<br>
>>> cairo_set_font_size(cr, 100);<br>
>>> cairo_text_path(cr, "C"); <br>
>>> <br>
>>> if(!k) { <br>
>>> <br>
>>> double width, height;<br>
>>> <br>
>>> cairo_fill_extents(cr, &x1, &y1, &x2, &y2);<br>
>>> <br>
>>> tx = -x1;<br>
>>> ty = -y1; <br>
>>> <br>
>>> width = fabs(x2 - x1);<br>
>>> height = fabs(y2 - y1);<br>
>> <br>
>>> width = ceil(width * 4.6);<br>
>>> height = ceil(height * 4.6);<br>
>>> <br>
>>> cairo_destroy(cr);<br>
>>> cairo_surface_destroy(surface);<br>
>>> <br>
>>> surface =<br>
>>> cairo_image_surface_create(CAIRO_FORMAT_A8, width, height);<br>
>>> cr = cairo_create(surface); <br>
>>> <br>
>>> cairo_matrix_init(&cm, 4.6, 0, 0, 4.6, 0, 0);<br>
>>> cairo_set_matrix(cr, &cm); <br>
>>> }<br>
>>> }<br>
>>> <br>
>>> cairo_fill(cr);<br>
>> <br>
>>> cairo_surface_flush(surface);<br>
>>> cairo_surface_write_to_png(surface, "tmp.png");<br>
>>> <br>
>>> cairo_destroy(cr);<br>
>>> cairo_surface_destroy(surface);<br>
>> <br>
>>> I'm attaching the resulting image. As you can see, there's at least<br>
>>> one row of the "C" shape missing at the bottom of the image.<br>
>> <br>
>>> Any ideas how to get this right? Is this some sort of floating<br>
>>> point inaccuracy and should I just add 1 to width/height to solve<br>
>>> this or am I doing something wrong here and there is a better way?<br>
>> <br>
>> <br>
>> <br>
>> -- <br>
>> Best regards,<br>
>> Andreas Falkenhahn mailto:<a href="mailto:andreas@falkenhahn.com" target="_blank">andreas@falkenhahn.com</a><br>
>> <br>
>> <br>
<br>
<br>
<br>
<br>
-- <br>
Best regards,<br>
Andreas Falkenhahn mailto:<a href="mailto:andreas@falkenhahn.com" target="_blank">andreas@falkenhahn.com</a><br>
<br>
</blockquote></div>