首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用Pango和(py)Cairo激活亚像素定位?

如何用Pango和(py)Cairo激活亚像素定位?
EN

Stack Overflow用户
提问于 2020-05-07 09:08:25
回答 1查看 330关注 0票数 0

我刚看到这篇文章说,亚像素定位是在2019年添加到Pango 1.44中的--不过也有一些问题。显然是默认关闭。这两篇文章都说需要来自开罗的最新大师。也许在此期间它已经被释放了。

实际上有两个问题:

  • 我需要哪个版本/版本的Cairo (或者pycairo )来激活这个特性?还是我(仍然)需要从(未发布的)主版构建cairo?
  • 如何激活Cairo (pycairo) / pangocairo?的亚像素定位

有多个Python绑定到pangocairo。我在Python 3中使用了以下方法:

代码语言:javascript
复制
import cairo

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

我的文本呈现如下:

代码语言:javascript
复制
_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()
EN

回答 1

Stack Overflow用户

发布于 2020-05-07 13:45:01

我需要哪个版本/版本的Cairo (或者pycairo )来激活这个特性?还是我(仍然)需要从(未发布的)主版构建cairo?

随机猜测:这与开罗的src/cairo-ft-font.c有关,因此我快速查看了git log src/cairo-ft-font.c的输出。第三个条目听起来像您要寻找的内容:

代码语言:javascript
复制
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.

那么,包含此提交的第一个版本是什么?

代码语言:javascript
复制
$ git describe --contains ea9329215d3431ded51a71b724baf0edc25ad633
fatal: cannot describe 'ea9329215d3431ded51a71b724baf0edc25ad633'

嗯,似乎没有发布,所以你需要主人。

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

你不知道,据我所知,它是自动激活的。那么问题是:我如何使它停用呢?答案似乎也是“你不知道”。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61653970

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档