Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text has green outline when saved to PNG file #355

Open
ghost opened this issue Jan 15, 2024 · 6 comments
Open

Text has green outline when saved to PNG file #355

ghost opened this issue Jan 15, 2024 · 6 comments

Comments

@ghost
Copy link

ghost commented Jan 15, 2024

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
Copy link
Collaborator

stuaxo commented Jan 15, 2024

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.

@ghost
Copy link
Author

ghost commented Jan 16, 2024

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
Copy link
Collaborator

stuaxo commented Jan 16, 2024

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
Copy link

psychon commented Jan 16, 2024

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
Copy link
Collaborator

stuaxo commented Jan 16, 2024

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
Copy link

psychon commented Jan 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants