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

BUG: Avoid isolating the graphics state multiple times #2224

Closed
Closed
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
23 changes: 23 additions & 0 deletions pypdf/generic/_data_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,8 @@ def __init__(
super().set_data(b_(stream_data))
self.forced_encoding = forced_encoding

self._has_isolated_graphics_state: Optional[bool] = None

def clone(
self,
pdf_dest: Any,
Expand Down Expand Up @@ -1247,12 +1249,33 @@ def operations(self, operations: List[Tuple[Any, Any]]) -> None:
self._operations = operations
self._data = b""

@property
def has_isolated_graphics_state(self) -> bool:
if self._has_isolated_graphics_state is None:
if self._operations:
self._has_isolated_graphics_state = (
self._operations[0] == ([], b"q") and self._operations[-1] == ([], b"Q")
)
elif self._data:
# Check for the character with the linebreak as inserted by `isolate_graphics_state`.
self._has_isolated_graphics_state = self._data[:2] == b"q\n" and self._data[-2:] == b"Q\n"
else:
# Empty stream.
self._has_isolated_graphics_state = True

return self._has_isolated_graphics_state

def isolate_graphics_state(self) -> None:
if self.has_isolated_graphics_state:
# No need to isolate again.
return

if self._operations:
self._operations.insert(0, ([], "q"))
self._operations.append(([], "Q"))
elif self._data:
self._data = b"q\n" + b_(self._data) + b"Q\n"
self._has_isolated_graphics_state = True

# This overrides the parent method:
def write_to_stream(
Expand Down
50 changes: 50 additions & 0 deletions tests/test_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1322,6 +1322,56 @@ def test_get_contents_from_nullobject():


@pytest.mark.enable_socket()
def test_has_isolated_graphics_state():
# Real example.
url = "https://github.com/py-pdf/pypdf/files/12428859/out1.pdf"
name = "isolate-graphics-state.pdf"
page = PdfReader(BytesIO(get_data_from_url(url, name=name))).pages[0]
content_stream = page.get_contents()
assert content_stream is not None

assert content_stream.has_isolated_graphics_state is False
content_stream.isolate_graphics_state()
assert content_stream.has_isolated_graphics_state is True

# Empty stream handling.
content_stream = ContentStream(stream=None, pdf="dummy.pdf")
assert content_stream.has_isolated_graphics_state is True

# Handling of string-based checks.
content_stream = ContentStream(stream=None, pdf="dummy.pdf")
content_stream.set_data(b"q\n 841.680 0 0 595.200 0.000 0.000 cm\n/Im0 Do\nQ\n\n \n")
assert content_stream.has_isolated_graphics_state is False

content_stream = ContentStream(stream=None, pdf="dummy.pdf")
content_stream.set_data(b"q\n 841.680 0 0 595.200 0.000 0.000 cm\n/Im0 Do\nQ\n")
assert content_stream.has_isolated_graphics_state is True

# Dummy example to test caching.
content_stream = ContentStream(stream=None, pdf="dummy.pdf")
assert content_stream._has_isolated_graphics_state is None
content_stream._has_isolated_graphics_state = True
assert content_stream.has_isolated_graphics_state is True
content_stream._has_isolated_graphics_state = False
assert content_stream.has_isolated_graphics_state is False


@pytest.mark.enable_socket()
def test_isolate_graphics_state():
url = "https://github.com/py-pdf/pypdf/files/12428859/out1.pdf"
name = "isolate-graphics-state.pdf"
page = PdfReader(BytesIO(get_data_from_url(url, name=name))).pages[0]
content_stream = page.get_contents()
assert content_stream is not None

# This page is not considered isolated at the beginning due to the final characters.
assert content_stream._data == b"q\n 841.680 0 0 595.200 0.000 0.000 cm\n/Im0 Do\nQ\n\n \n"
content_stream.isolate_graphics_state()
assert content_stream._data == b"q\nq\n 841.680 0 0 595.200 0.000 0.000 cm\n/Im0 Do\nQ\n\n \nQ\n"
content_stream.isolate_graphics_state()
assert content_stream._data == b"q\nq\n 841.680 0 0 595.200 0.000 0.000 cm\n/Im0 Do\nQ\n\n \nQ\n"


def test_pos_text_in_textvisitor():
"""See #2200"""
url = "https://github.com/py-pdf/pypdf/files/12675974/page_178.pdf"
Expand Down
Loading