Skip to content

Commit 3a16c14

Browse files
committed
write_html via text regions all except tables
1 parent ee0feec commit 3a16c14

File tree

60 files changed

+229
-169
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+229
-169
lines changed

fpdf/fpdf.py

+64-52
Original file line numberDiff line numberDiff line change
@@ -2781,7 +2781,6 @@ def cell(
27812781
max_width=w,
27822782
trailing_nl=False,
27832783
),
2784-
w,
27852784
h,
27862785
border,
27872786
new_x=new_x,
@@ -2794,7 +2793,6 @@ def cell(
27942793
def _render_styled_text_line(
27952794
self,
27962795
text_line: TextLine,
2797-
w: float = None,
27982796
h: float = None,
27992797
border: Union[str, int] = 0,
28002798
new_x: XPos = XPos.RIGHT,
@@ -2817,8 +2815,6 @@ def _render_styled_text_line(
28172815
Args:
28182816
text_line (TextLine instance): Contains the (possibly empty) tuple of
28192817
fragments to render.
2820-
w (float): Cell width. Default value: None, meaning to fit text width.
2821-
If 0, the cell extends up to the right margin.
28222818
h (float): Cell height. Default value: None, meaning an height equal
28232819
to the current font size.
28242820
border: Indicates if borders must be drawn around the cell.
@@ -2847,25 +2843,30 @@ def _render_styled_text_line(
28472843

28482844
if padding is None:
28492845
padding = Padding(0, 0, 0, 0)
2850-
2851-
if padding.left == 0 and padding.right == 0:
2852-
horizontal_margin = self.c_margin
2853-
else:
2854-
horizontal_margin = 0
2846+
l_c_margin = r_c_margin = 0
2847+
if padding.left == 0:
2848+
l_c_margin = self.c_margin
2849+
if padding.right == 0:
2850+
r_c_margin = self.c_margin
28552851

28562852
styled_txt_width = text_line.text_width
28572853
if not styled_txt_width:
28582854
for i, frag in enumerate(text_line.fragments):
28592855
unscaled_width = frag.get_width(initial_cs=i != 0)
28602856
styled_txt_width += unscaled_width
28612857

2862-
28632858
w = text_line.max_width
2859+
if w is None:
2860+
if not text_line.fragments:
2861+
raise ValueError(
2862+
"'text_line' must have fragments if 'text_line.text_width' is None"
2863+
)
2864+
w = styled_txt_width + l_c_margin + r_c_margin
2865+
elif w == 0:
2866+
w = self.w - self.r_margin - self.x
28642867
if center:
2865-
self.x = (
2866-
self.w / 2 if align == Align.X else self.l_margin + (self.epw - w) / 2
2867-
)
2868-
if align == Align.X:
2868+
self.x = self.l_margin + (self.epw - w) / 2
2869+
elif text_line.align == Align.X:
28692870
self.x -= w / 2
28702871

28712872
max_font_size = 0 # how much height we need to accomodate.
@@ -2922,16 +2923,16 @@ def _render_styled_text_line(
29222923
last_used_color = self.fill_color
29232924
if text_line.fragments:
29242925
if text_line.align == Align.R:
2925-
dx = w - horizontal_margin - styled_txt_width
2926+
dx = w - l_c_margin - styled_txt_width
29262927
elif text_line.align in [Align.C, Align.X]:
29272928
dx = (w - styled_txt_width) / 2
29282929
else:
2929-
dx = horizontal_margin
2930+
dx = l_c_margin
29302931
s_start += dx
29312932
word_spacing = 0
29322933
if text_line.align == Align.J and text_line.number_of_spaces:
29332934
word_spacing = (
2934-
w - horizontal_margin - horizontal_margin - styled_txt_width
2935+
w - l_c_margin - r_c_margin - styled_txt_width
29352936
) / text_line.number_of_spaces
29362937
sl.append(
29372938
f"BT {(self.x + dx) * k:.2f} "
@@ -3065,7 +3066,7 @@ def _render_styled_text_line(
30653066
self.x = s_start + s_width
30663067
elif new_x == XPos.WCONT:
30673068
if s_width:
3068-
self.x = s_start + s_width - horizontal_margin
3069+
self.x = s_start + s_width - r_c_margin
30693070
else:
30703071
self.x = s_start
30713072
elif new_x == XPos.CENTER:
@@ -3392,10 +3393,11 @@ def multi_cell(
33923393
center (bool): center the cell horizontally on the page.
33933394
padding (float or Sequence): padding to apply around the text. Default value: 0.
33943395
When one value is specified, it applies the same padding to all four sides.
3395-
When two values are specified, the first padding applies to the top and bottom, the second to the left and right.
3396-
When three values are specified, the first padding applies to the top, the second to the right and left, the third to the bottom.
3397-
When four values are specified, the paddings apply to the top, right, bottom, and left in that order (clockwise)
3398-
If padding for left and right ends up being non-zero then c_margin is ignored.
3396+
When two values are specified, the first padding applies to the top and bottom, the second to
3397+
the left and right. When three values are specified, the first padding applies to the top,
3398+
the second to the right and left, the third to the bottom. When four values are specified,
3399+
the paddings apply to the top, right, bottom, and left in that order (clockwise)
3400+
If padding for left or right ends up being non-zero then respective c_margin is ignored.
33993401
34003402
Center overrides values for horizontal padding
34013403
@@ -3493,8 +3495,13 @@ def multi_cell(
34933495
# Apply padding to contents
34943496
# decrease maximum allowed width by padding
34953497
# shift the starting point by padding
3496-
w = w - padding.right - padding.left
3497-
maximum_allowed_width = w - 2 * self.c_margin
3498+
maximum_allowed_width = w = w - padding.right - padding.left
3499+
clearance_margins = []
3500+
# If we don't have padding on either side, we need a clearance margin.
3501+
if not padding.left:
3502+
clearance_margins.append(self.c_margin)
3503+
if not padding.right:
3504+
clearance_margins.append(self.c_margin)
34983505
self.x += padding.left
34993506
self.y += padding.top
35003507

@@ -3522,6 +3529,7 @@ def multi_cell(
35223529
multi_line_break = MultiLineBreak(
35233530
styled_text_fragments,
35243531
maximum_allowed_width,
3532+
clearance_margins,
35253533
align=align,
35263534
print_sh=print_sh,
35273535
wrapmode=wrapmode,
@@ -3561,7 +3569,6 @@ def multi_cell(
35613569
has_line_after = not is_last_line or should_render_bottom_blank_cell
35623570
new_page = self._render_styled_text_line(
35633571
text_line,
3564-
w,
35653572
h=current_cell_height,
35663573
border="".join(
35673574
(
@@ -3593,7 +3600,6 @@ def multi_cell(
35933600
max_width=w,
35943601
trailing_nl=False,
35953602
),
3596-
w,
35973603
h=h,
35983604
border="".join(
35993605
(
@@ -3696,31 +3702,28 @@ def write(
36963702
multi_line_break = MultiLineBreak(
36973703
styled_text_fragments,
36983704
lambda h: max_width,
3705+
(self.c_margin, self.c_margin),
36993706
print_sh=print_sh,
37003707
wrapmode=wrapmode,
37013708
)
37023709
# first line from current x position to right margin
37033710
first_width = self.w - self.x - self.r_margin
3704-
max_width = first_width - 2 * self.c_margin
3711+
max_width = first_width
37053712
text_line = multi_line_break.get_line()
37063713
# remaining lines fill between margins
37073714
full_width = self.w - self.l_margin - self.r_margin
3708-
max_width = full_width - 2 * self.c_margin
3715+
max_width = full_width
37093716
while (text_line) is not None:
37103717
text_lines.append(text_line)
37113718
text_line = multi_line_break.get_line()
37123719
if not text_lines:
37133720
return False
37143721

37153722
for text_line_index, text_line in enumerate(text_lines):
3716-
if text_line_index == 0:
3717-
line_width = first_width
3718-
else:
3719-
line_width = full_width
3723+
if text_line_index > 0:
37203724
self.ln()
37213725
new_page = self._render_styled_text_line(
37223726
text_line,
3723-
line_width,
37243727
h=h,
37253728
border=0,
37263729
new_x=XPos.WCONT,
@@ -4196,11 +4199,18 @@ def ln(self, h=None):
41964199
the amount passed as parameter.
41974200
41984201
Args:
4199-
h (float): The height of the break.
4200-
By default, the value equals the height of the last printed cell.
4202+
h (float, optional): The height of the break.
4203+
By default, the value equals the height of the last printed cell, or absent that
4204+
the height of the current font.
4205+
If no 'h' is given, nothing has been written yet, and no font size is set, nothing happens.
42014206
"""
42024207
self.x = self.l_margin
4203-
self.y += self._lasth if h is None else h
4208+
if h:
4209+
self.y += h
4210+
elif self._lasth:
4211+
self.y += self._lasth
4212+
# elif self.font_size:
4213+
# self.y += self.font_size
42044214

42054215
def get_x(self):
42064216
"""Returns the abscissa of the current position."""
@@ -4846,31 +4856,33 @@ def table(self, *args, **kwargs):
48464856
Detailed usage documentation: https://py-pdf.github.io/fpdf2/Tables.html
48474857
48484858
Args:
4849-
rows: optional. Sequence of rows (iterable) of str to initiate the table cells with text content
4850-
align (str, fpdf.enums.Align): optional, default to CENTER. Sets the table horizontal position relative to the page,
4851-
when it's not using the full page width
4852-
borders_layout (str, fpdf.enums.TableBordersLayout): optional, default to ALL. Control what cell borders are drawn
4859+
rows: optional. Sequence of rows (iterable) of str to initiate the table cells with text content.
4860+
align (str, fpdf.enums.Align): optional, default to CENTER. Sets the table horizontal position
4861+
relative to the page, when it's not using the full page width.
4862+
borders_layout (str, fpdf.enums.TableBordersLayout): optional, default to ALL. Control what cell
4863+
borders are drawn.
48534864
cell_fill_color (int, tuple, fpdf.drawing.DeviceGray, fpdf.drawing.DeviceRGB): optional.
4854-
Defines the cells background color
4855-
cell_fill_mode (str, fpdf.enums.TableCellFillMode): optional. Defines which cells are filled with color in the background
4856-
col_widths (int, tuple): optional. Sets column width. Can be a single number or a sequence of numbers
4865+
Defines the cells background color.
4866+
cell_fill_mode (str, fpdf.enums.TableCellFillMode): optional. Defines which cells are filled
4867+
with color in the background.
4868+
col_widths (int, tuple): optional. Sets column width. Can be a single number or a sequence of numbers.
48574869
first_row_as_headings (bool): optional, default to True. If False, the first row of the table
4858-
is not styled differently from the others
4859-
gutter_height (float): optional vertical space between rows
4860-
gutter_width (float): optional horizontal space between columns
4870+
is not styled differently from the others.
4871+
gutter_height (float): optional vertical space between rows.
4872+
gutter_width (float): optional horizontal space between columns.
48614873
headings_style (fpdf.fonts.FontFace): optional, default to bold.
48624874
Defines the visual style of the top headings row: size, color, emphasis...
4863-
line_height (number): optional. Defines how much vertical space a line of text will occupy
4864-
markdown (bool): optional, default to False. Enable markdown interpretation of cells textual content
4875+
line_height (number): optional. Defines how much vertical space a line of text will occupy.
4876+
markdown (bool): optional, default to False. Enable markdown interpretation of cells textual content.
48654877
text_align (str, fpdf.enums.Align): optional, default to JUSTIFY. Control text alignment inside cells.
4866-
width (number): optional. Sets the table width
4878+
width (number): optional. Sets the table width.
48674879
wrapmode (fpdf.enums.WrapMode): "WORD" for word based line wrapping (default),
48684880
"CHAR" for character based line wrapping.
48694881
padding (number, tuple, Padding): optional. Sets the cell padding. Can be a single number or a sequence
48704882
of numbers, default:0
4871-
If padding for left and right ends up being non-zero then c_margin is ignored.
4872-
outer_border_width (number): optional. The outer_border_width will trigger rendering of the outer border of
4873-
the table with the given width regardless of any other defined border styles.
4883+
If padding for left or right ends up being non-zero then the respective c_margin is ignored.
4884+
outer_border_width (number): optional. The outer_border_width will trigger rendering of the outer
4885+
border of the table with the given width regardless of any other defined border styles.
48744886
"""
48754887
table = Table(self, *args, **kwargs)
48764888
yield table

0 commit comments

Comments
 (0)