// generate the formatted text from a given box, with the specified prefix and the last style and color static string generateText( StylisedTextBox box, string prefix, ref CharacterStyle lastStyle, ref Color lastColor) { // if empty return empty var characters = box.GetStylisedCharacters(); if (characters.Count == 0) { return(string.Empty); } var sb = new StringBuilder(); for (int i = 0; i < characters.Count; i++) { // if it's not the first character, update the last style if (i > 0) { lastStyle = characters[i - 1].Style; lastColor = characters[i - 1].Color; } // if the character color has changed if (lastColor != characters[i].Color) { sb.Append(prefix); sb.Append(ColorCode[characters[i].Color]); // the style is reset every time the color changes // so if it's not the same as the initial style, update it if (characters[i].Style != initialStyle) { sb.Append(getFormat(prefix, characters[i].Style)); } } // else if the character style has changed else if (lastStyle != characters[i].Style) { sb.Append(prefix); sb.Append(FormatCode[CharacterStyle.Reset]); if (lastColor != initialColor) { sb.Append(prefix); sb.Append(ColorCode[characters[i].Color]); } sb.Append(getFormat(prefix, characters[i].Style)); } sb.Append(characters[i].Character); } return(sb.ToString()); }
// calculates the current stylised text box text length in pixels static double calculatePixelsLength(StylisedTextBox box) { double result = 0; var characters = box.GetStylisedCharacters(); foreach (var character in characters) { result += getCharacterPixelSize( character.Character, character.Style.HasFlag(CharacterStyle.Bold)); } return(result); }