Skip to content

Commit

Permalink
release v2.0.20
Browse files Browse the repository at this point in the history
  • Loading branch information
jorisschellekens committed Feb 27, 2022
1 parent e5e1377 commit 7938687
Show file tree
Hide file tree
Showing 257 changed files with 342 additions and 386,011 deletions.
2 changes: 1 addition & 1 deletion borb/io/write/ascii_art/ascii_logo.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
borb version 2.0.19
borb version 2.0.20
Joris Schellekens
62 changes: 59 additions & 3 deletions borb/pdf/canvas/layout/annotation/sound_annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
default, a speaker) to indicate that it represents a sound. Table 185 shows the annotation dictionary entries
specific to this type of annotation. Sound objects are discussed in 13.3, “Sounds.”
"""
from pathlib import Path

from borb.io.read.types import Name, Dictionary, String, Boolean, Decimal as bDecimal
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.annotation.annotation import Annotation


Expand All @@ -20,6 +24,58 @@ class SoundAnnotation(Annotation):
specific to this type of annotation. Sound objects are discussed in 13.3, “Sounds.”
"""

def __init__(self):
super(SoundAnnotation, self).__init__()
# TODO
@staticmethod
def _make_canonical_file_path(p: str) -> str:
try:
return Path(p).as_uri()
except:
return p

def __init__(
self,
bounding_box: Rectangle,
url_to_mp3_file: str,
):
super(SoundAnnotation, self).__init__(bounding_box=bounding_box)
self._url_to_mp3_file: str = url_to_mp3_file

# (Required) The type of annotation that this dictionary describes; shall be
# Link for a link annotation.
self[Name("Subtype")] = Name("Screen")

# (Optional; PDF 1.1) An action that shall be performed when the link
# annotation is activated (see 12.6, “Actions”).
self[Name("A")] = Dictionary()
self["A"][Name("Type")] = Name("Action")
self["A"][Name("S")] = Name("Rendition")
self["A"][Name("OP")] = bDecimal(0)

# A/R
self["A"][Name("R")] = Dictionary()
self[Name("A")][Name("R")][Name("Type")] = Name("Rendition")
self[Name("A")][Name("R")][Name("S")] = Name("MR")

# A/R/C
self[Name("A")][Name("R")][Name("C")] = Dictionary()
self[Name("A")][Name("R")][Name("C")][Name("Type")] = Name("MediaClip")
self[Name("A")][Name("R")][Name("C")][Name("S")] = Name("MCD")
self[Name("A")][Name("R")][Name("C")][Name("CT")] = String("video/mp4")

# A/R/C/D
# fmt: off
self[Name("A")][Name("R")][Name("C")][Name("D")] = Dictionary()
self[Name("A")][Name("R")][Name("C")][Name("D")][Name("Type")] = Name("Filespec")
self[Name("A")][Name("R")][Name("C")][Name("D")][Name("FS")] = Name("URL")
self[Name("A")][Name("R")][Name("C")][Name("D")][Name("F")] = String(SoundAnnotation._make_canonical_file_path(self._url_to_mp3_file))
# fmt: on

# A/R/C/P
# fmt: off
self[Name("A")][Name("R")][Name("C")][Name("P")] = Dictionary()
self[Name("A")][Name("R")][Name("C")][Name("P")][Name("TF")] = String("TEMPACCESS")
# fmt: on

# A/R
self[Name("A")][Name("R")][Name("P")] = Dictionary()
self[Name("A")][Name("R")][Name("P")][Name("BE")] = Dictionary()
self[Name("A")][Name("R")][Name("P")][Name("BE")][Name("C")] = Boolean(True)
77 changes: 75 additions & 2 deletions borb/pdf/canvas/layout/forms/push_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

from borb.io.read.types import Dictionary, Name, List, String, Stream, Boolean
from borb.io.read.types import Decimal as bDecimal
from borb.pdf.canvas.color.color import HexColor, Color, X11Color
from borb.pdf.canvas.color.color import HexColor, Color
from borb.pdf.canvas.font.simple_font.font_type_1 import StandardType1Font
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf.canvas.layout.annotation.square_annotation import SquareAnnotation
from borb.pdf.canvas.layout.forms.form_field import FormField
from borb.pdf.canvas.layout.layout_element import Alignment
from borb.pdf.canvas.layout.text.line_of_text import LineOfText
Expand Down Expand Up @@ -209,3 +208,77 @@ def _do_layout(self, page: "Page", layout_box: Rectangle) -> Rectangle:

# return Rectangle
return text_layout_box


class JavaScriptPushButton(PushButton):
"""
This implementation of FormField represents a push button that triggers JavaScript.
"""

def __init__(
self,
javascript: str,
text: str,
background_color: typing.Optional[Color] = HexColor("efefef"),
border_bottom: bool = True,
border_color: Color = HexColor("767676"),
border_left: bool = True,
border_right: bool = True,
border_top: bool = True,
border_width: Decimal = Decimal(1),
field_name: typing.Optional[str] = None,
font_size: typing.Optional[Decimal] = Decimal(12),
font_color: Color = HexColor("000000"),
horizontal_alignment: Alignment = Alignment.LEFT,
margin_bottom: typing.Optional[Decimal] = Decimal(0),
margin_left: typing.Optional[Decimal] = Decimal(0),
margin_right: typing.Optional[Decimal] = Decimal(0),
margin_top: typing.Optional[Decimal] = Decimal(0),
padding_bottom: Decimal = Decimal(2),
padding_left: Decimal = Decimal(6),
padding_right: Decimal = Decimal(6),
padding_top: Decimal = Decimal(2),
):
super(JavaScriptPushButton, self).__init__(
text=text,
background_color=background_color,
border_bottom=border_bottom,
border_color=border_color,
border_left=border_left,
border_right=border_right,
border_top=border_top,
border_width=border_width,
field_name=field_name,
font_size=font_size,
font_color=font_color,
horizontal_alignment=horizontal_alignment,
margin_bottom=margin_bottom,
margin_left=margin_left,
margin_right=margin_right,
margin_top=margin_top,
padding_bottom=padding_bottom,
padding_left=padding_left,
padding_right=padding_right,
padding_top=padding_top,
)
self._javascript: str = javascript

def _init_widget_dictionary(self, page: Page) -> None:
# call to super
super(JavaScriptPushButton, self)._init_widget_dictionary(page)

# build JavaScript stream object
javascript_stream = Stream()
javascript_stream[Name("Type")] = Name("JavaScript")
javascript_stream[Name("DecodedBytes")] = bytes(self._javascript, "latin1")
javascript_stream[Name("Bytes")] = zlib.compress(
javascript_stream[Name("DecodedBytes")], 9
)
javascript_stream[Name("Length")] = bDecimal(
len(javascript_stream[Name("Bytes")])
)
javascript_stream[Name("Filter")] = Name("FlateDecode")

# modify action dictionary of PushButton (super)
self._widget_dictionary[Name("AA")][Name("D")][Name("S")] = Name("JavaScript")
self._widget_dictionary[Name("AA")][Name("D")][Name("JS")] = javascript_stream
Loading

0 comments on commit 7938687

Please sign in to comment.