Browse Source

Improved text measuring performance

Marcin Ziąbek 4 years ago
parent
commit
175db288f6
2 changed files with 64 additions and 4 deletions
  1. 62 2
      QuestPDF.Examples/TextExamples.cs
  2. 2 2
      QuestPDF/Elements/Text/TextBlock.cs

+ 62 - 2
QuestPDF.Examples/TextExamples.cs

@@ -30,7 +30,7 @@ namespace QuestPDF.Examples
                         .Text(text =>
                         {
                             text.DefaultTextStyle(TextStyle.Default);
-                            text.AlignRight();
+                            text.AlignLeft();
                             text.ParagraphSpacing(10);
 
                             text.Span(Placeholders.LoremIpsum());
@@ -59,11 +59,71 @@ namespace QuestPDF.Examples
 
                             text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
                             
-                            text.Span("\nThis is \npage number ");
+                            text.NewLine();
+                       
+                            text.Span(Placeholders.Sentence());
                         });
                 });
         }
 
+        [Test]
+        public void SpaceIssue()
+        {
+            RenderingTest
+                .Create()
+                .PageSize(PageSizes.A4)
+                .FileName()
+                .ProducePdf()
+                .ShowResults()
+                .Render(container =>
+                {
+                    container
+                        .Padding(20)
+                        .Padding(10)
+                        .Box()
+                        .Border(1)
+                        .Padding(5)
+                        .Padding(10)
+                        .Debug()
+                        .Text(text =>
+                        {
+                            text.DefaultTextStyle(TextStyle.Default);
+                            text.AlignLeft();
+                            text.ParagraphSpacing(10);
+
+                            text.Span(Placeholders.LoremIpsum());
+
+                            text.NewLine();
+
+                            text.Span("This text is a normal text, ");
+                            text.Span("this is a bold text, ", TextStyle.Default.Bold());
+                            text.Span("this is a red and underlined text, ", TextStyle.Default.Color(Colors.Red.Medium).Underlined());
+                            text.Span("and this is slightly bigger text.", TextStyle.Default.Size(16));
+
+                            text.NewLine();
+
+                            text.Span("The new text element also supports injecting custom content between words: ");
+                            text.Element().PaddingBottom(-10).Height(16).Width(32).Image(Placeholders.Image);
+                            text.Span(".");
+
+                            text.NewLine();
+
+                            text.Span("This is page number ");
+                            text.CurrentPageNumber();
+                            text.Span(" out of ");
+                            text.TotalPages();
+
+                            text.NewLine();
+
+                            text.ExternalLocation("Please visit QuestPDF website", "https://www.questpdf.com");
+                            
+                            text.NewLine();
+                       
+                            text.Span("This is target text that does not show up.");
+                        });
+                });
+        }
+        
         [Test]
         public void PageNumber()
         {

+ 2 - 2
QuestPDF/Elements/Text/TextBlock.cs

@@ -27,7 +27,7 @@ namespace QuestPDF.Elements.Text
             if (!RenderingQueue.Any())
                 return new FullRender(Size.Zero);
             
-            var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height);
+            var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height).ToList();
 
             if (!lines.Any())
                 return new PartialRender(Size.Zero);
@@ -51,7 +51,7 @@ namespace QuestPDF.Elements.Text
 
         internal override void Draw(Size availableSpace)
         {
-            var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height + Size.Epsilon).ToList();
+            var lines = DivideTextItemsIntoLines(availableSpace.Width, availableSpace.Height).ToList();
             
             if (!lines.Any())
                 return;