-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Introduce TextOptions #20107
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
Merged
Merged
Introduce TextOptions #20107
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1774740
Introduce TextOptions API
Gillibald cca4438
Store BaselinePixelAlignment as byte. Move to a dedicated file.
Gillibald d21e68e
Merge branch 'master' into feature/TextOptions
Gillibald 9c4252c
Update API suppressions
MrJul 62d27dd
Merge branch 'master' into fork/Gillibald/feature/TextOptions
MrJul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| namespace Avalonia.Media | ||
| { | ||
| /// <summary> | ||
| /// Specifies the baseline pixel alignment options for rendering text or graphics. | ||
| /// </summary> | ||
| /// <remarks>Use this enumeration to control whether the baseline of rendered content is aligned to the | ||
| /// pixel grid, which can affect visual crispness and positioning. The value may influence rendering quality, | ||
| /// especially at small font sizes or when precise alignment is required.</remarks> | ||
| public enum BaselinePixelAlignment : byte | ||
| { | ||
| /// <summary> | ||
| /// The baseline pixel alignment is unspecified. | ||
| /// </summary> | ||
| Unspecified, | ||
|
|
||
| /// <summary> | ||
| /// The baseline is aligned to the pixel grid. | ||
| /// </summary> | ||
| Aligned, | ||
|
|
||
| /// <summary> | ||
| /// The baseline is not aligned to the pixel grid. | ||
| /// </summary> | ||
| Unaligned | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| namespace Avalonia.Media | ||
| { | ||
| /// <summary> | ||
| /// Specifies the level of hinting applied to text glyphs during rendering. | ||
| /// Text hinting adjusts glyph outlines to improve readability and crispness, | ||
| /// especially at small font sizes or low DPI. This enum controls the amount | ||
| /// of grid-fitting and outline adjustment performed. | ||
| /// </summary> | ||
| public enum TextHintingMode : byte | ||
| { | ||
| /// <summary> | ||
| /// Hinting mode is not explicitly specified. The default will be used. | ||
| /// </summary> | ||
| Unspecified, | ||
|
|
||
| /// <summary> | ||
| /// No hinting, outlines are scaled only. | ||
| /// </summary> | ||
| None, | ||
|
|
||
| /// <summary> | ||
| /// Minimal hinting, preserves glyph shape. | ||
| /// </summary> | ||
| Light, | ||
|
|
||
| /// <summary> | ||
| /// Aggressive grid-fitting, maximum crispness at low DPI. | ||
| /// </summary> | ||
| Strong | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| namespace Avalonia.Media | ||
| { | ||
| /// <summary> | ||
| /// Provides options for controlling text rendering behavior, including rendering mode, hinting mode, and baseline | ||
| /// pixel alignment. Used to configure how text appears within visual elements. | ||
| /// </summary> | ||
| /// <remarks>TextOptions encapsulates settings that influence the clarity, sharpness, and positioning of | ||
| /// rendered text. These options can be applied to visual elements to customize text appearance for different | ||
| /// display scenarios, such as optimizing for readability at small font sizes or ensuring pixel-perfect alignment. | ||
| /// The struct supports merging with other instances to inherit unspecified values, and exposes attached properties | ||
| /// for use with visuals.</remarks> | ||
| public readonly record struct TextOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets the text rendering mode used to control how text glyphs are rendered. | ||
| /// </summary> | ||
| public TextRenderingMode TextRenderingMode { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets the text rendering hinting mode used to optimize the display of text. | ||
| /// </summary> | ||
| /// <remarks>The hinting mode determines how text is rendered to improve clarity and readability, | ||
| /// especially at small font sizes. Changing this value may affect the appearance of text depending on the | ||
| /// rendering engine and display device.</remarks> | ||
| public TextHintingMode TextHintingMode { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Gets a value indicating whether the text baseline should be aligned to the pixel grid. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// When enabled, the vertical position of the text baseline is snapped to whole pixel boundaries. | ||
| /// This ensures consistent sharpness and reduces blurriness caused by fractional positioning, | ||
| /// particularly at small font sizes or low DPI settings. | ||
| /// </remarks> | ||
| public BaselinePixelAlignment BaselinePixelAlignment { get; init; } | ||
|
|
||
| /// <summary> | ||
| /// Merges this instance with <paramref name="other"/> using inheritance semantics: unspecified values on this | ||
| /// instance are taken from <paramref name="other"/>. | ||
| /// </summary> | ||
| public TextOptions MergeWith(TextOptions other) | ||
| { | ||
| var textRenderingMode = TextRenderingMode; | ||
|
|
||
| if (textRenderingMode == TextRenderingMode.Unspecified) | ||
| { | ||
| textRenderingMode = other.TextRenderingMode; | ||
| } | ||
|
|
||
| var textHintingMode = TextHintingMode; | ||
|
|
||
| if (textHintingMode == TextHintingMode.Unspecified) | ||
| { | ||
| textHintingMode = other.TextHintingMode; | ||
| } | ||
|
|
||
| var baselinePixelAlignment = BaselinePixelAlignment; | ||
|
|
||
| if (baselinePixelAlignment == BaselinePixelAlignment.Unspecified) | ||
| { | ||
| baselinePixelAlignment = other.BaselinePixelAlignment; | ||
| } | ||
|
|
||
| return new TextOptions | ||
| { | ||
| TextRenderingMode = textRenderingMode, | ||
| TextHintingMode = textHintingMode, | ||
| BaselinePixelAlignment = baselinePixelAlignment | ||
| }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the TextOptions attached value for a visual. | ||
| /// </summary> | ||
| public static TextOptions GetTextOptions(Visual visual) | ||
| { | ||
| return visual.TextOptions; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the TextOptions attached value for a visual. | ||
| /// </summary> | ||
| public static void SetTextOptions(Visual visual, TextOptions value) | ||
| { | ||
| visual.TextOptions = value; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the TextRenderingMode attached property for a visual. | ||
| /// </summary> | ||
| public static TextRenderingMode GetTextRenderingMode(Visual visual) | ||
| { | ||
| return visual.TextOptions.TextRenderingMode; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the TextRenderingMode attached property for a visual. | ||
| /// </summary> | ||
| public static void SetTextRenderingMode(Visual visual, TextRenderingMode value) | ||
| { | ||
| visual.TextOptions = visual.TextOptions with { TextRenderingMode = value }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the TextHintingMode attached property for a visual. | ||
| /// </summary> | ||
| public static TextHintingMode GetTextHintingMode(Visual visual) | ||
| { | ||
| return visual.TextOptions.TextHintingMode; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the TextHintingMode attached property for a visual. | ||
| /// </summary> | ||
| public static void SetTextHintingMode(Visual visual, TextHintingMode value) | ||
| { | ||
| visual.TextOptions = visual.TextOptions with { TextHintingMode = value }; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets the BaselinePixelAlignment attached property for a visual. | ||
| /// </summary> | ||
| public static BaselinePixelAlignment GetBaselinePixelAlignment(Visual visual) | ||
| { | ||
| return visual.TextOptions.BaselinePixelAlignment; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Sets the BaselinePixelAlignment attached property for a visual. | ||
| /// </summary> | ||
| public static void SetBaselinePixelAlignment(Visual visual, BaselinePixelAlignment value) | ||
| { | ||
| visual.TextOptions = visual.TextOptions with { BaselinePixelAlignment = value }; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.