1
0
Fork 0

Merging upstream version 0.9.0.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-02-08 08:01:00 +01:00
parent 768d0cd35e
commit 91745cfec8
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
6 changed files with 215 additions and 12 deletions

View file

@ -3,28 +3,27 @@ A low-level PDF generator.
"""
import base64
import re
import zlib
from codecs import BOM_UTF16_BE
from hashlib import md5
from math import ceil, log
VERSION = __version__ = '0.7.0'
VERSION = __version__ = '0.9.0'
def _to_bytes(item):
"""Convert item to bytes."""
if isinstance(item, bytes):
return item
elif isinstance(item, Object):
return item.data
elif isinstance(item, float):
if item.is_integer():
return f'{int(item):d}'.encode('ascii')
return str(int(item)).encode('ascii')
else:
return f'{item:f}'.rstrip('0').encode('ascii')
elif isinstance(item, int):
return f'{item:d}'.encode('ascii')
elif isinstance(item, Object):
return item.data
return str(item).encode('ascii')
@ -366,6 +365,44 @@ class Stream(Object):
_to_bytes(a), _to_bytes(b), _to_bytes(c),
_to_bytes(d), _to_bytes(e), _to_bytes(f), b'cm')))
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.
"""
if self.compress:
data = zlib.compress(raw_data)
else:
data = raw_data
enc_data = base64.a85encode(data)
self.stream.append(
b' '.join(
(
b'BI',
b'/W', _to_bytes(width),
b'/H', _to_bytes(height),
b'/BPC', _to_bytes(bpc),
b'/CS',
b'/Device' + color_space.encode(),
b'/F',
b'[/A85 /Fl]' if self.compress else b'/A85',
b'/L', _to_bytes(len(enc_data) + 2),
b'ID',
enc_data + b'~>',
b'EI',
)
)
)
@property
def data(self):
stream = b'\n'.join(_to_bytes(item) for item in self.stream)