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 @@ -31,7 +31,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
9 changes: 9 additions & 0 deletions src/Css/Style.php
Original file line number Diff line number Diff line change
Expand Up @@ -1992,6 +1992,15 @@ protected function set_prop_color($prop, $color)
$this->_props_computed[$prop] = (is_array($munged_color) ? $munged_color["hex"] : $munged_color);
}

public function set_direction($direction)
{
$this->_props["direction"] = $direction;
$this->_prop_cache["direction"] = null;
if ($direction === 'rtl') {
$this->text_align = 'right';
}
bsweeney marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets color
*
Expand Down
38 changes: 35 additions & 3 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 @@ -219,7 +220,15 @@ 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);

if ($style->direction === 'rtl') {
$bidi = new Bidi($text, null, null, 'R', false);
$text = $bidi->getString();
}

$frame->set_text($text);

if ($text == "") {
break;
}
Expand All @@ -228,15 +237,32 @@ protected function _layout_line()
break;

case "pre":
if ($style->direction === 'rtl') {
$bidi = new Bidi($text, null, null, 'R', false);
$text = $bidi->getString();
}

$split = $this->_newline_break($text);
$add_line = $split !== false;
break;

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

if ($style->direction === 'rtl') {
$bidi = new Bidi($text, null, null, 'R', false);
$text = $bidi->getString();
}

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

case "pre-wrap":
if ($style->direction === 'rtl') {
$bidi = new Bidi($text, null, null, 'R', false);
$text = $bidi->getString();
}

$split = $this->_newline_break($text);

if (($tmp = $this->_line_break($text)) !== false) {
Expand All @@ -249,7 +275,13 @@ 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);

if ($style->direction === 'rtl') {
$bidi = new Bidi($text, null, null, 'R', false);
$text = $bidi->getString();
}
$frame->set_text($text);

if ($text == "") {
break;
Expand Down