Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTL Language Support #2107

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"ext-dom": "*",
"ext-mbstring": "*",
"phenx/php-font-lib": "^0.5.1",
"phenx/php-svg-lib": "^0.3.3"
"phenx/php-svg-lib": "^0.3.3",
"tecnickcom/tc-lib-unicode": "~1.3.11"
},
"require-dev": {
"phpunit/phpunit": "^7.5",
Expand Down
7 changes: 7 additions & 0 deletions src/Css/AttributeTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class AttributeTranslator
],
'rules' => '!set_table_rules',
'width' => 'width: %s;',
'dir' => 'direction: %s;'
],
'hr' => [
'align' => '!set_hr_align', // Need to grab width to set 'left' & 'right' correctly
Expand All @@ -70,6 +71,7 @@ class AttributeTranslator
],
'div' => [
'align' => 'text-align: %s;',
'dir' => 'direction: %s;'
],
'h1' => [
'align' => 'text-align: %s;',
Expand All @@ -95,6 +97,7 @@ class AttributeTranslator
],
'p' => [
'align' => 'text-align: %s;',
'dir' => 'direction: %s;'
],
// 'col' => array(
// 'align' => '',
Expand All @@ -115,6 +118,7 @@ class AttributeTranslator
'nowrap' => 'white-space: nowrap;',
'valign' => 'vertical-align: %s;',
'width' => 'width: %s;',
'dir' => 'direction: %s;'
],
'tfoot' => [
'align' => '!set_table_row_align',
Expand All @@ -127,6 +131,7 @@ class AttributeTranslator
'nowrap' => 'white-space: nowrap;',
'valign' => 'vertical-align: %s;',
'width' => 'width: %s;',
'dir' => 'direction: %s;'
],
'thead' => [
'align' => '!set_table_row_align',
Expand All @@ -142,6 +147,7 @@ class AttributeTranslator
'bgcolor' => '!set_background_color',
'link' => '!set_body_link',
'text' => '!set_color',
'dir' => 'direction: %s;'
],
'br' => [
'clear' => 'clear: %s;',
Expand Down Expand Up @@ -177,6 +183,7 @@ class AttributeTranslator
'li' => [
'type' => 'list-style-type: %s;',
'value' => 'counter-reset: -dompdf-default-counter %d;',
'dir' => 'direction: %s;'
],
'pre' => [
'width' => 'width: %s;',
Expand Down
47 changes: 43 additions & 4 deletions src/FrameReflower/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Dompdf\FrameReflower;

use Com\Tecnick\Unicode\Bidi;
use Dompdf\FrameDecorator\Block as BlockFrameDecorator;
use Dompdf\FrameDecorator\Text as TextFrameDecorator;
use Dompdf\FontMetrics;
Expand Down Expand Up @@ -221,7 +222,10 @@ protected function _layout_line()
switch ($style->white_space) {
default:
case "normal":
$frame->set_text($text = $this->_collapse_white_space($text));
$text = $this->_collapse_white_space($text);

$frame->set_text($text);

if ($text == "") {
break;
}
Expand All @@ -235,7 +239,9 @@ protected function _layout_line()
break;

case "nowrap":
$frame->set_text($text = $this->_collapse_white_space($text));
$text = $this->_collapse_white_space($text);

$frame->set_text($text);
break;

case "pre-wrap":
Expand All @@ -252,7 +258,9 @@ protected function _layout_line()

case "pre-line":
// Collapse white-space except for \n
$frame->set_text($text = preg_replace("/[ \t]+/u", " ", $text));
$text = preg_replace("/[ \t]+/u", " ", $text);

$frame->set_text($text);

if ($text == "") {
break;
Expand Down Expand Up @@ -298,10 +306,21 @@ protected function _layout_line()
$frame->split_text($split);

$t = $frame->get_text();
$replaceText = false;

// Remove any trailing newlines
if ($split > 1 && $t[$split - 1] === "\n" && !$frame->is_pre()) {
$frame->set_text(mb_substr($t, 0, -1));
$t = mb_substr($t, 0, -1);
$replaceText = true;
}

if ($style->direction === 'rtl') {
$t = $this->bidi_transform($t);
$replaceText = true;
}

if ($replaceText) {
$frame->set_text($t);
}

// Do we need to trim spaces on wrapped lines? This might be desired, however, we
Expand Down Expand Up @@ -338,6 +357,10 @@ protected function _layout_line()
$t = ltrim($t);
}

if ($style->direction === 'rtl') {
$t = $this->bidi_transform($t);
}

$frame->set_text($t);
}

Expand Down Expand Up @@ -512,4 +535,20 @@ public function calculate_auto_width()
{
return $this->_frame->recalculate_width();
}

protected function bidi_transform(string $text): string
{
// match (multiple) whitespace chars or one non whitespace char at start and end
$matched = preg_match('/^(\s+|[^\s]).*(\s+|[^\s])$/', $text, $keepWhitespace);

$bidi = new Bidi($text, null, null, 'R', true);
$text = $bidi->getString();

if ($matched) {
$text = (ctype_space($keepWhitespace[1]) ? $keepWhitespace[1] : '') . ltrim($text);
$text = rtrim($text) . (ctype_space($keepWhitespace[2]) ? $keepWhitespace[2] : '');
}

return $text;
}
}