[cairo] Text stuffing with Cairo/pycairo?

cecashon at aol.com cecashon at aol.com
Wed May 1 17:40:55 UTC 2019


 
It is worth mentioning here that when you draw lines around the box with the usual way of doing this,

  cairo_fill_preserve(cr);
  cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
  cairo_stroke(cr);
  
 the scale will cause the lines to be non uniform. This is mentioned in the Cairo Tutorial under Working with Transforms.

To get the lines around the box to be uniform, the transformed points can be mapped to device space or the starting coordinates that cairo uses. Then the lines around the box can be drawn. The following shows this.

Have fun drawing.
Eric

...
    //Order transforms and draw.
    cairo_save(cr);
    cairo_translate(cr, center_box_x, center_box_y);
    cairo_rotate(cr, rotate);
    cairo_scale(cr, box_width/fabs(2.0*move_x), box_height/fabs(2.0*move_y));
    cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 0.4);
    double r1=-fabs(scale_x);
    double r2=-fabs(scale_y);
    double r3=fabs(2.0*scale_x);
    double r4=fabs(2.0*scale_y);
    cairo_rectangle(cr, r1, r2, r3, r4);
    cairo_fill(cr);
    //Save device cordinates for the rectangle so that lines are drawn correctly.
    double c1=r1;
    double c2=r2;
    double c3=r1;
    double c4=r2+r4;
    double c5=r1+r3;
    double c6=r2+r4;
    double c7=r1+r3;
    double c8=r2;
    cairo_user_to_device(cr, &c1, &c2);
    cairo_user_to_device(cr, &c3, &c4);
    cairo_user_to_device(cr, &c5, &c6);
    cairo_user_to_device(cr, &c7, &c8);
    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
    cairo_move_to(cr, move_x, move_y);
    cairo_show_text(cr, string);
    cairo_restore(cr); 

    //Draw box lines.
    cairo_save(cr);
    cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
    cairo_set_line_width(cr, 4.0);
    //Reset transform matrix.
    cairo_identity_matrix(cr);
    cairo_move_to(cr, c1, c2);
    cairo_line_to(cr, c3, c4);
    cairo_line_to(cr, c5, c6);
    cairo_line_to(cr, c7, c8);
    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);
  }

 
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20190501/f90f4aa1/attachment.html>


More information about the cairo mailing list