SkiaSharp icon indicating copy to clipboard operation
SkiaSharp copied to clipboard

[QUESTION] How to enable Subpixel rendering on Windows?

Open dontpanic92 opened this issue 5 years ago • 7 comments

Hi there,

I'm currently trying to use SkiaSharp for rendering fonts, however I cannot manage to make the text rendered with the subpixel antialias technique. Here is my sample code (using the winform control in SkiaSharp.View):

        public Form1()
        {
            InitializeComponent();
            this.skControl1.PaintSurface += SkControl1_PaintSurface;
        }

        private void SkControl1_PaintSurface(object sender, SkiaSharp.Views.Desktop.SKPaintSurfaceEventArgs e)
        {
            var canvas = e.Surface.Canvas;
            var typeface = SKTypeface.FromFamilyName("Fira Code", SKFontStyleWeight.Normal, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright);

            var paint = new SKPaint()
            {
                Color = new SKColor(0, 0, 0),
                Typeface = typeface,
                TextSize = 15,
                IsAntialias = true,
                SubpixelText = true,
            };
            var shaper = new SKShaper(typeface);

            canvas.DrawShapedText(shaper, "test -> figgabcd==abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 40, 40, paint);
            canvas.DrawText("test -> figgabcd==abcdefgabcdefgabcdefgabcdefgabcdefgabcdefg", 40, 60, paint);
        }

The rendering result:

image

Nether DrawText nor DrawShapedText from SkiaSharp.Harfbuzz gave us the subpixel rendering (Harfbuzz gave us the correct ligatures which was expected). Is there any other settings I need to set?

dontpanic92 avatar Apr 22 '19 05:04 dontpanic92

ClearType is only possible on surfaces that don't use premultiplied alpha and I guess here an alpha channel is used.

Gillibald avatar Sep 21 '19 21:09 Gillibald

Related question from Rust: https://github.com/rust-skia/rust-skia/issues/331 They seem to have got this working.

charlesroddie avatar Dec 24 '20 18:12 charlesroddie

In order to enable subpixel antialiasing for text, one needs to enable subpixel antialiased edging:

var paint = new SKPaint
{
	Color = SKColors.Black,
	IsAntialias = true,
	Style = SKPaintStyle.Fill,
	TextAlign = SKTextAlign.Center,
	TextSize = 24
};
var font = new SKFont
{
	Edging = SKFontEdging.SubpixelAntialias,
	Size = 24
};
canvas.DrawText(
	"SkiaSharp",
	scaledSize.Width / 2,
	(scaledSize.Height + paint.TextSize) / 2,
	font,
	paint);

image

cilliemalan avatar May 14 '21 12:05 cilliemalan

Good find @cilliemalan . The skia api is terrible here with fonts inside typefaces and typefaces inside fonts and properties duplicated among fonts and paints.

What Skia View are you using? When we try this on a Forms SKCanvasView on UWP we get the same bug in rendering as https://github.com/rust-skia/rust-skia/issues/331#issuecomment-623083102 .

charlesroddie avatar May 14 '21 15:05 charlesroddie

This is the SkiaSharp Basic Desktop sample.

I just changed a few lines

I tried the same on the UWP sample: image

Bigger: image

UWP Files: UWPSkiaTest.zip

cilliemalan avatar May 14 '21 20:05 cilliemalan

I played around with the sample application provided by @cilliemalan, and I noticed that subpixel rendering is not performed well for some SkiaSharp versions.

The subpixel rendering works fine using 2.80.2. I additionally tested 2.80.3 and 2.80.4-preview.x, and those versions also worked fine for me.

However, I noticed that using the last 2.88.0-preview.x versions, the sample app doesn't display subpixel rendering.

So, sumarizing:

  • 2.80.2: Subpixel displayed ok.
  • 2.80.3: Subpixel displayed ok.
  • 2.80.4-preview.x: Subpixel displayed ok.
  • 2.88.0-preview.x: Subpixel is NOT displayed ok.

danipen avatar Jan 10 '22 07:01 danipen

@mattleibow

danipen avatar Aug 22 '22 21:08 danipen

@danipen I answered in #2308

themcoo avatar Nov 28 '22 01:11 themcoo