Răsfoiți Sursa

Fixed text alignment

Marcin Ziąbek 4 ani în urmă
părinte
comite
df5c5524ee

+ 2 - 2
QuestPDF.Examples/TextBenchmark.cs

@@ -92,8 +92,8 @@ namespace QuestPDF.Examples
                     
                     
                 var lineFrom = chapterPointers[index];
                 var lineFrom = chapterPointers[index];
                 var lineTo = chapterPointers[index + 1] - 1;
                 var lineTo = chapterPointers[index + 1] - 1;
-                    
-                var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x))
+
+                var lines = book.Skip(lineFrom + 1).Take(lineTo - lineFrom).Where(x => !string.IsNullOrWhiteSpace(x));
                 var content = string.Join(Environment.NewLine, lines);
                 var content = string.Join(Environment.NewLine, lines);
 
 
                 yield return new BookChapter
                 yield return new BookChapter

+ 45 - 6
QuestPDF.Examples/TextExamples.cs

@@ -1,4 +1,5 @@
-using System.Linq;
+using System;
+using System.Linq;
 using NUnit.Framework;
 using NUnit.Framework;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Examples.Engine;
 using QuestPDF.Fluent;
 using QuestPDF.Fluent;
@@ -14,19 +15,52 @@ namespace QuestPDF.Examples
         {
         {
             RenderingTest
             RenderingTest
                 .Create()
                 .Create()
-                .PageSize(PageSizes.A4)
+                .PageSize(500, 400)
                 .FileName()
                 .FileName()
-                .ProducePdf()
+                .ProduceImages()
                 .ShowResults()
                 .ShowResults()
                 .Render(container =>
                 .Render(container =>
                 {
                 {
                     container
                     container
-                        .Padding(20)
+                        .Padding(10)
                         .Box()
                         .Box()
                         .Border(1)
                         .Border(1)
-                        .Padding(5)
+                        .Padding(10)
                         .Text(text =>
                         .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("Let's start with bold text. ", TextStyle.Default.Bold().BackgroundColor(Colors.Grey.Lighten3).Size(16));
                             text.Span("Let's start with bold text. ", TextStyle.Default.Bold().BackgroundColor(Colors.Grey.Lighten3).Size(16));
                             text.Span("Then something bigger. ", TextStyle.Default.Size(28).Color(Colors.DeepOrange.Darken2).BackgroundColor(Colors.Yellow.Lighten3).Underlined());
                             text.Span("Then something bigger. ", TextStyle.Default.Size(28).Color(Colors.DeepOrange.Darken2).BackgroundColor(Colors.Yellow.Lighten3).Underlined());
                             text.Span("And tiny \r\n teeny-tiny. ", TextStyle.Default.Size(6));
                             text.Span("And tiny \r\n teeny-tiny. ", TextStyle.Default.Size(6));
@@ -50,9 +84,14 @@ namespace QuestPDF.Examples
                             // foreach (var i in Enumerable.Range(1, 100))
                             // foreach (var i in Enumerable.Range(1, 100))
                             // {
                             // {
                             //     text.Span($"{i}: {Placeholders.Sentence()} ", TextStyle.Default.Size(12 + i / 5).LineHeight(2.75f - i / 50f).Color(Placeholders.Color()).BackgroundColor(Placeholders.BackgroundColor()));   
                             //     text.Span($"{i}: {Placeholders.Sentence()} ", TextStyle.Default.Size(12 + i / 5).LineHeight(2.75f - i / 50f).Color(Placeholders.Color()).BackgroundColor(Placeholders.BackgroundColor()));   
-                            // }
+                            // }*/
                         });
                         });
                 });
                 });
         }
         }
+
+        T MyFunc<T>(T arg)
+        {
+            return arg;
+        }
     }
     }
 }
 }

+ 23 - 13
QuestPDF/Elements/Text/TextBlock.cs

@@ -1,4 +1,5 @@
-using System.Collections.Generic;
+using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Drawing.SpacePlan;
 using QuestPDF.Elements.Text.Calculation;
 using QuestPDF.Elements.Text.Calculation;
@@ -62,14 +63,9 @@ namespace QuestPDF.Elements.Text
             {
             {
                 widthOffset = 0f;
                 widthOffset = 0f;
 
 
-                var emptySpace = availableSpace.Width - line.Width;
+                var alignmentOffset = GetAlignmentOffset(line.Width);
 
 
-                if (Alignment == HorizontalAlignment.Center)
-                    emptySpace /= 2f;
-
-                if (Alignment != HorizontalAlignment.Left)
-                    Canvas.Translate(new Position(emptySpace, 0));
-                
+                Canvas.Translate(new Position(alignmentOffset, 0));
                 Canvas.Translate(new Position(0, -line.Ascent));
                 Canvas.Translate(new Position(0, -line.Ascent));
             
             
                 foreach (var item in line.Elements)
                 foreach (var item in line.Elements)
@@ -92,12 +88,10 @@ namespace QuestPDF.Elements.Text
                     widthOffset += item.Measurement.Width;
                     widthOffset += item.Measurement.Width;
                 }
                 }
             
             
-                if (Alignment != HorizontalAlignment.Right)
-                    Canvas.Translate(new Position(emptySpace, 0));
-                
-                Canvas.Translate(new Position(-line.Width - emptySpace, line.Ascent));
-
+                Canvas.Translate(new Position(-alignmentOffset, 0));
+                Canvas.Translate(new Position(-line.Width, line.Ascent));
                 Canvas.Translate(new Position(0, line.LineHeight));
                 Canvas.Translate(new Position(0, line.LineHeight));
+                
                 heightOffset += line.LineHeight;
                 heightOffset += line.LineHeight;
             }
             }
             
             
@@ -116,6 +110,22 @@ namespace QuestPDF.Elements.Text
             
             
             if (!RenderingQueue.Any())
             if (!RenderingQueue.Any())
                 ResetState();
                 ResetState();
+
+            float GetAlignmentOffset(float lineWidth)
+            {
+                if (Alignment == HorizontalAlignment.Left)
+                    return 0;
+
+                var emptySpace = availableSpace.Width - lineWidth;
+
+                if (Alignment == HorizontalAlignment.Right)
+                    return emptySpace;
+
+                if (Alignment == HorizontalAlignment.Center)
+                    return emptySpace / 2;
+
+                throw new ArgumentException();
+            }
         }
         }
 
 
         public IEnumerable<TextLine> DivideTextItemsIntoLines(float availableWidth, float availableHeight)
         public IEnumerable<TextLine> DivideTextItemsIntoLines(float availableWidth, float availableHeight)