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

MAINT: Deprecate interiour_color with replacement interior_color #2706

Merged
merged 8 commits into from
Jun 7, 2024
22 changes: 16 additions & 6 deletions pypdf/annotations/_markup_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from abc import ABC
from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Union

from .._utils import deprecate_with_replacement
from ..constants import AnnotationFlag
from ..generic import ArrayObject, DictionaryObject
from ..generic._base import (
Expand Down Expand Up @@ -209,9 +210,13 @@ def __init__(
self,
rect: Union[RectangleObject, Tuple[float, float, float, float]],
*,
interiour_color: Optional[str] = None,
interior_color: Optional[str] = None,
**kwargs: Any,
):
if "interiour_color" in kwargs:
deprecate_with_replacement("interiour_color", "interior_color", "6.0.0")
interior_color = kwargs["interiour_color"]
del kwargs["interiour_color"]
super().__init__(**kwargs)
self.update(
{
Expand All @@ -221,9 +226,9 @@ def __init__(
}
)

if interiour_color:
if interior_color:
self[NameObject("/IC")] = ArrayObject(
[FloatObject(n) for n in hex_to_rgb(interiour_color)]
[FloatObject(n) for n in hex_to_rgb(interior_color)]
)


Expand Down Expand Up @@ -257,10 +262,15 @@ def __init__(
self,
rect: Union[RectangleObject, Tuple[float, float, float, float]],
*,
interiour_color: Optional[str] = None,
interior_color: Optional[str] = None,
**kwargs: Any,
):
if "interiour_color" in kwargs:
deprecate_with_replacement("interiour_color", "interior_color", "6.0.0")
interior_color = kwargs["interiour_color"]
del kwargs["interiour_color"]
super().__init__(**kwargs)

self.update(
{
NameObject("/Type"): NameObject("/Annot"),
Expand All @@ -269,9 +279,9 @@ def __init__(
}
)

if interiour_color:
if interior_color:
self[NameObject("/IC")] = ArrayObject(
[FloatObject(n) for n in hex_to_rgb(interiour_color)]
[FloatObject(n) for n in hex_to_rgb(interior_color)]
)


Expand Down
42 changes: 41 additions & 1 deletion tests/test_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,53 @@
from pathlib import Path

from pypdf import PdfReader, PdfWriter
from pypdf.annotations import FreeText, Text
from pypdf.annotations import Ellipse, FreeText, Rectangle, Text

TESTS_ROOT = Path(__file__).parent.resolve()
PROJECT_ROOT = TESTS_ROOT.parent
RESOURCE_ROOT = PROJECT_ROOT / "resources"


def test_ellipse_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

# Act
ellipse_annotation = Ellipse(
rect=(50, 550, 500, 650),
interior_color="ff0000",
)
writer.add_annotation(0, ellipse_annotation)

# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)


def test_rectangle_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "crazyones.pdf"
reader = PdfReader(pdf_path)
page = reader.pages[0]
writer = PdfWriter()
writer.add_page(page)

# Act
rectangle_annotation = Rectangle(
rect=(50, 550, 500, 650),
interior_color="ff0000",
)
writer.add_annotation(0, rectangle_annotation)

# Assert: You need to inspect the file manually
with open(pdf_file_path, "wb") as fp:
writer.write(fp)


def test_text_annotation(pdf_file_path):
# Arrange
pdf_path = RESOURCE_ROOT / "outline-without-title.pdf"
Expand Down
Loading