将可视化LTR转换为逻辑RTL的c#算法
本文关键字:RTL 算法 可视化 LTR 转换 | 更新日期: 2023-09-27 18:10:22
我在Unity3d中工作,使用c#脚本,用希伯来语和英语编写字符串。Unity3d的问题是在编写UI文本时不支持RTL语言(如希伯来语)。
问题是这样的:原文:מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה,探地雷达(探地雷达)- - -
在统一:探地雷达-התיגולונכטלעתססבתמרשא,תכרעמהניהףלטעהתכרעמ.(探地雷达)
我试图破解文本打印到文本框的顺序一个月了,我不能让它刚刚好。我编写了以下函数:
public string CorrectText (string text)
{
string result = "";
string temp = "";
string charListString = "";
List<Char> charList = new List<char> ();
char[] charArray = text.ToCharArray ();
Array.Reverse (charArray);
//go through each char in charArray
for (int x = 0; x <= charArray.Length -1; x++) {
//if the current char we're examing isn't a Space char -
if (!char.IsWhiteSpace (charArray [x])) {
//add it to charList
charList.Add (charArray [x]);
} else { //if the current char we're examing is a Space char -
charListString = new string (charList.ToArray ());
//if charListString doesn't contains English or Numeric chars -
if (!Regex.IsMatch (charListString, "^[0-9a-zA-Z.,()-]*$")) {
//go through each char in charList
for (int y = 0; y <= charList.Count - 1; y++) {
//add the current char to temp as is (not flipped)
temp += charList [y];
}
//add temp to result
if (x < charArray.Length - 1)
result += temp + " ";
if (x == charArray.Length - 1)
result += temp;
//clear charList and temp
charList.Clear ();
temp = "";
} else { //if temp contains English or Numeric chars -
//go through each char in charList - This flipps the order of letters
for (int y = charList.Count - 1; y >= 0; y--) {
//add the current char to temp
temp += charList [y];
}
//add temp to result
if (x < charArray.Length - 1)
result += temp + " ";
if (x == charArray.Length - 1)
result += temp;
//clear charList and temp
charList.Clear ();
temp = "";
}
}
}
return result;
}
这几乎解决了这个问题,但是文本是从下往上写的,例如:
输入:מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה,探地雷达(探地雷达)- - -
输出:(探地雷达)-………………מערכתהעטלףהינהמערכת,אשרמתבססתעלטכנולוגיתה- GPR
和括号有时会在句子中混在一起,如图所示:)穿透地面(雷达- - - - - - - - - - - - - - - - - - - - -
我在网上找到了一个工具,把视觉希伯来语变成逻辑希伯来语,当我把翻转的文本复制到统一时,它像魔术一样工作!
但是我在网上找不到任何有用的信息,关于如何用c#制作我自己的脚本,做同样的事情。
我设法克服了这个…类:
- 将字符串赋值给text元素
- 使用
Canvas.ForceUpdateCanvases()
或其他方法强制画布更新 - 获取文本组件的缓存文本生成器(不是用于布局)。如果你没有强制更新或等待下一帧,它就会欺骗你;所以不要跳过第二步。
- 使用缓存文本生成器的
getLines
获取文本换行信息;它保存了每行开始的索引。 - 取这些值的最大差值,您可以在换行前获得每行可能的字符量。
- 在翻转字符之前,使用您在上一步中发现的行长度手动插入''n'(新行)到原始字符串!
- 对缠绕的字符串进行反转处理。如果你按不同的顺序做这些事情,你会发现第一行是最短的,而不是最后一行。所以先换行,再换行。
- 将字符串设置为文本组件,它应该可以工作。通常。