TCAX 字幕特效制作工具官方论坛 | ASS | TCAS | Python | Aegisub | Lua

 找回密码
 新人加入
查看: 2634|回复: 1
打印 上一主题 下一主题

Using PangoCairo with PyGObject API [复制链接]

Rank: 4

跳转到指定楼层
楼主
发表于 2022-3-26 03:00:33 |只看该作者 |倒序浏览
本帖最后由 Seekladoom 于 2022-3-26 03:02 编辑

Source:
https://stackoverflow.com/questi ... -with-pygobject-api

I am porting a Python2 script that uses Pango for drawing text to a Cairo surface. It works fine using the old PyGtk API with the pangocairo package. My system (Debian Jesse) doesn't have Python3 packages for PyGtk and instead uses the newer Gtk+ libraries with the PyGObject API.

I want to create a pangocairo.CairoContext object but it seems to be missing in the new API. The PangoCairo package has a create_context() function but it generates a PangoContext object that doesn't have the methods I need.

So far I have this:
  1. import cairo
  2. from gi.repository import Pango
  3. from gi.repository import PangoCairo

  4. surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
  5. ctx = cairo.Context(surf)
  6. pctx = PangoCairo.create_context(ctx) # Creates a PangoContext
  7. pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL) # This fails
复制代码


The old Python2 code that works:
  1. import cairo
  2. import pango
  3. import pangocairo

  4. surf = cairo.ImageSurface(cairo.FORMAT_ARGB32, 8, 8)
  5. ctx = cairo.Context(surf)
  6. pctx = pangocairo.CairoContext(ctx)
  7. pctx.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
复制代码

Does anyone have a solution for this? Is there any good documentation on how PangoCairo should be used with the new API?

Rank: 4

沙发
发表于 2022-3-26 03:02:47 |只看该作者
It looks like the library has been rearranged a bit. The Pango context (now Pango.Context) is retrieved from the Pango.Layout object now. Here is a working solution:
  1. import cairo
  2. from gi.repository import Pango
  3. from gi.repository import PangoCairo

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

  8. fo = cairo.FontOptions()
  9. fo.set_antialias(cairo.ANTIALIAS_SUBPIXEL)
  10. PangoCairo.context_set_font_options(pctx, fo)
复制代码
您需要登录后才可以回帖 登录 | 新人加入

GitHub|TCAX 主页

GMT+8, 2024-11-1 07:56

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部
RealH