Esempio n. 1
0
        private static TextLineRun CreateTextLineRun(StringRange stringRange, TextRun textRun, int length, TextParagraphProperties paragraphProperties)
        {
            var run = new TextLineRun
            {
                StringRange = stringRange,
                TextRun     = textRun,
                Length      = length
            };

            var tf            = run.Typeface;
            var formattedText = new FormattedText
            {
                Text     = stringRange.ToString(run.Length),
                Typeface = new Typeface(tf.FontFamily, tf.Style, tf.Weight),
                FontSize = run.FontSize
            };

            run._formattedText = formattedText;

            var size = formattedText.Bounds.Size;

            run._formattedTextSize = size;

            run.Width = size.Width;

            run._glyphWidths = new GlyphWidths(
                run.StringRange,
                run.Typeface.GlyphTypeface,
                run.FontSize,
                paragraphProperties.DefaultIncrementalTab);

            return(run);
        }
Esempio n. 2
0
        internal static TextLineRun CreateRunForText(StringRange stringRange, TextRun textRun, double widthLeft, bool emergencyWrap, bool breakOnTabs)
        {
            var run = new TextLineRun
            {
                StringRange = stringRange,
                TextRun     = textRun,
                Length      = textRun.Length
            };

            var tf            = run.Typeface;
            var formattedText = new FormattedText
            {
                Text     = stringRange.ToString(),
                Typeface = new Typeface(tf.FontFamily, tf.Style, tf.Weight),
                FontSize = run.FontSize
            };

            run._formattedText = formattedText;

            var size = formattedText.Bounds.Size;

            run._formattedTextSize = size;

            run.Width = size.Width;

            run._glyphWidths = new GlyphWidths(
                run.StringRange,
                run.Typeface.GlyphTypeface,
                run.FontSize);

            return(run);
        }
Esempio n. 3
0
        private static TextLineRun Create(TextSource textSource, StringRange stringRange, TextRun textRun, int index, double widthLeft, TextParagraphProperties paragraphProperties)
        {
            if (textRun is TextCharacters)
            {
                return(CreateRunForSpecialChars(textSource, stringRange, textRun, index, paragraphProperties) ??
                       CreateRunForText(stringRange, textRun, widthLeft, paragraphProperties));
            }

            if (textRun is TextEndOfLine)
            {
                return(new TextLineRun(textRun.Length, textRun)
                {
                    IsEnd = true
                });
            }

            if (textRun is TextEmbeddedObject embeddedObject)
            {
                double width = embeddedObject.GetSize(double.PositiveInfinity).Width;
                return(new TextLineRun(textRun.Length, textRun)
                {
                    IsEmbedded = true,
                    _glyphWidths = new double[] { width },
                    // Embedded objects must propagate their width to the container.
                    // Otherwise text runs after the embedded object are drawn at the same x position.
                    Width = width
                });
            }

            throw new NotSupportedException("Unsupported run type");
        }
Esempio n. 4
0
        private static TextLineRun Create(TextSource textSource, StringRange stringRange, TextRun textRun, int index, int widthLeft)
        {
            if (textRun is TextCharacters)
            {
                return(CreateRunForEol(textSource, stringRange, textRun, index) ??
                       CreateRunForText(stringRange, textRun, widthLeft, false, true));
            }

            if (textRun is TextEndOfLine)
            {
                return(new TextLineRun(textRun.Length, textRun)
                {
                    IsEnd = true
                });
            }

            if (textRun is TextEmbeddedObject)
            {
                return(new TextLineRun(textRun.Length, textRun)
                {
                    IsEmbedded = true, _glyphWidths = new int[textRun.Length]
                });
            }

            throw new NotSupportedException("Unsupported run type");
        }
Esempio n. 5
0
        internal static TextLineRun CreateRunForText(StringRange stringRange, TextRun textRun, double widthLeft, bool emergencyWrap, bool breakOnTabs)
        {
            var run = new TextLineRun
            {
                StringRange = stringRange,
                TextRun     = textRun,
                Length      = textRun.Length
            };

            var formattedText = new FormattedText
            {
                Text     = stringRange.ToString(),
                Typeface = new Typeface(run.Typeface, run.FontSize)
            };


            run._formattedText = formattedText;

            var size = formattedText.Measure();

            run._formattedTextSize = size;

            run.Width = size.Width;

            run.SetGlyphWidths();

            return(run);
        }
Esempio n. 6
0
            internal GlyphWidths(StringRange range, GlyphTypeface typeFace, double fontSize)
            {
                _range    = range;
                _typeFace = typeFace;
                _scale    = fontSize / _typeFace.DesignEmHeight;

                InitGlyphWidths();
            }
