GTK4 snaphot.append_layout seems to ignore pango_context.set_matrix

Hello! While exploring the slick new GTK4 I tried to rotate some text using two approaches.

First configuring a transformation matrix for the pango layout’s context. This shows the text unchanged seemingly ignoring the matrix.
Second adding transformation as additional snapshot element. This rotates the text as expected.

I plan to have maybe ten to hundred snapshot elements laying out mathtext. So my questions would be: What did I do wrong? Is one of the approaches more efficient? Should I use cairo drawing? (From following some of the developers’ posts I get the feeling Cairo shall be replaced in the long run.)

#! /usr/bin/env python3

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk, Gdk, Pango, Graphene

class Custom(Gtk.DrawingArea):
    def __init__(self):
        Gtk.DrawingArea.__init__(self)
    def do_snapshot(self, snapshot):
        color = Gdk.RGBA()
        color.parse('black')
        matrix = Pango.Matrix()
        matrix.xx= 0; matrix.xy= 1; matrix.x0=0
        matrix.yx=-1; matrix.yy= 0; matrix.y0=0
        context = self.create_pango_context()
        context.set_matrix(matrix)
        layout = Pango.Layout.new(context)
        layout.set_text('foobar', -1)
        offset = Graphene.Point.alloc()
        offset.init(100,100)
        snapshot.translate(offset)
        snapshot.append_layout(layout, color)
    def do_snapshot(self, snapshot):
        text = self.create_pango_layout('foobar')
        color = Gdk.RGBA()
        color.parse('black')
        offset = Graphene.Point.alloc()
        offset.init(100,100)
        snapshot.translate(offset)
        snapshot.rotate(90)
        snapshot.append_layout(text, color)

def activate(app):
    wdg = Custom()
    win = Gtk.ApplicationWindow.new(app)
    win.set_title('Window')
    win.set_default_size(200, 200)
    win.set_child(wdg)
    win.show()

def main():
    app = Gtk.Application.new(None, 0)
    app.connect('activate', activate)
    return app.run()

if __name__ == '__main__':
    try: main()
    except KeyboardInterrupt:
        print()

Not sure why the pango matrix would be ignored - the code looks like it should work

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