Browse Source

Added support for custom ellipsis for the ClampLines feature

Marcin Ziąbek 1 year ago
parent
commit
8b74dad0a3

+ 21 - 0
Source/QuestPDF.Examples/TextExamples.cs

@@ -1084,5 +1084,26 @@ namespace QuestPDF.Examples
                      }
                      }
                  });
                  });
          }
          }
+         
+         [Test]
+         public void ClampLines()
+         {
+             RenderingTest
+                 .Create()
+                 .PageSize(300, 100)
+                
+                 .ProduceImages()
+                 .ShowResults()
+                 .Render(container =>
+                 {
+                     container
+                         .Padding(5)
+                         .MinimalBox()
+                         .Border(1)
+                         .Padding(10)
+                         .Text(Placeholders.Paragraph())
+                         .ClampLines(2, " [...]");
+                 });
+         }
     }
     }
 }
 }

+ 5 - 1
Source/QuestPDF/Elements/Text/TextBlock.cs

@@ -17,6 +17,7 @@ namespace QuestPDF.Elements.Text
         
         
         public TextHorizontalAlignment? Alignment { get; set; }
         public TextHorizontalAlignment? Alignment { get; set; }
         public int? LineClamp { get; set; }
         public int? LineClamp { get; set; }
+        public string LineClampEllipsis { get; set; }
         public List<ITextBlockItem> Items { get; set; } = new();
         public List<ITextBlockItem> Items { get; set; } = new();
 
 
         private SkParagraph Paragraph { get; set; }
         private SkParagraph Paragraph { get; set; }
@@ -230,11 +231,14 @@ namespace QuestPDF.Elements.Text
 
 
         private void BuildParagraph()
         private void BuildParagraph()
         {
         {
+            using var clampLinesEllipsis = new SkText(LineClampEllipsis);
+            
             var paragraphStyle = new ParagraphStyleConfiguration
             var paragraphStyle = new ParagraphStyleConfiguration
             {
             {
                 Alignment = MapAlignment(Alignment ?? TextHorizontalAlignment.Start),
                 Alignment = MapAlignment(Alignment ?? TextHorizontalAlignment.Start),
                 Direction = MapDirection(ContentDirection),
                 Direction = MapDirection(ContentDirection),
-                MaxLinesVisible = LineClamp ?? 1_000_000
+                MaxLinesVisible = LineClamp ?? 1_000_000,
+                LineClampEllipsis = clampLinesEllipsis.Instance
             };
             };
             
             
             var builder = SkParagraphBuilderPoolManager.Get(paragraphStyle);
             var builder = SkParagraphBuilderPoolManager.Get(paragraphStyle);

+ 6 - 2
Source/QuestPDF/Fluent/TextExtensions.cs

@@ -111,9 +111,10 @@ namespace QuestPDF.Fluent
         }
         }
 
 
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
-        public TextBlockDescriptor ClampLines(int maxLines)
+        public TextBlockDescriptor ClampLines(int maxLines, string ellipsis = TextDescriptor.DefaultLineClampEllipsis)
         {
         {
             TextBlock.LineClamp = maxLines;
             TextBlock.LineClamp = maxLines;
+            TextBlock.LineClampEllipsis = ellipsis;
             return this;
             return this;
         }
         }
     }
     }
@@ -123,6 +124,8 @@ namespace QuestPDF.Fluent
         internal TextBlock TextBlock { get; } = new();
         internal TextBlock TextBlock { get; } = new();
         private TextStyle? DefaultStyle { get; set; }
         private TextStyle? DefaultStyle { get; set; }
 
 
+        internal const string DefaultLineClampEllipsis = "…";
+        
         /// <summary>
         /// <summary>
         /// Applies a consistent text style for the whole content within this <see cref="TextExtensions.Text">Text</see> element.
         /// Applies a consistent text style for the whole content within this <see cref="TextExtensions.Text">Text</see> element.
         /// </summary>
         /// </summary>
@@ -178,9 +181,10 @@ namespace QuestPDF.Fluent
         }
         }
 
 
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
         /// <include file='../Resources/Documentation.xml' path='documentation/doc[@for="text.clampLines"]/*' />
-        public void ClampLines(int maxLines)
+        public void ClampLines(int maxLines, string ellipsis = DefaultLineClampEllipsis)
         {
         {
             TextBlock.LineClamp = maxLines;
             TextBlock.LineClamp = maxLines;
+            TextBlock.LineClampEllipsis = ellipsis;
         }
         }
         
         
         [Obsolete("This method is not supported since the 2024.3 version. Please split your text into separate paragraphs, combine using the Column element that also provides the Spacing capability.")]
         [Obsolete("This method is not supported since the 2024.3 version. Please split your text into separate paragraphs, combine using the Column element that also provides the Spacing capability.")]

+ 1 - 0
Source/QuestPDF/Skia/Text/SkParagraphBuilder.cs

@@ -9,6 +9,7 @@ internal record struct ParagraphStyleConfiguration
     public TextAlign Alignment;
     public TextAlign Alignment;
     public TextDirection Direction;
     public TextDirection Direction;
     public int MaxLinesVisible;
     public int MaxLinesVisible;
+    public IntPtr LineClampEllipsis; // SKText
 
 
     internal enum TextAlign
     internal enum TextAlign
     {
     {