private void UpdateFormattedText() { if (_FormattedText == null) { return; } _FormattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue; _FormattedText.TextAlignment = TextAlignment; _FormattedText.Trimming = TextTrimming; var converter = TypeDescriptor.GetConverter(typeof(System.Drawing.Font)); var font = (System.Drawing.Font)converter.ConvertFromInvariantString(Typeface); textDecorations.Clear(); if (font.Strikeout) { textDecorations.Add(TextDecorations.Strikethrough); } if (font.Underline) { textDecorations.Add(TextDecorations.Underline); } _FormattedText.SetFontSize(font.SizeInPoints); _FormattedText.SetFontFamily(new FontFamily(font.Name)); _FormattedText.SetFontWeight(font.Bold ? FontWeights.Bold : FontWeights.Regular); _FormattedText.SetFontStyle(font.Italic ? FontStyles.Italic : FontStyles.Normal); _FormattedText.SetTextDecorations(textDecorations); }
private void EnsureFormattedText() { if (_formattedText != null) { return; } IsAnimated = false; //Wird in RemoveCodes() auf true gesetzt, falls irgendwas tatsächlcih effektiv obfuscated ist #pragma warning disable CS0618 // Type or member is obsolete _formattedText = new FormattedText( RemoveCodes(Text), CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily, FontStyles.Normal, FontWeight, FontStretches.Normal), FontSize, new SolidColorBrush(Foreground)) { TextAlignment = TextAlignment }; #pragma warning restore CS0618 // Type or member is obsolete var currentColor = Foreground; var currentStyle = FontStyles.Normal; var currentWeight = FontWeight; var currentDecorations = new TextDecorationCollection(); var lastPos = 0; var i = 0; foreach (Match m in Formatting.MinecraftFormattings.Matches(Text)) { i++; var realIndex = m.Index - i * 2 + 2; _formattedText.SetForegroundBrush(new SolidColorBrush(currentColor), lastPos, realIndex - lastPos); _formattedText.SetFontStyle(currentStyle, lastPos, realIndex - lastPos); _formattedText.SetFontWeight(currentWeight, lastPos, realIndex - lastPos); _formattedText.SetTextDecorations(currentDecorations.CloneCurrentValue(), lastPos, realIndex - lastPos); var c = m.Groups[1].Value[0]; switch (c) { case 'r': case 'R': currentColor = Foreground; currentStyle = FontStyles.Normal; currentWeight = GetNormalFont(); currentDecorations.Clear(); break; case 'l': case 'L': currentWeight = GetBoldFont(); break; case 'o': case 'O': currentStyle = FontStyles.Italic; break; case 'm': case 'M': currentDecorations.Add(TextDecorations.Strikethrough); break; case 'n': case 'N': currentDecorations.Add(TextDecorations.Underline); break; default: currentColor = Colors.FromChar(c) ?? currentColor; //Color codes reset the current formatting! currentStyle = FontStyles.Normal; currentWeight = GetNormalFont(); currentDecorations.Clear(); break; } lastPos = realIndex; } _formattedText.SetForegroundBrush(new SolidColorBrush(currentColor), lastPos, _formattedText.Text.Length - lastPos); _formattedText.SetFontStyle(currentStyle, lastPos, _formattedText.Text.Length - lastPos); _formattedText.SetFontWeight(currentWeight, lastPos, _formattedText.Text.Length - lastPos); _formattedText.SetTextDecorations(currentDecorations.CloneCurrentValue(), lastPos, _formattedText.Text.Length - lastPos); }