khaled-alshamaa / ar-php

Set of functionalities enable Arabic website developers to serve professional search, present and process Arabic content in PHP
Other
286 stars 51 forks source link

Text becoms upside down when using Arabic->utf8Glyphs #15

Closed Sohib closed 3 years ago

Sohib commented 3 years ago

This code snippet tells it all, the first part of this string become last part which make a sentence no readable. also it's adding \n at the end of each line which not exist at the original string, i have fixed this$max parameter to 300.

if you notice 'السلام ' was the in the bingeing at the string but after using utf8Glyphs it become the last one.

use ArPHP\I18N\Arabic;

$arabic = new Arabic();
$str = "السلام عليكم في البداية نقولها وهذا نص رقم ١ و بعده نصين رقم ٢ و بعده نصات ٣ وبالنهاية لا نقول الا الحمدلله  حمدا كثيرا";

echo($arabic->utf8Glyphs($str,300));
/*
ﺍﺮﻴﺜﻛ ﺍﺪﻤﺣ  ﻪﻠﻟﺪﻤﺤﻟﺍ ﻻﺍ ﻝﻮﻘﻧ ﻻ ﺔﻳﺎﻬﻨﻟﺎﺑﻭ ٣   ﺕﺎﺼﻧ ﻩﺪﻌﺑ ﻭ ٢   ﻢﻗﺭ ﻦﻴﺼﻧ ﻩﺪﻌﺑ ﻭ ١   ﻢﻗﺭ ﺺﻧ ﺍﺬﻫﻭ ﺎﻬﻟﻮﻘﻧ ﺔﻳﺍﺪﺒﻟﺍ ﻲﻓ ﻢﻜﻴﻠﻋ ﻡﻼﺴﻟﺍ
*/

is there a way to fix the Glyphs without changing the order of the string?

khaled-alshamaa commented 3 years ago

My dear friend @Sohib, this is exactly what the utf8Glyphs method was developed to do in order to fix the simple Unicode rendering performed by GD and PDF libraries that does not support connecting glyphs of Arabic language and right-to-left writing direction properly (see issue examples here). It is not supposed to print out the output of that method using the echo function (there is no need as all browsers support Arabic glyphs and RTL languages very well).

On the other hand, the added new line character \n is the expected behavior because the default value of the max_char parameter is 50, and your string exceeded that limit, so the utf8Glyphs method injects it into your string to perform line-wrapping. For more information, please check the utf8Glyphs documentation here.

Finally, I am not aware of any use case that may need to perform the glyphs joining without reordering chars due to the lack of supporting right-to-left languages. If you have one, I will be more than happy to have a look at it and may add such an option to the library.

Sohib commented 3 years ago

Thanks @khaled-alshamaa for responding so quickly. im sorry i wasn't able to clarify this issue. i am using this library with dompdf and the text was't showing correctly for long lines.

here is the code snippet to show the error

<?php

require 'vendor/autoload.php';

use Dompdf\Dompdf;
use Dompdf\Options;

function html2pdf($string)
{
    $options = new Options();
    $options->set('defaultFont', 'DejaVu Sans');

    // instantiate and use the dompdf class
    $dompdf = new Dompdf($options);
    $dompdf->loadHtml($string);
    // (Optional) Setup the paper size and orientation
    $dompdf->setPaper('A4', 'landscape');
    // Render the HTML as PDF
    $dompdf->render();

    // Output the generated PDF to Browser
    return $dompdf->stream();
}

$arabic = new \ArPHP\I18N\Arabic();
$arabicText = $arabic->utf8Glyphs("السلام عليكم في البداية نقولها وهذا نص رقم ١ و بعده نصين رقم ٢ و بعده نصات ٣ وبالنهاية لا نقول الا الحمدلله  حمدا كثيرا",
    300);

html2pdf("<div style=\"width: 100px\">$arabicText</div>");

this will result the text to be displayed incorrectly like the following

Screen Shot 2021-06-30 at 6 02 56 PM

its working correctly for short string but when using long lines it will display in wired order.

khaled-alshamaa commented 3 years ago

My dear friend @Sohib, Dompdf is an HTML to PDF converter. So, you need to format your input in HTML probably in the first place.

Therefore, after you set the maximum number of characters in every single line (i.e., the second parameter of the utf8Glyphs method, for example, 50), you need to call the nl2br() PHP function to replace all new line characters \n by break line tag <br>.

Also, to handle the right text alignment properly, you can use the HTML paragraph tag with an alignment property set to the right (e.g. <p align="right">$arabicText</p>).

I just alter the last two lines of your script like the following:

$arabicText = $arabic->utf8Glyphs("السلام عليكم في البداية نقولها وهذا نص رقم ١ و بعده نصين رقم ٢ و بعده نصات ٣ وبالنهاية لا نقول الا الحمدلله  حمدا كثيرا", 
                                  50);

$arabicText = nl2br($arabicText);

html2pdf("<p align=\"right\">$arabicText</p>");

This will do the trick, and here is the example result: pdf

khaled-alshamaa commented 3 years ago

Dear @Sohib, please close this issue if my answer above resolve your problem successfully ;-)

Sohib commented 3 years ago

Thank for your help

badrshs commented 3 years ago

Hi @khaled-alshamaa , thanks a lot for this help. I'm still facing something similar to what @Sohib has , image

I'm using dompdf , and as you can see the first line start from the bottom to the top . any idea what casing that ?

khaled-alshamaa commented 3 years ago

Hi @badrshs, Can you please share the code snippet that generates this output?