private static void ConvertTextBoxToGeometry(TextBox textBox, List <PathGeometry> pathList) { if (!textBox.IsArrangeValid) { textBox.UpdateLayout(); } Typeface typeface = new Typeface(textBox.FontFamily, textBox.FontStyle, textBox.FontWeight, textBox.FontStretch); string text = textBox.Text; for (int index = 0; index < text.Length; ++index) { PathConversionHelper.ConvertFormattedTextToGeometry(new FormattedText(text.Substring(index, 1), CultureInfo.CurrentCulture, textBox.FlowDirection, typeface, textBox.FontSize, textBox.Foreground), textBox.GetRectFromCharacterIndex(index).TopLeft, pathList); } }
private static void ConvertTextRangeToGeometry(TextRange textRange, FlowDirection flowDirection, List <PathGeometry> pathList) { TextPointer position1 = textRange.Start.GetInsertionPosition(LogicalDirection.Forward); for (TextPointer insertionPosition = position1.GetNextInsertionPosition(LogicalDirection.Forward); insertionPosition != null && insertionPosition.CompareTo(textRange.End) <= 0; insertionPosition = insertionPosition.GetNextInsertionPosition(LogicalDirection.Forward)) { TextRange textRange1 = new TextRange(position1, insertionPosition); Rect characterRect = position1.GetCharacterRect(LogicalDirection.Forward); characterRect.Union(insertionPosition.GetCharacterRect(LogicalDirection.Backward)); FontFamily fontFamily = (FontFamily)textRange1.GetPropertyValue(TextElement.FontFamilyProperty); FontStyle style = (FontStyle)textRange1.GetPropertyValue(TextElement.FontStyleProperty); FontWeight weight = (FontWeight)textRange1.GetPropertyValue(TextElement.FontWeightProperty); FontStretch stretch = (FontStretch)textRange1.GetPropertyValue(TextElement.FontStretchProperty); double emSize = (double)textRange1.GetPropertyValue(TextElement.FontSizeProperty); Typeface typeface = new Typeface(fontFamily, style, weight, stretch); PathConversionHelper.ConvertFormattedTextToGeometry(new FormattedText(textRange1.Text, CultureInfo.CurrentCulture, flowDirection, typeface, emSize, (Brush)Brushes.Red), characterRect.TopLeft, pathList); position1 = insertionPosition; } }