Browse Source

RTL: text

MarcinZiabek 3 years ago
parent
commit
9f6c091706
2 changed files with 68 additions and 21 deletions
  1. 52 0
      QuestPDF.Examples/RightToLeftExamples.cs
  2. 16 21
      QuestPDF/Elements/Text/TextBlock.cs

+ 52 - 0
QuestPDF.Examples/RightToLeftExamples.cs

@@ -205,6 +205,7 @@ namespace QuestPDF.Examples
                         .Grid(grid =>
                         {
                             grid.Spacing(25);
+                            grid.AlignRight();
                             
                             foreach (var i in Enumerable.Range(1, 6))
                             {
@@ -213,5 +214,56 @@ namespace QuestPDF.Examples
                         });
                 });
         }
+        
+        [Test]
+        public void Inlined()
+        {
+            RenderingTest
+                .Create()
+                .ProduceImages()
+                .PageSize(600, 800)
+                .ShowResults()
+                .Render(container =>
+                {
+                    container
+                        .Padding(25)
+                        .Border(1)
+                        .ContentFromRightToLeft()
+                        .Inlined(inlined =>
+                        {
+                            inlined.Spacing(25);
+                            
+                            foreach (var i in Enumerable.Range(5, 10))
+                            {
+                                inlined.Item().Background(Placeholders.BackgroundColor()).Height(25 + i * 5).Width(i * 25);
+                            }
+                        });
+                });
+        }
+        
+        [Test]
+        public void Text()
+        {
+            RenderingTest
+                .Create()
+                .ProduceImages()
+                .PageSize(600, 600)
+                .ShowResults()
+                .Render(container =>
+                {
+                    container
+                        .Padding(25)
+                        .MinimalBox()
+                        .Border(1)
+                        .ContentFromRightToLeft()
+                        .Text(text =>
+                        {
+                            text.DefaultTextStyle(x => x.FontSize(32));
+                            
+                            foreach (var i in Enumerable.Range(1, 100))
+                                text.Span($"{i}").FontColor(Placeholders.Color()).BackgroundColor("#2000");
+                        });
+                });
+        }
     }
 }

+ 16 - 21
QuestPDF/Elements/Text/TextBlock.cs

@@ -8,8 +8,10 @@ using QuestPDF.Infrastructure;
 
 namespace QuestPDF.Elements.Text
 {
-    internal class TextBlock : Element, IStateResettable
+    internal class TextBlock : Element, IStateResettable, IContentDirectionAware
     {
+        public ContentDirection ContentDirection { get; set; }
+        
         public HorizontalAlignment Alignment { get; set; } = HorizontalAlignment.Left;
         public List<ITextBlockItem> Items { get; set; } = new List<ITextBlockItem>();
 
@@ -85,18 +87,12 @@ namespace QuestPDF.Elements.Text
             if (!lines.Any())
                 return;
             
-            var heightOffset = 0f;
-            var widthOffset = 0f;
-            
+            var topOffset = 0f;
+
             foreach (var line in lines)
             {
-                widthOffset = 0f;
+                var leftOffset = GetAlignmentOffset(line.Width);
 
-                var alignmentOffset = GetAlignmentOffset(line.Width);
-                
-                Canvas.Translate(new Position(alignmentOffset, 0));
-                Canvas.Translate(new Position(0, -line.Ascent));
-            
                 foreach (var item in line.Elements)
                 {
                     var textDrawingRequest = new TextDrawingRequest
@@ -111,21 +107,20 @@ namespace QuestPDF.Elements.Text
                         TotalAscent = line.Ascent
                     };
                 
+                    var canvasOffset = ContentDirection == ContentDirection.LeftToRight
+                        ? new Position(leftOffset, topOffset - line.Ascent)
+                        : new Position(availableSpace.Width - leftOffset - item.Measurement.Width, topOffset - line.Ascent);
+                    
+                    Canvas.Translate(canvasOffset);
                     item.Item.Draw(textDrawingRequest);
-                
-                    Canvas.Translate(new Position(item.Measurement.Width, 0));
-                    widthOffset += item.Measurement.Width;
+                    Canvas.Translate(canvasOffset.Reverse());
+                    
+                    leftOffset += item.Measurement.Width;
                 }
-            
-                Canvas.Translate(new Position(-alignmentOffset, 0));
-                Canvas.Translate(new Position(-line.Width, line.Ascent));
-                Canvas.Translate(new Position(0, line.LineHeight));
                 
-                heightOffset += line.LineHeight;
+                topOffset += line.LineHeight;
             }
-            
-            Canvas.Translate(new Position(0, -heightOffset));
-            
+
             lines
                 .SelectMany(x => x.Elements)
                 .GroupBy(x => x.Item)