[cairo] Text stuffing with Cairo/pycairo?

Warren Vick wvick at europa.uk.com
Tue Jun 25 06:09:42 UTC 2019


I’ve made some good progress working with pyCairo after porting Eric’s C code below. I am, however, having a problem with selecting fonts…

I’m using pyCairo on Windows. No matter what font I select using select_font_face on a context, I seem to get Arial output in my PDF. Does pyCairo pick-up fonts installed on Windows or do the TTF files need to be copied somewhere specific? Or, have I misunderstood what is meant by the face name? e.g. “Noto Sans Condensed”.

Regards,
Warren

From: cecashon at aol.com <cecashon at aol.com>
Sent: 01 May 2019 18:41
To: Warren Vick <wvick at europa.uk.com>; cairo at cairographics.org
Subject: Re: [cairo] Text stuffing with Cairo/pycairo?


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/20190625/6c4c9d4d/attachment.html>


More information about the cairo mailing list