Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param $left
- * @param $right
- * @param int $charSizeMultiply
- * @param bool $nowrap
- * @return FormattedRow[]
- */
- public function justifyRow($left, $right, $charSizeMultiply = null, $nowrap = false) {
- $maxLength = floor($this->_receiptWidth / ($charSizeMultiply ?? 1));
- $minLength = floor($maxLength / 2);
- $leftLen = mb_strlen($left);
- $rightLen = mb_strlen($right);
- $result = [];
- $sectionLength = $maxLength - $this->_alignmentJustifyOffset;
- if($leftLen + $rightLen > $sectionLength) {
- if($nowrap) {
- $leftRows = $this->divideTextByRow($left, $sectionLength);
- foreach ($leftRows as $n => $leftRow) {
- $leftRowLen = mb_strlen($leftRow);
- if($leftRowLen + $rightLen > $sectionLength && $n === 0) {
- $r = $this->formatRow($leftRow, self::ALIGNMENT_LEFT, $charSizeMultiply);
- } else {
- $r = $this->justifyRow($leftRow, $right, $charSizeMultiply, $nowrap);
- }
- $result = array_merge($result, $r);
- }
- } else {
- if($leftLen > $rightLen || $leftLen > $minLength) {
- $leftRows = $this->divideTextByRow($left, $sectionLength - $rightLen >= $minLength ? $sectionLength - $rightLen : $sectionLength);
- $leftRowsCount = sizeof($leftRows);
- foreach ($leftRows as $n => $leftRow) {
- if ($n === $leftRowsCount - 1) {
- $r = $this->justifyRow($leftRow, $right, $charSizeMultiply, $nowrap);
- } else {
- $r = $this->formatRow($leftRow, self::ALIGNMENT_LEFT, $charSizeMultiply);
- }
- $result = array_merge($result, $r);
- }
- } else {
- Yii::error(print_r(["'{$left}'", "'{$right}'", $minLength, $maxLength, $sectionLength], true));
- $rightRows = $this->divideTextByRow($right, $sectionLength - $leftLen >= $minLength ? $sectionLength - $leftLen : $sectionLength);
- foreach ($rightRows as $n => $rightRow) {
- if ($n === 0) {
- $r = $this->justifyRow($left, $rightRow, $charSizeMultiply, $nowrap);
- } else {
- $r = $this->formatRow($rightRow, self::ALIGNMENT_RIGHT, $charSizeMultiply);
- }
- $result = array_merge($result, $r);
- }
- }
- }
- } else {
- $result = $this->formatRow($left.str_repeat(' ', $maxLength - $leftLen - $rightLen).$right, self::ALIGNMENT_LEFT, $charSizeMultiply);
- }
- return $result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement