Adds Test Cases, Neater Code For CMap Assignment

pull/264/head
Fakabbir Amin 2019-07-24 11:56:06 +05:30
parent b4c261b647
commit f1a4dcea88
2 changed files with 32 additions and 4 deletions

View File

@ -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 '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)

View File

@ -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()