From f1a4dcea88e2ada84f737c23b12bf0d3f9a57c49 Mon Sep 17 00:00:00 2001 From: Fakabbir Amin Date: Wed, 24 Jul 2019 11:56:06 +0530 Subject: [PATCH] Adds Test Cases, Neater Code For CMap Assignment --- pdfminer/pdffont.py | 8 ++++---- tests/test_pdfencoding.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/pdfminer/pdffont.py b/pdfminer/pdffont.py index a09c5c4..9f24afb 100644 --- a/pdfminer/pdffont.py +++ b/pdfminer/pdffont.py @@ -649,7 +649,7 @@ class PDFCIDFont(PDFFont): self.cidsysteminfo = dict_value(spec.get('CIDSystemInfo', {})) self.cidcoding = '%s-%s' % (resolve1(self.cidsysteminfo.get('Registry', b'unknown')).decode("latin1"), resolve1(self.cidsysteminfo.get('Ordering', b'unknown')).decode("latin1")) - self.cmap_setter(spec, strict) + self.cmap = self.get_cmap_from_spec(spec, strict) try: descriptor = dict_value(spec['FontDescriptor']) @@ -697,7 +697,7 @@ class PDFCIDFont(PDFFont): PDFFont.__init__(self, descriptor, widths, default_width=default_width) return - def cmap_setter(self, spec, strict): + def get_cmap_from_spec(self, spec, strict): """ For certain PDFs, Encoding Type isn't mentioned as an attribute of Encoding but as an attribute of CMapName, where CMapName is an @@ -723,9 +723,9 @@ class PDFCIDFont(PDFFont): raise PDFFontError('CMapName unspecified for encoding') cmap_name = 'unknown' if cmap_name in IDENTITY_ENCODER: - self.cmap = CMapDB.get_cmap(cmap_name) + return CMapDB.get_cmap(cmap_name) else: - self.cmap = CMap() + return CMap() def __repr__(self): return '' % (self.basefont, self.cidcoding) diff --git a/tests/test_pdfencoding.py b/tests/test_pdfencoding.py index 9ed4e9e..396d12d 100644 --- a/tests/test_pdfencoding.py +++ b/tests/test_pdfencoding.py @@ -44,6 +44,34 @@ class TestPDFEncoding(): font = PDFCIDFont(None, spec) assert isinstance(font.cmap, IdentityCMap) + def test_encoding_identityH_as_PSLiteral_stream(self): + stream = PDFStream({'CMapName':PSLiteral('Identity-H')}, '') + spec = {'Encoding': stream} + font = PDFCIDFont(None, spec) + assert isinstance(font.cmap, IdentityCMap) + + def test_encoding_identityV_as_PSLiteral_stream(self): + stream = PDFStream({'CMapName':PSLiteral('Identity-V')}, '') + spec = {'Encoding': stream} + font = PDFCIDFont(None, spec) + assert isinstance(font.cmap, IdentityCMap) + + def test_encoding_identityH_as_stream(self): + stream = PDFStream({'CMapName':'Identity-H'}, '') + spec = {'Encoding': stream} + font = PDFCIDFont(None, spec) + assert isinstance(font.cmap, IdentityCMap) + + def test_encoding_identityV_as_stream(self): + stream = PDFStream({'CMapName':'Identity-V'}, '') + spec = {'Encoding': stream} + font = PDFCIDFont(None, spec) + assert isinstance(font.cmap, IdentityCMap) + + def test_font_without_spec(self): + font = PDFCIDFont(None, {}) + assert isinstance(font.cmap, CMap) + if __name__ == '__main__': nose.runmodule()