dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.84k stars 1.67k forks source link

Maui Graphics need FillString/FillText APIs #10822

Open stcmz opened 1 year ago

stcmz commented 1 year ago

Description

Back in the times of System.Drawing.Graphics, we can pass any brush to the DrawString method. This includes LinearGradientBrush. In Maui Graphics, DrawString/DrawText takes the FontColor property instead of the stroke or paint in drawing a string. Unlike other Draw*** and Fill*** pairs, there are also no FillString or FillText variants.

So, I would love to have some new FillString/FillText and StrokeString/StrokeText APIs be added to the ICanvas interface. To ensure the backward compatibility, the behavior of the existing DrawString/DrawText methods will not be changed. The FillString/FillText APIs shall make use of the paint set with SetFillPaint while the StrokeString/StrokeText APIs shall make use of the stroke settings (StrokeColor, StrokeSize, etc.)

Public API Changes

public void ICanvas.FillString(
    string value,
    float x,
    float y,
    HorizontalAlignment horizontalAlignment);

public void ICanvas.FillString(
    string value,
    float x,
    float y,
    float width,
    float height,
    HorizontalAlignment horizontalAlignment,
    VerticalAlignment verticalAlignment,
    TextFlow textFlow = TextFlow.ClipBounds,
    float lineSpacingAdjustment = 0);

public void ICanvas.FillText(
    IAttributedText value,
    float x,
    float y,
    float width,
    float height);

public void ICanvas.StrokeString(
    string value,
    float x,
    float y,
    HorizontalAlignment horizontalAlignment);

public void ICanvas.StrokeString(
    string value,
    float x,
    float y,
    float width,
    float height,
    HorizontalAlignment horizontalAlignment,
    VerticalAlignment verticalAlignment,
    TextFlow textFlow = TextFlow.ClipBounds,
    float lineSpacingAdjustment = 0);

public void ICanvas.StrokeText(
    IAttributedText value,
    float x,
    float y,
    float width,
    float height);

Intended Use-Case

This will enhance the ability of text/string drawing in Microsoft.Maui.Graphics. Effects like gradient color filled text or hollow out text will be achievable. Without the proposed APIs, one has to use SkiaSharp to convert the text to SKPath and then convert back to PathF, which will be consumed by the ICanvas to draw the text. Custom extension methods can be implemented. However, since the Font and FontSize properties are not readable in ICanvas, additional parameters for the font have to be passed in.

ghost commented 1 year ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.