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.52k stars 540 forks source link

[BUG] `DrawShapedText` throws an exception #1798

Open Youssef1313 opened 3 years ago

Youssef1313 commented 3 years ago

Description

DrawShapedText

Code


public void Render(SKSurface surface)
{
    var paint = new SKPaint
    {
        TextEncoding = SKTextEncoding.Utf16,
        IsStroke = false,
        IsAntialias = true,
        LcdRenderText = true,
        SubpixelText = true
    };

    surface.Canvas.DrawShapedText(line, 0, 0, paint);
}

Expected Behavior

No crash

Actual Behavior

Exception due to paint.GetFont().Typeface being null in:

https://github.com/mono/SkiaSharp/blob/2ad29861d5a40d3bf78c28ab0a9cb02a8f0fe437/source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz.Shared/CanvasExtensions.cs#L10-L17

as the constructor doesn't accept null

https://github.com/mono/SkiaSharp/blob/2ad29861d5a40d3bf78c28ab0a9cb02a8f0fe437/source/SkiaSharp.HarfBuzz/SkiaSharp.HarfBuzz.Shared/SKShaper.cs#L15-L17

The code was working with DrawText, and I'm switching to DrawShapedText to fix the RTL languages.

Basic Information

Detailed IDE/OS information (click to expand) ``` PASTE ANY DETAILED VERSION INFO HERE ```

Screenshots

Reproduction Link

taublast commented 6 months ago

When drawing at 0,0 you are drawing outside the canvas bounds, which might result in a crash depending on the drawing context. For example, in case of a hardware-accelerated surface you'll crash 100%. When drawing text coordinates are the lower-left corner of the text, not top-left one.

Gillibald commented 6 months ago

Text is drawn at the baseline

Youssef1313 commented 6 months ago

The outlined crash is probably less-relevant with the deprecation of SKPaint.Font. The overload I was using in this issue is now deprecated anyway.