[cairo] stroke extents after scaling and rotation
Behdad Esfahbod
behdad at behdad.org
Wed Jun 11 07:02:28 PDT 2008
On Tue, 2008-06-10 at 15:46 +0200, Theo Veenker wrote:
> Hi,
>
> Regarding this thread:
> http://lists.cairographics.org/archives/cairo/2008-May/014207.html
>
> I found out I did something wrong in the test program there. I've
> changed it to the code below. This does work as expected,
No. This updated code of you is wrong. It takes the extents in the
rotated user space, but draws the box in device space.
behdad
> but when
> the target path is scaled > 1 then the stroke extents don't quite
> follow the ink. As demonstrated in the attached images. For the
> the horizontal line case and the vertical line case the y resp x
> extents are one line width too small.
>
> // gcc -o extents2 $(pkg-config --cflags --libs cairo) extents2.c
> #include <cairo.h>
> #include <math.h>
>
> int main()
> {
> cairo_surface_t *surface;
> cairo_t *cr;
> double x1, y1, x2, y2;
> int n;
>
> double offsetx = 60, offsety = 60;
> double scalex = 1.0, scaley = 1.0;
> double rotangle = 0.0 / 360.0 * M_PI;
>
> surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 620, 220);
> cr = cairo_create(surface);
>
> cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
> cairo_paint(cr);
>
> for (n = 0; n < 12; n++) {
> rotangle = n * 30.0 / 360.0 * M_PI;
> cairo_save(cr);
> cairo_translate(cr, offsetx, offsety);
> cairo_scale(cr, scalex, scaley);
> cairo_rotate(cr, rotangle);
> cairo_move_to(cr, -25, 0);
> cairo_line_to(cr, 25, 0);
> cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
> cairo_set_line_width(cr, 10);
> cairo_stroke(cr);
> cairo_restore(cr);
>
> cairo_save(cr);
> cairo_save(cr);
> cairo_translate(cr, offsetx, offsety);
> cairo_scale(cr, scalex, scaley);
> cairo_rotate(cr, rotangle);
> cairo_move_to(cr, -25, 0);
> cairo_line_to(cr, 25, 0);
> cairo_restore(cr);
> cairo_set_source_rgb(cr, 0.0, 0.0, 1.0);
> cairo_set_line_width(cr, 10);
> cairo_stroke_extents(cr, &x1, &y1, &x2, &y2);
> cairo_restore(cr);
>
> cairo_save(cr);
> cairo_new_path(cr);
> cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
> cairo_set_source_rgb(cr, 1.0, 0.0, 0.0);
> cairo_set_line_width(cr, 0.5);
> cairo_stroke(cr);
> cairo_restore(cr);
>
> offsetx += 100;
> if (n == 5) {
> offsety += 100;
> offsetx = 60;
> }
> }
>
> cairo_destroy(cr);
>
> cairo_surface_write_to_png(surface, "extents.png");
> cairo_surface_destroy(surface);
>
> return 0;
> }
>
> Could this be a bug or do I still not get it?
>
> Regards,
> Theo
>
--
behdad
http://behdad.org/
"Those who would give up Essential Liberty to purchase a little
Temporary Safety, deserve neither Liberty nor Safety."
-- Benjamin Franklin, 1759
More information about the cairo
mailing list