Skip to content

Commit

Permalink
Rolling back changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
lcgeneralprojects committed Jun 15, 2024
1 parent 91feb6f commit b880e1d
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 20 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
* fixed incoherent indentation of long list entries - _cf._ [issue #1073](https://github.com/py-pdf/fpdf2/issues/1073)
* default values for `top_margin` and `bottom_margin` in `HTML2FPDF._new_paragraph()` calls are now correctly converted into chosen document units.
### Changed
* Removed an obscure and undocumented [feature](https://github.com/py-pdf/fpdf2/issues/1198) of [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html), which used to magically pass local variables as arguments.
* [`FPDF.table()`](https://py-pdf.github.io/fpdf2/Tables.html) now raises an error when a single row is too high to be rendered on a single page
* `HTML2FPDF.tag_indents` can now be non-integer. Indentation of HTML elements is now independent of font size and bullet strings.
* No spacing controlled by `HTML2FPDF.list_vertical_margin` is created for nested HTML `<li>` elements in contrast with prior respect for `Paragraph.top_margin` when handling `Paragraph`s created when handling `<ul>` and `<ol>` start tags.
Expand Down Expand Up @@ -672,4 +671,4 @@ prevented strings passed first to the text-rendering methods to be displayed.
### Modified
* turned `accept_page_break` into a property
* unit tests now use the standard `unittest` lib
* massive code cleanup using `flake8`
* massive code cleanup using `flake8`
1 change: 1 addition & 0 deletions docs/TextRegion.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ For more typographical control, you can use the following arguments. Most of tho
* indent (float, optional): determines the indentation of the paragraph. (Default: 0.0 mm)
* bullet_r_margin (float, optional) - determines the relative displacement of the bullet along the x-axis. The distance is between the rightmost point of the bullet to the leftmost point of the paragraph's text. (Default: 2.0 mm)
* bullet_string (str, optional): determines the fragments and text lines of the bullet. (Default: "")
* bullet_r_margin (float, optional): determines the spacing between the bullet and the bulleted line
* skip_leading_spaces (float, optional) - removes all space characters at the beginning of each line.
* wrapmode (WrapMode, optional)

Expand Down
23 changes: 10 additions & 13 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,25 +415,22 @@ def write_html(self, text, *args, **kwargs):
text (str): HTML content to render
image_map (function): an optional one-argument function that map <img> "src"
to new image URLs
li_tag_indent (int): [**DEPRECATED since v2.7.8**]
numeric indentation of <li> elements - Set tag_indents instead
dd_tag_indent (int): [**DEPRECATED since v2.7.8**]
numeric indentation of <dd> elements - Set tag_indents instead
li_tag_indent (int): [**DEPRECATED since v2.7.8**] numeric indentation of <li> elements - Set tag_indents instead
dd_tag_indent (int): [**DEPRECATED since v2.7.8**] numeric indentation of <dd> elements - Set tag_indents instead
table_line_separators (bool): enable horizontal line separators in <table>
ul_bullet_char (str): bullet character preceding <li> items in <ul> lists.
li_prefix_color (tuple | str | drawing.Device* instance):
color for bullets or numbers preceding <li> tags.
li_prefix_color (tuple | str | drawing.Device* instance): color for bullets or numbers preceding <li> tags.
This applies to both <ul> & <ol> lists.
heading_sizes (dict): [**DEPRECATED since v2.7.8**]
font size per heading level names ("h1", "h2"...) - Set tag_styles instead
pre_code_font (str): [**DEPRECATED since v2.7.8**]
font to use for <pre> & <code> blocks - Set tag_styles instead
heading_sizes (dict): [**DEPRECATED since v2.7.8**] font size per heading level names ("h1", "h2"...) - Set tag_styles instead
pre_code_font (str): [**DEPRECATED since v2.7.8**] font to use for <pre> & <code> blocks - Set tag_styles instead
warn_on_tags_not_matching (bool): control warnings production for unmatched HTML tags
tag_indents (dict):
mapping of HTML tag names to numeric values representing their horizontal left identation
tag_indents (dict): mapping of HTML tag names to numeric values representing their horizontal left identation
tag_styles (dict): mapping of HTML tag names to colors
"""
html2pdf = self.HTML2FPDF_CLASS(self, *args, **kwargs)
kwargs2 = vars(self)
# Method arguments must override class & instance attributes:
kwargs2.update(kwargs)
html2pdf = self.HTML2FPDF_CLASS(self, *args, **kwargs2)
html2pdf.feed(text)

def _set_min_pdf_version(self, version):
Expand Down
Binary file modified test/html/html_blockquote_indent.pdf
Binary file not shown.
Binary file modified test/html/html_customize_ul.pdf
Binary file not shown.
Binary file modified test/html/html_li_tag_indent.pdf
Binary file not shown.
163 changes: 158 additions & 5 deletions test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,27 @@ def test_html_customize_ul(tmp_path):
<li><b>term1</b>: definition1</li>
<li><b>term2</b>: definition2</li>
</ul>"""
pdf = FPDF()

# 1. Customizing through class attributes:
class CustomPDF(FPDF):
def __init__(self):
super().__init__()
self.li_tag_indent = 40
self.ul_bullet_char = "\x86"

pdf = CustomPDF()
pdf.set_font_size(30)
pdf.add_page()
with pytest.warns(DeprecationWarning): # li_tag_indent
# Customizing through optional method arguments:
for indent, bullet in ((5, "\x86"), (10, "\x9b"), (15, "\xac"), (20, "\xb7")):
pdf.write_html(html)
pdf.ln()
# 2. Customizing through instance attributes:
pdf.li_tag_indent = 60
pdf.ul_bullet_char = "\x9b"
pdf.write_html(html)
pdf.ln()
# 3. Customizing through optional method arguments:
for indent, bullet in ((80, "\xac"), (100, "\xb7")):
pdf.write_html(html, li_tag_indent=indent, ul_bullet_char=bullet)
pdf.ln()
assert_pdf_equal(pdf, HERE / "html_customize_ul.pdf", tmp_path)
Expand Down Expand Up @@ -637,15 +652,26 @@ def test_html_blockquote_indent(tmp_path): # issue-1074
pdf = FPDF()
pdf.add_page()
html = "Text before<blockquote>foo</blockquote>Text afterwards"
pdf.write_html(html, tag_indents={"blockquote": 5})
pdf.write_html(html, tag_indents={"blockquote": 20})
html = (
"<blockquote>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod"
"tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,"
"quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
"Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu"
"fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,"
"sunt in culpa qui officia deserunt mollit anim id est laborum.</blockquote>"
)
pdf.write_html(html, tag_indents={"blockquote": 40})
assert_pdf_equal(pdf, HERE / "html_blockquote_indent.pdf", tmp_path)


def test_html_li_tag_indent(tmp_path):
pdf = FPDF()
pdf.add_page()
with pytest.warns(DeprecationWarning):
pdf.write_html("<ul><li>item</li></ul>", li_tag_indent=10)
pdf.write_html("<ul><li>item 1</li></ul>", li_tag_indent=40)
pdf.write_html("<ul><li>item 2</li></ul>", li_tag_indent=50)
pdf.write_html("<ul><li>item 3</li></ul>", li_tag_indent=60)
assert_pdf_equal(pdf, HERE / "html_li_tag_indent.pdf", tmp_path)


Expand Down Expand Up @@ -686,3 +712,130 @@ def test_html_ol_ul_line_height(tmp_path):
</ul>"""
)
assert_pdf_equal(pdf, HERE / "html_ol_ul_line_height.pdf", tmp_path)


def test_html_long_list_entries(tmp_path):
pdf = FPDF()
pdf.add_page()
html = f"<ul><li>{'A ' * 200}</li></ul>"
pdf.write_html(html)
assert_pdf_equal(pdf, HERE / "html_long_list_entries.pdf", tmp_path)


def test_html_long_ol_bullets(tmp_path):
pdf = FPDF()
pdf.add_page()
html_arabic_indian = f"""
<ol start="{10**100}">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
"""
html_roman = f"""
<ol start="{10**5}" type="i">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
"""
pdf.write_html(html_arabic_indian)
pdf.write_html(html_roman, type="i")
pdf.write_html(html_arabic_indian, tag_indents={"li": 50})
pdf.write_html(html_roman, tag_indents={"li": 100})
assert_pdf_equal(pdf, HERE / "html_long_ol_bullets.pdf", tmp_path)


def test_html_measurement_units(tmp_path):
for unit in ["pt", "mm", "cm", "in"]:
pdf = FPDF(unit=unit)
pdf.add_page()
html = """
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
<blockquote>Blockquote text</blockquote>
<dt>Description title</dt>
<dd>Description details</dd>
"""
pdf.write_html(html)
assert_pdf_equal(pdf, HERE / "html_measurement_units.pdf", tmp_path)


def test_bulleted_paragraphs():
pdf = FPDF()
pdf.add_page()
text_columns = pdf.text_columns(skip_leading_spaces=True)
cases = [
{"indent": 1, "bullet_string": None},
{
"indent": 10,
"bullet_string": "",
"bullet_r_margin": 2,
},
{
"indent": 20,
"bullet_string": "a",
"bullet_r_margin": 0,
},
{
"indent": -20,
"bullet_string": "abcd",
"bullet_r_margin": 4,
},
{
"indent": 1000,
"bullet_string": "abcd\nfghi",
"bullet_r_margin": -3,
},
]
pdf.set_font("helvetica", "B", 16)
for case in cases:
try:
text_columns.paragraph(
indent=case.get("indent"),
bullet_string=case.get("bullet_string"),
bullet_r_margin=case.get("bullet_r_margin"),
)
text_columns.end_paragraph()
except FPDFException as error:
pytest.fail(
f"case: (indent: {case['indent']}, bullet_string: {case['bullet']})\n"
+ str(error)
)
bad_bullet_string = "我"
with pytest.raises(FPDFException) as error:
text_columns.paragraph(indent=1, bullet_string=bad_bullet_string)
expected_msg = (
f'Character "{bad_bullet_string}" at index {0} in text is outside the range of characters '
f'supported by the font used: "{pdf.font_family+pdf.font_style}". Please consider using a Unicode font.'
)
assert str(error.value) == expected_msg


def test_html_list_vertical_margin(tmp_path):
pdf = FPDF()
for margin_value in (None, 4, 8, 16):
pdf.add_page()
html = f"""
This page uses `list_vertical_margin` value of {margin_value}
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
<ol>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ol>
"""
pdf.write_html(html, list_vertical_margin=margin_value)
assert_pdf_equal(pdf, HERE / "html_list_vertical_margin.pdf", tmp_path)

0 comments on commit b880e1d

Please sign in to comment.