SkiaSharp
SkiaSharp copied to clipboard
[QUESTION] How to enable Subpixel rendering on Windows?
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:
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?
ClearType is only possible on surfaces that don't use premultiplied alpha and I guess here an alpha channel is used.
Related question from Rust: https://github.com/rust-skia/rust-skia/issues/331 They seem to have got this working.
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);
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 .
This is the SkiaSharp Basic Desktop sample.
I just changed a few lines
I tried the same on the UWP sample:
Bigger:
UWP Files: UWPSkiaTest.zip
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.
@mattleibow
@danipen I answered in #2308