Skip to main content
TextFormatter Class
 

Provides services for formatting text and breaking text lines using a custom text layout client.

Namespace:   System.Windows.Media.TextFormatting
Assembly:  PresentationCore (in PresentationCore.dll)

SystemObject
  System.Windows.Media.TextFormattingTextFormatter

public abstract class TextFormatter : IDisposable
public ref class TextFormatter abstract : IDisposable
[<AbstractClass>]
type TextFormatter = 
    class
        interface IDisposable
    end
Public MustInherit Class TextFormatter
	Implements IDisposable
NameDescription
System_CAPS_protmethod TextFormatter

Initializes a new instance of the TextFormatter class.

NameDescription
System_CAPS_pubmethod System_CAPS_static Create

Creates a new instance of the TextFormatter class. This is a static method.

System_CAPS_pubmethod System_CAPS_static Create

Creates a new instance of the TextFormatter class with the specified formatting mode. This is a static method.

System_CAPS_pubmethod Dispose

Releases all managed and unmanaged resources used by the TextFormatter object.

System_CAPS_pubmethod Equals

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethod Finalize

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethod FormatLine

Creates a TextLine that is used for formatting and displaying document content.

System_CAPS_pubmethod FormatLine

Creates a TextLine that is used for formatting and displaying document content.

System_CAPS_pubmethod FormatMinMaxParagraphWidth

Returns a value that represents the smallest and largest possible paragraph width that can fully contain the specified text content.

System_CAPS_pubmethod FormatMinMaxParagraphWidth

Returns a value that represents the smallest and largest possible paragraph width that can fully contain the specified text content.

System_CAPS_pubmethod GetHashCode

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethod GetType

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_protmethod MemberwiseClone

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethod ToString

Returns a string that represents the current object.(Inherited from Object.)

TextFormatter is the WPF text engine that provides services for formatting text and breaking text lines. TextFormatter can handle different text character formats and paragraph styles, and includes support for international text layout.

Unlike a traditional text API, the TextFormatter interacts with a text layout client through a set of callback methods. It requires the client to provide these methods in an implementation of the TextSource class. The following diagram illustrates the interaction of the text formatter client and the text layout client.

Diagram of text layout client and TextFormatter

Interaction between the text layout client and the TextFormatter object

TextFormatter provides support for text features, such as OpenType typography, as well as support for text run, line, and paragraph level features.

The following table is a list of the key TextFormatter supported services for text features.

Feature

Description

Background brush

The Brush that is used for the background color. See the property.

Baseline alignment

Determines the baseline distance of the line. See the property.

Font linking

Provides font linking through the use of composite fonts.

Foreground brush

The Brush that is used for the foreground color. See the property.

OpenType

Advanced OpenType typographic features, such contextual alternates and standard ligatures. See the TextRunTypographyProperties class properties.

Typeface

Determines the typeface, as well as, weight, style, and stretch. See the property.

Text decoration

Provides support for TextDecoration objects, such as underline or strikethrough. See the property.

Text effects

Provides support for TextEffect objects. See the property.

The following table is a list of the key TextFormatter supported services for paragraph-level features.

Feature

Description

Flow direction

Flow direction of the paragraph—left-to-right, or right-to-left. See the property.

Line indentation

Indentation for each line in the paragraph. See the property.

Marker

Marker characteristics of the first line in the paragraph. See the property.

Paragraph indentation

Indentation for the first line in the paragraph. See the property.

Tabs

Includes support for tab alignment and tab leader.

Width

Determine minimum and maximum paragraph width. See the FormatMinMaxParagraphWidth methods.

Word wrap

Determine how text wraps when it reaches the flow edge. See the property.

The following table is a list of the key TextFormatter supported services for line-level features.

Feature

Description

Alignment

Text alignment—left, right, centered, or justified. See the property.

Line height

Height of the line. See the property.

Line collapsing

Line collapsing support. See the Collapse method.

Black box measurement

Represents the metrics used to lay out a character in a device font. See the CharacterMetrics class.

Caret support

Caret navigation and editing support. See the TextLine character hit properties, such as GetNextCaretCharacterHit and GetPreviousCaretCharacterHit.

The following table is a list of the key TextFormatter supported services for text run-level features.

Feature

Description

Hidden run

Range of characters that are not visible. See the TextHidden object.

Breaking

Line and paragraph breaking.

The following example shows how to use a TextFormatter object to generate formatted lines of text that can be displayed as drawing objects.

// Create a TextFormatter object.
TextFormatter formatter = TextFormatter.Create();

// Create common paragraph property settings.
CustomTextParagraphProperties customTextParagraphProperties
    = new CustomTextParagraphProperties();

// Format each line of text from the text store and draw it.
while (textStorePosition < customTextSource.Text.Length)
{
    // Create a textline from the text store using the TextFormatter object.
    using (TextLine myTextLine = formatter.FormatLine(
        customTextSource,
        textStorePosition,
        96 * 6,
        customTextParagraphProperties,
        null))
    {
        // Draw the formatted text into the drawing context.
        myTextLine.Draw(drawingContext, linePosition, InvertAxes.None);

        // Update the index position in the text store.
        textStorePosition += myTextLine.Length;

        // Update the line position coordinate for the displayed line.
        linePosition.Y += myTextLine.Height;
    }
}
' Create a TextFormatter object.
Dim formatter As TextFormatter = TextFormatter.Create()

' Create common paragraph property settings.
Dim customTextParagraphProperties As New CustomTextParagraphProperties()

' Format each line of text from the text store and draw it.
Do While textStorePosition < customTextSource.Text.Length
	' Create a textline from the text store using the TextFormatter object.
	Using myTextLine As TextLine = formatter.FormatLine(customTextSource, textStorePosition, 96 * 6, customTextParagraphProperties, Nothing)
		' Draw the formatted text into the drawing context.
		myTextLine.Draw(drawingContext, linePosition, InvertAxes.None)

		' Update the index position in the text store.
		textStorePosition += myTextLine.Length

		' Update the line position coordinate for the displayed line.
		linePosition.Y += myTextLine.Height
	End Using
Loop
.NET Framework
Available since 3.0

Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top