get bounded box using cairo_stroke_extents

Mark Olesen <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <CAExh6bYSr5P2y8BSOatNWR22eVHtYiy7nw4gmAQyoHeTZXRMHw@mail.gmail.com>
I am trying to get the bounded box using cairo_stroke_extents
(cairo_fill_extents has similar issue). I wind up with x1 being
way off. The rest look okay. I have attached a png. The blue
rectangle is the bounds box. Any suggestions?

Thanks in advance.

#include <cairo.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

int
main (int argc, char *argv[])
{
  cairo_surface_t* cs;
  cairo_t *cr;
  double angle;
  double skewx;
  cairo_matrix_t matrix;
  double x1, x2, y1, y2;

  cs= cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 180.,100.);
  cr= cairo_create(cs);

  cairo_set_line_width(cr, 1.);

  cairo_save(cr);
  cairo_set_source_rgb(cr, 1., 0., 0.);
  cairo_rectangle(cr, 50., 20., 50., 50.);
  cairo_stroke(cr);
  cairo_restore(cr);

  cairo_save(cr);
  angle= 45.;
  skewx= tan(angle * M_PI / 180.);
  cairo_matrix_init(
      &matrix,
      1.0, 0.0,
      skewx, 1.0,
      50.0, 20.0);
  cairo_transform(cr, &matrix);
  cairo_rectangle(cr, 0., 0., 50., 50.);
  cairo_translate(cr, -50., -20.); // *Lawrence D'Oliveiro tip*
  cairo_set_source_rgb(cr, 0., 0., 0.);
  cairo_stroke_extents(cr, &x1, &y1, &x2, &y2);
  cairo_stroke(cr);
  cairo_restore(cr);

  printf("(%lf, %lf) (%lf,%lf)\n", x1, y1, x2, y2);
/*
x1 is way off
(-1.500000, 19.500000) (151.500000,70.500000)
*/
  cairo_set_source_rgb(cr, 0., 0., 1.);
  cairo_rectangle(cr, x1, y1, x2-x1, y2-y1);
  cairo_stroke(cr);

  cairo_surface_write_to_png(cs, "skew.png");

  cairo_destroy(cr);
  cairo_surface_destroy(cs);

  return 0;
}

-- 
cairo mailing list
[email protected]
https://lists.cairographics.org/mailman/listinfo/cairo
skew.png (image/png, 728 B) - not displayed
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.