Skip to content

Commit

Permalink
apply BIDI algo on prepareText
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaasuni committed Mar 20, 2024
1 parent 0f39e09 commit e70300f
Showing 1 changed file with 34 additions and 10 deletions.
44 changes: 34 additions & 10 deletions src/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ abstract class Text extends \Com\Tecnick\Pdf\Cell
'height' => 0,
];

/**
* Estimate the number of rows required to print a text string on a retangular block.
*
* @param string $txt Text string to be processed.
* @param float $width Max line width.
*/
public function getNumLines(string $txt, float $width = 0): int
{
if ($txt === '') {
return 0;
}

return 1; // @TODO
}

/**
* Returns the PDF code to render a line of text.
*
Expand Down Expand Up @@ -101,7 +116,7 @@ public function getTextLine(

$ordarr = [];
$dim = [];
$this->prepareText($txt, $ordarr, $dim);
$this->prepareText($txt, $ordarr, $dim, $forcedir);

return $this->getOutTextLine(
$txt,
Expand Down Expand Up @@ -216,21 +231,29 @@ protected function getOutTextLine(
/**
* Cleanup the input text, convert it to UTF-8 array and get the dimensions.
*
* @param string $txt Clean text string to be processed.
* @param array<int, int> $ordarr Array of UTF-8 codepoints (integer values).
* @param TTextDims $dim Array of dimensions
* @param string $txt Clean text string to be processed.
* @param array<int, int> $ordarr Array of UTF-8 codepoints (integer values).
* @param TTextDims $dim Array of dimensions
* @param string $forcedir If 'R' forces RTL, if 'L' forces LTR.
*/
protected function prepareText(
string &$txt,
array &$ordarr,
array &$dim
array &$dim,
string $forcedir = '',
): void {
if ($txt === '') {
return;
}

$txt = $this->cleanupText($txt);
$ordarr = $this->uniconv->strToOrdArr($txt);

if ($this->isunicode && !$this->font->isCurrentByteFont()) {
$bidi = new Bidi($txt, null, $ordarr, $forcedir);
$ordarr = $this->replaceUnicodeChars($bidi->getOrdArray());
}

$dim = $this->font->getOrdArrDims($ordarr);
}

Expand Down Expand Up @@ -354,8 +377,7 @@ protected function getJustifiedString(
if ($this->font->isCurrentByteFont()) {
$txt = $this->uniconv->latinArrToStr($this->uniconv->uniArrToLatinArr($ordarr));
} else {
$bidi = new Bidi($txt, null, $ordarr, $forcedir);
$unistr = $this->replaceUnicodeChars($bidi->getString());
$unistr = implode('', $this->uniconv->ordArrToChrArr($ordarr));
$txt = $this->uniconv->toUTF16BE($unistr);
}

Expand Down Expand Up @@ -586,12 +608,14 @@ protected function getOutTextObject(string $raw = ''): string
/**
* Replace characters for languages like Thai.
*
* @param string $str String to process.
* @param array<int, int> $ordarr Array of UTF-8 codepoints (integer values).
*
* @return array<int, int> Array of UTF-8 codepoints (integer values).
*/
protected function replaceUnicodeChars(string $str): string
protected function replaceUnicodeChars(array $ordarr): array
{
// @TODO
return $str;
return $ordarr;
}

/**
Expand Down

0 comments on commit e70300f

Please sign in to comment.