Selaa lähdekoodia

Added possibility to force text direction

MarcinZiabek 3 vuotta sitten
vanhempi
sitoutus
b31d097b61

+ 6 - 0
QuestPDF/Drawing/TextShaper.cs

@@ -29,6 +29,12 @@ namespace QuestPDF.Drawing
             PopulateBufferWithText(buffer, text);
             PopulateBufferWithText(buffer, text);
             buffer.GuessSegmentProperties();
             buffer.GuessSegmentProperties();
 
 
+            if (TextStyle.Direction == TextDirection.LeftToRight)
+                buffer.Direction = Direction.LeftToRight;
+            
+            if (TextStyle.Direction == TextDirection.RightToLeft)
+                buffer.Direction = Direction.RightToLeft;
+            
             ShaperFont.Shape(buffer);
             ShaperFont.Shape(buffer);
             
             
             var length = buffer.Length;
             var length = buffer.Length;

+ 22 - 0
QuestPDF/Fluent/TextSpanDescriptorExtensions.cs

@@ -164,5 +164,27 @@ namespace QuestPDF.Fluent
         }
         }
         
         
         #endregion
         #endregion
+        
+        #region Direction
+
+        public static T DirectionAuto<T>(this T descriptor) where T : TextSpanDescriptor
+        {
+            descriptor.MutateTextStyle(x => x.DirectionAuto());
+            return descriptor;
+        }
+        
+        public static T DirectionFromLeftToRight<T>(this T descriptor) where T : TextSpanDescriptor
+        {
+            descriptor.MutateTextStyle(x => x.DirectionFromLeftToRight());
+            return descriptor;
+        }
+        
+        public static T DirectionFromRightToLeft<T>(this T descriptor) where T : TextSpanDescriptor
+        {
+            descriptor.MutateTextStyle(x => x.DirectionFromRightToLeft());
+            return descriptor;
+        }
+
+        #endregion
     }
     }
 }
 }

+ 24 - 0
QuestPDF/Fluent/TextStyleExtensions.cs

@@ -165,5 +165,29 @@ namespace QuestPDF.Fluent
         }
         }
 
 
         #endregion
         #endregion
+
+        #region Direction
+
+        private static TextStyle TextDirection(this TextStyle style, TextDirection textDirection)
+        {
+            return style.Mutate(TextStyleProperty.Direction, textDirection);
+        }
+        
+        public static TextStyle DirectionAuto(this TextStyle style)
+        {
+            return style.TextDirection(Infrastructure.TextDirection.Auto);
+        }
+        
+        public static TextStyle DirectionFromLeftToRight(this TextStyle style)
+        {
+            return style.TextDirection(Infrastructure.TextDirection.LeftToRight);
+        }
+        
+        public static TextStyle DirectionFromRightToLeft(this TextStyle style)
+        {
+            return style.TextDirection(Infrastructure.TextDirection.RightToLeft);
+        }
+
+        #endregion
     }
     }
 }
 }

+ 9 - 0
QuestPDF/Infrastructure/TextDirection.cs

@@ -0,0 +1,9 @@
+namespace QuestPDF.Infrastructure
+{
+    internal enum TextDirection
+    {
+        Auto,
+        LeftToRight,
+        RightToLeft
+    }
+}

+ 2 - 0
QuestPDF/Infrastructure/TextStyle.cs

@@ -16,6 +16,7 @@ namespace QuestPDF.Infrastructure
         internal bool? HasStrikethrough { get; set; }
         internal bool? HasStrikethrough { get; set; }
         internal bool? HasUnderline { get; set; }
         internal bool? HasUnderline { get; set; }
         internal bool? WrapAnywhere { get; set; }
         internal bool? WrapAnywhere { get; set; }
+        internal TextDirection? Direction { get; set; }
 
 
         internal TextStyle? Fallback { get; set; }
         internal TextStyle? Fallback { get; set; }
 
 
@@ -32,6 +33,7 @@ namespace QuestPDF.Infrastructure
             HasStrikethrough = false,
             HasStrikethrough = false,
             HasUnderline = false,
             HasUnderline = false,
             WrapAnywhere = false,
             WrapAnywhere = false,
+            Direction = TextDirection.Auto,
             Fallback = null
             Fallback = null
         };
         };
 
 

+ 16 - 1
QuestPDF/Infrastructure/TextStyleManager.cs

@@ -17,7 +17,8 @@ namespace QuestPDF.Infrastructure
         HasStrikethrough,
         HasStrikethrough,
         HasUnderline,
         HasUnderline,
         WrapAnywhere,
         WrapAnywhere,
-        Fallback
+        Fallback,
+        Direction
     }
     }
     
     
     internal static class TextStyleManager
     internal static class TextStyleManager
@@ -192,6 +193,19 @@ namespace QuestPDF.Infrastructure
                 
                 
                 return origin with { Fallback = castedValue };
                 return origin with { Fallback = castedValue };
             }
             }
+            
+            if (property == TextStyleProperty.Direction)
+            {
+                if (!overrideValue && origin.Direction != null)
+                    return origin;
+
+                var castedValue = (TextDirection?)value;
+                
+                if (origin.Direction == castedValue)
+                    return origin;
+                
+                return origin with { Direction = castedValue };
+            }
 
 
             throw new ArgumentOutOfRangeException(nameof(property), property, "Expected to mutate the TextStyle object. Provided property type is not supported.");
             throw new ArgumentOutOfRangeException(nameof(property), property, "Expected to mutate the TextStyle object. Provided property type is not supported.");
         }
         }
@@ -238,6 +252,7 @@ namespace QuestPDF.Infrastructure
             result = MutateStyle(result, TextStyleProperty.HasStrikethrough, parent.HasStrikethrough, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.HasStrikethrough, parent.HasStrikethrough, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.HasUnderline, parent.HasUnderline, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.HasUnderline, parent.HasUnderline, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.WrapAnywhere, parent.WrapAnywhere, overrideStyle);
             result = MutateStyle(result, TextStyleProperty.WrapAnywhere, parent.WrapAnywhere, overrideStyle);
+            result = MutateStyle(result, TextStyleProperty.Direction, parent.Direction, overrideStyle);
             
             
             if (applyFallback)
             if (applyFallback)
                 result = MutateStyle(result, TextStyleProperty.Fallback, parent.Fallback, overrideStyle);
                 result = MutateStyle(result, TextStyleProperty.Fallback, parent.Fallback, overrideStyle);