Iterating through specific style runs

The text subsystem also provides an iterator that searches for specific style runs. You create a filter that defines the style runs you are interested in; for example, you might want to locate all sequences of text with a particular font style applied. You use this filter with TStyleRunIterator, which iterates through a text object and locates these style runs, whether or not they coincide with the internal storage of all the styling information for that range of text.

You can also choose whether to differentiate style runs that share a style name but may have different values. For example, if a range of text includes both red and blue characters, you could either handle the red and blue ranges separately or as a single run.

This figure shows the classes that allow you to iterate through style runs:


TStyleRunIterator provides the protocol for searching through a text object for specific style runs. TStyleRunIterator takes a filter that describes the style runs you want the iterator to return.

TStyleFilter is the base class for all style filters. Several concrete subclasses are provided:

Two classes are also provided for filtering the results of a base filter, TNotStyleFilter and TBoundaryStyleFilter. See "Combining filters" on page 57 for more information on using these classes.

Style filters

Each TStyleRunIterator works with a filter that describes the type of run you want to extract. The abstract class TStyleFilter provides the protocol for style filters. Several concrete derived classes are provided for common searching needs:

Figures 12 through 15 demonstrate the results of using these filters to iterate through the style runs in an instance of text.

Using the default filter--TNullStyleFilter--returns the style runs as shown in Figure 12.


As shown in Figure 13, using a TSingleStyleFilter with a color style extracts the runs containing characters 0-9 and 15-19. Using a TSingleStyleFilter with a point size style extracts the runs containing characters 0-4 and 5-14.


As shown in Figure 14, using a TSingleTypeStyleFilter with a color style extracts the runs containing characters 0-9 and 15-19. Using a TSingleTypeStyleFilter with a point size style identifies a single run containing characters 0-14.


As shown in Figure 15, using a TSingleValueStyleFilter with a point size 9 style extracts the run containing characters 5-14.


Combining filters

The TStyleRunIterator class also supports using style filters with other filters. You can develop a filter that takes the results of a base filter and filters them again.


Two classes for combining filters are provided:

    For example, if you iterate through the text object shown in figures 12 through 15 using a TBoundaryStyleFilter with a TSingleStyleFilter that filters for a point size style, the results are three runs as shown in Figure 16:



Example: Using TStyleRunIterator

To use TStyleRunIterator, you:

  1. Create a style filter (if you do not create a style filter, a TNullStyleFilter is used).
    You can construct a filter using either a style name or an actual style object.
  2. Create a TStyleRunIterator, setting a pointer to the text and attaching the filter.
  3. Call First, Last, or Set to establish the starting point for the search.
  4. Call Next or Previous to iterate through the text.
This example searches through the text instance styledText, returning style runs that contain a color style.

      void
      GetColorRuns(TText& styledText)
      {
          TTextColorStyle style;
          TSingleStyleFilter colorFilter(style);
      
          TStyleRunIterator findColors(&styledText,colorFilter);
      
          TTextRange foundRange;
          Boolean status = findColors.First(foundRange);
      
          while(status) {
              // Do something interesting...
              status = findColors.Next(foundRange);
          }
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker