本文整理汇总了C#中ITextPointer.GetTextInRun方法的典型用法代码示例。如果您正苦于以下问题:C# ITextPointer.GetTextInRun方法的具体用法?C# ITextPointer.GetTextInRun怎么用?C# ITextPointer.GetTextInRun使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ITextPointer
的用法示例。
在下文中一共展示了ITextPointer.GetTextInRun方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetTextInRun
// Worker for GetText, accepts any ITextPointer.
internal static string GetTextInRun(ITextPointer position, LogicalDirection direction)
{
char[] text;
int textLength;
int getTextLength;
textLength = position.GetTextRunLength(direction);
text = new char[textLength];
getTextLength = position.GetTextInRun(direction, text, 0, textLength);
Invariant.Assert(getTextLength == textLength, "textLengths returned from GetTextRunLength and GetTextInRun are innconsistent");
return new string(text);
}
示例2: GetTextWithLimit
// Like GetText, excepts also accepts a limit parameter -- no text is returned past
// this second position.
// limit may be null, in which case it is ignored.
internal static int GetTextWithLimit(ITextPointer thisPointer, LogicalDirection direction, char[] textBuffer, int startIndex, int count, ITextPointer limit)
{
int charsCopied;
if (limit == null)
{
// No limit, just call GetText.
charsCopied = thisPointer.GetTextInRun(direction, textBuffer, startIndex, count);
}
else if (direction == LogicalDirection.Forward && limit.CompareTo(thisPointer) <= 0)
{
// Limit completely blocks the read.
charsCopied = 0;
}
else if (direction == LogicalDirection.Backward && limit.CompareTo(thisPointer) >= 0)
{
// Limit completely blocks the read.
charsCopied = 0;
}
else
{
int maxCount;
// Get an upper bound on the amount of text to copy.
// Since GetText always stops on non-text boundaries, it's
// ok if the count too high, it will get truncated anyways.
if (direction == LogicalDirection.Forward)
{
maxCount = Math.Min(count, thisPointer.GetOffsetToPosition(limit));
}
else
{
maxCount = Math.Min(count, limit.GetOffsetToPosition(thisPointer));
}
maxCount = Math.Min(count, maxCount);
charsCopied = thisPointer.GetTextInRun(direction, textBuffer, startIndex, maxCount);
}
return charsCopied;
}
示例3: if
/// <summary>
/// </summary>
void ITextSelection.SetCaretToPosition(ITextPointer caretPosition, LogicalDirection direction, bool allowStopAtLineEnd, bool allowStopNearSpace)
{
// We need a pointer with appropriate direction,
// becasue it will be used in textRangeBase.Select method for
// pointer normalization.
caretPosition = caretPosition.CreatePointer(direction);
// Normalize the position in its logical direction - to get to text content over there.
caretPosition.MoveToInsertionPosition(direction);
// We need a pointer with the reverse direction to confirm
// the line wrapping position. So we can ensure Bidi caret navigation.
// Bidi can have the different caret position by setting the
// logical direction, so we have to only set the logical direction
// as the forward for the real line wrapping position.
ITextPointer reversePosition = caretPosition.CreatePointer(direction == LogicalDirection.Forward ? LogicalDirection.Backward : LogicalDirection.Forward);
// Check line wrapping condition
if (!allowStopAtLineEnd &&
((TextPointerBase.IsAtLineWrappingPosition(caretPosition, this.TextView) &&
TextPointerBase.IsAtLineWrappingPosition(reversePosition, this.TextView)) ||
TextPointerBase.IsNextToPlainLineBreak(caretPosition, LogicalDirection.Backward) ||
TextSchema.IsBreak(caretPosition.GetElementType(LogicalDirection.Backward))))
{
// Caret is at wrapping position, and we are not allowed to stay at end of line,
// so we choose forward direction to appear in the begiinning of a second line
caretPosition.SetLogicalDirection(LogicalDirection.Forward);
}
else
{
if (caretPosition.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text &&
caretPosition.GetPointerContext(LogicalDirection.Forward) == TextPointerContext.Text)
{
// This is statistically most typical case. No "smartness" needed
// to choose standard Forward orientation for the caret.
// NOTE: By using caretPosition's direction we solve BiDi caret orientation case:
// The orietnation reflects a direction from where caret has been moved
// or orientation where mouse clicked. So we will stick with appropriate
// character.
// Nothing to do. The caretPosition is good to go.
}
else if (!allowStopNearSpace)
{
// There are some tags around, and we are not allowed to choose a side near to space.
// So we need to perform some content analysis.
char[] charBuffer = new char[1];
if (caretPosition.GetPointerContext(direction) == TextPointerContext.Text &&
caretPosition.GetTextInRun(direction, charBuffer, 0, 1) == 1 &&
Char.IsWhiteSpace(charBuffer[0]))
{
LogicalDirection oppositeDirection = direction == LogicalDirection.Forward ? LogicalDirection.Backward : LogicalDirection.Forward;
// Check formatting switch condition at this position
FlowDirection initialFlowDirection = (FlowDirection)caretPosition.GetValue(FrameworkElement.FlowDirectionProperty);
bool moved = caretPosition.MoveToInsertionPosition(oppositeDirection);
if (moved &&
initialFlowDirection == (FlowDirection)caretPosition.GetValue(FrameworkElement.FlowDirectionProperty) &&
(caretPosition.GetPointerContext(oppositeDirection) != TextPointerContext.Text ||
caretPosition.GetTextInRun(oppositeDirection, charBuffer, 0, 1) != 1 ||
!Char.IsWhiteSpace(charBuffer[0])))
{
// In the opposite direction we have a non-space
// character. So we choose that direction
direction = oppositeDirection;
caretPosition.SetLogicalDirection(direction);
}
}
}
}
// Now that orientation of a caretPosition is identified,
// build an empty selection at this position
TextRangeBase.BeginChange(this);
try
{
TextRangeBase.Select(this, caretPosition, caretPosition);
// Note how Select method works for the case of empty range:
// It creates a single instance TextPointer normalized and oriented
// in a direction taken from caretPosition:
ITextSelection thisSelection = this;
Invariant.Assert(thisSelection.Start.LogicalDirection == caretPosition.LogicalDirection); // orientation must be as passed
Invariant.Assert(this.IsEmpty);
//Invariant.Assert((object)thisSelection.Start == (object)thisSelection.End); // it must be the same instance of TextPointer
//Invariant.Assert(TextPointerBase.IsAtInsertionPosition(thisSelection.Start, caretPosition.LogicalDirection)); // normalization must be done in the same diredction as orientation
// Clear active positions when selection is empty
SetActivePositions(null, null);
}
finally
{
TextRangeBase.EndChange(this);
}
//.........这里部分代码省略.........
示例4: IsNextToPlainLineBreak
// <summary>
// Checks if there is a Environment.NewLine symbol immediately
// next to the position. Used only for plain text scenarios.
// RichText case will always return false.
// </summary>
internal static bool IsNextToPlainLineBreak(ITextPointer thisPosition, LogicalDirection direction)
{
char[] textBuffer = new char[2];
int actualCount = thisPosition.GetTextInRun(direction, textBuffer, /*startIndex:*/0, /*count:*/2);
return
(actualCount == 1 && IsCharUnicodeNewLine(textBuffer[0]))
||
(actualCount == 2 &&
(
(direction == LogicalDirection.Backward && IsCharUnicodeNewLine(textBuffer[1]))
||
(direction == LogicalDirection.Forward && IsCharUnicodeNewLine(textBuffer[0]))
)
);
}
示例5: IsInsideCompoundSequence
// Returns true if the position is inside of a pair of surrogate characters
// or inside of Newline sequence "\r\n".
// Such position is not valid position for caret stopping or for text insertion.
private static bool IsInsideCompoundSequence(ITextPointer position)
{
// OK, so we're surrounded by text runs (possibly empty), try getting a character
// in each direction -- it's OK to position the caret if there's no characters
// before or after it
Char[] neighborhood = new char[2];
if (position.GetTextInRun(LogicalDirection.Backward, neighborhood, 0, 1) == 1 &&
position.GetTextInRun(LogicalDirection.Forward, neighborhood, 1, 1) == 1)
{
if (Char.IsSurrogatePair(neighborhood[0], neighborhood[1]) ||
neighborhood[0] == '\r' && neighborhood[1] == '\n')
{
return true;
}
// Check for combining marks.
//
// See Unicode 3.1, Section 3.5 (Combination), D13 and D14 for
// strict definitions of "combining character" and "base character".
//
// The CLR source for StringInfo is also informative.
//
// In brief: we're looking for a character followed by a
// combining mark.
//
UnicodeCategory category1 = Char.GetUnicodeCategory(neighborhood[1]);
if (category1 == UnicodeCategory.SpacingCombiningMark ||
category1 == UnicodeCategory.NonSpacingMark ||
category1 == UnicodeCategory.EnclosingMark)
{
UnicodeCategory category0 = Char.GetUnicodeCategory(neighborhood[0]);
if (category0 != UnicodeCategory.Control &&
category0 != UnicodeCategory.Format &&
category0 != UnicodeCategory.OtherNotAssigned)
{
return true;
}
}
}
return false;
}
示例6: GetNextTextPosition
// Returns the position preceeding the next text character in a specified
// direction, or null if no such position exists.
// The scan will halt if limit is encounted; limit may be null.
private static ITextPointer GetNextTextPosition(ITextPointer position, ITextPointer limit, LogicalDirection direction, out char character)
{
bool foundText = false;
character = (char)0;
while (position != null &&
!foundText &&
(limit == null || position.CompareTo(limit) < 0))
{
switch (position.GetPointerContext(direction))
{
case TextPointerContext.Text:
char[] buffer = new char[1];
position.GetTextInRun(direction, buffer, 0, 1);
character = buffer[0];
foundText = true;
break;
case TextPointerContext.ElementStart:
case TextPointerContext.ElementEnd:
if (TextSchema.IsFormattingType(position.GetElementType(direction)))
{
position = position.CreatePointer(+1);
}
else
{
position = null;
}
break;
case TextPointerContext.EmbeddedElement:
case TextPointerContext.None:
default:
position = null;
break;
}
}
return position;
}
示例7: SetFindTextAndFindTextPositionMap
/// <summary>
/// Set the find text content from reading the text on the current text position.
/// </summary>
/// <returns>
/// Returns the number of characters actually loaded into the findText array.
/// </returns>
private static int SetFindTextAndFindTextPositionMap(
ITextPointer startPosition,
ITextPointer endPosition,
ITextPointer navigator,
LogicalDirection direction,
bool matchLast,
char[] findText,
int[] findTextPositionMap)
{
Invariant.Assert(startPosition.CompareTo(navigator) <= 0);
Invariant.Assert(endPosition.CompareTo(navigator) >= 0);
int runCount;
int inlineCount = 0;
int findTextLength = 0;
// Set the first offset which is zero on TextBufferSize + 1 location of
// the text position map in case of the backward searching
if (matchLast && findTextLength == 0)
{
findTextPositionMap[findTextPositionMap.Length - 1] = 0;
}
while ((matchLast ? startPosition.CompareTo(navigator) : navigator.CompareTo(endPosition)) < 0)
{
switch (navigator.GetPointerContext(direction))
{
case TextPointerContext.Text:
runCount = navigator.GetTextRunLength(direction);
runCount = Math.Min(runCount, findText.Length - findTextLength);
if (!matchLast)
{
runCount = Math.Min(runCount, navigator.GetOffsetToPosition(endPosition));
navigator.GetTextInRun(direction, findText, findTextLength, runCount);
for (int i = findTextLength; i < findTextLength + runCount; i++)
{
findTextPositionMap[i] = i + inlineCount;
}
}
else
{
runCount = Math.Min(runCount, startPosition.GetOffsetToPosition(navigator));
navigator.GetTextInRun(
direction,
findText,
findText.Length - findTextLength - runCount,
runCount);
// Set the text offest for the amount of runCount from the last index
// of text position map
int mapIndex = findText.Length - findTextLength - 1;
for (int i = findTextLength; i < findTextLength + runCount; i++)
{
findTextPositionMap[mapIndex--] = i + inlineCount + 1;
}
}
// Move the navigator position for the amount of runCount
navigator.MoveByOffset(matchLast ? - runCount : runCount);
findTextLength += runCount;
break;
case TextPointerContext.None:
case TextPointerContext.ElementStart:
case TextPointerContext.ElementEnd:
if (IsAdjacentToFormatElement(navigator, direction))
{
// Filter out formatting tags since find text content is plain.
inlineCount++;
}
else
{
if (!matchLast)
{
// Stick in a line break to account for the block element.
findText[findTextLength] = '\n';
findTextPositionMap[findTextLength] = findTextLength + inlineCount;
findTextLength++;
}
else
{
// Increse the find text length first since adding text and map reversely
findTextLength++;
// Stick in a line break to account for the block element and
// add text offset on the last index of text position map
findText[findText.Length - findTextLength] = '\n';
findTextPositionMap[findText.Length - findTextLength] = findTextLength + inlineCount;
}
}
//.........这里部分代码省略.........