Skip to content

Commit

Permalink
MAGETWO-85660: #11743: [GitHub] AbstractPdf - ZendException font is n…
Browse files Browse the repository at this point in the history
…ot set #1016
  • Loading branch information
Oleksii Korshenko authored Dec 22, 2017
2 parents 322bb01 + edf2b41 commit e462a63
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 19 deletions.
62 changes: 43 additions & 19 deletions app/code/Magento/Sales/Model/Order/Pdf/AbstractPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ public function newPage(array $settings = [])
* feed int; x position (required)
* font string; font style, optional: bold, italic, regular
* font_file string; path to font file (optional for use your custom font)
* font_size int; font size (default 7)
* font_size int; font size (default 10)
* align string; text align (also see feed parametr), optional left, right
* height int;line spacing (default 10)
*
Expand Down Expand Up @@ -1005,24 +1005,8 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet
foreach ($lines as $line) {
$maxHeight = 0;
foreach ($line as $column) {
$fontSize = empty($column['font_size']) ? 10 : $column['font_size'];
if (!empty($column['font_file'])) {
$font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
$page->setFont($font, $fontSize);
} else {
$fontStyle = empty($column['font']) ? 'regular' : $column['font'];
switch ($fontStyle) {
case 'bold':
$font = $this->_setFontBold($page, $fontSize);
break;
case 'italic':
$font = $this->_setFontItalic($page, $fontSize);
break;
default:
$font = $this->_setFontRegular($page, $fontSize);
break;
}
}
$font = $this->setFont($page, $column);
$fontSize = $column['font_size'];

if (!is_array($column['text'])) {
$column['text'] = [$column['text']];
Expand All @@ -1033,6 +1017,8 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet
foreach ($column['text'] as $part) {
if ($this->y - $lineSpacing < 15) {
$page = $this->newPage($pageSettings);
$font = $this->setFont($page, $column);
$fontSize = $column['font_size'];
}

$feed = $column['feed'];
Expand Down Expand Up @@ -1066,4 +1052,42 @@ public function drawLineBlocks(\Zend_Pdf_Page $page, array $draw, array $pageSet

return $page;
}

/**
* Set page font.
*
* column array format
* font string; font style, optional: bold, italic, regular
* font_file string; path to font file (optional for use your custom font)
* font_size int; font size (default 10)
*
* @param \Zend_Pdf_Page $page
* @param array $column
* @return \Zend_Pdf_Resource_Font
* @throws \Zend_Pdf_Exception
*/
private function setFont($page, &$column)
{
$fontSize = empty($column['font_size']) ? 10 : $column['font_size'];
$column['font_size'] = $fontSize;
if (!empty($column['font_file'])) {
$font = \Zend_Pdf_Font::fontWithPath($column['font_file']);
$page->setFont($font, $fontSize);
} else {
$fontStyle = empty($column['font']) ? 'regular' : $column['font'];
switch ($fontStyle) {
case 'bold':
$font = $this->_setFontBold($page, $fontSize);
break;
case 'italic':
$font = $this->_setFontItalic($page, $fontSize);
break;
default:
$font = $this->_setFontRegular($page, $fontSize);
break;
}
}

return $font;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Model\Order\Pdf;

use PHPUnit_Framework_MockObject_MockObject as MockObject;

/**
* Tests Sales Order PDF abstract model.
*
* @see \Magento\Sales\Model\Order\Pdf\AbstarctPdf
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractPdfTest extends \PHPUnit\Framework\TestCase
{
/**
* Tests Draw lines method.
* Test case when text block cover more than one page.
*/
public function testDrawLineBlocks()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

// Setup most constructor dependencies
$paymentData = $objectManager->create(\Magento\Payment\Helper\Data::class);
$string = $objectManager->create(\Magento\Framework\Stdlib\StringUtils::class);
$scopeConfig = $objectManager->create(\Magento\Framework\App\Config\ScopeConfigInterface::class);
$filesystem = $objectManager->create(\Magento\Framework\Filesystem::class);
$config = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Config::class);
$pdfTotalFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\Total\Factory::class);
$pdfItemsFactory = $objectManager->create(\Magento\Sales\Model\Order\Pdf\ItemsFactory::class);
$locale = $objectManager->create(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class);
$translate = $objectManager->create(\Magento\Framework\Translate\Inline\StateInterface::class);
$addressRenderer = $objectManager->create(\Magento\Sales\Model\Order\Address\Renderer::class);

// Test model
/** @var \Magento\Sales\Model\Order\Pdf\AbstractPdf|MockObject $model */
$model = $this->getMockForAbstractClass(
\Magento\Sales\Model\Order\Pdf\AbstractPdf::class,
[
$paymentData,
$string,
$scopeConfig,
$filesystem,
$config,
$pdfTotalFactory,
$pdfItemsFactory,
$locale,
$translate,
$addressRenderer,
],
'',
true,
true,
true,
['getPdf', '_getPdf']
);
$pdf = new \Zend_Pdf();
$model->expects($this->any())->method('getPdf')->will($this->returnValue($pdf));
$model->expects($this->any())->method('_getPdf')->will($this->returnValue($pdf));

/** Generate multiline block, that cover more than one page */
$lines = [];
for ($lineNumber = 1; $lineNumber <= 100; $lineNumber++) {
$lines[] = [[
'feed' => 0,
'font_size' => 10,
'text' => 'Text line ' . $lineNumber,
]];
}
$draw = [[
'height' => 12,
'lines' => $lines,
]];

$page = $model->newPage(['page_size' => \Zend_Pdf_Page::SIZE_A4]);

$model->drawLineBlocks($page, $draw);
$this->assertEquals(
3,
count($pdf->pages)
);
}
}

0 comments on commit e462a63

Please sign in to comment.