[cairo] Text stuffing with Cairo/pycairo?

Warren Vick wvick at europa.uk.com
Wed Jun 26 09:25:56 UTC 2019


I’ve managed to answer my own question so here it is, just in case it’s useful for anyone else in the future.

Cairo’s built-in font support is quite basic, which is the reason why they’re referred to as “toy fonts”. The documentation concedes that Cairo alone will probably not be sufficient for professional standard work and recommends a pairing with the Pango project.

For my immediate project, I’m going to have to stick to Arial now, but that’s not the end of the world since it’s not a bad font for my intended purpose. While the docs say any CSS2 style face name will work (e.g. “serif, sans-serif”), in Windows, the only font faces that seem to work for me are “Arial” and “Times”. “Courier” goes very wrong!

Having worked with it for a few days now, I quite like Cairo, but feel it’s a shame the text support is not as strong as I’d hoped for.

Regards,
Warren

From: cairo <cairo-bounces at cairographics.org> On Behalf Of Warren Vick
Sent: 25 June 2019 07:24
To: cairo at cairographics.org
Subject: Re: [cairo] Text stuffing with Cairo/pycairo?

Curiously, when I just select the font face as simply “Times”, the output is different (serif) and Acrobat reports the embedded font as “TimesNewRomanPSMT”. I don’t have a font of that name installed, as far as I’m aware!

Regards,
Warren

From: cairo <cairo-bounces at cairographics.org<mailto:cairo-bounces at cairographics.org>> On Behalf Of Warren Vick
Sent: 25 June 2019 07:10
To: cairo at cairographics.org<mailto:cairo at cairographics.org>
Subject: Re: [cairo] Text stuffing with Cairo/pycairo?

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<mailto:cecashon at aol.com> <cecashon at aol.com<mailto:cecashon at aol.com>>
Sent: 01 May 2019 18:41
To: Warren Vick <wvick at europa.uk.com<mailto:wvick at europa.uk.com>>; cairo at cairographics.org<mailto: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/20190626/9c5427a2/attachment.html>


More information about the cairo mailing list