Using PangoCairo with the PyGObject API

I am porting a Python2 script that uses Pango to draw text on the surface of Cairo. It works fine using the old PyGtk API with the package pangocairo. My system (Debian Jesse) does not have Python3 packages for PyGtk and instead uses the new Gtk + libraries with the PyGObject API.

I want to create an object pangocairo.CairoContext, but it seems to be missing from the new API. A package pangocairohas a function create_context(), but it generates an object PangoContextthat does not have the methods that I need.

So far I have this:

import cairo
from gi.repository import Pango
from gi.repository import PangoCairo

surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
ctx = cairo.Context(surf)
pctx = PangoCairo.create_context(ctx) # Creates a PangoContext
pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) # This fails

Old Python2 code that works:

import cairo
import pango
import pangocairo

surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
ctx = cairo.Context(surf)
pctx = pangocairo.CairoContext(ctx)
pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)

Does anyone have a solution for this? Is there any good documentation on how pangocairoto use it with the new API?

+4
1

, . Pango ( Pango.Context) Pango.Layout. :

import cairo
from gi.repository import Pango
from gi.repository import PangoCairo

surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
ctx = cairo.Context(surf)
layout = PangoCairo.create_layout(ctx)
pctx = layout.get_context()

fo = cairo.FontOptions()
fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
PangoCairo.context_set_font_options(pctx, fo)
+4

Source: https://habr.com/ru/post/1658549/


All Articles