Skiasharp: [BUG] Font issue

Created on 30 Jul 2020  路  6Comments  路  Source: mono/SkiaSharp

Description
I upgraded my app to 2.80.1 and then to 2.80.2-preview.9, and the way fonts work on Android seem to have changed.
Also tested on iOS 13.6 and fonts work as expected.

Code

I determine the typeface as follows:

            var sampleChar = 'A';
            var fontManager = SKFontManager.Default;
            var typeface = fontManager.MatchCharacter(sampleChar);


            if (typeface == null)
            {
                foreach (var font in fontManager.FontFamilies)
                {
                    if (font.ToLower().Contains("helvetica"))
                    {
                        typeface = fontManager.MatchFamily(font, SKFontStyle.Normal);
                        break;
                    }
                }
            }

            _textPaint.Typeface = typeface;

And then drawtext as follows:

canvas.DrawText(textString,
    -info.Width / (float) _canvasScale /
    2 + offset, // + textString.Length * _textPaint.TextSize/2 /(float)scale ,
    -info.Height / (float) _canvasScale / 2 + _textPaint.TextSize * 1.1f,
    _textPaint);

Expected Behavior

Text should display the same as on 1.68.3

Actual Behavior
Typeface on Android change and text no longer displays as intended

Basic Information

  • Version with issue: 2.80.1 and then to 2.80.2-preview.9
  • Last known good version: 1.68.3
  • IDE: Microsoft Visual Studio 2019 Version 16.6.5
  • Platform Target Frameworks:

    • Android: Android 10 API Level 29
  • Target Devices:

    Pixel 2 Emulator running Android 10
    Samsung S8 running Android 9


Detailed IDE/OS information (click to expand)


Xamarin   16.6.000.1064 (d16-6@b03f31e)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.6.0.318 (remotes/origin/d16-6@66afc5421)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.6.40 (1f1466f)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   10.3.1.4 (d16-6/3a10de9)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 165f4b0
    Java.Interop: xamarin/java.interop/d16-6@2cab35c
    ProGuard: xamarin/proguard/master@905836d
    SQLite: xamarin/sqlite/3.31.1@49232bc
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-6@bfb66f3


Xamarin.iOS and Xamarin.Mac SDK   13.18.3.2 (ce0cc74)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Screenshots

Android Text display on 2.80.1 and then to 2.80.2-preview.9
image

Android Text display on 1.68.3
image

Reproduction Link

os-Android os-Linux status-skia-update-required type-bug

All 6 comments

I am not able to reproduce this... I tried a few strings and all the fonts, but no luck.

Are you able to attach an app with this happening?

| Emulator (Pixel 2 v10) | Razor Phone (9.0) |
| :---------: | :------------: |
| image | image |

Sample app attached.
PlotTextTest.zip

With 2.80.2-preview.9 sample app output as follows on Pixel 2 Emulator running Android 10 and Samsung S8 running Android 9:
image

With 1.68.3 sample app output as follows on Pixel 2 Emulator running Android 10 and Samsung S8 running Android 9:
image

FYI: While creating the attached sample also experience #1395 and had to install SkiaSharp package in addition to SkiSharp.View.Forms for 2.80.2-preview.9 to function without crashing. Crash as follows:

System.DllNotFoundException: 'libSkiaSharp.so assembly: type: member:(null)'

07-31 12:36:17.577 E/mono ( 8291):
07-31 12:36:17.577 E/mono ( 8291): Unhandled Exception:
07-31 12:36:17.577 E/mono ( 8291): System.DllNotFoundException: libSkiaSharp.so assembly: type: member:(null)
07-31 12:36:17.577 E/mono ( 8291): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.7(intptr,intptr,intptr)
07-31 12:36:17.577 E/mono ( 8291): at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.7(intptr,intptr,intptr)
07-31 12:36:17.578 E/mono-rt ( 8291): [ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException: libSkiaSharp.so assembly: type: member:(null)
07-31 12:36:17.578 E/mono-rt ( 8291): at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.7(intptr,intptr,intptr)
07-31 12:36:17.578 E/mono-rt ( 8291): at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.7(intptr,intptr,intptr)

I am looking at the issue, and I think it _cause_ is due to the small font size and the new way skia calculates things.

Google has this problem too: https://fiddle.skia.org/c/7ca3e587eeac78b4054ee40f409a5fe1

I will communicate with them and see what the fix is.

image

I started a discussion about this: https://groups.google.com/g/skia-discuss/c/Qx7brrWyv9Y

Seems to be just Android and Linux - or FreeType.

@DawidBester I came across this same issue. Adding HarfBuzzSharp and using canvas.DrawShapedText fixed it for me. Maybe it'll help in your case too?

https://www.nuget.org/packages/HarfBuzzSharp

@daltonks Thanks, your workaround works!

Just added SkiaSharp.HarfBuzz 2.80.2-preview.9 with canvas.DrawShapedText to the sample app, and the text displays correctly on an Android 10 Emulator.

The preference is to try to avoid adding another NuGet with code changes in the production app, but this will be my fallback.

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alanricker picture alanricker  路  3Comments

antongit picture antongit  路  3Comments

NotOfficer picture NotOfficer  路  3Comments

michaldobrodenka picture michaldobrodenka  路  3Comments

joreg picture joreg  路  4Comments