Persian text results in reversed order of lines when using `wrap()` · Issue #1396 · Intervention/image · GitHub
Skip to content

Persian text results in reversed order of lines when using wrap() #1396

Open

Description

@hmssg

Hello, I checked that in Farsi language, it reverses the text when warping

1807641428114306

Its correct form should be as below

من
سبحان قادری
نسب هستم

Activity

  1. olivervogel commented on Aug 17, 2024

    @olivervogel
    Member

    Since I can't read the language, it's hard for me to debug correctly here.

    I will gladly accept pull requests that address this issue.

    For example, I can't tell if the following code block produces correct output. For me it seems correct.

    use Intervention\Image\ImageManager;
    use Intervention\Image\Typography\FontFactory;
    
    $image = ImageManager::gd()
        ->create(400, 200)
        ->fill('ddd');
    
    $text = 'من
    سبحان قادری
    نسب هستم';
    
    $image->text($text, 350, 150, function (FontFactory $font) {
        $font->filename('./ArialUnicodeMS.ttf');
        $font->size(40);
        $font->color('ff5500');
        $font->lineHeight(1.6);
        $font->wrap(300);
        $font->align('right');
    });

    localhost

  2. changed the title [-]Persian text warp[/-] [+]Persian text wrap[/+] on Aug 17, 2024
  3. changed the title [-]Persian text wrap[/-] [+]Persian text results in reversed order of lines when using `wrap()`[/+] on Aug 18, 2024
  4. hmssg commented on Aug 23, 2024

    @hmssg
    Author

    سلام به همه دوستان فارسی زبان من با این کد تونستم فنی درستش کنم

    $lines = explode("\n", wordwrap($text, 70));

            foreach ($lines as $i => $line) {
                $image->text(PersianRender::render($line), 540, 800 + ($i * 75), function (FontFactory $font){
                    $font->filename(base_path('public/font/Peyda-Black.ttf'));
                    $font->size(50);
                    $font->color('fff');
                    $font->align('center');
                    $font->valign('middle');
                    $font->lineHeight(3);
                });
            }
    
  5. gammalogic commented on Nov 9, 2024

    @gammalogic

    A 3rd party library such as AR-PHP is needed to convert the UTF-8 characters to the correct glyphs. For example, if you look at this text

    شما اهل کجا هستيد؟

    and compare it with the image below you can see that a library is needed to generate the text correctly:

    Persian Text Example

    I have used this text because it has a question mark at the end, which in RTL text should of course be at the left of the text.

    In the image, when the text is wrapped it is wrapped in the wrong direction so that each following line of text is higher than the previous line (which is not correct because Arabic and Persian are still read from top to bottom like Western languages).

    In my script, if I use this code

    $text = 'شما اهل کجا هستيد؟';
    $text = $Arabic->utf8Glyphs($text);
    
    $lines = explode("\n", wordwrap($text, 20));
    $lines = array_reverse($lines);
    
    foreach ($lines as $i => $line) {
        $image->text($line, 450, 360 + ($i * 34), function (FontFactory $font) {
            $font->filename('./B-Lotus/B Lotus_0.ttf');
            $font->size(44);
            $font->color('ff5500');
            $font->lineHeight(1.6);
            //$font->wrap(60);
            $font->align('right');
        });
    }

    then I get this image:

    Persian Text Example

    I am not sure if this fixes the problem, but it is something that users could add to their scripts. This assumes that the rendering of the text is still correct and reads correctly?

  6. olivervogel commented on Dec 31, 2024

    @olivervogel
    Member

    I will gladly accept pull requests that address this issue. Since I can't read the language, it's hard for me to debug correctly here.

  7. gammalogic commented on Dec 31, 2024

    @gammalogic

    I will gladly accept pull requests that address this issue. Since I can't read the language, it's hard for me to debug correctly here.

    In order to support this, I think you would need to add a property that allows the users to specify the language direction, for example $font->rtl(true). The value would normally be set to false by default, but if the value is set to true you would then process the text using the example code I have added. This scheme should work for the most common RTL languages (Arabic, Persian, and Hebrew). If there is a demand for it then I will be happy to try and create a PR.

  8. olivervogel commented on Jan 1, 2025

    @olivervogel
    Member

    Thanks @gammalogic

    I think your solution works fine. Since an integration would introduce AR-PHP as a dependency, I prefer to leave this external.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions