Пример #1
0
        public void Should_Produce_Wrapped_And_Trimmed_Lines(string text, string[] expectedLines)
        {
            using (Start())
            {
                var typeface = new Typeface("Verdana");

                var defaultProperties = new GenericTextRunProperties(typeface, 32, foregroundBrush: Brushes.Black);

                var styleSpans = new[]
                {
                    new ValueSpan <TextRunProperties>(0, 5,
                                                      new GenericTextRunProperties(typeface, 48)),
                    new ValueSpan <TextRunProperties>(6, 11,
                                                      new GenericTextRunProperties(new Typeface("Verdana", weight: FontWeight.Bold), 32)),
                    new ValueSpan <TextRunProperties>(28, 28,
                                                      new GenericTextRunProperties(new Typeface("Verdana", FontStyle.Italic), 32))
                };

                var textSource = new FormattedTextSource(text.AsMemory(), defaultProperties, styleSpans);

                var formatter = new TextFormatterImpl();

                var currentPosition = 0;

                var currentHeight = 0d;

                var currentLineIndex = 0;

                while (currentPosition < text.Length && currentLineIndex < expectedLines.Length)
                {
                    var textLine =
                        formatter.FormatLine(textSource, currentPosition, 300,
                                             new GenericTextParagraphProperties(defaultProperties, textWrap: TextWrapping.WrapWithOverflow));

                    currentPosition += textLine.TextRange.Length;

                    if (textLine.Width > 300 || currentHeight + textLine.Height > 240)
                    {
                        textLine = textLine.Collapse(new TextTrailingWordEllipsis(300, defaultProperties));
                    }

                    currentHeight += textLine.Height;

                    var currentText = text.Substring(textLine.TextRange.Start, textLine.TextRange.Length);

                    Assert.Equal(expectedLines[currentLineIndex], currentText);

                    currentLineIndex++;
                }

                Assert.Equal(expectedLines.Length, currentLineIndex);
            }
        }
Пример #2
0
        public void Should_Not_Alter_TextRuns_After_TextStyles_Were_Applied(string text)
        {
            using (Start())
            {
                var formatter = new TextFormatterImpl();

                var defaultProperties = new GenericTextRunProperties(Typeface.Default);

                var paragraphProperties =
                    new GenericTextParagraphProperties(defaultProperties, textWrap: TextWrapping.NoWrap);

                var foreground = new SolidColorBrush(Colors.Red).ToImmutable();

                var expectedTextLine = formatter.FormatLine(new SingleBufferTextSource(text, defaultProperties),
                                                            0, double.PositiveInfinity, paragraphProperties);

                var expectedRuns = expectedTextLine.TextRuns.Cast <ShapedTextCharacters>().ToList();

                var expectedGlyphs = expectedRuns.SelectMany(x => x.GlyphRun.GlyphIndices).ToList();

                for (var i = 0; i < text.Length; i++)
                {
                    for (var j = 1; i + j < text.Length; j++)
                    {
                        var spans = new[]
                        {
                            new ValueSpan <TextRunProperties>(i, j,
                                                              new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: foreground))
                        };

                        var textSource = new FormattedTextSource(text.AsMemory(), defaultProperties, spans);

                        var textLine =
                            formatter.FormatLine(textSource, 0, double.PositiveInfinity, paragraphProperties);

                        var shapedRuns = textLine.TextRuns.Cast <ShapedTextCharacters>().ToList();

                        var actualGlyphs = shapedRuns.SelectMany(x => x.GlyphRun.GlyphIndices).ToList();

                        Assert.Equal(expectedGlyphs, actualGlyphs);
                    }
                }
            }
        }
Пример #3
0
        public void Should_Format_TextRuns_With_TextRunStyles()
        {
            using (Start())
            {
                const string text = "0123456789";

                var defaultProperties =
                    new GenericTextRunProperties(Typeface.Default, 12, foregroundBrush: Brushes.Black);

                var GenericTextRunPropertiesRuns = new[]
                {
                    new ValueSpan <TextRunProperties>(0, 3, defaultProperties),
                    new ValueSpan <TextRunProperties>(3, 3,
                                                      new GenericTextRunProperties(Typeface.Default, 13, foregroundBrush: Brushes.Black)),
                    new ValueSpan <TextRunProperties>(6, 3,
                                                      new GenericTextRunProperties(Typeface.Default, 14, foregroundBrush: Brushes.Black)),
                    new ValueSpan <TextRunProperties>(9, 1, defaultProperties)
                };

                var textSource = new FormattedTextSource(text.AsMemory(), defaultProperties, GenericTextRunPropertiesRuns);

                var formatter = new TextFormatterImpl();

                var textLine = formatter.FormatLine(textSource, 0, double.PositiveInfinity,
                                                    new GenericTextParagraphProperties(defaultProperties));

                Assert.Equal(text.Length, textLine.TextRange.Length);

                for (var i = 0; i < GenericTextRunPropertiesRuns.Length; i++)
                {
                    var GenericTextRunPropertiesRun = GenericTextRunPropertiesRuns[i];

                    var textRun = textLine.TextRuns[i];

                    Assert.Equal(GenericTextRunPropertiesRun.Length, textRun.Text.Length);
                }
            }
        }