From 4d3b13fb08d949516b16ce7350e30bc3b9d77446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dvo=C5=99=C3=A1k=20V=C3=A1clav?= Date: Fri, 26 Jan 2018 12:11:10 +0100 Subject: [PATCH] issue #2959: improve Info setting and dumping --- Tests/test_file_pdf.py | 18 +++++++++--------- src/PIL/PdfImagePlugin.py | 12 ++++++------ src/PIL/pdfParser.py | 25 +++---------------------- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/Tests/test_file_pdf.py b/Tests/test_file_pdf.py index fc5ee2469..a5813bf7a 100644 --- a/Tests/test_file_pdf.py +++ b/Tests/test_file_pdf.py @@ -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") diff --git a/src/PIL/PdfImagePlugin.py b/src/PIL/PdfImagePlugin.py index c836175d3..2f1da8bd5 100644 --- a/src/PIL/PdfImagePlugin.py +++ b/src/PIL/PdfImagePlugin.py @@ -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 diff --git a/src/PIL/pdfParser.py b/src/PIL/pdfParser.py index 05e6a1c54..c64e13a06 100644 --- a/src/PIL/pdfParser.py +++ b/src/PIL/pdfParser.py @@ -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: