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

Implement SvgTextSizeCalculator #97

Merged

Conversation

antonkomarev
Copy link
Collaborator

This is only the first step towards stopping using GD for icon rendering. To completely remove the GD dependency, we need to make backward incompatible changes.

This PR introduces new SvgTextSizeCalculator class which will allow us to make proof of concept in a safe manner. In a future major versions we could define it as default fallback calculator.

Calculate badge width using GD (default):

$this->poser = new Poser([
    new SvgPlasticRender(),
    new SvgFlatRender(),
    new SvgFlatSquareRender(),
    new SvgForTheBadgeRenderer(),
]);

Calculate badge width using SVG (new):

$this->poser = new Poser([
    new SvgPlasticRender(new SvgTextSizeCalculator()),
    new SvgFlatRender(new SvgTextSizeCalculator()),
    new SvgFlatSquareRender(new SvgTextSizeCalculator()),
    new SvgForTheBadgeRenderer(null, new SvgTextSizeCalculator()),
]);

Note: SvgForTheBadgeRenderer using its own class to compute text width using EasySVG (where I got an inspiration), but it using GD too because of the parent method call. So to avoid using GD you should provide new calculator implementation as second argument.

@antonkomarev antonkomarev force-pushed the implement-svg-text-size-calculator branch from ab0a3bf to a541299 Compare October 11, 2023 21:30
@antonkomarev
Copy link
Collaborator Author

I'm going to review this code in the coming days, maybe it will be slightly modified. I really appreciate your feedback.

@antonkomarev antonkomarev changed the title Implement SvgTextSizeCalculator Draft: Implement SvgTextSizeCalculator Oct 11, 2023
@antonkomarev antonkomarev changed the title Draft: Implement SvgTextSizeCalculator Implement SvgTextSizeCalculator Oct 11, 2023
@antonkomarev antonkomarev marked this pull request as draft October 11, 2023 21:33
Copy link
Collaborator

@garak garak left a comment

Choose a reason for hiding this comment

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

The code is OK for me, but it needs to be tested

@antonkomarev
Copy link
Collaborator Author

I'm working on a new more cleaner implementation. Will return when it will be done.

@antonkomarev antonkomarev force-pushed the implement-svg-text-size-calculator branch 4 times, most recently from 7c44cae to 49bccd4 Compare October 16, 2023 19:50
@antonkomarev
Copy link
Collaborator Author

antonkomarev commented Oct 16, 2023

@garak I've reworked this PR. All the logic of parsing SVG fonts and unicode characters (code points) extracted to separated unit tested packages:

If you are OK with such solution I will continue to work towards this implementation.

Motivation behind this change: reading and parsing SVG fonts and unicode strings not that easy as it might look like. For example current solution does not allow us to use composition characters like composable emojis. We may add support later in separate package and do not change this package code, because solution may be very complicated.

Things to do before merge:

  • Cover with high level tests
  • Tweak API of reading fonts
  • Make calculator results same as GD one (if possible)
  • Make stable releases of dependencies

@antonkomarev
Copy link
Collaborator Author

antonkomarev commented Oct 16, 2023

I have one question about GD calculator. There is a code:

public function calculateWidth(string $text, int $size = self::TEXT_SIZE): float
{
    $size = $this->convertToPt($size);
    $box  = \imagettfbbox($size, 0, $this->fontPath, $text);

    return \round(\abs($box[2] - $box[0]) + self::SHIELD_PADDING_EXTERNAL + self::SHIELD_PADDING_INTERNAL, 1);
}

private function convertToPt(int $pixels): float
{
    return \round($pixels * 0.75, 1);
}

Why do we decreasing font size using round($pixels * 0.75, 1)?

Update: I suppose I found an answer. GD1 used pixels as first argument, GD2 uses points.

@antonkomarev antonkomarev force-pushed the implement-svg-text-size-calculator branch 4 times, most recently from f7a3e6d to bbadaa4 Compare January 4, 2024 13:11
@antonkomarev
Copy link
Collaborator Author

antonkomarev commented Jan 4, 2024

Tests are failing on code style. Same as in #101. It looks like CS Fixer issue, need fix it and re-run here.

@antonkomarev antonkomarev self-assigned this Jan 4, 2024
@antonkomarev
Copy link
Collaborator Author

The code is OK for me, but it needs to be tested

@garak I wrote tests for the new SvgTextCalculator and for GdTextCalculator (they were missing).

@antonkomarev antonkomarev marked this pull request as ready for review January 4, 2024 13:26
@garak
Copy link
Collaborator

garak commented Jan 5, 2024

Tests are failing on code style. Same as in #101. It looks like CS Fixer issue, need fix it and re-run here.

I guess you can easily fix them. If you want to save time, my advice is to run php-cs-fixer on your local environment before pushing your commit.

On a second thought, probably it's useless to have the CS checked inside versions. Please remove that part, let's keep only the initial global CS check

@antonkomarev
Copy link
Collaborator Author

@garak what part are you talking about? CS fails on infrastructure code, not committed one.

@garak
Copy link
Collaborator

garak commented Jan 5, 2024

@JellyBellyDev
Copy link
Collaborator

I'm fixing it here: #102

@JellyBellyDev
Copy link
Collaborator

Please rebase! ;)

@garak
Copy link
Collaborator

garak commented Jan 5, 2024

I'm fixing it here: #102

but you fixed the symptom, not the problem itself :-(

@antonkomarev antonkomarev force-pushed the implement-svg-text-size-calculator branch from bbadaa4 to 8ee8901 Compare January 5, 2024 18:23
@antonkomarev
Copy link
Collaborator Author

@garak @JellyBellyDev rebased, ready for review

@antonkomarev
Copy link
Collaborator Author

I'm fixing it here: #102

but you fixed the symptom, not the problem itself :-(

it is good as temporary solution, but later could be changed as well

@antonkomarev antonkomarev merged commit 6bc1998 into badges:master Jan 6, 2024
4 checks passed
@antonkomarev antonkomarev deleted the implement-svg-text-size-calculator branch January 6, 2024 18:14
@antonkomarev
Copy link
Collaborator Author

@garak @JellyBellyDev who can make minor release?

@JellyBellyDev
Copy link
Collaborator

Done! 😉

@antonkomarev
Copy link
Collaborator Author

antonkomarev commented Jan 7, 2024

@JellyBellyDev release does not appeared in Packagist: https://packagist.org/packages/badges/poser

@JellyBellyDev
Copy link
Collaborator

Now yes! 👍🏻

@JellyBellyDev
Copy link
Collaborator

I always forget that I don't have the privileges to set the package self-update hook on packagist and with each release I have to update it manually

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid using GD module to render SVG images
3 participants