Skip to content

Commit

Permalink
Extra HTML line height fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Sep 29, 2021
1 parent fb9af3d commit 5b418f1
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions fpdf/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ def handle_starttag(self, tag, attrs):
if attrs:
self.align = attrs.get("align")
if tag in self.heading_sizes:
self.font_stack.append((self.font_face, self.font_size, self.font_color))
self.heading_level = int(tag[1:])
hsize = self.heading_sizes[tag]
self.pdf.ln(5 * hsize)
Expand Down Expand Up @@ -441,7 +442,7 @@ def handle_starttag(self, tag, attrs):
self.bullet[self.indent - 1] = bullet
bullet = f"{bullet}. "
self.pdf.write(self.h, f"{' ' * self.li_tag_indent * self.indent}{bullet} ")
self.set_text_color()
self.set_text_color(*self.font_color)
if tag == "font":
# save previous font state:
self.font_stack.append((self.font_face, self.font_size, self.font_color))
Expand All @@ -458,7 +459,7 @@ def handle_starttag(self, tag, attrs):
if "size" in attrs:
self.font_size = int(attrs.get("size"))
self.set_font()
self.set_text_color()
self.set_text_color(*self.font_color)
if tag == "table":
self.table = {k.lower(): v for k, v in attrs.items()}
if "width" not in self.table:
Expand Down Expand Up @@ -558,16 +559,17 @@ def handle_endtag(self, tag):
LOGGER.debug("ENDTAG %s", tag)
if tag in self.heading_sizes:
self.heading_level = None
face, size, color = self.font_stack.pop()
self.set_font(face, size)
self.set_text_color(*color)
self.pdf.ln(self.h)
self.set_font()
self.set_text_color()
self.align = None
if tag == "pre":
# recover last font state, color is ignored as pre doesn't change it
face, size, color = self.font_stack.pop()
self.set_font(face, size)
self.set_text_color(*color)
if tag == "blockquote":
self.set_text_color()
self.set_text_color(*self.font_color)
self.indent -= 1
self.pdf.ln(3)
if tag == "strong":
Expand Down Expand Up @@ -621,7 +623,7 @@ def handle_endtag(self, tag):
face, size, color = self.font_stack.pop()
self.font_color = color
self.set_font(face, size)
self.set_text_color()
self.set_text_color(*self.font_color)
if tag == "center":
self.align = None

Expand All @@ -647,18 +649,15 @@ def set_style(self, tag=None, enable=False):
self.pdf.set_font(style=style)

def set_text_color(self, r=None, g=0, b=0):
if r is None:
self.pdf.set_text_color(*self.font_color)
else:
self.pdf.set_text_color(r, g, b)
self.pdf.set_text_color(r, g, b)

def put_link(self, txt):
# Put a hyperlink
self.set_text_color(0, 0, 255)
self.set_style("u", True)
self.pdf.write(self.h, txt, self.href)
self.set_style("u", False)
self.set_text_color()
self.set_text_color(*self.font_color)

# pylint: disable=no-self-use
def render_toc(self, pdf, outline):
Expand Down
Binary file modified test/html/html_custom_heading_sizes.pdf
Binary file not shown.
Binary file modified test/html/html_features.pdf
Binary file not shown.
Binary file modified test/html/html_heading_hebrew.pdf
Binary file not shown.
Binary file modified test/html/html_headings_line_height.pdf
Binary file not shown.
Binary file modified test/outline/test_html_toc.pdf
Binary file not shown.
Binary file modified test/outline/test_html_toc_2_pages.pdf
Binary file not shown.

0 comments on commit 5b418f1

Please sign in to comment.