mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-25 17:36:18 +03:00
issue #2959: improve Info setting and dumping
This commit is contained in:
parent
f9566877f3
commit
4d3b13fb08
|
@ -137,13 +137,13 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf = pdfParser.PdfParser(pdf_filename)
|
||||
self.assertEqual(len(pdf.pages), 1)
|
||||
self.assertEqual(len(pdf.info), 1)
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info[b"Producer"]), "pdfParser")
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info["Producer"]), "pdfParser")
|
||||
# append some info
|
||||
pdf.info[b"Title"] = pdfParser.encode_text("abc")
|
||||
pdf.info[b"Author"] = pdfParser.encode_text("def")
|
||||
pdf.info[b"Subject"] = pdfParser.encode_text("ghi")
|
||||
pdf.info[b"Keywords"] = pdfParser.encode_text("jkl")
|
||||
pdf.info[b"Creator"] = pdfParser.encode_text("hopper()")
|
||||
pdf.info["Title"] = "abc"
|
||||
pdf.info["Author"] = "def"
|
||||
pdf.info["Subject"] = "ghi"
|
||||
pdf.info["Keywords"] = "jkl"
|
||||
pdf.info["Creator"] = "hopper()"
|
||||
with open(pdf_filename, "r+b") as f:
|
||||
f.seek(0, os.SEEK_END)
|
||||
pdf.write_xref_and_trailer(f)
|
||||
|
@ -151,7 +151,7 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf = pdfParser.PdfParser(pdf_filename)
|
||||
self.assertEqual(len(pdf.pages), 1)
|
||||
self.assertEqual(len(pdf.info), 6)
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc")
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info["Title"]), "abc")
|
||||
# append two images
|
||||
mode_CMYK = hopper("CMYK")
|
||||
mode_P = hopper("P")
|
||||
|
@ -160,8 +160,8 @@ class TestFilePdf(PillowTestCase):
|
|||
pdf = pdfParser.PdfParser(pdf_filename)
|
||||
self.assertEqual(len(pdf.pages), 3)
|
||||
self.assertEqual(len(pdf.info), 6)
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info[b"Title"]), "abc")
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info[b"Producer"]), "pdfParser")
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info["Title"]), "abc")
|
||||
self.assertEqual(pdfParser.decode_text(pdf.info["Producer"]), "pdfParser")
|
||||
|
||||
def test_pdf_append_to_bytesio(self):
|
||||
im = hopper("RGB")
|
||||
|
|
|
@ -62,17 +62,17 @@ def _save(im, fp, filename, save_all=False):
|
|||
existing_pdf = pdfParser.PdfParser()
|
||||
|
||||
if title:
|
||||
existing_pdf.info[b"Title"] = pdfParser.encode_text(title)
|
||||
existing_pdf.info["Title"] = title
|
||||
if author:
|
||||
existing_pdf.info[b"Author"] = pdfParser.encode_text(author)
|
||||
existing_pdf.info["Author"] = author
|
||||
if subject:
|
||||
existing_pdf.info[b"Subject"] = pdfParser.encode_text(subject)
|
||||
existing_pdf.info["Subject"] = subject
|
||||
if keywords:
|
||||
existing_pdf.info[b"Keywords"] = pdfParser.encode_text(keywords)
|
||||
existing_pdf.info["Keywords"] = keywords
|
||||
if creator:
|
||||
existing_pdf.info[b"Creator"] = pdfParser.encode_text(creator)
|
||||
existing_pdf.info["Creator"] = creator
|
||||
if producer:
|
||||
existing_pdf.info[b"Producer"] = pdfParser.encode_text(producer)
|
||||
existing_pdf.info["Producer"] = producer
|
||||
|
||||
#
|
||||
# make sure image data is available
|
||||
|
|
|
@ -221,24 +221,10 @@ class PdfArray(list):
|
|||
|
||||
|
||||
class PdfDict(UserDict):
|
||||
#def __init__(self, *args, orig_ref=None, pdf=None, **kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
UserDict.__init__(self, *args, **kwargs)
|
||||
#self.orig_ref = kwargs.pop("orig_ref", None)
|
||||
#self.pdf = kwargs.pop("pdf", None)
|
||||
#self.is_changed = False
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self.is_changed = True
|
||||
UserDict.__setitem__(self, key, value)
|
||||
|
||||
def __bytes__(self):
|
||||
#if self.orig_ref is not None:
|
||||
# if self.is_changed:
|
||||
# if self.pdf is not None:
|
||||
# del self.pdf.xref_table[self.orig_ref.object_id]
|
||||
# else:
|
||||
# return bytes(self.orig_ref)
|
||||
out = bytearray(b"<<")
|
||||
for key, value in self.items():
|
||||
if value is None:
|
||||
|
@ -248,17 +234,12 @@ class PdfDict(UserDict):
|
|||
out.extend(bytes(PdfName(key)))
|
||||
out.extend(b" ")
|
||||
out.extend(value)
|
||||
#out += b"\n%s %s" % (PdfName(key), value)
|
||||
out.extend(b"\n>>")
|
||||
return bytes(out)
|
||||
#return out + b"\n>>"
|
||||
|
||||
__str__ = __bytes__
|
||||
|
||||
#def write(self, f, orig_ref=None, pdf=None):
|
||||
# self.orig_ref = orig_ref
|
||||
# self.pdf = pdf
|
||||
# f.write(bytes(self))
|
||||
if str == bytes:
|
||||
__str__ = __bytes__
|
||||
|
||||
|
||||
class PdfBinary:
|
||||
|
@ -290,7 +271,7 @@ def pdf_repr(x):
|
|||
elif isinstance(x, list):
|
||||
return bytes(PdfArray(x))
|
||||
elif isinstance(x, str) and str != bytes:
|
||||
return pdf_repr(x.encode("utf-8"))
|
||||
return pdf_repr(encode_text(x))
|
||||
elif isinstance(x, bytes):
|
||||
return b"(" + x.replace(b"\\", b"\\\\").replace(b"(", b"\\(").replace(b")", b"\\)") + b")" # XXX escape more chars? handle binary garbage
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue
Block a user