[cairo] Cairo transforms

Wouter Klouwen cairo at dublet.org
Mon Feb 23 11:57:27 PST 2015


Hello,

For my application I'm using Cairo & Pango to do some font rendering and 
have stumbled into a bit of a problem.

The text I'm rendering goes into a surface and that surface data is 
loaded into a texture, which is displayed via OpenGL. But I need to 
rotate the text around a pivot point. I want to do this in Cairo so that 
the texture can remain pixel aligned and the font re-rendered with the 
appropriate rotation.

To start off with, I measure the texture with Pango's layout and 
transform the rectangle with their matrix to get the correct bounding 
box for the text:

         int             textureWidth, textureHeight;

        // set up font layout, etc..
         pango_layout_set_text(layout, testText.c_str(), testText.size());
         pango_layout_context_changed(layout);
        // Measure
         pango_layout_get_size(layout, &textureWidth, &textureHeight);

         textureWidth = FROM_PANGO_SCALE(textureWidth);
         textureHeight = FROM_PANGO_SCALE(textureHeight);

         PangoMatrix matrix = PANGO_MATRIX_INIT;
         pango_matrix_translate(&matrix, -textureWidth * 0.5, 
-textureHeight * 0.5);
         pango_matrix_rotate(&matrix, double(rotation) * (180.0 / M_PI));
         pango_matrix_translate(&matrix, textureWidth * 0.5, 
textureHeight * 0.5);

         PangoRectangle rect = { 0, 0, textureWidth, textureHeight };
         pango_matrix_transform_pixel_rectangle(&matrix, &rect);

Then I create the surface of the dimensions calculated, do the same 
transform with Cairo and draw the text as a path:

            cairo_surface_t *surface = 
cairo_image_surface_create_for_data(...);

             cairo_t *renderContext = cairo_create(surface);
             cairo_translate(renderContext, -textureWidth * 0.5, 
-textureHeight * 0.5);
             cairo_rotate(renderContext, rotation);
             cairo_translate(renderContext, textureWidth * 0.5, 
textureHeight * 0.5);


             // Render text
             cairo_new_path(renderContext);
             cairo_move_to(renderContext, 0, 0);

            // Set some render options..

             pango_cairo_update_layout(renderContext, layout);

             pango_cairo_layout_path(renderContext, layout);
             cairo_fill(renderContext);

No matter what, I can't seem to get the last cairo_translate to do 
anything, it always keeps rotating the text around 0, 0. I've tried 
creating some cairo_matrix_ts and multiplying those, then applying that 
matrix to the cairo_t. I've even tried calculating the matrix with a 
third party library just to ensure it was correct and setting it with that.

Can someone please enlighten me as to what I'm doing wrong?

Thanks,
      W


More information about the cairo mailing list