Example #1
0
        /// <summary>
        /// Updates the layout and applies specified text style overrides.
        /// </summary>
        private void UpdateLayout()
        {
            if (_text.IsEmpty || MathUtilities.IsZero(MaxWidth) || MathUtilities.IsZero(MaxHeight))
            {
                var textLine = CreateEmptyTextLine(0);

                TextLines = new List <TextLine> {
                    textLine
                };

                Size = new Size(0, textLine.LineMetrics.Size.Height);
            }
            else
            {
                var textLines = new List <TextLine>();

                double width = 0.0, height = 0.0;

                var currentPosition = 0;

                var textSource = new FormattedTextSource(_text,
                                                         _paragraphProperties.DefaultTextRunProperties, _textStyleOverrides);

                TextLineBreak previousLineBreak = null;

                while (currentPosition < _text.Length && (MaxLines == 0 || textLines.Count < MaxLines))
                {
                    var textLine = TextFormatter.Current.FormatLine(textSource, currentPosition, MaxWidth,
                                                                    _paragraphProperties, previousLineBreak);

                    previousLineBreak = textLine.LineBreak;

                    textLines.Add(textLine);

                    UpdateBounds(textLine, ref width, ref height);

                    if (!double.IsPositiveInfinity(MaxHeight) && height > MaxHeight)
                    {
                        break;
                    }

                    currentPosition += textLine.TextRange.Length;

                    if (currentPosition != _text.Length || textLine.LineBreak == null)
                    {
                        continue;
                    }

                    var emptyTextLine = CreateEmptyTextLine(currentPosition);

                    textLines.Add(emptyTextLine);
                }

                Size = new Size(width, height);

                TextLines = textLines;
            }
        }
Example #2
0
        /// <summary>
        /// Updates the layout and applies specified text style overrides.
        /// </summary>
        private void UpdateLayout()
        {
            if (_text.IsEmpty || Math.Abs(MaxWidth) < double.Epsilon || Math.Abs(MaxHeight) < double.Epsilon)
            {
                var textLine = CreateEmptyTextLine(0);

                TextLines = new List <TextLine> {
                    textLine
                };

                Bounds = new Rect(textLine.LineMetrics.BaselineOrigin.X, 0, 0, textLine.LineMetrics.Size.Height);
            }
            else
            {
                var textLines = new List <TextLine>();

                double left = 0.0, right = 0.0, bottom = 0.0;

                var lineBreaker = new LineBreakEnumerator(_text);

                var currentPosition = 0;

                while (currentPosition < _text.Length && (MaxLines == 0 || textLines.Count < MaxLines))
                {
                    int length;

                    if (lineBreaker.MoveNext())
                    {
                        if (!lineBreaker.Current.Required)
                        {
                            continue;
                        }

                        length = lineBreaker.Current.PositionWrap - currentPosition;

                        if (currentPosition + length < _text.Length)
                        {
                            //The line breaker isn't treating \n\r as a pair so we have to fix that here.
                            if (_text[lineBreaker.Current.PositionMeasure] == '\n' &&
                                _text[lineBreaker.Current.PositionWrap] == '\r')
                            {
                                length++;
                            }
                        }
                    }
                    else
                    {
                        length = _text.Length - currentPosition;
                    }

                    var remainingLength = length;

                    while (remainingLength > 0 && (MaxLines == 0 || textLines.Count < MaxLines))
                    {
                        var textSlice = _text.AsSlice(currentPosition, remainingLength);

                        var textSource = new FormattedTextSource(textSlice, _paragraphProperties.DefaultTextStyle, _textStyleOverrides);

                        var textLine = TextFormatter.Current.FormatLine(textSource, 0, MaxWidth, _paragraphProperties);

                        UpdateBounds(textLine, ref left, ref right, ref bottom);

                        textLines.Add(textLine);

                        if (!double.IsPositiveInfinity(MaxHeight) && bottom + textLine.LineMetrics.Size.Height > MaxHeight)
                        {
                            currentPosition = _text.Length;
                            break;
                        }

                        if (_paragraphProperties.TextTrimming != TextTrimming.None)
                        {
                            currentPosition += remainingLength;

                            break;
                        }

                        remainingLength -= textLine.Text.Length;

                        currentPosition += textLine.Text.Length;
                    }
                }

                if (lineBreaker.Current.Required && currentPosition == _text.Length)
                {
                    var emptyTextLine = CreateEmptyTextLine(currentPosition);

                    UpdateBounds(emptyTextLine, ref left, ref right, ref bottom);

                    textLines.Add(emptyTextLine);
                }

                Bounds = new Rect(left, 0, right, bottom);

                TextLines = textLines;
            }
        }
Example #3
0
        /// <summary>
        /// Updates the layout and applies specified text style overrides.
        /// </summary>
        private void UpdateLayout()
        {
            if (_text.IsEmpty || MathUtilities.IsZero(MaxWidth) || MathUtilities.IsZero(MaxHeight))
            {
                var textLine = CreateEmptyTextLine(0);

                TextLines = new List <TextLine> {
                    textLine
                };

                Size = new Size(0, textLine.LineMetrics.Size.Height);
            }
            else
            {
                var textLines = new List <TextLine>();

                double width = 0.0, height = 0.0;

                var currentPosition = 0;

                var textSource = new FormattedTextSource(_text,
                                                         _paragraphProperties.DefaultTextRunProperties, _textStyleOverrides);

                TextLine previousLine = null;

                while (currentPosition < _text.Length)
                {
                    var textLine = TextFormatter.Current.FormatLine(textSource, currentPosition, MaxWidth,
                                                                    _paragraphProperties, previousLine?.TextLineBreak);

                    currentPosition += textLine.TextRange.Length;

                    if (textLines.Count > 0)
                    {
                        if (textLines.Count == MaxLines || !double.IsPositiveInfinity(MaxHeight) &&
                            height + textLine.LineMetrics.Size.Height > MaxHeight)
                        {
                            if (previousLine?.TextLineBreak != null && _textTrimming != TextTrimming.None)
                            {
                                var collapsedLine =
                                    previousLine.Collapse(GetCollapsingProperties(MaxWidth));

                                textLines[textLines.Count - 1] = collapsedLine;
                            }

                            break;
                        }
                    }

                    var hasOverflowed = textLine.LineMetrics.HasOverflowed;

                    if (hasOverflowed && _textTrimming != TextTrimming.None)
                    {
                        textLine = textLine.Collapse(GetCollapsingProperties(MaxWidth));
                    }

                    textLines.Add(textLine);

                    UpdateBounds(textLine, ref width, ref height);

                    previousLine = textLine;

                    if (currentPosition != _text.Length || textLine.TextLineBreak == null)
                    {
                        continue;
                    }

                    var emptyTextLine = CreateEmptyTextLine(currentPosition);

                    textLines.Add(emptyTextLine);
                }

                Size = new Size(width, height);

                TextLines = textLines;
            }
        }