endroid / qr-code

QR Code Generator
https://endroid.nl
MIT License
4.41k stars 725 forks source link

Huge Notosans font file and strange behavior from fonts #352

Closed UVLabs closed 2 years ago

UVLabs commented 2 years ago

I noticed two things here:

1 - The Notosans font is adding 16 ! MB to your library and as far as I can tell the differences between open sans and notosans are minimal: http://www.identifont.com/differences?first=Open%20Sans&second=Noto%20Sans&p=1 So I think Notosans can be dropped all together.

2 - There is some weird behavior happening with the two fonts. I noticed that even though Notosans was being set as the font for the label. Opensans would actually be used, I checked the g and I characters to confirm this

$label = Label::create( $qr_code_label )
        ->setFont( new NotoSans( 18 ) );

But if i try to explicitly set Open sans as the font i want:

$label = Label::create( $qr_code_label )
        ->setFont( new OpenSans( 18 ) );

And delete the Notosans font from the library, it throws the exception error https://github.com/endroid/qr-code/blob/master/src/Label/Font/Font.php#L26 so it seems something isn't passing along somewhere

Can you confirm that the opensans font is always used regardless of setting Notosans, and can you possibly just drop NotoSans all together to reduce the size of the library? I can change the code to remove the validation line but I think many other people would not want 1 font to add 16MB to what they're building

endroid commented 2 years ago

Hi @UVLabs thank you. The open sans font has a much smaller character set, where noto sans also includes many non-european characters to make sure the default works for users all over the world. For instance, the text "有限公司" whould just be rendered as some blocks. So removing it would mean a BC break as text would not be rendered correctly anymore. I am open for suggestions if you have a smaller font that supports the same character set.

Regarding the Label class: you get this error because when you create a Label using the create method (which only accepts a text parameter) a Label instance is created with the NotoSans font. Since you removed it it will not find the path and throw an exception. For your local setup where the font is missing you could use the "new Label(...)" syntax to directly pass your font. Good luck!