Adds Test, Removes Unnecessary Assumptions

pull/264/head
Fakabbir Amin 2019-07-17 11:38:00 +05:30
parent cc40af3d2b
commit fa400431f5
2 changed files with 61 additions and 19 deletions

View File

@ -128,14 +128,7 @@ class Type1FontHeaderParser(PSStackParser):
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-') NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
CMAP_ENCODER = { IDENTITY_ENCODER = ('Identity-H', 'Identity-V')
'DLIdent-H': 'Identity-H',
'OneByteIdentityH': 'Identity-H',
'Identity-H': 'Identity-H',
'DLIdent-V': 'Identity-V',
'OneByteIdentityV': 'Identity-V',
'Identity-V': 'Identity-V'
}
## CFFFont ## CFFFont
## (Format specified in Adobe Technical Note: #5176 ## (Format specified in Adobe Technical Note: #5176
@ -724,21 +717,14 @@ class PDFCIDFont(PDFFont):
cmap_name = 'unknown' cmap_name = 'unknown'
if type(cmap_name) is PDFStream: if type(cmap_name) is PDFStream:
if 'CMapName' in cmap_name: if 'CMapName' in cmap_name:
cmap_key = cmap_name.get('CMapName').cmap_name cmap_name = cmap_name.get('CMapName').name
try:
cmap_name = CMAP_ENCODER[cmap_key]
except:
cmap_name = cmap_key
raise PDFFontError('Unidentified encoding mentioned. %s is not supported' % cmap_name)
else: else:
if strict: if strict:
raise PDFFontError('Encoding is unspecified') raise PDFFontError('CMapName unspecified for encoding')
cmap_name = 'unknown' cmap_name = 'unknown'
try: if cmap_name in IDENTITY_ENCODER:
self.cmap = CMapDB.get_cmap(cmap_name) self.cmap = CMapDB.get_cmap(cmap_name)
except CMapDB.CMapNotFound as e: else:
if strict:
raise PDFFontError(e)
self.cmap = CMap() self.cmap = CMap()
def __repr__(self): def __repr__(self):

56
tests/test_pdfencoding.py Normal file
View File

@ -0,0 +1,56 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import nose, logging, os
from pdfminer.cmapdb import IdentityCMap, CMap
from pdfminer.pdffont import PDFCIDFont
from pdfminer.pdftypes import PDFStream
from pdfminer.psparser import PSLiteral
# 'DLIdent-H': 'Identity-H',
# 'OneByteIdentityH': 'Identity-H',
# 'Identity-H': 'Identity-H',
# 'DLIdent-V': 'Identity-V',
# 'OneByteIdentityV': 'Identity-V',
# 'Identity-V': 'Identity-V'
class TestPDFEncoding():
def test_cmapname_onebyteidentityV(self):
stream = PDFStream({'CMapName': PSLiteral('OneByteIdentityV')}, '')
spec = {'Encoding': stream}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, CMap)
def test_cmapname_onebyteidentityH(self):
stream = PDFStream({'CMapName': PSLiteral('OneByteIdentityH')}, '')
spec = {'Encoding': stream}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, CMap)
def test_cmapname_V(self):
stream = PDFStream({'CMapName': PSLiteral('V')}, '')
spec = {'Encoding': stream}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, CMap)
def test_cmapname_H(self):
stream = PDFStream({'CMapName': PSLiteral('H')}, '')
spec = {'Encoding': stream}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, CMap)
def test_encoding_identityH(self):
spec = {'Encoding': PSLiteral('Identity-H')}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, IdentityCMap)
def test_encoding_identityV(self):
spec = {'Encoding': PSLiteral('Identity-V')}
font = PDFCIDFont(None, spec)
assert isinstance(font.cmap, IdentityCMap)
if __name__ == '__main__':
nose.runmodule()