[cairo] Text stuffing with Cairo/pycairo?

Warren Vick wvick at europa.uk.com
Tue Apr 30 20:34:11 UTC 2019


Hello Eric,

I was about to get started so your reply is most timely and appreciated.

I’m competent in C and can see what you’re doing here. I should be able to port to Python easily.

Thanks again.

/W

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


Hi Warren,

Have you made some progress with this?

Well, you might need some font metrics and get the transforms in order to be able to do this. The following is in C but it might be helpful. Probably need to change a few things around but it is a start.

Eric


//gcc -Wall cairo_text_size.c -o cairo_text_size `pkg-config --cflags --libs cairo` -lm

#include<cairo.h>
#include<cairo-pdf.h>
#include<stdio.h>
#include<math.h>

static void text_in_box(cairo_t *cr, double center_box_x, double center_box_y, double box_width, double box_height, double rotate, char *string);

int main()
  {
    double width=600.0;
    double height=600.0;
    char *string1="Center String";
    char *string2="Translate Stringssssss!";
    char *string3="Rotate String";
    char *string4="Joy!!!";

    cairo_surface_t *surface=cairo_pdf_surface_create("cairo_test1.pdf", width, height);
    cairo_t *cr=cairo_create(surface);

    //Paint background.
    cairo_set_source_rgba(cr, 1.0, 1.0, 1.0, 1.0);
    cairo_paint(cr);

    //Draw from the center of surface. Good for testing text and box alignment.
    cairo_translate(cr, width/2.0, height/2.0);
    //Scale the drawing if needed.
    cairo_scale(cr, 1.0, 1.0);

    //Draw cartesian coordinates.
    cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
    cairo_move_to(cr, -width/2.0, 0.0);
    cairo_line_to(cr, width/2.0, 0.0);
    cairo_stroke(cr);
    cairo_move_to(cr, 0.0, -height/2.0);
    cairo_line_to(cr, 0.0, height/2.0);
    cairo_stroke(cr);

    cairo_select_font_face(cr, "courier", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
    //Since the font gets scaled to the box the default font size should be fine.
    //cairo_set_font_size(cr, 10.0);

    //Draw text at center.
    text_in_box(cr, 0.0, 0.0, 350.0, 100.0, 0.0, string1);

    //Translate text.
    text_in_box(cr, -120.0, -150.0, 300.0, 30.0, 0.0, string2);

    //Rotate text.
    text_in_box(cr, 150.0, 150.0, 121.0, 52.0, M_PI/4.0, string3);

    text_in_box(cr, -150.0, 185.0, 121.0, 52.0, -M_PI/4.0, string4);

    text_in_box(cr, 150.0, -190.0, 225.0, 52.0, -M_PI/4.0, string4);

    printf("Output to cairo_test1.pdf\n");

    cairo_destroy(cr);
    cairo_surface_destroy(surface);

    return 0;
  }
static void text_in_box(cairo_t *cr, double center_box_x, double center_box_y, double box_width, double box_height, double rotate, char *string)
  {
    double move_x=0.0;
    double move_y=0.0;
    double scale_x=0.0;
    double scale_y=0.0;
    cairo_text_extents_t te;
    cairo_font_extents_t fe;

    //Get some font metrics.
    cairo_text_extents(cr, string, &te);
    cairo_font_extents(cr, &fe);
    move_x=-te.width/2.0-te.x_bearing;
    move_y=-te.height/2.0-te.y_bearing;
    //Pad the rectangle a little.
    scale_x=-te.width/2.0-te.x_bearing-4.0;
    scale_y=-te.height/2.0-te.y_bearing+fe.descent;

    //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);
    cairo_rectangle(cr, -fabs(scale_x), -fabs(scale_y), fabs(2.0*scale_x), fabs(2.0*scale_y));
    cairo_fill(cr);
    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);
  }


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cairographics.org/archives/cairo/attachments/20190430/0eeb6ff5/attachment-0001.html>


More information about the cairo mailing list