1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-29 23:58:34 +01:00

GenericFont support for zero-width spaces

This commit is contained in:
Ell 2020-12-31 17:22:51 +01:00
parent 9b1abf119e
commit b6ef243cf4

View file

@ -21,6 +21,11 @@ namespace MLEM.Font {
/// Whereas a regular <see cref="SpriteFont"/> would have to explicitly support this character for width calculations, generic fonts implicitly support it in <see cref="MeasureString"/>. /// Whereas a regular <see cref="SpriteFont"/> would have to explicitly support this character for width calculations, generic fonts implicitly support it in <see cref="MeasureString"/>.
/// </summary> /// </summary>
public const char Nbsp = '\u00A0'; public const char Nbsp = '\u00A0';
/// <summary>
/// This field holds the unicode representation of a zero-width space.
/// Whereas a regular <see cref="SpriteFont"/> would have to explicitly support this character for width calculations and string splitting, generic fonts implicitly support it in <see cref="MeasureString"/> and <see cref="SplitString"/>.
/// </summary>
public const char Zwsp = '\u8203';
/// <summary> /// <summary>
/// The bold version of this font. /// The bold version of this font.
@ -108,6 +113,9 @@ namespace MLEM.Font {
case Nbsp: case Nbsp:
xOffset += this.MeasureChar(' ').X; xOffset += this.MeasureChar(' ').X;
break; break;
case Zwsp:
// don't add width for a zero-width space
break;
default: default:
xOffset += this.MeasureChar(c).X; xOffset += this.MeasureChar(c).X;
break; break;
@ -154,7 +162,7 @@ namespace MLEM.Font {
/// <summary> /// <summary>
/// Splits a string to a given maximum width, adding newline characters between each line. /// Splits a string to a given maximum width, adding newline characters between each line.
/// Also splits long words. /// Also splits long words and supports zero-width spaces.
/// </summary> /// </summary>
/// <param name="text">The text to split into multiple lines</param> /// <param name="text">The text to split into multiple lines</param>
/// <param name="width">The maximum width that each line should have</param> /// <param name="width">The maximum width that each line should have</param>
@ -164,7 +172,7 @@ namespace MLEM.Font {
var total = new StringBuilder(); var total = new StringBuilder();
foreach (var line in text.Split('\n')) { foreach (var line in text.Split('\n')) {
var curr = new StringBuilder(); var curr = new StringBuilder();
foreach (var word in line.Split(' ')) { foreach (var word in line.Split(' ', Zwsp)) {
if (this.MeasureString(word).X * scale >= width) { if (this.MeasureString(word).X * scale >= width) {
if (curr.Length > 0) { if (curr.Length > 0) {
total.Append(curr).Append('\n'); total.Append(curr).Append('\n');