当我使用Pango将文本画到透明的Cairo记录表面上时,文本会严重褪色,特别是在半透明的反别名区域。例如,此图像具有某些文本的正确(顶部)和不正确(底部)表示。。(放大前一张图像的片段)。
我的问题,显然,是如何获得正确的颜色渲染时,使用一个透明的记录表面。至少,我认为这个问题与我不太了解泛凯罗管道有关。如果是在潘戈或开罗的窃听器,请告诉我。
用于生成上述图像的代码:
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
int main(int argc, char **argv)
{
cairo_surface_t *canvas = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
320, 60);
cairo_t *cctx = cairo_create(canvas);
cairo_surface_t *surf = cairo_recording_surface_create(
CAIRO_CONTENT_COLOR_ALPHA, NULL);
cairo_t *ctx = cairo_create(surf);
PangoFontDescription *fd = pango_font_description_new();
PangoLayout *layout = pango_cairo_create_layout(ctx);
PangoAttrList *attrs = pango_attr_list_new();
PangoAttribute *at;
cairo_set_source_rgba(cctx, 1, 1, 1, 1);
cairo_paint(cctx);
cairo_translate(cctx, 16, 8);
pango_font_description_set_family(fd, "DejaVu Sans Mono");
pango_layout_set_font_description(layout, fd);
pango_layout_set_text(layout, "Bless the Maker and His water", -1);
pango_layout_set_attributes(layout, attrs);
at = pango_attr_foreground_new(0, 0xffff, 0xffff);
pango_attr_list_change(attrs, at);
at = pango_attr_foreground_alpha_new(0xffff);
pango_attr_list_change(attrs, at);
cairo_save(ctx);
cairo_set_source_rgba(ctx, 1, 1, 1, 1);
cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
cairo_paint(ctx);
cairo_restore(ctx);
pango_cairo_update_layout(ctx, layout);
pango_cairo_show_layout(ctx, layout);
cairo_set_source_surface(cctx, surf, 0, 0);
cairo_paint(cctx);
cairo_save(ctx);
cairo_set_source_rgba(ctx, 0, 0, 0, 0);
cairo_set_operator(ctx, CAIRO_OPERATOR_SOURCE);
cairo_paint(ctx);
cairo_restore(ctx);
pango_cairo_update_layout(ctx, layout);
pango_cairo_show_layout(ctx, layout);
cairo_translate(cctx, 0, 24);
cairo_set_source_surface(cctx, surf, 0, 0);
cairo_paint(cctx);
cairo_surface_write_to_png(canvas, "test.png");
g_object_unref(layout);
pango_attr_list_unref(attrs);
pango_font_description_free(fd);
cairo_destroy(ctx);
cairo_destroy(cctx);
cairo_surface_destroy(surf);
cairo_surface_destroy(canvas);
return 0;
}更新:--这似乎是由视频管道中的某个东西引起的,而且可能是一个bug。我可以用AMD显卡在两个盒子上复制这个,但不能用nVidia卡在一个盒子上。我把这个留着以防有人知道有什么办法。
发布于 2018-03-17 10:07:50
首先:示例代码在这里不会重现问题。在结果图像中没有“彩色条纹”。
如果允许我猜的话: Cairo为您假定了RGB的亚像素顺序。根据cairo_subpixel_order_t的文档,这只用于CAIRO_ANTIALIAS_SUBPIXEL。因此,我建议你:
cairo_font_options_t *opts = cairo_get_font_options(some_cairo_context);
cairo_font_options_set_antialias(opts, CAIRO_ANTIALIAS_GRAY); // or _NONE
cairo_set_font_options(some_cairo_context, opts);
cairo_font_options_destroy(opts);发布于 2018-03-22 13:19:59
事实证明@uli-schlachter几乎是正确的:您必须在Pango布局对象上设置反别名选项:
PangoContext *pctx = pango_layout_get_context(layout);
cairo_font_options_t *opts = cairo_font_options_create();
cairo_font_options_set_antialias(opts, CAIRO_ANTIALIAS_GRAY);
pango_cairo_context_set_font_options(pctx, opts);
cairo_font_options_destroy(opts);https://stackoverflow.com/questions/49319212
复制相似问题