-
Notifications
You must be signed in to change notification settings - Fork 252
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
Fixing: bullet/top_margin combo & width of indented paragraphs & some HTML bugs #1218
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,8 +156,9 @@ def generate_bullet_frags_and_tl(self, bullet_string: str, bullet_r_margin: floa | |
bullet_fragments, | ||
max_width=self._region.get_width, | ||
margins=( | ||
self.pdf.c_margin + (self.indent - fragments_width - bullet_r_margin), | ||
self.pdf.c_margin, | ||
self._region.h_margins[0] | ||
+ (self.indent - fragments_width - bullet_r_margin), | ||
self._region.h_margins[1], | ||
), | ||
align=self.text_align or self._region.text_align or Align.L, | ||
wrapmode=self.wrapmode, | ||
|
@@ -182,7 +183,8 @@ def build_lines(self, print_sh) -> List[LineWrapper]: | |
multi_line_break = MultiLineBreak( | ||
self._text_fragments, | ||
max_width=self._region.get_width, | ||
margins=(self.pdf.c_margin + self.indent, self.pdf.c_margin), | ||
margins=self._region.h_margins, | ||
indent=self.indent, | ||
align=self.text_align or self._region.text_align or Align.L, | ||
print_sh=print_sh, | ||
wrapmode=self.wrapmode, | ||
|
@@ -222,7 +224,7 @@ def __init__( | |
title=None, | ||
alt_text=None, | ||
): | ||
self.region = region | ||
self._region = region | ||
self.name = name | ||
if align: | ||
align = Align.coerce(align) | ||
|
@@ -246,7 +248,7 @@ def build_line(self): | |
# We do double duty as a "text line wrapper" here, since all the necessary | ||
# information is already in the ImageParagraph object. | ||
self.name, self.img, self.info = preload_image( | ||
self.region.pdf.image_cache, self.name | ||
self._region.pdf.image_cache, self.name | ||
) | ||
return self | ||
|
||
|
@@ -261,11 +263,11 @@ def render(self, col_left, col_width, max_height): | |
if self.height: | ||
h = self.height | ||
else: | ||
native_h = self.info["h"] / self.region.pdf.k | ||
native_h = self.info["h"] / self._region.pdf.k | ||
if self.width: | ||
w = self.width | ||
else: | ||
native_w = self.info["w"] / self.region.pdf.k | ||
native_w = self.info["w"] / self._region.pdf.k | ||
if native_w > col_width or self.fill_width: | ||
w = col_width | ||
else: | ||
|
@@ -281,7 +283,7 @@ def render(self, col_left, col_width, max_height): | |
elif self.align == Align.C: | ||
x += (col_width - w) / 2 | ||
if is_svg: | ||
return self.region.pdf._vector_image( | ||
return self._region.pdf._vector_image( | ||
name=self.name, | ||
svg=self.img, | ||
info=self.info, | ||
|
@@ -294,7 +296,7 @@ def render(self, col_left, col_width, max_height): | |
alt_text=self.alt_text, | ||
keep_aspect_ratio=self.keep_aspect_ratio, | ||
) | ||
return self.region.pdf._raster_image( | ||
return self._region.pdf._raster_image( | ||
name=self.name, | ||
img=self.img, | ||
info=self.info, | ||
|
@@ -519,7 +521,6 @@ def _render_column_lines(self, text_lines, top, bottom): | |
if ( | ||
text_rendered | ||
and tl_wrapper.first_line | ||
and not cur_bullet | ||
and cur_paragraph.top_margin | ||
and self.pdf.y > self.pdf.t_margin | ||
): | ||
|
@@ -631,6 +632,7 @@ def __init__( | |
self.balance = balance | ||
total_w = self.extents.right - self.extents.left | ||
col_width = (total_w - (ncols - 1) * gutter) / ncols | ||
self.h_margins = (pdf.c_margin, pdf.c_margin) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the current usage of this attribute, I wonder if having 2 distincts attributes |
||
# We calculate the column extents once in advance, and store them for lookup. | ||
c_left = self.extents.left | ||
self._cols = [Extents(c_left, c_left + col_width)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -327,3 +327,72 @@ def test_tcols_break_top_margin(tmp_path): # regression test for #1214 | |
) as par: | ||
par.write(text=LOREM_IPSUM) | ||
assert_pdf_equal(pdf, HERE / "tcols_break_top_margin.pdf", tmp_path) | ||
|
||
|
||
def test_tcols_bullets_indent(tmp_path): | ||
"""Ensure that the top/bottom margins work with indented/bulleted | ||
paragraphs. | ||
Ensure that indented paragraphs have a reduced total width. | ||
Comment on lines
+333
to
+335
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice test 👍 |
||
""" | ||
pdf = FPDF() | ||
pdf.add_page() | ||
pdf.set_font("Helvetica", "", 14) | ||
pdf.set_top_margin(50) | ||
pdf.set_auto_page_break(True, 50) | ||
pdf.rect(pdf.l_margin, pdf.t_margin, pdf.epw, pdf.eph) | ||
with pdf.text_columns(text_align="L", ncols=2) as cols: | ||
cols.write("Paragraphs with Top Margin\n\n") | ||
for _ in range(4): | ||
with cols.paragraph( | ||
text_align="J", | ||
top_margin=pdf.font_size * 4, | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
for _ in range(5): | ||
with cols.paragraph( | ||
text_align="J", | ||
top_margin=pdf.font_size * 4, | ||
indent=10, | ||
bullet_string="\x95", | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
pdf.add_page() | ||
pdf.rect(pdf.l_margin, pdf.t_margin, pdf.epw, pdf.eph) | ||
with pdf.text_columns(text_align="L", ncols=2) as cols: | ||
cols.write("Paragraphs with Bottom Margin\n\n") | ||
for _ in range(4): | ||
with cols.paragraph( | ||
text_align="J", | ||
bottom_margin=pdf.font_size * 4, | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
for _ in range(5): | ||
with cols.paragraph( | ||
text_align="J", | ||
bottom_margin=pdf.font_size * 4, | ||
indent=10, | ||
bullet_string="\x95", | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
pdf.add_page() | ||
pdf.rect(pdf.l_margin, pdf.t_margin, pdf.epw, pdf.eph) | ||
with pdf.text_columns(text_align="L", ncols=2) as cols: | ||
cols.write("Paragraphs with Top and Bottom Margin\n\n") | ||
for _ in range(4): | ||
with cols.paragraph( | ||
text_align="J", | ||
top_margin=pdf.font_size * 1.5, | ||
bottom_margin=pdf.font_size * 2.5, | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
for _ in range(5): | ||
with cols.paragraph( | ||
text_align="J", | ||
top_margin=pdf.font_size * 1.5, | ||
bottom_margin=pdf.font_size * 2.5, | ||
indent=10, | ||
bullet_string="\x95", | ||
) as par: | ||
par.write(text=LOREM_IPSUM[:100]) | ||
assert_pdf_equal(pdf, HERE / "tcols_bullets_indent.pdf", tmp_path) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
indent=self.indent
be provided there, instead of addingself.indent
to the left margin?