Esempio n. 7
0
        internal static TextLineRun CreateRunForText(StringRange stringRange, TextRun textRun, double widthLeft, bool emergencyWrap, bool breakOnTabs)
        {
            var tf            = textRun.Properties.Typeface;
            var formattedText = new FormattedText
            {
                Text     = stringRange.ToString(),
                Typeface = new Typeface(tf.FontFamily, tf.Style, tf.Weight),
                FontSize = textRun.Properties.FontSize
            };

            double x      = 0;
            int    offset = 0;
            int    lastWhitespaceIndex = -1;

            //Measure the width of the text that would fit in the space left
            while (x <= widthLeft && offset < textRun.Length)
            {
                x += formattedText.HitTestTextPosition(offset).Width;

                if (x <= widthLeft)
                {
                    if (char.IsWhiteSpace(stringRange[offset]))
                    {
                        lastWhitespaceIndex = offset;
                    }

                    offset++;
                }
            }

            //Wrap at the last letter if there was no whitespace in the run
            if (lastWhitespaceIndex == -1 && x > widthLeft)
            {
                lastWhitespaceIndex = offset;
            }

            var run = new TextLineRun
            {
                StringRange         = stringRange,
                TextRun             = textRun,
                Length              = textRun.Length,
                LastWhitespaceIndex = lastWhitespaceIndex
            };

            run._formattedText = formattedText;

            var size = formattedText.Bounds.Size;

            run._formattedTextSize = size;

            run.Width = size.Width;

            run.SetGlyphWidths();

            return(run);
        }
Esempio n. 8
0
        public void TrimEnd(int trimLength)
        {
            Length         = Math.Max(Length - trimLength, 1);
            StringRange    = new StringRange(StringRange.ToString(), 0, Length);
            _formattedText = new FormattedText()
            {
                Text     = StringRange.ToString(),
                Typeface = _formattedText.Typeface,
                FontSize = _formattedText.FontSize
            };

            var size = _formattedText.Bounds.Size;

            _formattedTextSize = size;

            Width = size.Width;
        }
Esempio n. 9
0
        internal static TextLineRun CreateRunForText(
            StringRange stringRange,
            TextRun textRun,
            double widthLeft,
            TextParagraphProperties paragraphProperties)
        {
            TextLineRun run = CreateTextLineRun(stringRange, textRun, textRun.Length, paragraphProperties);

            if (run.Width <= widthLeft)
            {
                return(run);
            }

            TextLineRun wrapped = PerformTextWrapping(run, widthLeft, paragraphProperties);

            wrapped.Width = run.Width;
            return(wrapped);
        }
Esempio n. 10
0
        private static TextLineRun CreateRunForSpecialChars(TextSource textSource, StringRange stringRange, TextRun textRun, int index, TextParagraphProperties paragraphProperties)
        {
            switch (stringRange[0])
            {
            case '\r':
                var runLength = 1;
                if (stringRange.Length > 1 && stringRange[1] == '\n')
                {
                    runLength = 2;
                }
                else if (stringRange.Length == 1)
                {
                    var nextRun = textSource.GetTextRun(index + 1);
                    var range   = nextRun.GetStringRange();
                    if (range.Length > 0 && range[0] == '\n')
                    {
                        var eolRun = new TextCharacters(NewlineString, textRun.Properties);
                        return(new TextLineRun(eolRun.Length, eolRun)
                        {
                            IsEnd = true
                        });
                    }
                }

                return(new TextLineRun(runLength, textRun)
                {
                    IsEnd = true
                });

            case '\n':
                return(new TextLineRun(1, textRun)
                {
                    IsEnd = true
                });

            case '\t':
                return(CreateRunForTab(textRun, paragraphProperties));

            default:
                return(null);
            }
        }
Esempio n. 11
0
        private static int FindPositionForTextWrapping(StringRange range, int maxIndex)
        {
            if (maxIndex > range.Length - 1)
            {
                maxIndex = range.Length - 1;
            }

            LineBreakEnumerator lineBreakEnumerator = new LineBreakEnumerator(
                new ReadOnlySlice <char>(range.String.AsMemory().Slice(range.OffsetToFirstChar, range.Length)));

            LineBreak?lineBreak = null;

            while (lineBreakEnumerator.MoveNext())
            {
                if (lineBreakEnumerator.Current.PositionWrap > maxIndex)
                {
                    break;
                }

                lineBreak = lineBreakEnumerator.Current;
            }

            return(lineBreak.HasValue ? lineBreak.Value.PositionWrap : maxIndex);
        }