Skip to content

Commit

Permalink
Merge pull request #8 from bastienlc/dev
Browse files Browse the repository at this point in the history
Add support for line breaks
  • Loading branch information
bastienlc authored Apr 25, 2024
2 parents af83a95 + b996047 commit 883bcbf
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ pip install -e .
* Add test and lint to CI.
* 2.2.0
* Support PDFs with pages of different sizes.
* 2.2.1
* Support line breaks in text watermark.

## License

Expand Down
10 changes: 8 additions & 2 deletions src/pdf_watermark/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
from reportlab.pdfgen import canvas

from pdf_watermark.options import Alignments, DrawingOptions, GridOptions, InsertOptions
from pdf_watermark.utils import change_base, draw_centered_image, fit_image
from pdf_watermark.utils import (
change_base,
draw_centered_image,
draw_centered_string_with_line_breaks,
fit_image,
)


def draw_one_watermark(
Expand All @@ -20,7 +25,8 @@ def draw_one_watermark(
x_prime, y_prime = change_base(x, y, rotation_matrix)

if drawing_options.text is not None:
watermark.drawCentredString(
draw_centered_string_with_line_breaks(
watermark,
x_prime,
y_prime,
drawing_options.text,
Expand Down
16 changes: 16 additions & 0 deletions src/pdf_watermark/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,22 @@ def draw_centered_image(
)


def draw_centered_string_with_line_breaks(
watermark: canvas.Canvas,
x: float,
y: float,
text: str,
):
text_lines = text.split(r"\n")
line_height = (
watermark._leading
) # line height is set when setting the font of the canvas
y += (len(text_lines) - 1) * line_height / 2 # also center the text vertically
for line in text_lines:
watermark.drawCentredString(x, y, line)
y -= watermark._leading


def change_base(x: float, y: float, rotation_matrix: np.ndarray) -> Tuple[float, float]:
# Since we rotated the original coordinates system, use the inverse of the rotation matrix
# (which is the transposed matrix) to get the coordinates we have to draw at
Expand Down
Binary file added tests/fixtures/2.pdf
Binary file not shown.
Binary file added tests/fixtures/3.pdf
Binary file not shown.
21 changes: 19 additions & 2 deletions tests/test_add_watermark_from_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@

INPUT = "tests/fixtures/input.pdf"
OUTPUT = "output.pdf"
FIXTURES = ["tests/fixtures/0.pdf", "tests/fixtures/1.pdf"]
FIXTURES = [
"tests/fixtures/0.pdf",
"tests/fixtures/1.pdf",
"tests/fixtures/2.pdf",
"tests/fixtures/3.pdf",
]


@pytest.fixture(autouse=True)
Expand All @@ -39,7 +44,19 @@ def cleanup():
image_scale=DEFAULTS.image_scale,
save_as_image=DEFAULTS.save_as_image,
dpi=DEFAULTS.dpi,
)
),
DrawingOptions(
watermark=r"watermark\nwith\nline\nbreaks",
opacity=DEFAULTS.opacity,
angle=DEFAULTS.angle,
text_color=DEFAULTS.text_color,
text_font=DEFAULTS.text_font,
text_size=DEFAULTS.text_size,
unselectable=DEFAULTS.unselectable,
image_scale=DEFAULTS.image_scale,
save_as_image=DEFAULTS.save_as_image,
dpi=DEFAULTS.dpi,
),
]

FILES_OPTIONS_FIXTURES = [FilesOptions(INPUT, OUTPUT)]
Expand Down

0 comments on commit 883bcbf

Please sign in to comment.