Translate/rotate 1 single pango cairo layout

Hi,

I have got a GtkDrawingArea, I am drawing with cairo and pango.

  GtkSettings *settings;

  PangoLayout *layout;
  PangoFontDescription *desc;

  PangoRectangle ink_rect, logical_rect;

  gchar *font_name;
  gchar *note_str;

  const gchar* note_strv[] = {
    "𝅝",
    "𝅗𝅥",
    "𝅘𝅥",
    "𝅘𝅥𝅮",
    "𝅘𝅥𝅯",
    NULL,
  };

  const gdouble font_size = 12.5;
  const gdouble key_font_size = 12.5;
  const gdouble x0 = 0.0;
  const gdouble y0 = 0.0;

  settings = gtk_settings_get_default();

  font_name = NULL;

  g_object_get(settings,
	       "gtk-font-name", &font_name,
               NULL);

  note_str = note_strv[2];

  if(note_str != NULL){
    layout = pango_cairo_create_layout(cr);
    pango_layout_set_text(layout,
                          note_str,
                          -1);
    desc = pango_font_description_from_string(font_name);
    pango_font_description_set_size(desc,
                                    key_font_size * PANGO_SCALE);
    pango_layout_set_font_description(layout,
                                      desc);
    pango_font_description_free(desc);

    pango_layout_get_extents(layout,
                             &ink_rect,
                             &logical_rect);

    cairo_move_to(cr,
                  x0,
                  y0);

    pango_cairo_show_layout(cr,
                            layout);

    g_object_unref(layout);
  }

If I call cairo_scale() then it scales additional layouts it shouldn’t. Howto prevent this?

I tried cairo_push_group() but this didn’t work as expected.

Just for reference my complete code:

https://git.savannah.nongnu.org/cgit/gsequencer.git/tree/ags/app/editor/ags_sheet_edit.c?h=4.4.x&id=bd0fb08a7ded5bad298d1915091652375b4cbd1d#n1406

best regards,
Joël

Just figured out the solution. So what was missing:

  • call cairo_translate() respecting string width and height
  • call cairo_rotate() with radians
  • call cairo_move_to() with additional quirks

cheers, Joël

There was a cairo example, drawing some rotated text, and saving to a PNG file. That was how I learned this stuff maybe 12 years ago. But I cant find it again. For scaling and rotating, it is necessary to first do a translation to the point that should stay unchanged, then rotate or scale, and then maybe translate back. Fine that you found the solution yourself.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.