-
Notifications
You must be signed in to change notification settings - Fork 259
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
Leading spaces on new lines ignored in <pre><code> tags #1063
Comments
Here is my hack to fix this and boy is it a hack :) in html._new_paragraph add a parameter with a default of None. In the method if the param is not None and not the same as the current region value, then created a new region and cached the current. Then when creating the paragraph after ending the current check this value to supply for the constructor
When handling the start pre tag supply the new parameter to _new_paragraph
In endtag when handling the pre tag assert that there is a cached region and reset it to the default and clear out the cached member instance
|
Thanks for reporting this, @dmail00. It looks like you're right, and the space skipping feature conflicts with self._paragraph = self._column.paragraph(
text_align=align,
line_height=line_height,
skip_leading_spaces=not self._pre_formatted,
top_margin=top_margin,
bottom_margin=bottom_margin,
) We could also use some more tests in this area to cover the various possible combinations of tags. Would you be interested to submit a PR? |
Complicated yes, because there seems to be a conflict between the default value False and a False value being an unset value. https://github.com/py-pdf/fpdf2/blob/master/fpdf/text_region.py#L328 And then even if it did, ._end_paragraph() calls self._column.render() which calls self.collect_lines() which calls paragraph.build_lines which calls MultiLineBreak which still defaults to the regions value if the paragraphs value is False. https://github.com/py-pdf/fpdf2/blob/master/fpdf/text_region.py#L121 Hence why I swapped out the region and later swapped it back in. As I said it is a hack as I am really not sure the correct method to apply in a clean manner without messing other things up that rely on the False value in paragraph defaulting to the regions value.
|
Here is the same hack yet more confined to the _end_paragraph method and using @gmischler suggestion.
|
Ah I see, but then this is the underlying problem that we should really fix, instead of trying to work around it. The default value for the |
@gmischler Have you actually tried that code? |
No, I just tried to explain the concept. Skipping leading spaces was a relatively late and quick addition to the text region and line wrapping code, for the benefit of the HTML parser. At that time I simply made sure that the existing tests kept giving the correct results (in most cases: more correct than before). Unfortunately the existing tests didn't contain any leading whitespace in a |
Just to give a quick update on this topic: this is a confirmed bug, and we would welcome a PR solving this problem! 🙂 @dmail00 provided a good minimal reproduction case that could be easily converted into a unit test in |
Describe the bug
This problem seems to be related to issue 547 and the mininal example is the test case for it with a slight modification.
class HTML2FPDF creates a self.pdf.text_columns and stores it in self._column this sets the region to ignore leading spaces, which is sensible. The self._paragraph instance is set to the return value of self._column.paragraph().
self._column.paragraph() has a default for skip_leading_spaces as False.
When a pre tag is found in the method handle_starttag a new paragraph is created, no value is provided for skip_leading_spaces , however even if False was provided (which is the defeault) this would be ignored and it would default to the region value, which as I noted about is set sensibibly to False.
I did attempt to set the paragraphs value to False after calling _new_paragraph in the "pre" tag handling but this also has no effect. This is because when collect_lines calls paragraph.build_lines the paragraphs value is set to False but it will then default to the regions value.
Error details
N/A
Minimal code
Please include some minimal Python code reproducing your issue:
If you don't know how to build a minimal reproducible example, please check this tutorial: https://stackoverflow.com/help/minimal-reproducible-example
Environment
Please provide the following information:
fpdf2
version used: 2.7.7The text was updated successfully, but these errors were encountered: