SixLabors / ImageSharp.Drawing

:pen: Extensions to ImageSharp containing a cross-platform 2D polygon manipulation API and drawing operations.
https://sixlabors.com/products/imagesharp-drawing/
Other
285 stars 38 forks source link

Fonts still antialias when Antialias is set to false #203

Closed parzivail closed 2 years ago

parzivail commented 2 years ago

Prerequisites

Description

Fonts still render with a very slight amount of antialiasing even when Antialias in GraphicsOptions is set to false.

Entirely possible I'm going about this the wrong way, but figured I'd sanity check.

Steps to Reproduce

  1. Obtain any TTF font. Ones tested to produce this example: BBSesque (pixel-sized TTF outlines), IBM Plex Sans Text (typical TTF)
  2. Run the following code to produce an image (saving omitted for brevity)
  3. Resulting image contains antialiased text, despite Antialias in GraphicsOptions being false.
private static Image<Rgb24> GenerateImage()
{
    var image = new Image<Rgb24>(400, 75);

    using var fs = ResourceHelper.GetEmbeddedResource("BBSesque.ttf");
    var collection = new FontCollection();
    var family = collection.Add(fs);

    var font36 = family.CreateFont(36);

    var textOpt = new TextOptions(font36)
    {
        Dpi = 96,
        Origin = new PointF(0, 0)
    };
    var graphicsOpt = new DrawingOptions()
    {
        GraphicsOptions = new GraphicsOptions
        {
            Antialias = false
        }
    };

    var blackBrush = new SolidBrush(Color.Black);
    var emptyPen = new Pen(Color.Transparent, 1);

    image.Mutate(g =>
    {
        g.Clear(Color.White);
        g.DrawText(graphicsOpt, textOpt, "Hello, World!", blackBrush, emptyPen);
    });

    return image;
}

System Configuration

JimBobSquarePants commented 2 years ago

Thanks for this @parzivail. I've migrated the issue to the correct repository.

Just to confirm the version numbers.

@tocsoft I have a hazy memory of you saying something about this before, something related to an IsDirty check. Am I right?

parzivail commented 2 years ago

Did some more testing and found that the transparent Pen emptyPen actually draws black in this instance, using the default alpha composition and color blending options. Not sure if it's related.

Edit: Seems like it might be. Using the smallest pen width I could without raising an exception (0.001) yielded different artifacts, and bumping the pen width to 0.01 almost eliminates the issue on smaller text. I made a 5:1 diagram hopefully illustrating what I've found so far. Image size is 2300x1151.

5:1 Table showing pen width vs pen color

JimBobSquarePants commented 2 years ago

Not related. You've chosen a pixel format,Rgb24, with no alpha component.

parzivail commented 2 years ago

Good point, oversight on my part. Shouldn't change the table, however, if you then interpret it as a Black pen and a Red one. Looks like the text might look as expected without the pen in that case.

tocsoft commented 2 years ago

@tocsoft I have a hazy memory of you saying something about this before, something related to an IsDirty check. Am I right?

@JimBobSquarePants you remembered correctly, to be honest I had forgot I'd seen the bug. #204 should fix it.

parzivail commented 2 years ago

Not related. You've chosen a pixel format,Rgb24, with no alpha component.

Back at my desk -- this actually ended up solving my issue. Being able to stroke the text with a Transparent pen was the answer for me, apologies for the confusion!

JimBobSquarePants commented 2 years ago

1.0.0-beta14.4 contains a proper fix for the antialiasing issue.