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

Allow keep_aspect_ratio flag in templates. #1119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Clock-Speed
Copy link

@Clock-Speed Clock-Speed commented Feb 21, 2024

Fixes #1118
Allows for using the keep_aspect_ratio flag from the image method to templates.
Example use:

tmpl = [
        {
            "name": "box",
            "type": "B",
            "x1": 0,
            "y1": 0,
            "x2": 50,
            "y2": 25,
        },
        {
            "name": "img",
            "type": "I",
            "x1": 0,
            "y1": 0,
            "x2": 50,
            "y2": 25,
            "keep_aspect_ratio": True,
        },
    ]

    img = qrcode.make("Test keep_aspect_ratio").get_image()

    pdf = FPDF()
    pdf.add_page()
    templ1 = FlexTemplate(pdf, tmpl)
    templ1["img"] = img
    templ1.render(offsetx=20, offsety=20)

Which gives the following output:
keep_aspect_ratio=true.pdf
Compared to when keep_aspect_ratio is set to False:
keep_aspect_ratio=false.pdf

Checklist:

  • The GitHub pipeline is OK (green),
    meaning that both pylint (static code analyzer) and black (code formatter) are happy with the changes of this PR.

  • A unit test is covering the code added / modified by this PR

  • This PR is ready to be merged

  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder

  • A mention of the change is present in CHANGELOG.md

By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

Add appropriate tests and update documentation.
Copy link
Member

@Lucas-C Lucas-C left a comment

Choose a reason for hiding this comment

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

Your PR is really nice!

Could please also add a mention of this addition in CHANGELOG.md?

@@ -84,6 +84,7 @@ def load_elements(self, elements):
"priority": int,
"multiline": (bool, type(None)),
"rotate": (int, float),
"keep_aspect_ratio": object, # "bool or equivalent",
Copy link
Member

Choose a reason for hiding this comment

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

Why not typing this as bool?

Copy link
Author

Choose a reason for hiding this comment

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

I was mostly just a little confused here with the types, as other fields also seem to be flags but are typed as objects (eg. bold & italic). Seems that "multiline" is also possibly a bool but it is typed as (bool, type(None)) for some reason.

Copy link
Member

Choose a reason for hiding this comment

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

@gmischler just a ping for you on this subject, as you have more experience than me with the templating system 🙂

Copy link
Collaborator

@gmischler gmischler Feb 22, 2024

Choose a reason for hiding this comment

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

It looks like I added this in #244.
The motivation was to allow other values with a True/False meaning, similar to what most library functions in Python allow. The documentation also specifies "enabled with True or equivalent value".
If there is a better/more explicit way to specify this, I haven't found it yet...

The other reason was that bold/italic/underline can also be specified in in a csv file, which uses ints instead of bools (more precisely: strings representing ints). This reason does not apply to keep_aspect_ratio, but the first one still does.

Copy link
Member

Choose a reason for hiding this comment

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

Alright!
So what do you think is the best there? int? (bool, int)?

IMHO we could stick to bool even if non-boolean "truthy" values are also valid, this would make the intent of this parameter clearer.

Copy link
Collaborator

Choose a reason for hiding this comment

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

IMHO we could stick to bool even if non-boolean "truthy" values are also valid, this would make the intent of this parameter clearer.

If we use bool, then we need an additional conversion (first from str to int, then to bool) for CSV files.

You'd expect there to be a standard way of expressing "bool or True/False equivalent", wouldn't you?
A quick search has not turned up anything useful for me...

Maybe just using bool is really the simplest way, although that is technically a backwards incompatible change.

@@ -190,6 +191,7 @@ def _varsep_float(s, default="0"):
("priority", int, False),
("multiline", self._parse_multiline, False),
("rotate", _varsep_float, False),
("keep_aspect_ratio", int, False),
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
("keep_aspect_ratio", int, False),
("keep_aspect_ratio", bool, False),

Copy link
Author

Choose a reason for hiding this comment

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

Same as above, the converter is int here in order to maintain similarity to the other boolean flags. Would need to test if this behaviour works (though I cannot see why it shouldn't).

Copy link
Collaborator

@gmischler gmischler Feb 22, 2024

Choose a reason for hiding this comment

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

Changing bold/italic/underline to using bool would require additional conversion magic when reading CSVs. Since we're evaluating strings there, currently int("0") evaluates correctly as False, while bool("0") would incorrectly evaluate to True.
It might be helpful to add some comments to the code explaining this...

Of couse, since keep_aspect_ratio is not supported via CSV, that one can use bool without a problem.

Copy link
Member

@Lucas-C Lucas-C Feb 22, 2024

Choose a reason for hiding this comment

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

Agreed!

@Lucas-C
Copy link
Member

Lucas-C commented Feb 27, 2024

Do you have enough information in our answers to adress the review feedbacks @Clock-Speed? 🙂

@Lucas-C
Copy link
Member

Lucas-C commented Feb 28, 2024

@allcontributors please add @Clock-Speed for code

Copy link

@Lucas-C

I've put up a pull request to add @Clock-Speed! 🎉

@Lucas-C
Copy link
Member

Lucas-C commented May 24, 2024

Hi!

Just a quick update about this: @Clock-Speed are you still willing to work on this feature?
If you don't, or do not have time for this anymore, that totally fine! 🙂
If so we will just close this PR.

Also, if you need feedback or help on this PR, please tell us! 🙂

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.

keep_aspect_ratio support for images in templates
3 participants