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

Make sure 'lift' is reset if Fragment changes lift in _render_styled_text_line #862

Merged
merged 2 commits into from
Jul 25, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
- [`FPDF.image()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.image): allowing images path starting with `data` to be passed as input
- text overflow is better handled by `FPDF.write()` & `FPDF.write_html()` - _cf._ [issue #847](https://github.com/PyFPDF/fpdf2/issues/847)
- the initial text color is preserved when using `FPDF.write_html()` - _cf._ [issue #846](https://github.com/PyFPDF/fpdf2/issues/846)
- handle superscript and subscript correctly when rendering `TextLine`- [Pull Request #862](https://github.com/PyFPDF/fpdf2/pull/862)
### Deprecated
- the `center` optional parameter of [`FPDF.cell()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.cell) is **no more** deprecated, as it allows for horizontal positioning, which is different from text alignment control with `align="C"`

Expand Down
5 changes: 4 additions & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2845,6 +2845,7 @@ def _render_styled_text_line(
s_width, underlines = 0, []
# We try to avoid modifying global settings for temporary changes.
current_ws = frag_ws = 0.0
current_lift = 0.0
current_char_vpos = CharVPos.LINE
current_font = self.current_font
current_text_mode = self.text_mode
Expand Down Expand Up @@ -2894,9 +2895,10 @@ def _render_styled_text_line(
current_font = frag.font
sl.append(f"/F{frag.font.i} {frag.font_size_pt:.2f} Tf")
lift = frag.lift
if lift != 0.0:
if lift != current_lift:
# Use text rise operator:
sl.append(f"{lift:.2f} Ts")
current_lift = lift
if (
frag.text_mode != TextMode.FILL
or frag.text_mode != current_text_mode
Expand Down Expand Up @@ -2983,6 +2985,7 @@ def _render_styled_text_line(
# pylint: disable=too-many-boolean-expressions
if (
current_ws != 0.0
or current_lift != 0.0
or current_char_vpos != CharVPos.LINE
or current_font != self.current_font
or current_text_mode != self.text_mode
Expand Down
32 changes: 32 additions & 0 deletions test/text/test_varied_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,3 +192,35 @@ def test_varfrags_text_mode(tmp_path):
pdf.ln()
pdf.write_fragments(frags, align=Align.C)
assert_pdf_equal(pdf, HERE / "varfrags_text_mode.pdf", tmp_path)


def test_varfrags_char_vpos(tmp_path):
pdf = FxFPDF()
pdf.add_page()
pdf.line(pdf.l_margin, pdf.t_margin, pdf.l_margin, pdf.h - pdf.b_margin)
pdf.line(
pdf.l_margin + pdf.epw,
pdf.t_margin,
pdf.l_margin + pdf.epw,
pdf.h - pdf.b_margin,
)
pdf.set_font("helvetica", "", 14)
f1 = Fragment(TEXT_1, pdf._get_current_graphics_state(), pdf.k)
pdf.char_vpos = "SUB"
f2 = Fragment(TEXT_2, pdf._get_current_graphics_state(), pdf.k)
f3 = Fragment(TEXT_3, f1.graphics_state, pdf.k)
pdf.char_vpos = "SUP"
f4 = Fragment(TEXT_4, pdf._get_current_graphics_state(), pdf.k)
f5 = Fragment(TEXT_5, f1.graphics_state, pdf.k)
frags = [f1, f2, f3, f4, f5]
pdf.write_fragments(frags)
pdf.ln()
pdf.ln()
pdf.write_fragments(frags, align=Align.J)
pdf.ln()
pdf.ln()
pdf.write_fragments(frags, align=Align.R)
pdf.ln()
pdf.ln()
pdf.write_fragments(frags, align=Align.C)
assert_pdf_equal(pdf, HERE / "varfrags_char_vpos.pdf", tmp_path)
Binary file added test/text/varfrags_char_vpos.pdf
Binary file not shown.