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

Resolves #1311 - <a> HTML tag applying styling rules to all following content #1316

Merged
merged 3 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ in order to get warned about deprecated features used in your code.
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.

## [2.8.3] - Not released yet
### Fixed
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): Fixed rendering of content following `<a>` tags; now correctly resets emphasis style post `</a>` tag: hyperlink styling contained within the tag authority. - [Issue #1311](https://github.com/py-pdf/fpdf2/issues/1311)

## [2.8.2] - 2024-12-16
### Added
Expand Down
23 changes: 6 additions & 17 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3243,7 +3243,7 @@ def _render_styled_text_line(
self._add_quad_points(self.x, self.y, w, h)

s_start = self.x
s_width, underlines = 0, []
s_width = 0
# We try to avoid modifying global settings for temporary changes.
current_ws = frag_ws = 0.0
current_lift = 0.0
Expand Down Expand Up @@ -3331,12 +3331,12 @@ def _render_styled_text_line(
initial_cs=i != 0
) + word_spacing * frag.characters.count(" ")
if frag.underline:
underlines.append(
(
sl.append(
self._do_underline(
self.x + dx + s_width,
self.y + (0.5 * h) + (0.3 * frag.font_size),
frag_width,
frag.font,
frag.font_size,
)
)
if frag.link:
Expand All @@ -3353,15 +3353,6 @@ def _render_styled_text_line(

sl.append("ET")

for start_x, ul_w, ul_font, ul_font_size in underlines:
sl.append(
self._do_underline(
start_x,
self.y + (0.5 * h) + (0.3 * ul_font_size),
ul_w,
ul_font,
)
)
if link:
self.link(
self.x + dx,
Expand Down Expand Up @@ -4014,13 +4005,11 @@ def multi_cell(
page_break_triggered = False

for text_line_index, text_line in enumerate(text_lines):
page_break_required = self.will_page_break(h + padding.bottom)
if page_break_required:
if self._perform_page_break_if_need_be(h + padding.bottom):
page_break_triggered = True
self._perform_page_break()
self.y += padding.top

if box_required and (text_line_index == 0 or page_break_required):
if box_required and (text_line_index == 0 or page_break_triggered):
# estimate how many cells can fit on this page
top_gap = self.y + padding.top
bottom_gap = padding.bottom + self.b_margin
Expand Down
4 changes: 2 additions & 2 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,7 @@ def feed(self, data):
LOGGER.warning("Missing HTML end tag for <%s>", self._tags_stack[-1])

def put_link(self, text):
"Put a hyperlink"
"Insert a hyperlink"
prev_style = FontFace(
family=self.font_family,
emphasis=self.font_emphasis,
Expand All @@ -1160,7 +1160,7 @@ def put_link(self, text):
# Restore previous style:
self.font_family = prev_style.family or self.font_family
self.font_size_pt = prev_style.size_pt or self.font_size_pt
self.font_emphasis |= prev_style.emphasis
self.font_emphasis = prev_style.emphasis
self.font_color = prev_style.color

# pylint: disable=no-self-use
Expand Down
Binary file modified test/encryption/encrypt_fonts.pdf
Binary file not shown.
Binary file modified test/fonts/fonts_otf.pdf
Binary file not shown.
Binary file modified test/graphics_context.pdf
Binary file not shown.
Binary file modified test/html/html_bold_italic_underline.pdf
Binary file not shown.
Binary file modified test/html/html_features.pdf
Binary file not shown.
Binary file modified test/html/html_format_within_p.pdf
Binary file not shown.
Binary file modified test/html/html_link_style.pdf
Binary file not shown.
Binary file added test/html/html_link_underline.pdf
Binary file not shown.
14 changes: 14 additions & 0 deletions test/html/test_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,20 @@ def test_html_and_section_title_styles_with_deprecated_TitleStyle():
)


def test_html_link_underline(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.write_html(
'<a href="https://py-pdf.github.io/fpdf2/">inside link</a> - outside link'
)
pdf.write_html(
'<a href="https://www.flickr.com/photos/ryzom/14726336322/in/album-72157645935788203/">Tryker</a>'
" - Ryzom"
' - <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA 2.0</a>'
)
assert_pdf_equal(pdf, HERE / "html_link_underline.pdf", tmp_path)


def test_html_link_style(tmp_path):
pdf = FPDF()
pdf.add_page()
Expand Down
Binary file modified test/hyperlinks.pdf
Binary file not shown.
Binary file modified test/internal_links.pdf
Binary file not shown.
Binary file modified test/link_to_other_document.pdf
Binary file not shown.
Binary file modified test/new_page_graphics_state.pdf
Binary file not shown.
Binary file modified test/outline/2_pages_outline.pdf
Binary file not shown.
Binary file modified test/outline/html_toc.pdf
Binary file not shown.
Binary file modified test/outline/html_toc_2_pages.pdf
Binary file not shown.
Binary file not shown.
Binary file modified test/outline/simple_outline.pdf
Binary file not shown.
16 changes: 6 additions & 10 deletions test/outline/test_outline.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,20 @@ def test_incoherent_start_section_hierarchy():
def test_start_section_horizontal_alignment(tmp_path): # issue-1282
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", "", 20)
pdf.set_font("Helvetica", size=20)

# left align
level0 = TextStyle("Helvetica", "", 20, (0, 0, 0), l_margin=Align.L)
level0 = TextStyle(font_size_pt=20, l_margin=Align.L)
pdf.set_section_title_styles(level0)
pdf.start_section("left aligned section")

# center align
level0 = TextStyle("Helvetica", "", 20, (0, 0, 0), l_margin=Align.C)
level0 = TextStyle(font_size_pt=20, l_margin=Align.C)
pdf.set_section_title_styles(level0)
pdf.start_section("center aligned section")

# right align
level0 = TextStyle("Helvetica", "", 20, (0, 0, 0), l_margin=Align.R)
level0 = TextStyle(font_size_pt=20, l_margin=Align.R)
pdf.set_section_title_styles(level0)
pdf.start_section("right aligned section")

Expand Down Expand Up @@ -236,18 +236,14 @@ def test_toc_with_font_style_override_bold(tmp_path): # issue-1072
pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
pdf.set_section_title_styles(
TextStyle("Helvetica", font_size_pt=20, color=(0, 0, 0))
)
pdf.set_section_title_styles(TextStyle("Helvetica", font_size_pt=20))
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold1.pdf", tmp_path)

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", style="B")
pdf.set_section_title_styles(
TextStyle("Helvetica", font_style="", font_size_pt=20, color=(0, 0, 0))
)
pdf.set_section_title_styles(TextStyle("Helvetica", font_size_pt=20, font_style=""))
pdf.start_section("foo")
assert_pdf_equal(pdf, HERE / "toc_with_font_style_override_bold2.pdf", tmp_path)

Expand Down
Binary file modified test/outline/toc_with_right_aligned_page_numbers.pdf
Binary file not shown.
Binary file modified test/table/table_with_rowspan_and_colspan.pdf
Binary file not shown.
Binary file modified test/template/flextemplate_leak.pdf
Binary file not shown.
Binary file modified test/template/template_nominal_hardcoded.pdf
Binary file not shown.
Binary file modified test/template/template_textstyles.pdf
Binary file not shown.
Binary file modified test/text/cell_ln_newpos.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown_bleeding.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown_bold_italic.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown_escaped.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown_with_ttf_fonts.pdf
Binary file not shown.
Binary file modified test/text/cell_markdown_with_ttf_fonts_escaped.pdf
Binary file not shown.
Binary file modified test/text/cell_newpos.pdf
Binary file not shown.
Binary file modified test/text/cell_newpos_charspaced.pdf
Binary file not shown.
Binary file modified test/text/cell_newpos_combined.pdf
Binary file not shown.
Binary file modified test/text/cell_newpos_stretched.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_ln_newpos.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_markdown.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_markdown_escaped.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_markdown_link.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_markdown_with_ttf_fonts.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_markdown_with_ttf_fonts_escaped.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_newpos.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_newpos_charspaced.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_newpos_combined.pdf
Binary file not shown.
Binary file modified test/text/multi_cell_newpos_stretched.pdf
Binary file not shown.
Binary file modified test/text/render_styled_newpos.pdf
Binary file not shown.
Binary file modified test/text_shaping/multi_cell_markdown_with_styling.pdf
Binary file not shown.