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

wrong result from get_string_width #1009

Closed
matipos2 opened this issue Nov 8, 2023 · 5 comments
Closed

wrong result from get_string_width #1009

matipos2 opened this issue Nov 8, 2023 · 5 comments
Labels

Comments

@matipos2
Copy link

matipos2 commented Nov 8, 2023

It seems that function get_string_width() gives wrong result. It's always 2 points less than I'd expect.

Minimal code
Minimal Python code reproducing the issue (I couldn't find any information about that behavior in documentation):

import fpdf

print(f"fpdf version: {fpdf.FPDF_VERSION}")
pdf = fpdf.FPDF(orientation='portrait', format='A4')
pdf.set_font("helvetica", "I", 8)
pdf.add_page()
text = "THIS is Text to test width"
text_length = pdf.get_string_width(text)

first_x = pdf.get_x()
print(f"x={pdf.get_x()}, y={pdf.get_y()}")
print(f"get_string_width={text_length}")
pdf.cell(text=text)
print(f"new x={pdf.get_x()}, new y={pdf.get_y()}")
print(f"expected x={first_x + text_length}")

print("----- now with custom font -----")
pdf.add_font("OpenSans", style="", fname="OpenSans-Regular.ttf")
pdf.ln(10)
pdf.set_font("OpenSans", "", 8)
text_length = pdf.get_string_width(text)

first_x = pdf.get_x()
print(f"x={pdf.get_x()}, y={pdf.get_y()}")
print(f"get_string_width={text_length}")
pdf.cell(text=text)
print(f"new x={pdf.get_x()}, new y={pdf.get_y()}")
print(f"expected x={first_x + text_length}")

Here is the result:

fpdf version: 2.7.6
x=9.999999999999998, y=9.999999999999998
get_string_width=31.368999999999996
new x=43.36899999999999, new y=9.999999999999998
expected x=41.36899999999999
----- now with custom font -----
x=9.999999999999998, y=20.0
get_string_width=32.322911111111104
new x=44.322911111111104, new y=20.0
expected x=42.322911111111104
@matipos2 matipos2 added the bug label Nov 8, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Nov 9, 2023

Hi @matipos2 and welcome

What you are observing is the impact of the cell margin setting (.c_margin).

You get get rid of this difference simply by setting pdf.c_margin = 0

We should probably better document this.
Where in our documentation would expect this to be explained clearly?
Maybe would you like to contribute a PR to improve our documentation with a mention of this?

@matipos2
Copy link
Author

matipos2 commented Nov 9, 2023

Hi @Lucas-C thank you for quick response.
I would add this description to cell and multi_cell. I'm not sure if it applies to others:

Final width of the cell is extended by the interior cell margin (c_margin property). In the result it will get the size: c_margin + string width + c_margin.

And additional note to the description of get_string_width:

Please note that it calculates only the string width. The width of the cell (pdf.cell()) will be wider by interior cell margin (pdf.c_margin).

If you accept, I may prepare a PR later on.

@Lucas-C
Copy link
Member

Lucas-C commented Nov 10, 2023

Looks great !
A PR would be welcome 🙂

I would suggest those slights rewordings, to keep those docstrings short:

  • in the docstring for FPDF.cell() & FPDF.multi_cell():

A cell has an horizontal padding, on the left & right sides, defined by the.c_margin property.

  • in the docstring for get_string_width()

Note that the width of a cell has some extra padding added to this width, on the left & right sides, equal to the .c_margin property.

What do you think?

@matipos2
Copy link
Author

I like your version more. The PR is ready: #1012

@Lucas-C
Copy link
Member

Lucas-C commented Nov 13, 2023

I merged your PR, thank you for your contribution!

Closing this now

@Lucas-C Lucas-C closed this as completed Nov 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants