如何使用pycairo创建字母间距属性?

时间:2019-04-05 10:22:33

标签: python pygobject pango

我正在使用Pango + Cairo(通过GObject)使用python3.7渲染文本,并希望通过创建属性并将该属性附加到我的pango布局中来设置字母间距

在pango的gnome文档中,我可以看到应该有一个名为pango_attr_letter_spacing_new的函数(自v1.6起)。但是,如果我运行Pango.attr_letter_spacing_new,则会收到错误消息:

AttributeError: 'gi.repository.Pango' object has no attribute 'attr_letter_spacing_new'

这感觉有点奇怪,因为我可以使用pango_attr_type_get_name,该版本仅在v1.22以后才可用。

我可以通过在<span letter_spacing="1234">中使用标记来解决问题,但我宁愿不走这条路。

最小的“工作”示例

# pip install pycairo==1.18.0 pygobject==3.32.0

import cairo
import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango, PangoCairo

width, height = 328, 48

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = cairo.Context(surface)
layout = PangoCairo.create_layout(context)

font_desc = Pango.font_description_from_string('Sans, 40px')
layout.set_font_description(font_desc)

# What I can do
layout.set_markup(f'<span letter_spacing="{1024 * 10}">Hello World</span>')

# What I would like to do
if False:
    letter_spacing_attr = Pango.attr_letter_spacing_new(1024 * 10)

    attr_list = Pango.AttrList()
    attr_list.insert(letter_spacing_attr)
    layout.set_attributes(attr_list)

    layout.set_text('Hello World')

PangoCairo.show_layout(context, layout)

with open('help-me.png', 'wb') as image_file:
    surface.write_to_png(image_file)

手动创建LetterSpacing属性

我已经能够找到枚举值Pango.AttrType.LETTER_SPACING,这使我可以执行以下操作:

c = Pango.AttrClass()
c.type = Pango.AttrType.LETTER_SPACING
a = Pango.Attribute()
a.init(c)

但是,我一直无法找到一种方法来设置它的值,这让我觉得这是错误的处理方法:|

将其插入Pango.AttrList中,给出错误(并不奇怪),并且下次我对Pango进行操作时使python进程出现段错误:

** (process:17183): WARNING **: 12:00:56.985: (gi/pygi-struct-marshal.c:287):pygi_arg_struct_from_py_marshal: runtime check failed: (g_type_is_a (g_type, G_TYPE_VARIANT) || !is_pointer || transfer == GI_TRANSFER_NOTHING)

其他线索

..可悲的是无处可去:(

  • pygtk listing a function pango.AttrLetterSpacing
    • Pango.AttrLetterSpacing => 'gi.repository.Pango' object has no attribute 'AttrLetterSpacing'
    • Pango.Attrbute.LetterSpacing => type object 'Attribute' has no attribute 'LetterSpacing'
  • 有关Vala的pango封装的文档(似乎也使用GObject),也显示了attr_letter_spacing_new function-并没有多大帮助,但建议该功能应该可以通过GObject使用,尽管我没有尝试过。

1 个答案:

答案 0 :(得分:1)

看起来这个问题已经解决了,使用 pycairo 1.20.0 和 PyGObject 3.40.1:

import cairo
import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango, PangoCairo

width, height = 328, 48

surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
context = cairo.Context(surface)
layout = PangoCairo.create_layout(context)

font_desc = Pango.font_description_from_string('Sans, 40px')
layout.set_font_description(font_desc)

letter_spacing_attr = Pango.attr_letter_spacing_new(1024 * 10)

attr_list = Pango.AttrList()
attr_list.insert(letter_spacing_attr)
layout.set_attributes(attr_list)

layout.set_text('Hello World')

PangoCairo.show_layout(context, layout)

with open('help-me.png', 'wb') as image_file:
    surface.write_to_png(image_file)