Skip to content

Commit

Permalink
fix: Move BBOX_NONE out of get_bound as only tags have no content
Browse files Browse the repository at this point in the history
Also ensure that only tags have no content by checking if there
is actually any text being shown by `TJ`
  • Loading branch information
dhdaines committed Nov 28, 2024
1 parent 3ef10b1 commit d516d93
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
23 changes: 21 additions & 2 deletions playa/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,10 +1691,27 @@ def bbox(self) -> Rect:
return get_bound(points)


BBOX_NONE = (-1, -1, -1, -1)


@dataclass
class TagObject(ContentObject):
"""A marked content point with no content."""

@property
def bbox(self) -> Rect:
"""A tag has no content and thus no bounding box.
To avoid needlessly complicating user code this returns
`BBOX_NONE` instead of `None` or throwing a exception.
Because that is a specific object, you can reliably check for
it with:
if obj.bbox is BBOX_NONE:
...
"""
return BBOX_NONE


@dataclass
class ImageObject(ContentObject):
Expand Down Expand Up @@ -2209,11 +2226,13 @@ def do_BT(self) -> None:

def do_ET(self) -> Iterator[ContentObject]:
"""End a text object"""
# Only output text if... there is text to output (we rewrite all text operators to TJ)
# Only output text if... there is text to output (we rewrite
# all text operators to TJ)
has_text = False
for item in self.textobj:
if item.operator == "TJ":
has_text = True
if any(b for b in item.args if isinstance(b, bytes)):
has_text = True
if has_text:
yield self.create(TextObject, textstate=self.textstate, items=self.textobj)
else:
Expand Down
12 changes: 6 additions & 6 deletions playa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ def isnumber(x: object) -> bool:


_T = TypeVar("_T")
BBOX_NONE = (-1, -1, -1, -1)


def get_bound(pts: Iterable[Point]) -> Rect:
"""Compute a minimal rectangle that covers all the points."""
try:
xs, ys = list(zip(*pts))
except ValueError: # Means pts was empty
return BBOX_NONE
"""Compute a minimal rectangle that covers all the points.
Raises:
ValueError on empty input (as there is no bounding box).
"""
xs, ys = list(zip(*pts))
x0 = min(xs)
y0 = min(ys)
x1 = max(xs)
Expand Down

0 comments on commit d516d93

Please sign in to comment.