0

我刚刚看到这篇文章说,亚像素定位是在 2019 年添加到 Pango 1.44 中的——尽管它并非没有问题。显然它默认是关闭的。两篇文章都说需要来自开罗的最新大师。也许它已经在此期间发布(?)。

其实有两个问题:

  • 我需要哪个版本的 Cairo(或 pycairo)来激活此功能?还是我(仍然)需要从(未发布的)大师构建开罗?
  • 如何使用 Cairo (pycairo) / pangocairo 激活亚像素定位?

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()
4

1 回答 1

1

我需要哪个版本的 Cairo(或 pycairo)来激活此功能?还是我(仍然)需要从(未发布的)大师构建开罗?

随机猜测:这与src/cairo-ft-font.cin cairo 有关,所以我快速查看了git log src/cairo-ft-font.c. 第三个条目听起来像您正在寻找的内容:

commit ea9329215d3431ded51a71b724baf0edc25ad633
Author: Matthias Clasen <mclasen@redhat.com>
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。

如何使用 Cairo (pycairo) / pangocairo 激活亚像素定位?

你没有。据我了解,它是自动激活的。那么问题是:我如何停用它?答案似乎也是“你没有”。

编辑:根据https://blogs.gnome.org/mclasen/2019/08/07/pango-1-44-wrap-up/,答案似乎(部分)在函数中pango_context_set_round_glyph_positions。这似乎处理了对 Pango 和 PangoCairo 部分的支持。

于 2020-05-07T13:45:01.597 回答