Skip to content

Commit

Permalink
STY: Simplify code (py-pdf#1101)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored and mtd91429 committed Jul 15, 2022
1 parent 22c5575 commit 0921b5a
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1059,14 +1059,11 @@ def add_bookmark_destination(
dest: Union[PageObject, TreeObject],
parent: Union[None, TreeObject, IndirectObject] = None,
) -> IndirectObject:
dest_ref = self._add_object(dest)

outline_ref = self.get_outline_root()

if parent is None:
parent = outline_ref
parent = self.get_outline_root()

parent = cast(TreeObject, parent.get_object())
dest_ref = self._add_object(dest)
parent.add_child(dest_ref, self)

return dest_ref
Expand Down Expand Up @@ -1137,24 +1134,21 @@ def add_bookmark(
:meth:`addLink()<addLink>` for details.
"""
page_ref = NumberObject(pagenum)
action = DictionaryObject()
zoom_args: ZoomArgsType = []
for a in args:
if a is not None:
zoom_args.append(NumberObject(a))
else:
zoom_args.append(NullObject())
zoom_args: ZoomArgsType = [
NullObject() if a is None else NumberObject(a) for a in args
]
dest = Destination(
NameObject("/" + title + " bookmark"), page_ref, NameObject(fit), *zoom_args
)
dest_array = dest.dest_array
action.update(
{
NameObject(GoToActionArguments.D): dest_array,
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}

action_ref = self._add_object(
DictionaryObject(
{
NameObject(GoToActionArguments.D): dest.dest_array,
NameObject(GoToActionArguments.S): NameObject("/GoTo"),
}
)
)
action_ref = self._add_object(action)
bookmark = _create_bookmark(action_ref, title, color, italic, bold)

if parent is None:
Expand Down Expand Up @@ -1537,26 +1531,21 @@ def add_link(
else:
rect = RectangleObject(rect)

zoom_args: ZoomArgsType = []
for a in args:
if a is not None:
zoom_args.append(NumberObject(a))
else:
zoom_args.append(NullObject())
zoom_args: ZoomArgsType = [
NullObject() if a is None else NumberObject(a) for a in args
]
dest = Destination(
NameObject("/LinkName"), page_dest, NameObject(fit), *zoom_args
) # TODO: create a better name for the link
dest_array = dest.dest_array

lnk = DictionaryObject()
lnk.update(
lnk = DictionaryObject(
{
NameObject("/Type"): NameObject(PG.ANNOTS),
NameObject("/Subtype"): NameObject("/Link"),
NameObject("/P"): page_link,
NameObject("/Rect"): rect,
NameObject("/Border"): ArrayObject(border_arr),
NameObject("/Dest"): dest_array,
NameObject("/Dest"): dest.dest_array,
}
)
lnk_ref = self._add_object(lnk)
Expand Down

0 comments on commit 0921b5a

Please sign in to comment.