Skip to content

Commit

Permalink
Make flags a property
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma committed Mar 31, 2023
1 parent bad1ceb commit 6fcd6a5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
17 changes: 15 additions & 2 deletions pypdf/annotations/_base.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
from abc import ABC

from ..constants import AnnotationFlag
from ..generic import NameObject
from ..generic._data_structures import DictionaryObject

NO_FLAGS = AnnotationFlag(0)


class AnnotationDictionary(DictionaryObject, ABC):
def __init__(self) -> None:
from ..generic._base import NameObject

# "rect" should not be added here as PolyLine can automatically set it
self[NameObject("/Type")] = NameObject("/Annot")
# The flags was NOT added to the constructor on purpose: We expect that
# most users don't want to change the default. If they want, they
# can use the property. The default is 0.

@property
def flags(self) -> AnnotationFlag:
return self.get(NameObject("/F"), AnnotationFlag(0))

@flags.setter
def flags(self, value: AnnotationFlag) -> None:
self[NameObject("/F")] = value


NO_FLAGS = AnnotationFlag(0)
14 changes: 8 additions & 6 deletions pypdf/annotations/_non_markup_annotations.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
from typing import Optional, Tuple, Union
from typing import Any, Optional, Tuple, Union

from ..constants import AnnotationFlag
from ..generic._base import (
BooleanObject,
NameObject,
NumberObject,
)
from ..generic._data_structures import DictionaryObject
from ..generic._rectangle import RectangleObject
from ._base import AnnotationDictionary

DEFAULT_ANNOTATION_FLAG = AnnotationFlag(0)

class Popup(DictionaryObject):

class Popup(AnnotationDictionary):
def __init__(
self,
*,
rect: Union[RectangleObject, Tuple[float, float, float, float]],
flags: int = 0,
parent: Optional[DictionaryObject] = None,
open: bool = False,
**kwargs: Any,
):
super().__init__(**kwargs)
self.update(
{
NameObject("/Type"): NameObject("/Annot"),
NameObject("/Subtype"): NameObject("/Popup"),
NameObject("/Rect"): RectangleObject(rect),
NameObject("/Open"): BooleanObject(open),
NameObject("/F"): NumberObject(flags),
}
)
if parent:
Expand Down
2 changes: 1 addition & 1 deletion pypdf/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ class PageLabelStyle:


class AnnotationFlag(IntFlag):
"""See 12.5.3 "Anntation Flags"."""
"""See 12.5.3 "Annotation Flags"."""

INVISIBLE = 1
HIDDEN = 2
Expand Down

0 comments on commit 6fcd6a5

Please sign in to comment.