Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

625 support identity cmap #626

Merged
merged 18 commits into from
Oct 13, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pdfminer/cmapdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ def dump(self, out: TextIO = sys.stdout,

class IdentityCMap(CMapBase):

def get_unichr(self, cid):
log.debug('get_unichr: %r, %r', self, cid)
return chr(cid)

def decode(self, code: bytes) -> Tuple[int, ...]:
n = len(code)//2
if n:
Expand Down
14 changes: 11 additions & 3 deletions pdfminer/pdffont.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,17 @@ def __init__(
BytesIO(self.fontfile.get_data()))
self.unicode_map: Optional[UnicodeMap] = None
if 'ToUnicode' in spec:
strm = stream_value(spec['ToUnicode'])
self.unicode_map = FileUnicodeMap()
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
if isinstance(spec['ToUnicode'], PDFStream):
strm = stream_value(spec['ToUnicode'])
self.unicode_map = FileUnicodeMap()
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
else:
cmap_name = literal_name(spec['ToUnicode'])
encoding = literal_name(spec['Encoding'])
if 'Identity' in cid_ordering \
or 'Identity' in cmap_name \
or 'Identity' in encoding:
self.unicode_map = self.cmap
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
if ttf:
try:
Expand Down
Binary file added samples/contrib/issue-625-identity-cmap.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions tests/test_highlevel_extracttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def run_with_file(sample_path):
"simple4.pdf": "Text1\nText2\nText3\n\n\f",
"contrib/issue_566_test_1.pdf": "ISSUE Date:2019-4-25 Buyer:黎荣",
"contrib/issue_566_test_2.pdf": "甲方:中国饮料有限公司(盖章)",
"contrib/issue-625-identity-cmap.pdf": "Termin płatności: 2021-05-03",
}


Expand Down Expand Up @@ -92,6 +93,12 @@ def test_issue_566_cid_range(self):
s = run_with_file(test_file)
self.assertEqual(s.strip(), test_strings[test_file])

def test_issue_625_identity_cmap(self):
test_file = "contrib/issue-625-identity-cmap.pdf"
lines = run_with_file(test_file).splitlines()

self.assertEqual(lines[6], test_strings[test_file])


class TestExtractPages(unittest.TestCase):
def _get_test_file_path(self):
Expand Down