Open Bip901 opened 2 months ago
Example:
This line is left-to-right because it starts with "T". שלום! This line is right-to-left because it starts with "ש". This line is left-to-right even though it contains Hebrew characters: שלום, because it starts with "T".
EDIT: Seems like GitHub messed this up too, it's only correct in the preview 😄
The bidi level of each line already indicates the strong direction. FlowDirection.DetectFromContent should be simple.
var text = "שלום, John Dough";
var codepointEnumerator = new CodepointEnumerator(text);
var codepoint = Codepoint.ReplacementCodepoint;
while (codepointEnumerator.MoveNext(out codepoint))
{
var flowDirection = GetFlowDirectionDirection(codepoint.BiDiClass);
if (flowDirection != null)
{
break;
}
}
return;
static FlowDirection? GetFlowDirectionDirection(BidiClass bidiClass)
{
switch (bidiClass)
{
case BidiClass.ArabicLetter:
case BidiClass.ArabicNumber:
case BidiClass.RightToLeft:
{
return FlowDirection.RightToLeft;
}
case BidiClass.LeftToRight:
{
return FlowDirection.LeftToRight;
}
}
return null;
}
Is your feature request related to a problem? Please describe.
Currently, we have
FlowDirection.LeftToRight
andFlowDirection.RightToLeft
, which are good for single-line, static content. However, text controls that display user-controlled text or text where some lines are RTL and others are LTR often need to detect the flow direction based on the content, such that lines starting with RTL characters haveFlowDirection.RightToLeft
. This is similar to theDetectFromContent
value ofTextAlignment
, just for the character order instead of the alignment.For example, the text "שלום, John Dough" starts with the RTL character "ש", so this textbox should be right-to-left:
Incorrect behavior
Correct behavior
Strong RTL Unicode characters are given by RFC 3454 section D.
Describe the solution you'd like
A new enum value for
FlowDirection
, e.g.FlowDirection.DetectFromContent
, or a boolean property on TextBox and TextBlock, e.g.DetectFlowDirectionFromContent
.Describe alternatives you've considered
I'm currently using a custom converter and a style. This solution is limited because it sets the FlowDirection for the entire textbox based on the first character, instead of the more granular control of a line-by-line solution similar to
TextAlignment.DetectFromContent
.(I herby license the code snippets above as CC0 so feel free to use them).
Additional context
No response