Compartilhar via


Formatação de Texto Avançada

The Windows Presentation Foundation (WPF) provides a robust set of APIs for including text in your application. Layout and interface do usuário (UI) APIs, such as TextBlock, provide the most common and general use elements for text presentation. Drawing APIs, such as GlyphRunDrawing and FormattedText, provide a means for including formatted text in drawings. At the most advanced level, WPF provides an extensible text formatting engine to control every aspect of text presentation, such as text store management, text run formatting management, and embedded object management.

This topic provides an introduction to WPF text formatting. It focuses on client implementation and use of the WPF text formatting engine.

ObservaçãoObservação:

Todos os exemplos de código neste documento podem ser encontrados no Formatação exemplo de texto avançado.

Este tópico contém as seguintes seções.

  • Pré-requisitos
  • Formatação de Texto Avançada
  • Using the Text Formatter
  • Implementing the Client Text Store
  • Providing Text Runs
  • Specifying Formatting Properties
  • Tópicos relacionados

Pré-requisitos

This topic assumes that you are familiar with the higher level APIs used for text presentation. Most user scenarios will not require the advanced text formatting APIs discussed in this topic. For an introduction to the different text APIs, see Documentos em Windows Presentation Foundation.

Formatação de Texto Avançada

The text layout and UI controls in WPF provide formatting properties that allow you to easily include formatted text in your application. These controls expose a number of properties to handle the presentation of text, which includes its typeface, size, and color. Under ordinary circumstances, these controls can handle the majority of text presentation in your application. No entanto, alguns cenários avançados exigem o controle do armazenamento de texto, bem sistema autônomo apresentação de texto. WPF Fornece um mecanismo para essa finalidade de formatação de texto extensível.

The advanced text formatting features found in WPF consist of a text formatting engine, a text store, text runs, and formatting properties. The text formatting engine, TextFormatter, creates lines of text to be used for presentation. This is achieved by initiating the line formatting process and calling the text formatter's FormatLine. The text formatter retrieves text runs from your text store by calling the store's GetTextRun method. The TextRun objects are then formed into TextLine objects by the text formatter and given to your application for inspection or display.

Using the Text Formatter

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

Diferente de uma API de texto tradicional, o TextFormatter interage com um cliente de layout de texto através de métodos de callback. Requer que o cliente forneça esses métodos em uma implementação da classe TextSource. O diagrama a seguir ilustra a interação de layout de texto entre a aplicação cliente e TextFormatter.

Interação entre aplicação e TextFormatter

Diagrama de cliente de layout de texto e TextFormatter

The text formatter is used to retrieve formatted text lines from the text store, which is an implementation of TextSource. This is done by first creating an instance of the text formatter by using the Create method. This method creates an instance of the text formatter and sets the maximum line height and width values. Assim que uma instância do formatador texto é criada, o processo de criação da linha é iniciado chamando o FormatLine método. TextFormatter responde à fonte de texto para recuperar o texto e os parâmetros de formatação para as seqüências de texto que formam uma linha.

In the following example, the process of formatting a text store is shown. The TextFormatter object is used to retrieve text lines from the text store and then format the text line for drawing into the DrawingContext.

Implementing the Client Text Store

When you extend the text formatting engine, you are required to implement and manage all aspects of the text store. This is not a trivial task. The text store is responsible for tracking text run properties, paragraph properties, embedded objects, and other similar content. It also provides the text formatter with individual TextRun objects which the text formatter uses to create TextLine objects.

Para lidar com a virtualização de armazenamento de texto, o armazenamento de texto deve ser derivado de TextSource. TextSource Define o método que usa o formatador de texto para recuperar seqüências de texto do armazenamento de texto. GetTextRun o método usado pelo formatador texto para recuperar texto é usado na formatação da linha. The call to GetTextRun is repeatedly made by the text formatter until one of the following conditions occurs:

  • A TextEndOfLine or a subclass is returned.

  • The accumulated width of text runs exceeds the maximum line width specified in either the call to create the text formatter or the call to the text formatter's FormatLine method.

  • A Unicode newline sequence, such as "CF", "LF", or "CRLF", is returned.

Providing Text Runs

The core of the text formatting process is the interaction between the text formatter and the text store. Your implementation of TextSource provides the text formatter with the TextRun objects and the properties with which to format the text runs. This interaction is handled by the GetTextRun method, which is called by the text formatter.

The following table shows some of the predefined TextRun objects.

TextRun Type

Uso

TextCharacters

The specialized text run used to pass a representation of character glyphs back to the text formatter.

TextEmbeddedObject

The specialized text run used to provide content in which measuring, hit testing, and drawing is done in whole, such as a button or image within the text.

TextEndOfLine

The specialized text run used to mark the end of a line.

TextEndOfParagraph

The specialized text run used to mark the end of a paragraph.

TextEndOfSegment

The specialized text run used to mark the end of a segment, such as to end the scope affected by a previous TextModifier run.

TextHidden

The specialized text run used to mark a range of hidden characters.

TextModifier

The specialized text run used to modify properties of text runs in its scope. The scope extends to the next matching TextEndOfSegment text run, or the next TextEndOfParagraph.

Any of the predefined TextRun objects can be subclassed. This allows your text source to provide the text formatter with text runs that include custom data.

The following example demonstrates a GetTextRun method. This text store returns TextRun objects to the text formatter for processing.

ObservaçãoObservação:

Neste exemplo, o armazenamento de texto fornece as mesmas propriedades de texto para todos os o texto. Advanced text stores would need to implement their own span management to allow individual characters to have different properties.

Specifying Formatting Properties

TextRun objects are formatted by using properties provided by the text store. Essas propriedades vêm em dois tipos, TextParagraphProperties e TextRunProperties. TextParagraphProperties tratar sistema autônomo propriedades inclusivo do parágrafo TextAlignment e FlowDirection. TextRunProperties propriedades que podem ser diferentes para cada fluxo em um parágrafo, sistema autônomo o pincel de primeiro plano, de texto Typefacee o dimensionar da fonte. To implement custom paragraph and custom text run property types, your application must create classes that derive from TextParagraphProperties and TextRunProperties respectively.

Consulte também

Conceitos

Tipografia em Windows Presentation Foundation

Documentos em Windows Presentation Foundation