private static TextLineRun CreateRunForTab(TextRun textRun, TextParagraphProperties paragraphProperties)
        {
            var tabRun      = new TextCharacters(TabString, textRun.Properties);
            var stringRange = tabRun.StringRange;
            var run         = new TextLineRun(1, tabRun)
            {
                IsTab       = true,
                StringRange = stringRange,
                Width       = paragraphProperties.DefaultIncrementalTab
            };

            run._glyphWidths = new double[] { run.Width };

            return(run);
        }
        private static TextLineRun CreateRunForTab(TextRun textRun)
        {
            var spaceRun    = new TextCharacters(" ", textRun.Properties);
            var stringRange = spaceRun.StringRange;
            var run         = new TextLineRun(1, spaceRun)
            {
                IsTab       = true,
                StringRange = stringRange,
                // TODO: get from para props
                Width = 40
            };

            run.SetGlyphWidths();

            return(run);
        }
        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);
        }
        internal static TextLineRun CreateRunForText(StringRange stringRange, TextRun textRun, int widthLeft, bool emergencyWrap, bool breakOnTabs)
        {
            var run = new TextLineRun
            {
                StringRange = stringRange,
                TextRun     = textRun,
                Length      = textRun.Length
            };

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

            run._formattedText = formattedText;

            var size = formattedText.Measure();

            run._formattedTextSize = size;

            run.Width = (int)size.Width;

            run.SetGlyphWidths();

            return(run);
        }
 private TextLineRun(int length, TextRun textRun)
 {
     Length  = length;
     TextRun = textRun;
 }
        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);
            }
        }
        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");
        }
        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");
        }