我刚刚遇到了 本文 说,2019年盘古1.44中加入了子像素定位--虽然不是没有问题。显然,它是 默认关闭. 两篇文章都说需要开罗的最新主。也许在此期间已经发布了(?)。
其实有两个问题。
pangocairo有多个Python绑定。我在Python 3中使用了以下方法。
import cairo
import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
from gi.repository import Pango, PangoCairo
我的文本渲染结果大致如下
_alignments = {
'tl': lambda width, height: (0.0, 0.0), # top left
'tc': lambda width, height: (-width / 2, 0.0), # top center
'tr': lambda width, height: (-width, 0.0), # top right
'cl': lambda width, height: (0.0, -height / 2), # center left
'cc': lambda width, height: (-width / 2, -height / 2), # center center
'cr': lambda width, height: (-width, -height / 2), # center right
'bl': lambda width, height: (0.0, -height), # bottom left
'bc': lambda width, height: (-width / 2, -height), # bottom center
'br': lambda width, height: (-width, -height), # bottom right
}
def make_font(family, size):
return Pango.font_description_from_string(f'{family:s} {size:.2f}')
font = make_font('Arial', 10.0)
x = 0.0
y = 0.0
angle = 0.0
font_color = (1.0, 0.0, 0.0, 0.5)
text = 'test'
_ctx.save() # _ctx is a cairo.Context object
layout = PangoCairo.create_layout(_ctx)
layout.set_font_description(font)
layout.set_markup(text, -1)
_ctx.set_source_rgba(*font_color)
_, text_extents = layout.get_pixel_extents()
text_width, text_height = text_extents.width, text_extents.height
_ctx.translate(x, y)
if angle != 0.0:
_ctx.rotate(angle)
_ctx.translate(*self._alignments[alignment](text_width, text_height))
_ctx.move_to(0.0, 0.0)
PangoCairo.show_layout(_ctx, layout)
_ctx.restore()
我需要哪个版本的Cairo(或者说pycairo)来激活这个功能?还是说我(仍然)需要从(未发布的)master版本中构建cairo?
随机猜测:这与 src/cairo-ft-font.c
的输出,所以我快速地看了一下。git log src/cairo-ft-font.c
. 第三个入口听起来像是你要找的东西,那么,第一个包含这个提交的版本是什么?
commit ea9329215d3431ded51a71b724baf0edc25ad633
Author: Matthias Clasen <[email protected]>
Date: Sat Jul 28 12:25:47 2018 +0000
image compositor: Support subpixel positioning
Support subpixel positioning with a 4x4 subpixel grid.
When compositing glyphs in the image compositor,
we store the subpixel phases in the high bits of the
glyph index. The _cairo_scaled_glyph_index() macro
has been updated to discard these bits. By storing
the phases in the glyph index, the glyph cache just
keeps working. When loading a glyph, the Freetype
font backend shifts the outline according to the
phases.
那么,包含这个提交的第一个版本是什么?
$ git describe --contains ea9329215d3431ded51a71b724baf0edc25ad633
fatal: cannot describe 'ea9329215d3431ded51a71b724baf0edc25ad633'
嗯,好像没有在任何一个版本中,所以你需要master。
如何用开罗(pycairo)pangocairo激活子像素定位?
你不需要。据我了解,它是自动激活的。那么问题是:我怎么停用它?而答案似乎也是 "你不需要"。
编辑:根据 https:/blogs.gnome.orgmclasen20190807pango-1-44-总结。,答案似乎是(部分)在函数 pango_context_set_round_glyph_positions
. 这似乎处理了Pango和PangoCairo部分的支持。