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 for using wrapmode='CHAR' in templates #1176

Merged
merged 10 commits into from
May 27, 2024
Merged

Conversation

carlhiggs
Copy link

@carlhiggs carlhiggs commented May 18, 2024

Fixes #1159

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

In the flextemplate test for the wrapmode functionality, I tested a range of possibilities described in text of each template element. The first three elements test that the new code doesn't do anything strange to existing funcitonality; the last one specifically tests that wrapmode='CHAR' wraps accordingly:

elements = [
    {
        "name": "multi",
        "type": "T",
        "x1": 80,
        "y1": 10,
        "x2": 170,
        "y2": 15,
        "text": "If multiline is False, the text should still not wrap even if wrapmode is specified.",
        "background": 0xEEFFFF,
        "multiline": False,
        "wrapmode": "WORD",
    },
    {
        "name": "multi",
        "type": "T",
        "x1": 80,
        "y1": 40,
        "x2": 170,
        "y2": 45,
        "text": "If multiline is True, and wrapmode is omitted, the text should wrap by word and not "
                "cause an error due to omission of the wrapmode argument.",
        "background": 0xEEFFFF,
        "multiline": True,
    },
    {
        "name": "multi",
        "type": "T",
        "x1": 80,
        "y1": 70,
        "x2": 170,
        "y2": 75,
        "text": "If multiline is True and the wrapmode argument is provided, it should not cause a "
            "problem even if using the default wrapmode of 'WORD'.",
        "background": 0xEEFFFF,
        "multiline": True,
        "wrapmode": "WORD",
    },
    {
        "name": "multi",
        "type": "T",
        "x1": 80,
        "y1": 100,
        "x2": 170,
        "y2": 105,
        "text": "If multiline is True and the wrapmode argument is provided, it should result in "
            "wrapping based on characters instead of words, regardless of language (i.e. even though "
            "this is designed to support scripts like Chinese and Japanese, wrapping long sentences "
            "like this in English still demonstrates functionality.)",
        "background": 0xEEFFFF,
        "multiline": True,
        "wrapmode": "CHAR",
    },
]
pdf = FPDF()
pdf.add_page()
pdf.set_font("courier", "", 10)
templ = FlexTemplate(pdf, elements)
templ.render()
assert_pdf_equal(pdf, HERE / "flextemplate_wrapmode.pdf", tmp_path)

The output of the above looks correct to me:
image

I ran black and checked pylint in vscode; I believe the changes I made should be all fine for these requirements.

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

Copy link
Collaborator

@gmischler gmischler left a comment

Choose a reason for hiding this comment

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

Nice and simple feature addition, thanks!

As you probably already noticed, creating tests for something like that is more work than the actual fix. And even more of those are needed here...

PS: There's this annoying failure in the test suite that keeps popping up, apparently from running black, without actually telling us what the problem is... This is most likely not your fault, but something that we need to fix in the internal pipeline.

docs/Templates.md Show resolved Hide resolved
test/template/test_flextemplate.py Show resolved Hide resolved
@carlhiggs carlhiggs requested a review from gmischler May 20, 2024 04:32
Copy link
Collaborator

@gmischler gmischler left a comment

Choose a reason for hiding this comment

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

Once we can sort out the issues with the test suite, then this looks ready for merging.

Thanks for the contribution, @carlhiggs !

"y1": 100,
"x2": 170,
"y2": 105,
"text": "If multiline is True and the wrapmode argument is provided, it should result in "
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor nitpick: instead of
the wrapmode argument is provided
this whould read
the wrapmode is "CHAR"
or it might confuse some future reader.

While this is technically test data, it also serves as a code comment here, and those should never contradict the actual code.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks @gmischler I've just made this update!

test/template/test_flextemplate.py Show resolved Hide resolved
@gmischler
Copy link
Collaborator

@allcontributors please add @carlhiggs for code

Copy link

@gmischler

I've put up a pull request to add @carlhiggs! 🎉

…duplicate elements in template and flextemplate tests for optional charwrap functionality, as per py-pdf#1176
@carlhiggs
Copy link
Author

carlhiggs commented May 27, 2024

I've just addressed latest round of review requests: more accurately referring to when 'wrapmode is "CHAR"' in one specific test's self-descriptive text (and updated test pdfs used to validate this to have the changed text too), and externalising duplicate text to a shared python file containing elements now used in both template and flextemplate tests of the optional wrapmode functionality. Black and pylint were run using CLI, and through pre-commit hook. The pylint warnings that remained did not relate to my tests or modifications, but to various "pylint.extensions" that apparently were impossible to load ("bad_plugin-value"?). I believe that is not an issue with this pull request, but something upstream. If this passes checks, do you think this could be could to merge @gmischler ?

@gmischler
Copy link
Collaborator

Thanks for the contribution, @carlhiggs !
Merging now.

@gmischler gmischler merged commit eabddee into py-pdf:master May 27, 2024
11 checks passed
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.

Option to define wrapmode="CHAR" within a template
2 participants