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

Generate an empty line if one long line begin with some spaces #649

Closed
CY-Qiu opened this issue Dec 28, 2022 · 4 comments · Fixed by #657
Closed

Generate an empty line if one long line begin with some spaces #649

CY-Qiu opened this issue Dec 28, 2022 · 4 comments · Fixed by #657
Assignees
Labels

Comments

@CY-Qiu
Copy link

CY-Qiu commented Dec 28, 2022

Error details
Generate an empty line if one long line begin with some spaces.
This is the current output:
image
This is what we want:
image

Minimal code

from fpdf import FPDF

pdf = FPDF()
pdf.add_page()
pdf.set_text_color(0, 0, 0)
pdf.add_font(family="simsun", fname="/usr/share/fonts/simsun.ttf", uni=True)
pdf.set_font("simsun", size=10)
text = "       鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美鸡你太美"
pdf.multi_cell(w=150, txt=text, border=1, fill=False, split_only=False)
pdf.output("test.pdf")

Environment

  • fpdf2 version used: 2.6.0

I checked the code, it seems that line_break.py needs some changes to make the spaces in the beginning of one line being ignored.

@CY-Qiu CY-Qiu added the bug label Dec 28, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Dec 28, 2022

Thank you for opening this issue @CY-Qiu 😃

However, I'm not sure that this is really a bug with fpdf2...

Users can call str.strip() themselves on the text input passed to .cell() / .multi_cell(). For example you could change your second-to-last line to this:

pdf.multi_cell(w=150, txt=text.strip(), border=1, fill=False, split_only=False)

@CY-Qiu
Copy link
Author

CY-Qiu commented Dec 28, 2022

The reason I add some spaces is I need indentation.
In the current design of fpdf2, It seems that add some spaces in the beginning of one sentence is the only choice.
So I cannot use strip to delete the spaces.

@Lucas-C
Copy link
Member

Lucas-C commented Dec 28, 2022

Oh OK, I initially misunderstood...

Ping @gmischler as I think you have more expertise regarding this than myself 😃

Do you think that fpdf2 could/should honor leading whitespaces in .cell() / .multi_cell()?

@gmischler
Copy link
Collaborator

An interesting case!

The reason for the current behaviour is in the "line wrapping by word" algorithm, which assumes that words are seperated by space characters. In our case here, the supplied "word" does not fit on one line, so multi_cell() will perform a line break at the most recent space character.
This is evidently inconvenient for chinese writing (and japanese, possibly others), which does not usually seperate words with spaces, but places all the characters right after each other.

The most correct solution would be for fpdf2 to offer an alternative "line wrapping by character" algorithm. I don't think that would be very hard to implement (just skip the space and hyphen hinting), but we need to think a bit about what the best API for that would be.

Do you think that fpdf2 could/should honor leading whitespaces in .cell() / .multi_cell()?

Of course it should. The task of fpdf2 is to render the characters supplied by the client code, and not to alter them (unless explicitly specified). And that is exactly what happens here too: Those space characters are represented by the initial empty line. If the rest of the text was short enough to fit that line, it would continue right after that indent as expected.

On the other hand: Is ist a good idea to use space characters for a first-line indent?
In general typographical terms, doing so is not usually encouraged.
With mult_cell(), it is currently the only way, though. A first_line_indent=x feature could be another enhancement idea.
With write(), you could just move pdf.x to the indented position any time you start a new paragraph, as long as you're happy to have your text left aligned (for now, anyway).

gmischler added a commit to gmischler/fpdf2 that referenced this issue Jan 1, 2023
gmischler added a commit to gmischler/fpdf2 that referenced this issue Feb 24, 2023
gmischler added a commit to gmischler/fpdf2 that referenced this issue Feb 28, 2023
Lucas-C added a commit that referenced this issue Feb 28, 2023
Co-authored-by: Lucas Cimon <925560+Lucas-C@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants