//---------------------------
// public static methods
//---------------------------
/// <summary>
/// resolves text effect on a text range to a list of text effect targets.
/// The method will walk the text and perform the following task:
/// 1) For each continous block of text, create a text effect targeting the scoping element
/// 2) For the text effect created, calculate the starting cp index and cp count for the effect
///
/// The method will create freezable copy of the TextEffect passed in and fill in
/// CharacterIndex and Count for the range.
/// </summary>
/// <param name="startPosition">starting text pointer</param>
/// <param name="endPosition">end text pointer</param>
/// <param name="effect">effect that is apply on the text</param>
public static TextEffectTarget[] Resolve(
TextPointer startPosition,
TextPointer endPosition,
TextEffect effect
)
{
if (effect == null)
throw new ArgumentNullException("effect");
ValidationHelper.VerifyPositionPair(startPosition, endPosition);
TextPointer effectStart = new TextPointer(startPosition);
// move to the first character symbol at or after Start position
MoveToFirstCharacterSymbol(effectStart);
TextEffect effectCopy;
List<TextEffectTarget> list = new List<TextEffectTarget>();
// we will now traverse the TOM and resolve text effects to the immediate parent
// of the characters. We are effectively applying the text effect onto
// block of continous text.
while (effectStart.CompareTo(endPosition) < 0)
{
// create a copy of the text effect
// so that we can set the CharacterIndex and Count
effectCopy = effect.Clone();
// create a position
TextPointer continuousTextEnd = new TextPointer(effectStart);
// move the position to the end of the continuous text block
MoveToFirstNonCharacterSymbol(continuousTextEnd, endPosition);
// make sure we are not out of the range
continuousTextEnd = (TextPointer)TextPointerBase.Min(continuousTextEnd, endPosition);
// set the character index to be the distance from the Start
// of this text block to the Start of the text container
effectCopy.PositionStart = effectStart.TextContainer.Start.GetOffsetToPosition(effectStart);
// count is the distance from the text block start to end
effectCopy.PositionCount = effectStart.GetOffsetToPosition(continuousTextEnd);
list.Add(
new TextEffectTarget(
effectStart.Parent,
effectCopy
)
);
// move the effectStart to the beginning of the next text block.
effectStart = continuousTextEnd;
MoveToFirstCharacterSymbol(effectStart);
}
return list.ToArray();
}