mono / SkiaSharp

SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server and desktop models to render images.
MIT License
4.48k stars 538 forks source link

[BUG] Width issue for Narrow Non breaking space in FrutigerNext LT LightCn font #2021

Open MohanaselvamJ opened 2 years ago

MohanaselvamJ commented 2 years ago

Description

Measurement difference found while comparing SkiaSharp library with System.Drawing

Text: Narrow Non Breaking Space (Unicode U+202F NARROW NO-BREAK SPACE) character Font: “FrutigerNext LT LightCn” Font size : 9 System.Drawing return width as 1.1953125. SkiaSharp return width as 4.5

Note: We have tried this in ASP.NET Core with .NET Core 3.1 target sample.

Code

int ascii = 8239; char nnbsp = (char)ascii; float width = SkiaMeasurng(nnbsp.ToString(), 9, "FrutigerNext LT"); private static float SkiaMeasurng(string text2, float fontsize, string fontName) { float skiaMeasurement = 0; SKFontStyleWeight weight = SKFontStyleWeight.Light; SKFontStyleSlant sKFontStyleSlant = SKFontStyleSlant.Upright; SKFontStyleWidth sKFontStyleWidth = SKFontStyleWidth.Condensed; using (SKPaint paint = new SKPaint()) { paint.Typeface = SKTypeface.FromFamilyName(fontName, weight, sKFontStyleWidth, sKFontStyleSlant); paint.Color = SKColors.Black; paint.IsAntialias = true; paint.TextSize = fontsize; paint.SubpixelText = true; paint.TextAlign = SKTextAlign.Left; paint.TextEncoding = SKTextEncoding.Utf8; SKRect rect = new SKRect(); skiaMeasurement = paint.MeasureText(text2, ref rect); return skiaMeasurement; } }

Expected Behavior

Width should be 1.1953125 as like System.Drawing

Actual Behavior

Width is 4.5

Basic Information

Reproduction Link

Run the above code example

Gillibald commented 2 years ago

Skia isn't performing any text shaping therfore you don't get similar results. No issue here.

MohanaselvamJ commented 2 years ago

@Gillibald , Thank for reply.

In my project, I am using SkiaSharp library for measuring characters. In the above code, I am facing different width for Narrow Non Breaking Space character in SkiaSharp library.

Could you please suggest me, does any thing need to modify in my measuring code snippet?

I am expecting same width as equivalent to System.Drawing. How can I get same width value in SkiaSharp?

Any help will be appreciated.

Gillibald commented 2 years ago

You can't. System.Drawing uses GDI under the hood. You can only get similar results. I suggest using RichTextKit on top of SkiaSharp.