Misplaced spaces between RTL and LTR strings

时间:2018-03-19 13:38:07

标签: c# unicode devexpress bidi

I have a web site built with DevExpress controls including Reports. The main language used is Hebrew so the basic direction is RTL. However there is often a need to include English text, LTR, mixed in the Hebrew text. Their web controls support RTL and usually there isn't a problem with mixed texts.

The problem is with their reports that up until recently did not support RTL. Creating a report entirely in Hebrew was not to much of a problem. Trouble starts when we have Hebrew and English mixed, then the text becomes messed up.

I succeeded in fixing that with the following code:

private string FixBiDirectionalString(string textToFix)
    {
        try
        {
            char RLE = '\u202B';
            char PDF = '\u202C';
            char LRM = '\u200E';
            char RLM = '\u200F';

            StringBuilder sb = new StringBuilder(textToFix.Replace("\r", "").Replace("\n", string.Format("{0}", '\u000A')));

            System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex("[A-Za-z0-9-+ ]+");
            System.Text.RegularExpressions.MatchCollection mc = r.Matches(sb.ToString());
            foreach (System.Text.RegularExpressions.Match m in mc)
            {
                double tmp;
                if (m.Value == " ")
                    continue;
                if (double.TryParse(RemoveAcceptedChars(m.Value), out tmp))
                    continue;
                sb.Replace(m.Value, LRM + m.Value + RLM);
            }

            return RLE + sb.ToString() + PDF;
        }
        catch { return Text; }

    }

    private string RemoveAcceptedChars(string p)
    {
        return p.Replace("+", "").Replace("-", "").Replace("*", "").Replace("/", "");
    }

This code is based on code I found in this article XtraReports RTL: bidirectional text drawing

然而,我仍然遇到希伯来语和英语单词之间的空格问题,这些空格不足或错位。

如何修复? (我还在使用不支持RTL的旧版报告。)

1 个答案:

答案 0 :(得分:1)

我通过首先修剪字符串中与英语字母表的正则表达式匹配的前导和尾随空格来修复它,然后相应地添加与unicode元素相关的空格。

  string mTrim = m.Value.Trim();
  sb.Replace(m.Value, " " + LRM + mTrim + " " + RLM);

这个问题是由于空格是中性或弱方向引起的,这意味着它们的方向取决于它们所处的文本,这里混合的文本会导致空间错位。因此,此代码强制一个空间成为一般RTL方向的一部分,一个空间成为LTR段的一部分。然后单词正确分开显示。