Skip to content
Open
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
74 changes: 29 additions & 45 deletions pydyf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ class Stream(Object):
"""PDF Stream object."""
def __init__(self, stream=None, extra=None, compress=False):
super().__init__()
#: Python array of data composing stream.
#: Python array of serializable data composing stream.
self.stream = stream or []
#: Metadata containing at least the length of the Stream.
self.extra = extra or {}
#: Compress the stream data if set to ``True``. Default is ``False``.
#: Compress the stream data if set to :obj:`True`. Default is :obj:`False`.
self.compress = compress

def begin_marked_content(self, tag, property_list=None):
Expand Down Expand Up @@ -197,15 +197,11 @@ def fill_stroke_and_close(self, even_odd=False):
def inline_image(self, width, height, color_space, bpc, raw_data):
"""Add an inline image.

:param width: The width of the image.
:type width: :obj:`int`
:param height: The height of the image.
:type height: :obj:`int`
:param colorspace: The color space of the image, f.e. RGB, Gray.
:type colorspace: :obj:`str`
:param bpc: The bits per component. 1 for BW, 8 for grayscale.
:type bpc: :obj:`int`
:param raw_data: The raw pixel data.
:param int width: The width of the image.
:param int height: The height of the image.
:param str colorspace: The color space of the image, f.e. RGB, Gray.
:param int bpc: The bits per component. 1 for BW, 8 for grayscale.
:param bytes raw_data: The raw pixel data.

"""
data = zlib.compress(raw_data) if self.compress else raw_data
Expand Down Expand Up @@ -297,8 +293,7 @@ def set_dash(self, dash_array, dash_phase):

:param dash_array: Dash pattern.
:type dash_array: :term:`iterable`
:param dash_phase: Start of dash phase.
:type dash_phase: :obj:`int`
:param int dash_phase: Start of dash phase.

"""
self.stream.append(b' '.join((
Expand Down Expand Up @@ -332,18 +327,12 @@ def set_line_width(self, width):
def set_matrix(self, a, b, c, d, e, f):
"""Set current transformation matrix.

:param a: Top left number in the matrix.
:type a: :obj:`int` or :obj:`float`
:param b: Top middle number in the matrix.
:type b: :obj:`int` or :obj:`float`
:param c: Middle left number in the matrix.
:type c: :obj:`int` or :obj:`float`
:param d: Middle middle number in the matrix.
:type d: :obj:`int` or :obj:`float`
:param e: Bottom left number in the matrix.
:type e: :obj:`int` or :obj:`float`
:param f: Bottom middle number in the matrix.
:type f: :obj:`int` or :obj:`float`
:param float a: Top left number in the matrix.
:param float b: Top middle number in the matrix.
:param float c: Middle left number in the matrix.
:param float d: Middle middle number in the matrix.
:param float e: Bottom left number in the matrix.
:param float f: Bottom middle number in the matrix.

"""
self.stream.append(b' '.join((
Expand All @@ -365,18 +354,12 @@ def set_state(self, state_name):
def set_text_matrix(self, a, b, c, d, e, f):
"""Set current text and text line transformation matrix.

:param a: Top left number in the matrix.
:type a: :obj:`int` or :obj:`float`
:param b: Top middle number in the matrix.
:type b: :obj:`int` or :obj:`float`
:param c: Middle left number in the matrix.
:type c: :obj:`int` or :obj:`float`
:param d: Middle middle number in the matrix.
:type d: :obj:`int` or :obj:`float`
:param e: Bottom left number in the matrix.
:type e: :obj:`int` or :obj:`float`
:param f: Bottom middle number in the matrix.
:type f: :obj:`int` or :obj:`float`
:param float a: Top left number in the matrix.
:param float b: Top middle number in the matrix.
:param float c: Middle left number in the matrix.
:param float d: Middle middle number in the matrix.
:param float e: Bottom left number in the matrix.
:param float f: Bottom middle number in the matrix.

"""
self.stream.append(b' '.join((
Expand Down Expand Up @@ -416,7 +399,7 @@ class String(Object):
"""PDF String object."""
def __init__(self, string=''):
super().__init__()
#: Unicode string.
#: Unicode string or encoded bytestring.
self.string = string

@property
Expand Down Expand Up @@ -505,8 +488,7 @@ def page_references(self):
def write_line(self, content, output):
"""Write line to output.

:param content: Content to write.
:type content: :obj:`bytes`
:param bytes content: Content to write.
:param output: Output stream.
:type output: binary :term:`file object`

Expand Down Expand Up @@ -553,19 +535,21 @@ def write(self, output, version=b'1.7', identifier=False, compress=False):
self.write_line(object_.indirect, output)

# Write compressed objects in object stream
stream = [[]]
stream = []
object_index_list = []
position = 0
for i, object_ in enumerate(compressed_objects):
data = object_.data
stream.append(data)
stream[0].append(object_.number)
stream[0].append(position)
object_index_list.append(object_.number)
object_index_list.append(position)
position += len(data) + 1
stream[0] = ' '.join(str(i) for i in stream[0])
object_index_string = ' '.join(str(i) for i in object_index_list)
stream.insert(0, object_index_string)
extra = {
'Type': '/ObjStm',
'N': len(compressed_objects),
'First': len(stream[0]) + 1,
'First': len(object_index_string) + 1,
}
object_stream = Stream(stream, extra, compress)
object_stream.offset = self.current_position
Expand Down