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

Added PDF font switcher #46

Merged
merged 15 commits into from
Jan 17, 2019
Merged

Added PDF font switcher #46

merged 15 commits into from
Jan 17, 2019

Conversation

HirokazuNishi
Copy link
Collaborator

Added PDF font switcher module for Japanese. It can generate Invoice, Shipment, and Creditmemo with embedded Japanese font.

Copy link
Contributor

@ishakhsuvarov ishakhsuvarov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some general comments:

  • Which license is used by the included font files? Is it ok to distribute it?
  • It's worth spending some additional and make all included code completely compatible with PHP 7.1+ standards.

@@ -47,9 +47,11 @@
<fieldset id="sales_convert_quote_address">
<field name="firstnamekana">
<aspect name="to_order_address" />
<aspect name="to_customer_address" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change set for this file seems to be out of scope for this Pull Request.

/**
* @var string
*/
protected $_dir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current edition of Magento Technical Guidelines suggests to use composition over inheritance, therefore it may be better to change modifier to private.

* Class Data
* @package MagentoJapan\Pdf\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having Helper classes is not required and sometimes may even cause confusion for 3rd party developers to figure out the purpose of the class. I would suggest moving configuration reading logic to a separate model, with clear name reflecting it's purpose.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. @HirokazuNishi, to convert this class from helper to service class you may rename it to MagentoJapan\Pdf\ModelConfig and inject only scope config and dir reader as dependencies.

/**
* font file path
*/
const FONT_DIR = 'lib/fonts/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This constant might as well be made configurable via di.xml files.

/**
* @var string
*/
protected $_dir;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding underscore to private properties is more or less outdates convention. It's safe to use names without it.

/**
* font file path
*/
const FONT_DIR = 'lib/fonts/';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a similar constant used in the Helper. It would make sense to make it configurable from a single point.

protected function _setFontItalic($object, $size = 7)
{
if ($this->getHelper()->getJapaneseFontIsActive()) {
$fontpath = $this->getHelper()->getJapaneseFont();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part (following 3 lines) seems to be shared across multiple classes and methods. It makes sense to extract it.

* Class DataTest
* @package MagentoJapan\Pdf\Test\Unit\Helper
*/
class DataTest extends \PHPUnit_Framework_TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately PHPUnit version used by Magento has been updated and this would not be compatible anymore with 2.2 and 2.3

@ishakhsuvarov ishakhsuvarov self-assigned this Nov 30, 2018
@ishakhsuvarov
Copy link
Contributor

Hi @HirokazuNishi

Thank you for the PR. I've done a brief code review. Please feel free to take a look at the comments above.

If you don't mind I would like to collaborate and push some of the fixes to your branch as well.

Thanks!

* Class Data
* @package MagentoJapan\Pdf\Helper
*/
class Data extends \Magento\Framework\App\Helper\AbstractHelper

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. @HirokazuNishi, to convert this class from helper to service class you may rename it to MagentoJapan\Pdf\ModelConfig and inject only scope config and dir reader as dependencies.

\Magento\Framework\Module\Dir\Reader $dirReader
)
{
$this->_dir = $dirReader->getModuleDir('', 'MagentoJapan_Pdf') . '/' . self::FONT_DIR;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inside constructor please only assign dependencies to private properties if something should be calculated it should be done when some method called

*/
protected function _setFontRegular($object, $size = 7)
{
if ($this->getHelper()->getJapaneseFontIsActive()) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such approach creates a lot of code duplication and if some 3rd party module has own pdf generated documents it will not be compatible.

We may try to create after plugin on Magento\Framework\Filesystem\Directory\ReadInterface::getAbsolutePath which will substitute result fort particular font paths. As we may limit check by in_array function call I believe there should not be dramatical performance degradation.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to set plugin against Magento\Framework\Filesystem\Directory\ReadInterface, but Magento\Framework\Filesystem\Directory\ReadFactory has hardcoded class name for generating Read class instance. Seems it's impossible to set plugin against ReadInterface.

@HirokazuNishi
Copy link
Collaborator Author

IPA font is distributed under IPA font license.

add IPA Font License text
@ishakhsuvarov
Copy link
Contributor

Static test failures regarding Kana module will be fixed in the scope of appropriate Pull Request

@ishakhsuvarov ishakhsuvarov merged commit 73ad28b into magento:2.3-develop Jan 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants