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

Support character based line wrapping (#649) #657

Merged
merged 7 commits into from
Feb 28, 2023

Conversation

gmischler
Copy link
Collaborator

e.g. Fixes #649

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

multi_cell() and write() now have a wrapmode argument, which can be "WORD" or "CHAR".

  • "WORD" is the default and works as before.
  • With "CHAR" all whitespace gets treated as normal characters and wrapping occurs at the first character that doesn't fit anymore. Trailing spaces on a line get removed to enable correct justification.

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

@codecov
Copy link

codecov bot commented Jan 2, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.02 🎉

Comparison is base (1093622) 93.68% compared to head (102752c) 93.71%.

❗ Current head 102752c differs from pull request most recent head f479430. Consider uploading reports for the commit f479430 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #657      +/-   ##
==========================================
+ Coverage   93.68%   93.71%   +0.02%     
==========================================
  Files          26       26              
  Lines        6923     6949      +26     
  Branches     1235     1240       +5     
==========================================
+ Hits         6486     6512      +26     
  Misses        260      260              
  Partials      177      177              
Impacted Files Coverage Δ
fpdf/enums.py 100.00% <100.00%> (ø)
fpdf/fpdf.py 91.71% <100.00%> (+<0.01%) ⬆️
fpdf/line_break.py 99.60% <100.00%> (+0.03%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

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.

Good job @gmischler!

@@ -42,7 +42,7 @@ def test_load_invalid_base64_data():


# ensure memory usage does not get too high - this value depends on Python version:
@memunit.assert_lt_mb(141)
@memunit.assert_lt_mb(200)
Copy link
Member

Choose a reason for hiding this comment

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

Please do not bump this above 150MB, it's not necessary

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What's the point?
The demand will rise with every new test added, and that seems unlikely to get fixed in pytest any time soon.
Is there any real disadvantage of adding some extra headroom here?

Copy link
Member

Choose a reason for hiding this comment

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

I'm still not sure this is due to pytest... Investigation is ongoing in #641

The point is that keeping those thresholds allows me to keep track of how fast / what causes memory usage increases.
So far they helped me to link the growing number of tests with this memory usage, but this could be spurious-correlation...
As long as the increasing memory usage problem is not solved, I'd prefer to keep those checks a low as possible, in hope this will help pinpoint the origin of the issue

A manual break happens any time the \\n character is met,
Upon method exit, the current position is left near the end of the text, ready for the next call to continue without a gap, potentially with a different font or size set. Returns a boolean indicating if page break was triggered.
space or soft-hyphen character (in word wrap mode) or at the current position (in
character break mode), and text continues from the left margin.
Copy link
Member

Choose a reason for hiding this comment

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

Similarly to the "Right-to-Left & Arabic Script workaround" section above,
maybe an explicit suggestion could be made of using wrapmode='CHAR' for langages like chinese or japanese that do not separate words with spaces?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The whole section is about limitations (and their workarounds) right now, which this really isn't.
But we can turn it into a general typography and language specifics section.

Copy link
Member

Choose a reason for hiding this comment

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

But we can turn it into a general typography and language specifics section.

Yes, this would be great!

CHANGELOG.md Show resolved Hide resolved
@Lucas-C Lucas-C changed the title Support character based line warpping (#649) Support character based line wrapping (#649) Jan 2, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Jan 2, 2023

You will need to rebase and fix some minor merge conflicts in CHANGELOG.md I think

I'm going to wait for this PR and then I'll publish a new release

@Lucas-C
Copy link
Member

Lucas-C commented Jan 9, 2023

Hi @gmischler!

Do you think you will have time to add the final touches to this PR in the close future?
No pressure, but I'm willing to handle them if you don't have time for this, in order to perform a new release after that 😊

@gmischler
Copy link
Collaborator Author

Do you think you will have time to add the final touches to this PR in the close future?

Sorry, I'm out of the country until end of next week. Will get to it after that.

@Lucas-C
Copy link
Member

Lucas-C commented Jan 11, 2023

OK, no worries, it can wait 😊
Thank you for your answer

@Lucas-C
Copy link
Member

Lucas-C commented Feb 24, 2023

Hi @gmischler!

Do you plan to continue this PR in the future? 😊
This would be a nice addition!

@gmischler
Copy link
Collaborator Author

Do you plan to continue this PR in the future?

Yes I do. Actually, I was about to get back to it in short order...

@gmischler
Copy link
Collaborator Author

As far as I am concerned, this is ready now.

Interesting that we're also running into a timeout here now:

2023-02-24T21:48:21.6096581Z +++++++++++++++++++++++++++++++++++ Timeout ++++++++++++++++++++++++++++++++++++
2023-02-24T21:48:21.6096765Z test/test_perfs.py::test_intense_image_rendering 

I have made it a habit of excluding test_perfs locally for months now, because it very regularly fails to complete in time...

@Lucas-C
Copy link
Member

Lucas-C commented Feb 24, 2023

As far as I am concerned, this is ready now.

Great! 😊
Could you please rebase your branch, so that the https://github.com/PyFPDF/fpdf2/pull/657/files diff only displays your changes?

Interesting that we're also running into a timeout here now:

2023-02-24T21:48:21.6096581Z +++++++++++++++++++++++++++++++++++ Timeout ++++++++++++++++++++++++++++++++++++
2023-02-24T21:48:21.6096765Z test/test_perfs.py::test_intense_image_rendering 

I have made it a habit of excluding test_perfs locally for months now, because it very regularly fails to complete in time...

Could you try removing PYTHONMALLOCSTATS from .github/workflows/continuous-integration-workflow.yml,
as I did in https://github.com/PyFPDF/fpdf2/pull/703/files#diff-59c82b60a237174fa32aa0d97791d34cd322b4f4d7a70cf9d26274e0d1d6945c?
This was an experiment from 3 days ago (#704), but it turned out to slow down tests and to expand the logs too much.

Apart from that and from the memory caps, test/test_perfs.py has been passing fine in the GitHub Actions pipeline for the past months, including in the latest PR #701, so there may be a reason if it stopped passing.

@Lucas-C Lucas-C mentioned this pull request Feb 27, 2023
5 tasks
@gmischler
Copy link
Collaborator Author

Could you please rebase your branch, so that the https://github.com/PyFPDF/fpdf2/pull/657/files diff only displays your changes?

My branch is up to date and only shows my own changes over there. No idea why ist shows so many other changes here.

Could you try removing PYTHONMALLOCSTATS from .github/workflows/continuous-integration-workflow.yml,

That seems to have done the trick.

@Lucas-C
Copy link
Member

Lucas-C commented Feb 27, 2023

My branch is up to date and only shows my own changes over there. No idea why ist shows so many other changes here.

This may be because there were "Merge commits" performed on your master branch:
master...gmischler:fpdf2:WrapByChar

If you don't mind, a simple solution could be to squash all the commits in this PR into a single commit 😊

@Lucas-C Lucas-C mentioned this pull request Feb 27, 2023
5 tasks
@gmischler
Copy link
Collaborator Author

Ok, after the recent changes to master, a new rebase seems to have done the trick.

@Lucas-C , as far as I can tell it's now ready to merge.

@Lucas-C Lucas-C merged commit ca2f8b0 into py-pdf:master Feb 28, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Feb 28, 2023

Merged!

Thank you @gmischler 😊

@gmischler gmischler deleted the WrapByChar branch March 1, 2023 13:23
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.

Generate an empty line if one long line begin with some spaces
2 participants