pygobject / pycairo

Python bindings for cairo
https://pycairo.readthedocs.io
Other
611 stars 83 forks source link

Text has green outline when saved to PNG file #355

Open randomcoder67 opened 5 months ago

randomcoder67 commented 5 months ago

Version: python-cairo 1.25.1-1

When drawing text, if the text is white there will be a green/yellow/blue outline when saving to png. Only occurs with text, normal drawing is unnaffected. Some examples below:

example

2

It doesn't affect the weather symbol thing as that's just normal drawing (arcs and lines)

The code used:

#!/usr/bin/env python3

import math
import cairo

# Initialise cairo context
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, 256, 256)
ctx = cairo.Context(surface)

ctx.move_to(2, 210)
ctx.set_source_rgb(1, 1, 1)
ctx.set_font_size(20)
ctx.show_text("12")

ctx.move_to(100, 210)
ctx.set_font_size(100)
ctx.show_text("12")

surface.write_to_png("2.png")  # Output to PNG
stuaxo commented 5 months ago

This looks like it is very probably an upstream cairo issue.

If you can port the program to C and cairo, you can check this - this sort of porting is one thing that LLMs can be quite good at.

randomcoder67 commented 5 months ago

This looks like it is very probably an upstream cairo issue.

If you can port the program to C and cairo, you can check this - this sort of porting is one thing that LLMs can be quite good at.

You're right, exact same issue occurs with equivalent c code:

#include <cairo.h>

int main (int argc, char *argv[]) {
    cairo_surface_t *surface;
    cairo_t *cr;

    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 256, 256);
    cr = cairo_create (surface);

    cairo_move_to(cr, 2, 210);
    cairo_set_source_rgb (cr, 1, 1, 1);
    cairo_set_font_size(cr, 20);
    cairo_show_text(cr, "12");

    cairo_move_to(cr, 100, 210);
    cairo_set_font_size(cr, 100);
    cairo_show_text(cr, "12");

    cairo_set_line_width (cr, 0.1);
    cairo_rectangle (cr, 0.25, 0.25, 0.5, 0.5);
    cairo_stroke (cr);

    /* Write output and clean up */
    cairo_surface_write_to_png (surface, "new.png");
    cairo_destroy (cr);
    cairo_surface_destroy (surface);

    return 0;
}

new

I'll make a bug report here: https://gitlab.freedesktop.org/cairo/cairo

stuaxo commented 5 months ago

Nice, post a link to it here when you do to help anyone coming across this in future.

It being a font rendering thing the trail may go beyond Cairo and into something like Pango.

psychon commented 5 months ago

Heh, it would be great to also provide the cross-linking on upstream bug reports. Anyway, Google brought me here and I just added a link to the this ticket to my cairo bug report.

It being a font rendering thing the trail may go beyond Cairo and into something like Pango.

Actually, my guess with these kinds of stuff is always fontconfig or freetype. Pango is not even used here. ;-)

Anyway, to quote myself on the upstream issue:

Looking at your image in Gimp, the large 1 has #efff00 on its left side and #03ffdc on its right side. This matches sub-pixel hinting. I guess you configured Fontconfig to force sub-pixel rendering. Google found the arch wiki which says on https://wiki.archlinux.org/title/font_configuration#Subpixel_rendering

Starting from FreeType 2.10.3, Arch Linux enables ClearType subpixel rendering by default

with a link going to https://gitlab.archlinux.org/archlinux/packaging/packages/freetype2/-/commit/35e925e946da41b7a2d7c297ee43c4cab386048c

So...

  • This is not cairo's doing
  • Freetype / Fontconfig is doing exactly what it is told / configured to do.

Sorry.

stuaxo commented 5 months ago

Actually, my guess with these kinds of stuff is always fontconfig or freetype. Pango is not even used here. ;-)

Ah, I always get bits of the font rendering stack mixed up.

psychon commented 5 months ago

Heh, same happens to me. I know at least that Pango and Harfbuzz exist to support complicated scripts (e.g. arabic). They select glyphs based on the input text. Fontconfig and freetype are about loading and rendering fonts. But I am certainly no font expert.