Skip to content

Right to Left languages add a space at the beginning of a line #96

Open

Description

@gbreen12

Not sure if I'm doing right to left correctly but when I render a multi line text, any line that isn't the end of the paragraph has an extra space at the beginning of the line. Here are a few examples:

In this example you can see the first 4 lines there is a slight space at the beginning but the last line is more right.

Image

In this example the first line of the second paragraph has a slight space but the second line is flush with the other lines.

Image

Here's the code I'm using to create the string:

public void DrawText(
    SKCanvas canvas,
    string text,
    SkiaTextLayoutOptions layoutOptions
)
{
    if (string.IsNullOrWhiteSpace(text))
    {
        return;
    }

    var richString = new RichString
    {
        MaxWidth = layoutOptions.MaxWidthPixels,
        MaxHeight = layoutOptions.MaxHeightPixels,
    };

    var margin = layoutOptions.MarginPixels;
    richString.MarginTop(margin).MarginBottom(margin).MarginLeft(margin).MarginRight(margin);

    richString.Add(text, layoutOptions.FontFamily, fontSize: DefaultFontSize);
    richString.Alignment(TextAlignment.Right);

    var options = new TextPaintOptions
    {
        Edging = SKFontEdging.Antialias,
    };

    var startingPoint = new SKPoint(layoutOptions.StartingX, layoutOptions.StartingY);
    richString.Paint(canvas, startingPoint, options);
}

Activity

  1. gbreen12 commented on May 2, 2025

    @gbreen12
    Author

    Here's a super simple implementation that shows the problem:

    const int docSize = 576;
    const string outputPath = @"C:\Users\gbcha\Downloads\hebrew.png";
    var richString = new RichString
    {
    	MaxWidth = docSize
    };
    
    richString.Add("אמא ואבא,\n\nהיה לנו כיף! עזרתם לנו נורא ואנחנו מודים לכם מקרב לבאני וולריו נהנו כלכך ממתנת הלילה במלון ואנחנו אסירי תודה. מחכים הבא!\nניתאי, עלמה, ולריו, וטליה");
    richString.Alignment(TextAlignment.Right);
    
    using var bitmap = new SKBitmap(docSize, docSize);
    using var bitmapCanvas = new SKCanvas(bitmap);
    bitmapCanvas.Clear(SKColors.White);
    richString.Paint(bitmapCanvas, new SKPoint(0, 0));
    
    using var image = SKImage.FromBitmap(bitmap);
    using var data = image.Encode(SKEncodedImageFormat.Png, 100);
    
    using var output = File.OpenWrite(outputPath);
    data.SaveTo(output);
    

    Output:

    Image

  2. gbreen12 commented on May 2, 2025

    @gbreen12
    Author

    If I remove the end of the line to make it so it doesn't wrap it removes the extra space:

    Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions