Skip to content

API Proposal: Add span overloads to TextRenderer #3651

Closed

Description

@JeremyKuhne

Proposal

We should add span overloads to TextRenderer to allow for efficient text rendering. Fortunately the Win32 APIs in play here take a length so we can use span data directly- we've already refactored internally to use ReadOnlySpan<char>.

namespace System.Windows.Forms
{
    public sealed class TextRenderer
    {
        // Existing:
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor);
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor);
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor);
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor);
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags);
        public static Size MeasureText(IDeviceContext dc, string text, Font font);
        public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize);
        public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags);
        public static Size MeasureText(string text, Font font);
        public static Size MeasureText(string text, Font font, Size proposedSize);
        public static Size MeasureText(string text, Font font, Size proposedSize, TextFormatFlags flags);

        // 1-1 Proposal
        // Note that backColor and proposedSize can't be defaulted as they are Color.Empty and Size(int.MaxValue, intMaxValue).
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, Color backColor);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, Color backColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, Color backColor);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags);
        public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags);
        public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font);
        public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Size proposedSize);
        public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Size proposedSize, TextFormatFlags flags);
        public static Size MeasureText(ReadOnlySpan<char> text, Font font);
        public static Size MeasureText(ReadOnlySpan<char> text, Font font, Size proposedSize);
        public static Size MeasureText(ReadOnlySpan<char> text, Font font, Size proposedSize, TextFormatFlags flags);
    }
}

Justification

Drawing and measuring text is a pretty common activity. This will allow significantly more efficient text rendering when new string allocations can be avoided (slicing other input, building off of a stack based character array, etc.). In conjunction with IDeviceContext on PaintEventArgs and other internal performance improvements, painting a string on a blank form can be done with < 15% of the allocations it used to take.

Implementation Details

We'll match throwing behavior of the original APIs. The original APIs did not throw for null or empty strings, they just no-op.

Activity

  1. added
    api-ready-for-review
    on Jul 27, 2020
  2. added this to the 5.0 RC1 milestone on Jul 27, 2020
  3. weltkante commented on Jul 27, 2020

    @weltkante
    Contributor

    Make sure you don't repeat the same mistake the existing interop does, namely allowing writing into immutable strings (this is probably still broken, see #1564). You should prevent calling native code writing into readonly spans by passing through the ModifyString flag unchecked.

    Do we want to include fixing #1564 in this API design discussion or should this just be about readonly APIs?

  4. added
    ⛔ blocked
    blocking
    and removed
    ⛔ blocked
    on Jul 28, 2020
  5. JeremyKuhne commented on Jul 28, 2020

    @JeremyKuhne
    MemberAuthor

    Make sure you don't repeat the same mistake

    Thanks for the reminder! I'll make sure the original issue is fixed- we can always add a new API for those that want the truncated behavior. Corrupting strings could go horribly, horribly wrong.

  6. added
    tenet-performance
    on Jul 28, 2020
  7. JeremyKuhne commented on Jul 28, 2020

    @JeremyKuhne
    MemberAuthor

    The Win32 API in play here is DrawTextExW. When drawing with ellipses it has the option to modify the input string to reflect what actually would be displayed. We didn't block this historically, but won't allow it going forward. In order to safely allow it we would have to have at least four new methods to match the full argument Measure and Draw methods.

  8. added
    api-approved
    and removed
    api-ready-for-review
    blocking
    on Jul 28, 2020
  9. terrajobst commented on Jul 28, 2020

    @terrajobst
    Contributor

    Video

    • Looks good as proposed
    namespace System.Windows.Forms
    {
        public sealed class TextRenderer
        {
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, Color backColor);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, Color backColor, TextFormatFlags flags);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Point pt, Color foreColor, TextFormatFlags flags);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, Color backColor);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags);
            public static void DrawText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags);
            public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font);
            public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Size proposedSize);
            public static Size MeasureText(IDeviceContext dc, ReadOnlySpan<char> text, Font font, Size proposedSize, TextFormatFlags flags);
            public static Size MeasureText(ReadOnlySpan<char> text, Font font);
            public static Size MeasureText(ReadOnlySpan<char> text, Font font, Size proposedSize);
            public static Size MeasureText(ReadOnlySpan<char> text, Font font, Size proposedSize, TextFormatFlags flags);
        }
    }
  10. added a commit that references this issue on Aug 10, 2020
  11. ghost added
    🚧 work in progress
    on Aug 10, 2020
  12. ghost removed
    🚧 work in progress
    on Aug 11, 2020
  13. ghost locked as resolved and limited conversation to collaborators on Jan 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-approvedtenet-performance

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions