A library to Bi-Directional and Reshape Arabic or Persian texts
$ dotnet add package BidiReshapeSharp
A library to Bi-Directional and Reshape Arabic or Persian texts
BidiReshapeSharp is a .NET library for pre-processing Right-to-Left (RTL) scripts, such as Arabic and Persian, for display in rendering environments that do not natively implement the Unicode Bidirectional Algorithm (UBA). There are good Python libraries that do this, but there was no library in C# to do the same. So I decided to do it myself. The library resolves two primary issues that arise in basic LTR rendering pipelines:
By using the shaping method, the input string is converted into a sequence of display characters that can be rendered correctly by any simple LTR text engine.
The library is available as a NuGet package.
Install-Package BidiReshapeSharp
dotnet add package BidiReshapeSharp
The primary functionality is exposed through the static BidiShaper class and its Reshape method.
The ProcessString method accepts a single string and returns the fully reshaped and reordered result using the default configuration settings.
using BidiReshapeSharp;
using System;
public class Program
{
public static void Main()
{
// Example 1: Basic Arabic phrase requiring character connection and ligatures (e.g., in لأمر).
string inputArabic = "الْكِتَابُ الْجَدِيدُ لأمر";
string outputArabic = BidiReshape.ProcessString(inputArabic);
Console.WriteLine($"Original (raw Unicode): {inputArabic}");
Console.WriteLine($"Reshaped (display-ready): {outputArabic}");
// The output string contains correctly connected glyphs and reordered segments.
// Example 2: Mixing RTL text, LTR numbers, and punctuation.
string inputMixed = "النص المختلط 123 مع ترقيم.";
string outputMixed = BidiReshape.ProcessString(inputMixed);
Console.WriteLine($"\nOriginal (mixed data): {inputMixed}");
Console.WriteLine($"Reshaped (correctly ordered): {outputMixed}");
}
}
Use the overloaded ProcessString method along with a ReshaperConfig object to apply custom shaping rules.
using BidiReshapeSharp;
using System;
using BidiReshapeSharp.Reshaper;
public class Program
{
public static void Main()
{
// Create a custom configuration object.
var customConfig = new ReshaperConfig
{
DeleteHarakat = true,
UseUnshapedInsteadOfIsolated = true
};
// We support persian texts with پچگژ too!
string input = "نمونه متن پارسی";
string output = BidiReshape.ProcessString(input, customConfig);
Console.WriteLine($"Configured Output: {output}");
}
}
Python Arabic Reshaper by mpcabd
BidiSharp by fsufyan