(e07f39206) Fixes to text wrapping: - No spaces between symbols in wrapped chinese texts. - Automatically determine if the symbols/words are chinese/japanese/korean instead of just determining how to do the wrapping based on the selected display language (because there may still be western words/names in the texts when using Chinese, or vice versa when for example viewing Workshop items with a Chinese description). - Fixed wrapping when texts mix western and Chinese symbols.

This commit is contained in:
Joonas Rikkonen
2019-05-07 13:20:01 +03:00
parent ba5b856a68
commit 310019f7a9
3 changed files with 45 additions and 22 deletions
@@ -3,6 +3,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Barotrauma
{
@@ -101,28 +102,47 @@ namespace Barotrauma
public static string WrapText(string text, float lineLength, ScalableFont font, float textScale = 1.0f) //TODO: could integrate this into the ScalableFont class directly
{
if (font.MeasureString(text).X < lineLength) return text;
Vector2 textSize = font.MeasureString(text);
if (textSize.X < lineLength) { return text; }
text = text.Replace("\n", " \n ");
string[] words;
if (TextManager.NoWhiteSpace)
List<string> words = new List<string>();
string currWord = "";
for (int i = 0; i<text.Length; i++)
{
words = new string[text.Length];
for (int i = 0; i < text.Length; i++)
if (isCJK.IsMatch(text[i].ToString()))
{
words[i] = text[i].ToString();
if (currWord.Length > 0)
{
words.Add(currWord);
currWord = "";
}
words.Add(text[i].ToString());
}
else if (text[i] == ' ')
{
if (currWord.Length > 0)
{
words.Add(currWord);
currWord = "";
}
}
else
{
currWord += text[i];
}
}
else
if (currWord.Length > 0)
{
words = text.Split(' ');
words.Add(currWord);
currWord = "";
}
StringBuilder wrappedText = new StringBuilder();
float linePos = 0f;
Vector2 spaceSize = font.MeasureString(" ") * textScale;
for (int i = 0; i < words.Length; ++i)
for (int i = 0; i < words.Count; ++i)
{
if (words[i].Length == 0)
{
@@ -180,10 +200,24 @@ namespace Barotrauma
linePos = size.X + spaceSize.X;
}
if (i < words.Length - 1) wrappedText.Append(" ");
if (i < words.Count - 1 && !isCJK.IsMatch(words[i]) && !isCJK.IsMatch(words[i + 1]))
{
wrappedText.Append(" ");
}
}
return wrappedText.ToString().Replace(" \n ", "\n");
}
static Regex isCJK = new Regex(
@"\p{IsHangulJamo}|" +
@"\p{IsCJKRadicalsSupplement}|" +
@"\p{IsCJKSymbolsandPunctuation}|" +
@"\p{IsEnclosedCJKLettersandMonths}|" +
@"\p{IsCJKCompatibility}|" +
@"\p{IsCJKUnifiedIdeographsExtensionA}|" +
@"\p{IsCJKUnifiedIdeographs}|" +
@"\p{IsHangulSyllables}|" +
@"\p{IsCJKCompatibilityForms}");
}
}
@@ -16,14 +16,6 @@ namespace Barotrauma
private static string[] serverMessageCharacters = new string[] { "~", "[", "]", "=" };
public static string Language;
public static bool NoWhiteSpace
{
get
{
if (!textPacks.ContainsKey(Language)) { return false; }
return textPacks[Language].Any(t => t.NoWhiteSpace);
}
}
private static HashSet<string> availableLanguages = new HashSet<string>();
public static IEnumerable<string> AvailableLanguages
@@ -11,9 +11,7 @@ namespace Barotrauma
public readonly string Language;
private Dictionary<string, List<string>> texts;
public readonly bool NoWhiteSpace;
private readonly string filePath;
public TextPack(string filePath)
@@ -25,7 +23,6 @@ namespace Barotrauma
if (doc == null || doc.Root == null) return;
Language = doc.Root.GetAttributeString("language", "Unknown");
NoWhiteSpace = doc.Root.GetAttributeBool("nowhitespace", false);
foreach (XElement subElement in doc.Root.Elements())
{