-
Notifications
You must be signed in to change notification settings - Fork 955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API Proposal: Add span overloads to TextRenderer
#3651
Comments
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 Do we want to include fixing #1564 in this API design discussion or should this just be about readonly APIs? |
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. |
The Win32 API in play here is |
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);
}
} |
This adds the span overloads per dotnet#3651. It also obsoletes `TextFormatFlags.ModifyString` and fully blocks it in the span overloads. We'll add a safe API for getting output strings in a future version.
* Add Span overloads in TextRenderer This adds the span overloads per #3651. It also obsoletes `TextFormatFlags.ModifyString` and fully blocks it in the span overloads. We'll add a safe API for getting output strings in a future version. * Add xml docs based off existing docs.microsoft.com docs.
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 useReadOnlySpan<char>
.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
onPaintEventArgs
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.
The text was updated successfully, but these errors were encountered: