- moved parser files from old interpeter into parser folder
- renamed interpreter_v2 to just interpreter - removed legacy code
This commit is contained in:
1 parent
487f9b6c08
commit
3ba0b95001
81 files changed
+138
-2565
No files matched your search
@@ -1,44 +0,0 @@
|
||||
from ..utils import LATIN_ENCODING, name2unicode
|
||||
from .PSStackParser import PSLiteral
|
||||
|
||||
class EncodingDB(object):
|
||||
|
||||
std2unicode = {}
|
||||
mac2unicode = {}
|
||||
win2unicode = {}
|
||||
pdf2unicode = {}
|
||||
for (name, std, mac, win, pdf) in LATIN_ENCODING:
|
||||
c = name2unicode(name)
|
||||
if std:
|
||||
std2unicode[std] = c
|
||||
if mac:
|
||||
mac2unicode[mac] = c
|
||||
if win:
|
||||
win2unicode[win] = c
|
||||
if pdf:
|
||||
pdf2unicode[pdf] = c
|
||||
|
||||
encodings = {
|
||||
'StandardEncoding': std2unicode,
|
||||
'MacRomanEncoding': mac2unicode,
|
||||
'WinAnsiEncoding': win2unicode,
|
||||
'PDFDocEncoding': pdf2unicode,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def get_encoding(klass, name, diff=None):
|
||||
cid2unicode = klass.encodings.get(name, klass.std2unicode)
|
||||
if diff:
|
||||
cid2unicode = cid2unicode.copy()
|
||||
cid = 0
|
||||
for x in diff:
|
||||
if isinstance(x, int):
|
||||
cid = x
|
||||
elif isinstance(x, PSLiteral):
|
||||
try:
|
||||
cid2unicode[cid] = name2unicode(x.name)
|
||||
except KeyError:
|
||||
pass
|
||||
cid += 1
|
||||
return cid2unicode
|
||||
|
||||
@@ -1,109 +0,0 @@
|
||||
import re
|
||||
import operator
|
||||
|
||||
from typing import List
|
||||
from io import BytesIO
|
||||
|
||||
from ..utils import int2byte, choplist, settings
|
||||
|
||||
from .PSStackParser import literal_name
|
||||
from .PSStackParser import PSStackParser
|
||||
from .PSStackParser import PSEOF, PSTypeError
|
||||
from .PSStackParser import KWD
|
||||
|
||||
from .PDFStream import PDFStream
|
||||
|
||||
class PDFContentParser(PSStackParser):
|
||||
|
||||
def __init__(self, streams: List[PDFStream]):
|
||||
self.streams = streams
|
||||
self.istream = 0
|
||||
PSStackParser.__init__(self, None)
|
||||
return
|
||||
|
||||
def fillfp(self):
|
||||
if not self.fp:
|
||||
if self.istream < len(self.streams):
|
||||
strm = PDFStream.validated_stream(self.streams[self.istream])
|
||||
self.istream += 1
|
||||
else:
|
||||
raise PSEOF('Unexpected EOF, file truncated?')
|
||||
self.fp = BytesIO(strm.get_data())
|
||||
return
|
||||
|
||||
def seek(self, pos):
|
||||
self.fillfp()
|
||||
PSStackParser.seek(self, pos)
|
||||
return
|
||||
|
||||
def fillbuf(self):
|
||||
if self.charpos < len(self.buf):
|
||||
return
|
||||
while 1:
|
||||
self.fillfp()
|
||||
self.bufpos = self.fp.tell()
|
||||
self.buf = self.fp.read(self.BUFSIZ)
|
||||
if self.buf:
|
||||
break
|
||||
self.fp = None
|
||||
self.charpos = 0
|
||||
return
|
||||
|
||||
def get_inline_data(self, pos, target=b'EI'):
|
||||
self.seek(pos)
|
||||
i = 0
|
||||
data = b''
|
||||
while i <= len(target):
|
||||
self.fillbuf()
|
||||
if i:
|
||||
c = operator.getitem(self.buf,self.charpos)
|
||||
c = int2byte(c)
|
||||
data += c
|
||||
self.charpos += 1
|
||||
if len(target) <= i and c.isspace():
|
||||
i += 1
|
||||
elif i < len(target) and c == (int2byte(target[i])):
|
||||
i += 1
|
||||
else:
|
||||
i = 0
|
||||
else:
|
||||
try:
|
||||
j = self.buf.index(target[0], self.charpos)
|
||||
data += self.buf[self.charpos:j+1]
|
||||
self.charpos = j+1
|
||||
i = 1
|
||||
except ValueError:
|
||||
data += self.buf[self.charpos:]
|
||||
self.charpos = len(self.buf)
|
||||
data = data[:-(len(target)+1)] # strip the last part
|
||||
data = re.sub(br'(\x0d\x0a|[\x0d\x0a])$', b'', data)
|
||||
return (pos, data)
|
||||
|
||||
def flush(self):
|
||||
self.add_results(*self.popall())
|
||||
return
|
||||
|
||||
KEYWORD_BI = KWD(b'BI')
|
||||
KEYWORD_ID = KWD(b'ID')
|
||||
KEYWORD_EI = KWD(b'EI')
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_BI:
|
||||
# inline image within a content stream
|
||||
self.start_type(pos, 'inline')
|
||||
elif token is self.KEYWORD_ID:
|
||||
try:
|
||||
(_, objs) = self.end_type('inline')
|
||||
if len(objs) % 2 != 0:
|
||||
raise PSTypeError('Invalid dictionary construct: %r' % objs)
|
||||
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs))
|
||||
(pos, data) = self.get_inline_data(pos+len(b'ID '))
|
||||
obj = PDFStream(d, data)
|
||||
self.push((pos, obj))
|
||||
self.push((pos, self.KEYWORD_EI))
|
||||
except PSTypeError:
|
||||
if settings.STRICT:
|
||||
raise
|
||||
else:
|
||||
self.push((pos, token))
|
||||
return
|
||||
@@ -1,235 +0,0 @@
|
||||
import hashlib as md5
|
||||
|
||||
from Crypto.Cipher import ARC4, AES
|
||||
from Crypto.Hash import SHA256
|
||||
|
||||
from .exceptions import * # pylint: disable=W0614
|
||||
from ..PDFStream import int_value, str_value, dict_value
|
||||
from ..PSStackParser import literal_name
|
||||
from ...utils import int2byte, struct
|
||||
|
||||
## PDFSecurityHandler
|
||||
##
|
||||
class PDFStandardSecurityHandler(object):
|
||||
|
||||
PASSWORD_PADDING = (b'(\xbfN^Nu\x8aAd\x00NV\xff\xfa\x01\x08'
|
||||
b'..\x00\xb6\xd0h>\x80/\x0c\xa9\xfedSiz')
|
||||
supported_revisions = (2, 3)
|
||||
|
||||
def __init__(self, docid, param, password=''):
|
||||
self.docid = docid
|
||||
self.param = param
|
||||
self.password = password
|
||||
self.encrypt_metadata = False
|
||||
self.init()
|
||||
return
|
||||
|
||||
def init(self):
|
||||
self.init_params()
|
||||
if self.r not in self.supported_revisions:
|
||||
raise PDFEncryptionError('Unsupported revision: param=%r' % self.param)
|
||||
self.init_key()
|
||||
return
|
||||
|
||||
def init_params(self):
|
||||
self.v = int_value(self.param.get('V', 0))
|
||||
self.r = int_value(self.param['R'])
|
||||
self.p = int_value(self.param['P'])
|
||||
self.o = str_value(self.param['O'])
|
||||
self.u = str_value(self.param['U'])
|
||||
self.length = int_value(self.param.get('Length', 40))
|
||||
return
|
||||
|
||||
def init_key(self):
|
||||
self.key = self.authenticate(self.password)
|
||||
if self.key is None:
|
||||
raise PDFPasswordIncorrect
|
||||
return
|
||||
|
||||
def is_printable(self):
|
||||
return bool(self.p & 4)
|
||||
|
||||
def is_modifiable(self):
|
||||
return bool(self.p & 8)
|
||||
|
||||
def is_extractable(self):
|
||||
return bool(self.p & 16)
|
||||
|
||||
def compute_u(self, key):
|
||||
if self.r == 2:
|
||||
# Algorithm 3.4
|
||||
return ARC4.new(key).encrypt(self.PASSWORD_PADDING) # 2
|
||||
else:
|
||||
# Algorithm 3.5
|
||||
hash = md5.md5(self.PASSWORD_PADDING) # 2
|
||||
hash.update(self.docid[0]) # 3
|
||||
result = ARC4.new(key).encrypt(hash.digest()) # 4
|
||||
for i in range(1, 20): # 5
|
||||
k = b''.join(int2byte(c ^ i) for c in iter(key))
|
||||
result = ARC4.new(k).encrypt(result)
|
||||
result += result # 6
|
||||
return result
|
||||
|
||||
def compute_encryption_key(self, password):
|
||||
# Algorithm 3.2
|
||||
password = (password + self.PASSWORD_PADDING)[:32] # 1
|
||||
hash = md5.md5(password) # 2
|
||||
hash.update(self.o) # 3
|
||||
hash.update(struct.pack('<l', self.p)) # 4
|
||||
hash.update(self.docid[0]) # 5
|
||||
if self.r >= 4:
|
||||
if not self.encrypt_metadata:
|
||||
hash.update(b'\xff\xff\xff\xff')
|
||||
result = hash.digest()
|
||||
n = 5
|
||||
if self.r >= 3:
|
||||
n = self.length // 8
|
||||
for _ in range(50):
|
||||
result = md5.md5(result[:n]).digest()
|
||||
return result[:n]
|
||||
|
||||
def authenticate(self, password):
|
||||
password = password.encode("latin1")
|
||||
key = self.authenticate_user_password(password)
|
||||
if key is None:
|
||||
key = self.authenticate_owner_password(password)
|
||||
return key
|
||||
|
||||
def authenticate_user_password(self, password):
|
||||
key = self.compute_encryption_key(password)
|
||||
if self.verify_encryption_key(key):
|
||||
return key
|
||||
else:
|
||||
return None
|
||||
|
||||
def verify_encryption_key(self, key):
|
||||
# Algorithm 3.6
|
||||
u = self.compute_u(key)
|
||||
if self.r == 2:
|
||||
return u == self.u
|
||||
return u[:16] == self.u[:16]
|
||||
|
||||
def authenticate_owner_password(self, password):
|
||||
# Algorithm 3.7
|
||||
password = (password + self.PASSWORD_PADDING)[:32]
|
||||
hash = md5.md5(password)
|
||||
if self.r >= 3:
|
||||
for _ in range(50):
|
||||
hash = md5.md5(hash.digest())
|
||||
n = 5
|
||||
if self.r >= 3:
|
||||
n = self.length // 8
|
||||
key = hash.digest()[:n]
|
||||
if self.r == 2:
|
||||
user_password = ARC4.new(key).decrypt(self.o)
|
||||
else:
|
||||
user_password = self.o
|
||||
for i in range(19, -1, -1):
|
||||
k = b''.join(int2byte(c ^ i) for c in iter(key))
|
||||
user_password = ARC4.new(k).decrypt(user_password)
|
||||
return self.authenticate_user_password(user_password)
|
||||
|
||||
def decrypt(self, objid, genno, data, attrs=None):
|
||||
return self.decrypt_rc4(objid, genno, data)
|
||||
|
||||
def decrypt_rc4(self, objid, genno, data):
|
||||
key = self.key + struct.pack('<L', objid)[:3] + struct.pack('<L', genno)[:2]
|
||||
hash = md5.md5(key)
|
||||
key = hash.digest()[:min(len(key), 16)]
|
||||
return ARC4.new(key).decrypt(data)
|
||||
|
||||
|
||||
class PDFStandardSecurityHandlerV4(PDFStandardSecurityHandler):
|
||||
|
||||
supported_revisions = (4,)
|
||||
|
||||
def init_params(self):
|
||||
super(PDFStandardSecurityHandlerV4, self).init_params()
|
||||
self.length = 128
|
||||
self.cf = dict_value(self.param.get('CF'))
|
||||
self.stmf = literal_name(self.param['StmF'])
|
||||
self.strf = literal_name(self.param['StrF'])
|
||||
self.encrypt_metadata = bool(self.param.get('EncryptMetadata', True))
|
||||
if self.stmf != self.strf:
|
||||
raise PDFEncryptionError('Unsupported crypt filter: param=%r' % self.param)
|
||||
self.cfm = {}
|
||||
for k, v in self.cf.items():
|
||||
f = self.get_cfm(literal_name(v['CFM']))
|
||||
if f is None:
|
||||
raise PDFEncryptionError('Unknown crypt filter method: param=%r' % self.param)
|
||||
self.cfm[k] = f
|
||||
self.cfm['Identity'] = self.decrypt_identity
|
||||
if self.strf not in self.cfm:
|
||||
raise PDFEncryptionError('Undefined crypt filter: param=%r' % self.param)
|
||||
return
|
||||
|
||||
def get_cfm(self, name):
|
||||
if name == 'V2':
|
||||
return self.decrypt_rc4
|
||||
elif name == 'AESV2':
|
||||
return self.decrypt_aes128
|
||||
else:
|
||||
return None
|
||||
|
||||
def decrypt(self, objid, genno, data, attrs=None, name=None):
|
||||
if not self.encrypt_metadata and attrs is not None:
|
||||
t = attrs.get('Type')
|
||||
if t is not None and literal_name(t) == 'Metadata':
|
||||
return data
|
||||
if name is None:
|
||||
name = self.strf
|
||||
return self.cfm[name](objid, genno, data)
|
||||
|
||||
def decrypt_identity(self, objid, genno, data):
|
||||
return data
|
||||
|
||||
def decrypt_aes128(self, objid, genno, data):
|
||||
key = self.key + struct.pack('<L', objid)[:3] + struct.pack('<L', genno)[:2] + b'sAlT'
|
||||
hash = md5.md5(key)
|
||||
key = hash.digest()[:min(len(key), 16)]
|
||||
return AES.new(key, mode=AES.MODE_CBC, IV=data[:16]).decrypt(data[16:])
|
||||
|
||||
|
||||
class PDFStandardSecurityHandlerV5(PDFStandardSecurityHandlerV4):
|
||||
|
||||
supported_revisions = (5,)
|
||||
|
||||
def init_params(self):
|
||||
super(PDFStandardSecurityHandlerV5, self).init_params()
|
||||
self.length = 256
|
||||
self.oe = str_value(self.param['OE'])
|
||||
self.ue = str_value(self.param['UE'])
|
||||
self.o_hash = self.o[:32]
|
||||
self.o_validation_salt = self.o[32:40]
|
||||
self.o_key_salt = self.o[40:]
|
||||
self.u_hash = self.u[:32]
|
||||
self.u_validation_salt = self.u[32:40]
|
||||
self.u_key_salt = self.u[40:]
|
||||
return
|
||||
|
||||
def get_cfm(self, name):
|
||||
if name == 'AESV3':
|
||||
return self.decrypt_aes256
|
||||
else:
|
||||
return None
|
||||
|
||||
def authenticate(self, password):
|
||||
password = password.encode('utf-8')[:127]
|
||||
hash = SHA256.new(password)
|
||||
hash.update(self.o_validation_salt)
|
||||
hash.update(self.u)
|
||||
if hash.digest() == self.o_hash:
|
||||
hash = SHA256.new(password)
|
||||
hash.update(self.o_key_salt)
|
||||
hash.update(self.u)
|
||||
return AES.new(hash.digest(), mode=AES.MODE_CBC, IV=b'\x00' * 16).decrypt(self.oe)
|
||||
hash = SHA256.new(password)
|
||||
hash.update(self.u_validation_salt)
|
||||
if hash.digest() == self.u_hash:
|
||||
hash = SHA256.new(password)
|
||||
hash.update(self.u_key_salt)
|
||||
return AES.new(hash.digest(), mode=AES.MODE_CBC, IV=b'\x00' * 16).decrypt(self.ue)
|
||||
return None
|
||||
|
||||
def decrypt_aes256(self, objid, genno, data):
|
||||
return AES.new(self.key, mode=AES.MODE_CBC, IV=data[:16]).decrypt(data[16:])
|
||||
@@ -1,232 +0,0 @@
|
||||
import re
|
||||
from logging import getLogger
|
||||
|
||||
from ...utils import settings, choplist, nunpack
|
||||
from ..PSStackParser import PSEOF
|
||||
from ..PSStackParser import KWD
|
||||
from ..PDFParser import PDFStreamParser
|
||||
from ..PDFStream import PDFStream
|
||||
from ..PDFStream import dict_value
|
||||
|
||||
from .constants import LITERAL_OBJSTM, LITERAL_XREF
|
||||
from .exceptions import PDFNoValidXRef, PDFSyntaxError
|
||||
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
## XRefs
|
||||
##
|
||||
class PDFBaseXRef(object):
|
||||
|
||||
def get_trailer(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_objids(self):
|
||||
return []
|
||||
|
||||
# Must return
|
||||
# (strmid, index, genno)
|
||||
# or (None, pos, genno)
|
||||
def get_pos(self, objid):
|
||||
raise KeyError(objid)
|
||||
|
||||
|
||||
## PDFXRef
|
||||
##
|
||||
class PDFXRef(PDFBaseXRef):
|
||||
|
||||
def __init__(self):
|
||||
self.offsets = {}
|
||||
self.trailer = {}
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFXRef: offsets=%r>' % (self.offsets.keys())
|
||||
|
||||
def load(self, parser):
|
||||
while True:
|
||||
try:
|
||||
(pos, line) = parser.nextline()
|
||||
if not line.strip():
|
||||
continue
|
||||
except PSEOF:
|
||||
raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
|
||||
if not line:
|
||||
raise PDFNoValidXRef('Premature eof: %r' % parser)
|
||||
if line.startswith(b'trailer'):
|
||||
parser.seek(pos)
|
||||
break
|
||||
f = line.strip().split(b' ')
|
||||
if len(f) != 2:
|
||||
raise PDFNoValidXRef('Trailer not found: %r: line=%r' % (parser, line))
|
||||
try:
|
||||
(start, nobjs) = map(int, f)
|
||||
except ValueError:
|
||||
raise PDFNoValidXRef('Invalid line: %r: line=%r' % (parser, line))
|
||||
for objid in range(start, start+nobjs):
|
||||
try:
|
||||
(_, line) = parser.nextline()
|
||||
except PSEOF:
|
||||
raise PDFNoValidXRef('Unexpected EOF - file corrupted?')
|
||||
f = line.strip().split(b' ')
|
||||
if len(f) != 3:
|
||||
raise PDFNoValidXRef('Invalid XRef format: %r, line=%r' % (parser, line))
|
||||
(pos, genno, use) = f
|
||||
if use != b'n':
|
||||
continue
|
||||
self.offsets[objid] = (None, int(pos), int(genno))
|
||||
log.info('xref objects: %r', self.offsets)
|
||||
self.load_trailer(parser)
|
||||
return
|
||||
|
||||
def load_trailer(self, parser):
|
||||
try:
|
||||
(_, kwd) = parser.nexttoken()
|
||||
assert kwd is KWD(b'trailer'), str(kwd)
|
||||
(_, dic) = parser.nextobject()
|
||||
except PSEOF:
|
||||
x = parser.pop(1)
|
||||
if not x:
|
||||
raise PDFNoValidXRef('Unexpected EOF - file corrupted')
|
||||
(_, dic) = x[0]
|
||||
self.trailer.update(dict_value(dic))
|
||||
log.debug('trailer=%r', self.trailer)
|
||||
return
|
||||
|
||||
def get_trailer(self):
|
||||
return self.trailer
|
||||
|
||||
def get_objids(self):
|
||||
return iter(self.offsets.keys())
|
||||
|
||||
def get_pos(self, objid):
|
||||
try:
|
||||
return self.offsets[objid]
|
||||
except KeyError:
|
||||
raise
|
||||
|
||||
|
||||
## PDFXRefFallback
|
||||
##
|
||||
class PDFXRefFallback(PDFXRef):
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFXRefFallback: offsets=%r>' % (self.offsets.keys())
|
||||
|
||||
PDFOBJ_CUE = re.compile(r'^(\d+)\s+(\d+)\s+obj\b')
|
||||
|
||||
def load(self, parser):
|
||||
parser.seek(0)
|
||||
while 1:
|
||||
try:
|
||||
(pos, line) = parser.nextline()
|
||||
except PSEOF:
|
||||
break
|
||||
if line.startswith(b'trailer'):
|
||||
parser.seek(pos)
|
||||
self.load_trailer(parser)
|
||||
log.info('trailer: %r', self.trailer)
|
||||
break
|
||||
line=line.decode('latin-1') #default pdf encoding
|
||||
m = self.PDFOBJ_CUE.match(line)
|
||||
if not m:
|
||||
continue
|
||||
(objid, genno) = m.groups()
|
||||
objid = int(objid)
|
||||
genno = int(genno)
|
||||
self.offsets[objid] = (None, pos, genno)
|
||||
# expand ObjStm.
|
||||
parser.seek(pos)
|
||||
(_, obj) = parser.nextobject()
|
||||
if isinstance(obj, PDFStream) and obj.get('Type') is LITERAL_OBJSTM:
|
||||
stream = PDFStream.validated_stream(obj)
|
||||
try:
|
||||
n = stream['N']
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('N is not defined: %r' % stream)
|
||||
n = 0
|
||||
parser1 = PDFStreamParser(stream.get_data())
|
||||
objs = []
|
||||
try:
|
||||
while 1:
|
||||
(_, obj) = parser1.nextobject()
|
||||
objs.append(obj)
|
||||
except PSEOF:
|
||||
pass
|
||||
n = min(n, len(objs)//2)
|
||||
for index in range(n):
|
||||
objid1 = objs[index*2]
|
||||
self.offsets[objid1] = (objid, index, 0)
|
||||
return
|
||||
|
||||
## PDFXRefStream
|
||||
##
|
||||
class PDFXRefStream(PDFBaseXRef):
|
||||
|
||||
def __init__(self):
|
||||
self.data = None
|
||||
self.entlen = None
|
||||
self.fl1 = self.fl2 = self.fl3 = None
|
||||
self.ranges = []
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFXRefStream: ranges=%r>' % (self.ranges)
|
||||
|
||||
def load(self, parser):
|
||||
(_, objid) = parser.nexttoken() # ignored
|
||||
(_, genno) = parser.nexttoken() # ignored
|
||||
(_, kwd) = parser.nexttoken()
|
||||
(_, stream) = parser.nextobject()
|
||||
if not isinstance(stream, PDFStream) or stream['Type'] is not LITERAL_XREF:
|
||||
raise PDFNoValidXRef('Invalid PDF stream spec.')
|
||||
size = stream['Size']
|
||||
index_array = stream.get('Index', (0, size))
|
||||
if len(index_array) % 2 != 0:
|
||||
raise PDFSyntaxError('Invalid index number')
|
||||
self.ranges.extend(choplist(2, index_array))
|
||||
(self.fl1, self.fl2, self.fl3) = stream['W']
|
||||
self.data = stream.get_data()
|
||||
self.entlen = self.fl1+self.fl2+self.fl3
|
||||
self.trailer = stream.attrs
|
||||
log.info('xref stream: objid=%s, fields=%d,%d,%d',
|
||||
', '.join(map(repr, self.ranges)),
|
||||
self.fl1, self.fl2, self.fl3)
|
||||
return
|
||||
|
||||
def get_trailer(self):
|
||||
return self.trailer
|
||||
|
||||
def get_objids(self):
|
||||
for (start, nobjs) in self.ranges:
|
||||
for i in range(nobjs):
|
||||
offset = self.entlen * i
|
||||
ent = self.data[offset:offset+self.entlen]
|
||||
f1 = nunpack(ent[:self.fl1], 1)
|
||||
if f1 == 1 or f1 == 2:
|
||||
yield start+i
|
||||
return
|
||||
|
||||
def get_pos(self, objid):
|
||||
index = 0
|
||||
for (start, nobjs) in self.ranges:
|
||||
if start <= objid and objid < start+nobjs:
|
||||
index += objid - start
|
||||
break
|
||||
else:
|
||||
index += nobjs
|
||||
else:
|
||||
raise KeyError(objid)
|
||||
offset = self.entlen * index
|
||||
ent = self.data[offset:offset+self.entlen]
|
||||
f1 = nunpack(ent[:self.fl1], 1)
|
||||
f2 = nunpack(ent[self.fl1:self.fl1+self.fl2])
|
||||
f3 = nunpack(ent[self.fl1+self.fl2:])
|
||||
if f1 == 1:
|
||||
return (None, f2, f3)
|
||||
elif f1 == 2:
|
||||
return (f2, f3, 0)
|
||||
else:
|
||||
# this is a free object
|
||||
raise KeyError(objid)
|
||||
@@ -1,324 +0,0 @@
|
||||
import hashlib as md5
|
||||
|
||||
from Crypto.Cipher import ARC4, AES
|
||||
from Crypto.Hash import SHA256
|
||||
from logging import getLogger
|
||||
|
||||
from ...utils import settings, choplist, decode_text
|
||||
from ..PSStackParser import KWD
|
||||
from ..PSStackParser import literal_name
|
||||
from ..PSStackParser import PSEOF
|
||||
from ..PDFStream import int_value, str_value, dict_value, list_value, decipher_all
|
||||
from ..PDFStream import PDFObjectNotFound, PDFTypeError
|
||||
from ..PDFParser import PDFStreamParser, PDFStream, PDFSyntaxError
|
||||
|
||||
from .exceptions import *
|
||||
from .PDFSecurityHandler import PDFStandardSecurityHandler
|
||||
from .PDFSecurityHandler import PDFStandardSecurityHandlerV4
|
||||
from .PDFSecurityHandler import PDFStandardSecurityHandlerV5
|
||||
from .PDFXRef import PDFXRefFallback, PDFXRef, PDFXRefStream
|
||||
|
||||
from .constants import LITERAL_CATALOG, LITERAL_OBJSTM
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
## PDFDocument
|
||||
##
|
||||
class PDFDocument(object):
|
||||
|
||||
"""PDFDocument object represents a PDF document.
|
||||
|
||||
Since a PDF file can be very big, normally it is not loaded at
|
||||
once. So PDF document has to cooperate with a PDF parser in order to
|
||||
dynamically import the data as processing goes.
|
||||
|
||||
Typical usage:
|
||||
doc = PDFDocument(parser, password)
|
||||
obj = doc.getobj(objid)
|
||||
|
||||
"""
|
||||
|
||||
security_handler_registry = {
|
||||
1: PDFStandardSecurityHandler,
|
||||
2: PDFStandardSecurityHandler,
|
||||
}
|
||||
if AES is not None:
|
||||
security_handler_registry[4] = PDFStandardSecurityHandlerV4
|
||||
if SHA256 is not None:
|
||||
security_handler_registry[5] = PDFStandardSecurityHandlerV5
|
||||
|
||||
def __init__(self, parser, password='', caching=True, fallback=True):
|
||||
"Set the document to use a given PDFParser object."
|
||||
self.caching = caching
|
||||
self.xrefs = []
|
||||
self.info = []
|
||||
self.catalog = None
|
||||
self.encryption = None
|
||||
self.decipher = None
|
||||
self._parser = None
|
||||
self._cached_objs = {}
|
||||
self._parsed_objs = {}
|
||||
self._parser = parser
|
||||
self._parser.set_document(self)
|
||||
self.is_printable = self.is_modifiable = self.is_extractable = True
|
||||
# Retrieve the information of each header that was appended
|
||||
# (maybe multiple times) at the end of the document.
|
||||
try:
|
||||
pos = self.find_xref(parser)
|
||||
self.read_xref_from(parser, pos, self.xrefs)
|
||||
except PDFNoValidXRef:
|
||||
pass # fallback = True
|
||||
if fallback:
|
||||
parser.fallback = True
|
||||
xref = PDFXRefFallback()
|
||||
xref.load(parser)
|
||||
self.xrefs.append(xref)
|
||||
for xref in self.xrefs:
|
||||
trailer = xref.get_trailer()
|
||||
if not trailer:
|
||||
continue
|
||||
# If there's an encryption info, remember it.
|
||||
if 'Encrypt' in trailer:
|
||||
#assert not self.encryption, str(self.encryption)
|
||||
self.encryption = (list_value(trailer['ID']),
|
||||
dict_value(trailer['Encrypt']))
|
||||
self._initialize_password(password)
|
||||
if 'Info' in trailer:
|
||||
self.info.append(dict_value(trailer['Info']))
|
||||
if 'Root' in trailer:
|
||||
# Every PDF file must have exactly one /Root dictionary.
|
||||
self.catalog = dict_value(trailer['Root'])
|
||||
break
|
||||
else:
|
||||
raise PDFSyntaxError('No /Root object! - Is this really a PDF?')
|
||||
if self.catalog.get('Type') is not LITERAL_CATALOG:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('Catalog not found!')
|
||||
return
|
||||
|
||||
KEYWORD_OBJ = KWD(b'obj')
|
||||
|
||||
# _initialize_password(password=b'')
|
||||
# Perform the initialization with a given password.
|
||||
def _initialize_password(self, password=''):
|
||||
(docid, param) = self.encryption
|
||||
if literal_name(param.get('Filter')) != 'Standard':
|
||||
raise PDFEncryptionError('Unknown filter: param=%r' % param)
|
||||
v = int_value(param.get('V', 0))
|
||||
factory = self.security_handler_registry.get(v)
|
||||
if factory is None:
|
||||
raise PDFEncryptionError('Unknown algorithm: param=%r' % param)
|
||||
handler = factory(docid, param, password)
|
||||
self.decipher = handler.decrypt
|
||||
self.is_printable = handler.is_printable()
|
||||
self.is_modifiable = handler.is_modifiable()
|
||||
self.is_extractable = handler.is_extractable()
|
||||
self._parser.fallback = False # need to read streams with exact length
|
||||
return
|
||||
|
||||
def _getobj_objstm(self, stream, index, objid):
|
||||
if stream.objid in self._parsed_objs:
|
||||
(objs, n) = self._parsed_objs[stream.objid]
|
||||
else:
|
||||
(objs, n) = self._get_objects(stream)
|
||||
if self.caching:
|
||||
self._parsed_objs[stream.objid] = (objs, n)
|
||||
i = n*2+index
|
||||
try:
|
||||
obj = objs[i]
|
||||
except IndexError:
|
||||
raise PDFSyntaxError('index too big: %r' % index)
|
||||
return obj
|
||||
|
||||
def _get_objects(self, stream):
|
||||
if stream.get('Type') is not LITERAL_OBJSTM:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('Not a stream object: %r' % stream)
|
||||
try:
|
||||
n = stream['N']
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('N is not defined: %r' % stream)
|
||||
n = 0
|
||||
parser = PDFStreamParser(stream.get_data())
|
||||
parser.set_document(self)
|
||||
objs = []
|
||||
try:
|
||||
while 1:
|
||||
(_, obj) = parser.nextobject()
|
||||
objs.append(obj)
|
||||
except PSEOF:
|
||||
pass
|
||||
return (objs, n)
|
||||
|
||||
def _getobj_parse(self, pos, objid):
|
||||
self._parser.seek(pos)
|
||||
(_, objid1) = self._parser.nexttoken() # objid
|
||||
(_, genno) = self._parser.nexttoken() # genno
|
||||
(_, kwd) = self._parser.nexttoken()
|
||||
# #### hack around malformed pdf files
|
||||
# copied from https://github.com/jaepil/pdfminer3k/blob/master/pdfminer/pdfparser.py#L399
|
||||
#to solve https://github.com/pdfminer/pdfminer.six/issues/56
|
||||
#assert objid1 == objid, str((objid1, objid))
|
||||
if objid1 != objid:
|
||||
x = []
|
||||
while kwd is not self.KEYWORD_OBJ:
|
||||
(_,kwd) = self._parser.nexttoken()
|
||||
x.append(kwd)
|
||||
if x:
|
||||
objid1 = x[-2]
|
||||
genno = x[-1]
|
||||
# #### end hack around malformed pdf files
|
||||
if objid1 != objid:
|
||||
raise PDFSyntaxError('objid mismatch: %r=%r' % (objid1, objid))
|
||||
|
||||
if kwd != KWD(b'obj'):
|
||||
raise PDFSyntaxError('Invalid object spec: offset=%r' % pos)
|
||||
(_, obj) = self._parser.nextobject()
|
||||
return obj
|
||||
|
||||
# can raise PDFObjectNotFound
|
||||
def getobj(self, objid):
|
||||
assert objid != 0
|
||||
if not self.xrefs:
|
||||
raise PDFException('PDFDocument is not initialized')
|
||||
log.debug('getobj: objid=%r', objid)
|
||||
if objid in self._cached_objs:
|
||||
(obj, genno) = self._cached_objs[objid]
|
||||
else:
|
||||
for xref in self.xrefs:
|
||||
try:
|
||||
(strmid, index, genno) = xref.get_pos(objid)
|
||||
except KeyError:
|
||||
continue
|
||||
try:
|
||||
if strmid is not None:
|
||||
stream = PDFStream.validated_stream(self.getobj(strmid))
|
||||
obj = self._getobj_objstm(stream, index, objid)
|
||||
else:
|
||||
obj = self._getobj_parse(index, objid)
|
||||
if self.decipher:
|
||||
obj = decipher_all(self.decipher, objid, genno, obj)
|
||||
|
||||
if isinstance(obj, PDFStream):
|
||||
obj.set_objid(objid, genno)
|
||||
break
|
||||
except (PSEOF, PDFSyntaxError):
|
||||
continue
|
||||
else:
|
||||
raise PDFObjectNotFound(objid)
|
||||
log.debug('register: objid=%r: %r', objid, obj)
|
||||
if self.caching:
|
||||
self._cached_objs[objid] = (obj, genno)
|
||||
return obj
|
||||
|
||||
def get_outlines(self):
|
||||
if 'Outlines' not in self.catalog:
|
||||
raise PDFNoOutlines
|
||||
|
||||
def search(entry, level):
|
||||
entry = dict_value(entry)
|
||||
if 'Title' in entry:
|
||||
if 'A' in entry or 'Dest' in entry:
|
||||
title = decode_text(str_value(entry['Title']))
|
||||
dest = entry.get('Dest')
|
||||
action = entry.get('A')
|
||||
se = entry.get('SE')
|
||||
yield (level, title, dest, action, se)
|
||||
if 'First' in entry and 'Last' in entry:
|
||||
for x in search(entry['First'], level+1):
|
||||
yield x
|
||||
if 'Next' in entry:
|
||||
for x in search(entry['Next'], level):
|
||||
yield x
|
||||
return
|
||||
return search(self.catalog['Outlines'], 0)
|
||||
|
||||
def lookup_name(self, cat, key):
|
||||
try:
|
||||
names = dict_value(self.catalog['Names'])
|
||||
except (PDFTypeError, KeyError):
|
||||
raise KeyError((cat, key))
|
||||
# may raise KeyError
|
||||
d0 = dict_value(names[cat])
|
||||
|
||||
def lookup(d):
|
||||
if 'Limits' in d:
|
||||
(k1, k2) = list_value(d['Limits'])
|
||||
if key < k1 or k2 < key:
|
||||
return None
|
||||
if 'Names' in d:
|
||||
objs = list_value(d['Names'])
|
||||
names = dict(choplist(2, objs))
|
||||
return names[key]
|
||||
if 'Kids' in d:
|
||||
for c in list_value(d['Kids']):
|
||||
v = lookup(dict_value(c))
|
||||
if v:
|
||||
return v
|
||||
raise KeyError((cat, key))
|
||||
return lookup(d0)
|
||||
|
||||
def get_dest(self, name):
|
||||
try:
|
||||
# PDF-1.2 or later
|
||||
obj = self.lookup_name('Dests', name)
|
||||
except KeyError:
|
||||
# PDF-1.1 or prior
|
||||
if 'Dests' not in self.catalog:
|
||||
raise PDFDestinationNotFound(name)
|
||||
d0 = dict_value(self.catalog['Dests'])
|
||||
if name not in d0:
|
||||
raise PDFDestinationNotFound(name)
|
||||
obj = d0[name]
|
||||
return obj
|
||||
|
||||
# find_xref
|
||||
def find_xref(self, parser):
|
||||
"""Internal function used to locate the first XRef."""
|
||||
# search the last xref table by scanning the file backwards.
|
||||
prev = None
|
||||
for line in parser.revreadlines():
|
||||
line = line.strip()
|
||||
log.debug('find_xref: %r', line)
|
||||
if line == b'startxref':
|
||||
break
|
||||
if line:
|
||||
prev = line
|
||||
else:
|
||||
raise PDFNoValidXRef('Unexpected EOF')
|
||||
log.info('xref found: pos=%r', prev)
|
||||
return int(prev)
|
||||
|
||||
# read xref table
|
||||
def read_xref_from(self, parser, start, xrefs):
|
||||
"""Reads XRefs from the given location."""
|
||||
parser.seek(start)
|
||||
parser.reset()
|
||||
try:
|
||||
(pos, token) = parser.nexttoken()
|
||||
except PSEOF:
|
||||
raise PDFNoValidXRef('Unexpected EOF')
|
||||
log.info('read_xref_from: start=%d, token=%r', start, token)
|
||||
if isinstance(token, int):
|
||||
# XRefStream: PDF-1.5
|
||||
parser.seek(pos)
|
||||
parser.reset()
|
||||
xref = PDFXRefStream()
|
||||
xref.load(parser)
|
||||
else:
|
||||
if token is parser.KEYWORD_XREF:
|
||||
parser.nextline()
|
||||
xref = PDFXRef()
|
||||
xref.load(parser)
|
||||
xrefs.append(xref)
|
||||
trailer = xref.get_trailer()
|
||||
log.info('trailer: %r', trailer)
|
||||
if 'XRefStm' in trailer:
|
||||
pos = int_value(trailer['XRefStm'])
|
||||
self.read_xref_from(parser, pos, xrefs)
|
||||
if 'Prev' in trailer:
|
||||
# find previous xref
|
||||
pos = int_value(trailer['Prev'])
|
||||
self.read_xref_from(parser, pos, xrefs)
|
||||
return
|
||||
@@ -1,6 +0,0 @@
|
||||
from ..PSStackParser import LIT
|
||||
|
||||
# some predefined literals and keywords.
|
||||
LITERAL_OBJSTM = LIT('ObjStm')
|
||||
LITERAL_XREF = LIT('XRef')
|
||||
LITERAL_CATALOG = LIT('Catalog')
|
||||
@@ -1,22 +0,0 @@
|
||||
from ..PDFParser import PDFSyntaxError
|
||||
from ..PDFStream import PDFException
|
||||
|
||||
## Exceptions
|
||||
##
|
||||
class PDFNoValidXRef(PDFSyntaxError):
|
||||
pass
|
||||
|
||||
class PDFNoOutlines(PDFException):
|
||||
pass
|
||||
|
||||
class PDFDestinationNotFound(PDFException):
|
||||
pass
|
||||
|
||||
class PDFEncryptionError(PDFException):
|
||||
pass
|
||||
|
||||
class PDFPasswordIncorrect(PDFEncryptionError):
|
||||
pass
|
||||
|
||||
class PDFTextExtractionNotAllowed(PDFEncryptionError):
|
||||
pass
|
||||
@@ -1,755 +0,0 @@
|
||||
|
||||
import sys
|
||||
import struct
|
||||
from io import BytesIO
|
||||
from abc import abstractmethod
|
||||
|
||||
from .cmapdb import CMapDB
|
||||
from .cmapdb import CMapParser
|
||||
from .cmapdb import FileUnicodeMap
|
||||
from .cmapdb import CMap
|
||||
from .EncodingDB import EncodingDB, name2unicode
|
||||
from .PSStackParser import PSStackParser
|
||||
from .PSStackParser import PSEOF
|
||||
from .PSStackParser import LIT
|
||||
from .PSStackParser import KWD
|
||||
from .PSStackParser import PSLiteral
|
||||
from .PSStackParser import literal_name
|
||||
from .PDFStream import PDFException
|
||||
from .PDFStream import resolve1
|
||||
from .PDFStream import int_value
|
||||
from .PDFStream import num_value
|
||||
from .PDFStream import list_value
|
||||
from .PDFStream import dict_value
|
||||
from .PDFStream import PDFStream
|
||||
from ..utils import settings, FONT_METRICS, apply_matrix_norm, nunpack, choplist, isnumber
|
||||
from .constants import COMMON_FONT_ATTRIBUTES
|
||||
|
||||
def get_widths(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, w) in enumerate(v):
|
||||
widths[char1+i] = w
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 3:
|
||||
(char1, char2, w) = r
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = w
|
||||
r = []
|
||||
return widths
|
||||
#assert get_widths([1]) == {}
|
||||
#assert get_widths([1,2,3]) == {1:3, 2:3}
|
||||
#assert get_widths([1,[2,3],6,[7,8]]) == {1:2,2:3, 6:7,7:8}
|
||||
|
||||
def get_widths2(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, (w, vx, vy)) in enumerate(choplist(3, v)):
|
||||
widths[char1+i] = (w, (vx, vy))
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 5:
|
||||
(char1, char2, w, vx, vy) = r # pylint: disable=E0632
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = (w, (vx, vy))
|
||||
r = []
|
||||
return widths
|
||||
#assert get_widths2([1]) == {}
|
||||
#assert get_widths2([1,2,3,4,5]) == {1:(3, (4,5)), 2:(3, (4,5))}
|
||||
#assert get_widths2([1,[2,3,4,5],6,[7,8,9]]) == {1:(2, (3,4)), 6:(7, (8,9))}
|
||||
|
||||
|
||||
## FontMetricsDB
|
||||
##
|
||||
class FontMetricsDB(object):
|
||||
|
||||
@classmethod
|
||||
def get_metrics(klass, fontname):
|
||||
return FONT_METRICS[fontname]
|
||||
|
||||
|
||||
## Type1FontHeaderParser
|
||||
##
|
||||
class Type1FontHeaderParser(PSStackParser):
|
||||
|
||||
KEYWORD_BEGIN = KWD(b'begin')
|
||||
KEYWORD_END = KWD(b'end')
|
||||
KEYWORD_DEF = KWD(b'def')
|
||||
KEYWORD_PUT = KWD(b'put')
|
||||
KEYWORD_DICT = KWD(b'dict')
|
||||
KEYWORD_ARRAY = KWD(b'array')
|
||||
KEYWORD_READONLY = KWD(b'readonly')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
|
||||
def __init__(self, data):
|
||||
PSStackParser.__init__(self, data)
|
||||
self._cid2unicode = {}
|
||||
return
|
||||
|
||||
def get_encoding(self):
|
||||
while 1:
|
||||
try:
|
||||
(cid, name) = self.nextobject()
|
||||
except PSEOF:
|
||||
break
|
||||
try:
|
||||
self._cid2unicode[cid] = name2unicode(name)
|
||||
except KeyError:
|
||||
pass
|
||||
return self._cid2unicode
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_PUT:
|
||||
((_, key), (_, value)) = self.pop(2) # pylint: disable=E0632
|
||||
if (isinstance(key, int) and
|
||||
isinstance(value, PSLiteral)):
|
||||
self.add_results((key, literal_name(value)))
|
||||
return
|
||||
|
||||
|
||||
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
|
||||
|
||||
|
||||
## CFFFont
|
||||
## (Format specified in Adobe Technical Note: #5176
|
||||
## "The Compact Font Format Specification")
|
||||
##
|
||||
def getdict(data):
|
||||
d = {}
|
||||
fp = BytesIO(data)
|
||||
stack = []
|
||||
while 1:
|
||||
c = fp.read(1)
|
||||
if not c:
|
||||
break
|
||||
b0 = ord(c)
|
||||
if b0 <= 21:
|
||||
d[b0] = stack
|
||||
stack = []
|
||||
continue
|
||||
if b0 == 30:
|
||||
s = ''
|
||||
loop = True
|
||||
while loop:
|
||||
b = ord(fp.read(1))
|
||||
for n in (b >> 4, b & 15):
|
||||
if n == 15:
|
||||
loop = False
|
||||
else:
|
||||
s += NIBBLES[n]
|
||||
value = float(s)
|
||||
elif 32 <= b0 and b0 <= 246:
|
||||
value = b0-139
|
||||
else:
|
||||
b1 = ord(fp.read(1))
|
||||
if 247 <= b0 and b0 <= 250:
|
||||
value = ((b0-247) << 8)+b1+108
|
||||
elif 251 <= b0 and b0 <= 254:
|
||||
value = -((b0-251) << 8)-b1-108
|
||||
else:
|
||||
b2 = ord(fp.read(1))
|
||||
if 128 <= b1:
|
||||
b1 -= 256
|
||||
if b0 == 28:
|
||||
value = b1 << 8 | b2
|
||||
else:
|
||||
value = b1 << 24 | b2 << 16 | struct.unpack('>H', fp.read(2))[0]
|
||||
stack.append(value)
|
||||
return d
|
||||
|
||||
|
||||
class CFFFont(object):
|
||||
|
||||
STANDARD_STRINGS = (
|
||||
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign',
|
||||
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
|
||||
'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period',
|
||||
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
|
||||
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
|
||||
'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash',
|
||||
'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a',
|
||||
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown',
|
||||
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section',
|
||||
'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
|
||||
'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash',
|
||||
'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet',
|
||||
'quotesinglbase', 'quotedblbase', 'quotedblright',
|
||||
'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
|
||||
'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve',
|
||||
'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut',
|
||||
'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash',
|
||||
'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
|
||||
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
|
||||
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
|
||||
'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
|
||||
'threequarters', 'twosuperior', 'registered', 'minus', 'eth',
|
||||
'multiply', 'threesuperior', 'copyright', 'Aacute',
|
||||
'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
|
||||
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
|
||||
'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde',
|
||||
'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde',
|
||||
'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave',
|
||||
'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex',
|
||||
'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
|
||||
'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
|
||||
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
|
||||
'odieresis', 'ograve', 'otilde', 'scaron', 'uacute',
|
||||
'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis',
|
||||
'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
|
||||
'dollarsuperior', 'ampersandsmall', 'Acutesmall',
|
||||
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
|
||||
'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
|
||||
'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
|
||||
'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
|
||||
'commasuperior', 'threequartersemdash', 'periodsuperior',
|
||||
'questionsmall', 'asuperior', 'bsuperior', 'centsuperior',
|
||||
'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior',
|
||||
'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior',
|
||||
'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
|
||||
'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
|
||||
'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
|
||||
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall',
|
||||
'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall',
|
||||
'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall',
|
||||
'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall',
|
||||
'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall',
|
||||
'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
|
||||
'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
|
||||
'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall',
|
||||
'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths',
|
||||
'onethird', 'twothirds', 'zerosuperior', 'foursuperior',
|
||||
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
|
||||
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
|
||||
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
|
||||
'seveninferior', 'eightinferior', 'nineinferior',
|
||||
'centinferior', 'dollarinferior', 'periodinferior',
|
||||
'commainferior', 'Agravesmall', 'Aacutesmall',
|
||||
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
|
||||
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall',
|
||||
'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
|
||||
'Igravesmall', 'Iacutesmall', 'Icircumflexsmall',
|
||||
'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
|
||||
'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
|
||||
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
|
||||
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall',
|
||||
'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
|
||||
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book',
|
||||
'Light', 'Medium', 'Regular', 'Roman', 'Semibold',
|
||||
)
|
||||
|
||||
class INDEX(object):
|
||||
|
||||
def __init__(self, fp):
|
||||
self.fp = fp
|
||||
self.offsets = []
|
||||
(count, offsize) = struct.unpack('>HB', self.fp.read(3))
|
||||
for _ in range(count+1):
|
||||
self.offsets.append(nunpack(self.fp.read(offsize)))
|
||||
self.base = self.fp.tell()-1
|
||||
self.fp.seek(self.base+self.offsets[-1])
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<INDEX: size=%d>' % len(self)
|
||||
|
||||
def __len__(self): # pylint: disable=E0303
|
||||
return len(self.offsets)-1
|
||||
|
||||
def __getitem__(self, i):
|
||||
self.fp.seek(self.base+self.offsets[i])
|
||||
return self.fp.read(self.offsets[i+1]-self.offsets[i])
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self[i] for i in range(len(self)))
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
# Header
|
||||
(_major, _minor, hdrsize, _) = struct.unpack('BBBB', self.fp.read(4))
|
||||
self.fp.read(hdrsize-4)
|
||||
# Name INDEX
|
||||
self.name_index = self.INDEX(self.fp)
|
||||
# Top DICT INDEX
|
||||
self.dict_index = self.INDEX(self.fp)
|
||||
# String INDEX
|
||||
self.string_index = self.INDEX(self.fp)
|
||||
# Global Subr INDEX
|
||||
self.subr_index = self.INDEX(self.fp)
|
||||
# Top DICT DATA
|
||||
self.top_dict = getdict(self.dict_index[0])
|
||||
(charset_pos,) = self.top_dict.get(15, [0])
|
||||
(encoding_pos,) = self.top_dict.get(16, [0])
|
||||
(charstring_pos,) = self.top_dict.get(17, [0])
|
||||
# CharStrings
|
||||
self.fp.seek(charstring_pos)
|
||||
self.charstring = self.INDEX(self.fp)
|
||||
self.nglyphs = len(self.charstring)
|
||||
# Encodings
|
||||
self.code2gid = {}
|
||||
self.gid2code = {}
|
||||
self.fp.seek(encoding_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
for (code, gid) in enumerate(struct.unpack('B'*n, self.fp.read(n))):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
code = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
code += 1
|
||||
else:
|
||||
raise ValueError('unsupported encoding format: %r' % format)
|
||||
# Charsets
|
||||
self.name2gid = {}
|
||||
self.gid2name = {}
|
||||
self.fp.seek(charset_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
n = self.nglyphs-1
|
||||
for (gid, sid) in enumerate(struct.unpack('>'+'H'*n, self.fp.read(2*n))):
|
||||
gid += 1
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
sid = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
sid += 1
|
||||
elif format == b'\x02':
|
||||
# Format 2
|
||||
assert False, str(('Unhandled', format))
|
||||
else:
|
||||
raise ValueError('unsupported charset format: %r' % format)
|
||||
#print self.code2gid
|
||||
#print self.name2gid
|
||||
#assert 0
|
||||
return
|
||||
|
||||
def getstr(self, sid):
|
||||
if sid < len(self.STANDARD_STRINGS):
|
||||
return self.STANDARD_STRINGS[sid]
|
||||
return self.string_index[sid-len(self.STANDARD_STRINGS)]
|
||||
|
||||
|
||||
## TrueTypeFont
|
||||
##
|
||||
class TrueTypeFont(object):
|
||||
|
||||
class CMapNotFound(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
self.tables = {}
|
||||
self.fonttype = fp.read(4)
|
||||
try:
|
||||
(ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
for _ in range(ntables):
|
||||
(name, _, offset, length) = struct.unpack('>4sLLL', fp.read(16))
|
||||
self.tables[name] = (offset, length)
|
||||
except struct.error:
|
||||
# Do not fail if there are not enough bytes to read. Even for
|
||||
# corrupted PDFs we would like to get as much information as
|
||||
# possible, so continue.
|
||||
pass
|
||||
return
|
||||
|
||||
def create_unicode_map(self):
|
||||
if 'cmap' not in self.tables:
|
||||
raise TrueTypeFont.CMapNotFound
|
||||
(base_offset, _) = self.tables['cmap']
|
||||
fp = self.fp
|
||||
fp.seek(base_offset)
|
||||
(_, nsubtables) = struct.unpack('>HH', fp.read(4))
|
||||
subtables = []
|
||||
for i in range(nsubtables):
|
||||
subtables.append(struct.unpack('>HHL', fp.read(8)))
|
||||
char2gid = {}
|
||||
# Only supports subtable type 0, 2 and 4.
|
||||
for (_1, _2, st_offset) in subtables:
|
||||
fp.seek(base_offset+st_offset)
|
||||
(fmttype, _, _) = struct.unpack('>HHH', fp.read(6))
|
||||
if fmttype == 0:
|
||||
char2gid.update(enumerate(struct.unpack('>256B', fp.read(256))))
|
||||
elif fmttype == 2:
|
||||
subheaderkeys = struct.unpack('>256H', fp.read(512))
|
||||
firstbytes = [0]*8192
|
||||
for (i, k) in enumerate(subheaderkeys):
|
||||
firstbytes[k//8] = i
|
||||
nhdrs = max(subheaderkeys)//8 + 1
|
||||
hdrs = []
|
||||
for i in range(nhdrs):
|
||||
(firstcode, entcount, delta, offset) = struct.unpack('>HHhH', fp.read(8))
|
||||
hdrs.append((i, firstcode, entcount, delta, fp.tell()-2+offset))
|
||||
for (i, firstcode, entcount, delta, pos) in hdrs:
|
||||
if not entcount:
|
||||
continue
|
||||
first = firstcode + (firstbytes[i] << 8)
|
||||
fp.seek(pos)
|
||||
for c in range(entcount):
|
||||
gid = struct.unpack('>H', fp.read(2))
|
||||
if gid:
|
||||
gid += delta
|
||||
char2gid[first+c] = gid
|
||||
elif fmttype == 4:
|
||||
(segcount, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
segcount //= 2
|
||||
ecs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
fp.read(2)
|
||||
scs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
idds = struct.unpack('>%dh' % segcount, fp.read(2*segcount))
|
||||
pos = fp.tell()
|
||||
idrs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
for (ec, sc, idd, idr) in zip(ecs, scs, idds, idrs):
|
||||
if idr:
|
||||
fp.seek(pos+idr)
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (struct.unpack('>H', fp.read(2))[0] + idd) & 0xffff
|
||||
else:
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (c + idd) & 0xffff
|
||||
else:
|
||||
assert False, str(('Unhandled', fmttype))
|
||||
# create unicode map
|
||||
unicode_map = FileUnicodeMap()
|
||||
for (char, gid) in char2gid.items():
|
||||
unicode_map.add_cid2unichr(gid, char)
|
||||
return unicode_map
|
||||
|
||||
|
||||
## Fonts
|
||||
##
|
||||
class PDFFontError(PDFException):
|
||||
pass
|
||||
|
||||
|
||||
class PDFUnicodeNotDefined(PDFFontError):
|
||||
pass
|
||||
|
||||
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
|
||||
LITERAL_TYPE1C = LIT('Type1C')
|
||||
|
||||
|
||||
# PDFFont
|
||||
class PDFFont(object):
|
||||
|
||||
def __init__(self, descriptor, widths, default_width=None):
|
||||
self.descriptor = descriptor
|
||||
self.widths = widths
|
||||
self.fontname = resolve1(descriptor.get('FontName', 'unknown'))
|
||||
if isinstance(self.fontname, PSLiteral):
|
||||
self.fontname = literal_name(self.fontname)
|
||||
self.flags = int_value(descriptor.get('Flags', 0))
|
||||
self.ascent = num_value(descriptor.get('Ascent', 0))
|
||||
self.descent = num_value(descriptor.get('Descent', 0))
|
||||
self.italic_angle = num_value(descriptor.get('ItalicAngle', 0))
|
||||
self.default_width = default_width or num_value(descriptor.get('MissingWidth', 0))
|
||||
self.leading = num_value(descriptor.get('Leading', 0))
|
||||
self.bbox = list_value(descriptor.get('FontBBox', (0, 0, 0, 0)))
|
||||
self.hscale = self.vscale = .001
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFFont>'
|
||||
|
||||
def is_vertical(self):
|
||||
return False
|
||||
|
||||
def is_multibyte(self):
|
||||
return False
|
||||
|
||||
def decode(self, bytes):
|
||||
return bytearray(bytes) # map(ord, bytes)
|
||||
|
||||
def get_ascent(self):
|
||||
return self.ascent * self.vscale
|
||||
|
||||
def get_descent(self):
|
||||
return self.descent * self.vscale
|
||||
|
||||
def get_width(self):
|
||||
w = self.bbox[2]-self.bbox[0]
|
||||
if w == 0:
|
||||
w = -self.default_width
|
||||
return w * self.hscale
|
||||
|
||||
def get_height(self):
|
||||
h = self.bbox[3]-self.bbox[1]
|
||||
if h == 0:
|
||||
h = self.ascent - self.descent
|
||||
return h * self.vscale
|
||||
|
||||
def char_width(self, cid):
|
||||
try:
|
||||
return self.widths[cid] * self.hscale
|
||||
except KeyError:
|
||||
try:
|
||||
return self.widths[self.to_unichr(cid)] * self.hscale
|
||||
except (KeyError, PDFUnicodeNotDefined):
|
||||
return self.default_width * self.hscale
|
||||
|
||||
def char_disp(self, cid):
|
||||
return 0
|
||||
|
||||
def string_width(self, s):
|
||||
return sum(self.char_width(cid) for cid in self.decode(s))
|
||||
|
||||
@abstractmethod
|
||||
def to_unichr(self, cid):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def font_weight(self):
|
||||
if 'FontWeight' in self.descriptor.keys():
|
||||
return self.descriptor['FontWeight']
|
||||
elif 'bold' in self.fontname.lower():
|
||||
return 'bold'
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_bold(self):
|
||||
return self.font_weight == 'bold' or self.font_weight > 549
|
||||
|
||||
|
||||
# PDFSimpleFont
|
||||
class PDFSimpleFont(PDFFont):
|
||||
|
||||
def __init__(self, descriptor, widths, spec):
|
||||
# Font encoding is specified either by a name of
|
||||
# built-in encoding or a dictionary that describes
|
||||
# the differences.
|
||||
if 'Encoding' in spec:
|
||||
encoding = resolve1(spec['Encoding'])
|
||||
else:
|
||||
encoding = LITERAL_STANDARD_ENCODING
|
||||
if isinstance(encoding, dict):
|
||||
name = literal_name(encoding.get('BaseEncoding', LITERAL_STANDARD_ENCODING))
|
||||
diff = list_value(encoding.get('Differences', []))
|
||||
self.cid2unicode = EncodingDB.get_encoding(name, diff)
|
||||
else:
|
||||
self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
PDFFont.__init__(self, descriptor, widths)
|
||||
return
|
||||
|
||||
def to_unichr(self, cid):
|
||||
if self.unicode_map:
|
||||
try:
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
return self.cid2unicode[cid]
|
||||
except KeyError:
|
||||
raise PDFUnicodeNotDefined(None, cid)
|
||||
|
||||
|
||||
# PDFType1Font
|
||||
class PDFType1Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
try:
|
||||
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
|
||||
except KeyError:
|
||||
descriptor = dict_value(spec.get('FontDescriptor', {}))
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 255))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
if 'Encoding' not in spec and 'FontFile' in descriptor:
|
||||
# try to recover the missing encoding info from the font file.
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile'))
|
||||
length1 = int_value(self.fontfile['Length1'])
|
||||
data = self.fontfile.get_data()[:length1]
|
||||
parser = Type1FontHeaderParser(BytesIO(data))
|
||||
self.cid2unicode = parser.get_encoding()
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType1Font: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFTrueTypeFont
|
||||
class PDFTrueTypeFont(PDFType1Font):
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFTrueTypeFont: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFType3Font
|
||||
class PDFType3Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec):
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 0))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
if 'FontDescriptor' in spec:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
else:
|
||||
descriptor = {'Ascent': 0, 'Descent': 0,
|
||||
'FontBBox': spec['FontBBox']}
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
self.matrix = tuple(list_value(spec.get('FontMatrix')))
|
||||
(_, self.descent, _, self.ascent) = self.bbox
|
||||
(self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType3Font>'
|
||||
|
||||
|
||||
# PDFCIDFont
|
||||
class PDFCIDFont(PDFFont):
|
||||
|
||||
def __init__(self, rsrcmgr, spec, strict=settings.STRICT):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
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"))
|
||||
try:
|
||||
name = literal_name(spec['Encoding'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('Encoding is unspecified')
|
||||
name = 'unknown'
|
||||
try:
|
||||
self.cmap = CMapDB.get_cmap(name)
|
||||
except CMapDB.CMapNotFound as e:
|
||||
if strict:
|
||||
raise PDFFontError(e)
|
||||
self.cmap = CMap()
|
||||
try:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise PDFFontError('FontDescriptor is missing')
|
||||
descriptor = {}
|
||||
ttf = None
|
||||
if 'FontFile2' in descriptor:
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile2'))
|
||||
ttf = TrueTypeFont(self.basefont,
|
||||
BytesIO(self.fontfile.get_data()))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
|
||||
if ttf:
|
||||
try:
|
||||
self.unicode_map = ttf.create_unicode_map()
|
||||
except TrueTypeFont.CMapNotFound:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
|
||||
except CMapDB.CMapNotFound as e:
|
||||
pass
|
||||
|
||||
self.vertical = self.cmap.is_vertical()
|
||||
if self.vertical:
|
||||
# writing mode: vertical
|
||||
widths = get_widths2(list_value(spec.get('W2', [])))
|
||||
self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in iter(widths.items()))
|
||||
(vy, w) = spec.get('DW2', [880, -1000])
|
||||
self.default_disp = (None, vy)
|
||||
widths = dict((cid, w) for (cid, (w, _)) in iter(widths.items()))
|
||||
default_width = w
|
||||
else:
|
||||
# writing mode: horizontal
|
||||
self.disps = {}
|
||||
self.default_disp = 0
|
||||
widths = get_widths(list_value(spec.get('W', [])))
|
||||
default_width = spec.get('DW', 1000)
|
||||
PDFFont.__init__(self, descriptor, widths, default_width=default_width)
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)
|
||||
|
||||
def is_vertical(self):
|
||||
return self.vertical
|
||||
|
||||
def is_multibyte(self):
|
||||
return True
|
||||
|
||||
def decode(self, bytes):
|
||||
return self.cmap.decode(bytes)
|
||||
|
||||
def char_disp(self, cid):
|
||||
"Returns an integer for horizontal fonts, a tuple for vertical fonts."
|
||||
return self.disps.get(cid, self.default_disp)
|
||||
|
||||
def to_unichr(self, cid):
|
||||
try:
|
||||
if not self.unicode_map:
|
||||
raise KeyError(cid)
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
raise PDFUnicodeNotDefined(self.cidcoding, cid)
|
||||
|
||||
|
||||
# main
|
||||
def main(argv):
|
||||
for fname in argv[1:]:
|
||||
fp = open(fname, 'rb')
|
||||
#font = TrueTypeFont(fname, fp)
|
||||
font = CFFFont(fname, fp)
|
||||
print (font)
|
||||
fp.close()
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
@@ -1,75 +0,0 @@
|
||||
class PDFGraphicStateColor:
|
||||
class NotSet(Exception): pass
|
||||
|
||||
def __init__(self):
|
||||
self.__rgb = None
|
||||
self.__gray = None
|
||||
self.__cmyk = None
|
||||
self.__custom = None
|
||||
|
||||
def get_color(self):
|
||||
if self.__rgb is not None:
|
||||
return 'rgb', self.rgb
|
||||
elif self.__cmyk is not None:
|
||||
return 'cmyk', self.cmyk
|
||||
elif self.__gray is not None:
|
||||
return 'gray', self.gray
|
||||
elif self.__custom is not None:
|
||||
return 'custom', self.custom
|
||||
else: return None, None
|
||||
|
||||
@property
|
||||
def is_set(self):
|
||||
return self.__rgb is not None or self.__cmyk is not None or self.__gray is not None
|
||||
|
||||
@property
|
||||
def rgb(self):
|
||||
if self.__rgb is not None: return [*self.__rgb]
|
||||
else: raise self.NotSet
|
||||
|
||||
@property
|
||||
def cmyk(self):
|
||||
if self.__cmyk is not None: return [*self.__cmyk]
|
||||
else: raise self.NotSet
|
||||
|
||||
@property
|
||||
def gray(self):
|
||||
if self.__gray is not None: return [float(self.__gray)]
|
||||
else: raise self.NotSet
|
||||
|
||||
@property
|
||||
def custom(self):
|
||||
if self.__custom is not None: return dict(self.__custom)
|
||||
else: raise self.NotSet
|
||||
|
||||
@rgb.setter
|
||||
def rgb(self, rgb):
|
||||
self.__rgb = rgb
|
||||
|
||||
@cmyk.setter
|
||||
def cmyk(self, cmyk):
|
||||
self.__cmyk = cmyk
|
||||
|
||||
@gray.setter
|
||||
def gray(self, color):
|
||||
self.__gray = color
|
||||
|
||||
@custom.setter
|
||||
def custom(self, color):
|
||||
self.__custom = color
|
||||
|
||||
def copy(self):
|
||||
cp = self.__class__()
|
||||
cp.gray = self.__gray
|
||||
cp.rgb = self.__rgb
|
||||
cp.cmyk = self.__cmyk
|
||||
cp.custom = self.__custom
|
||||
return cp
|
||||
|
||||
def __repr__(self):
|
||||
rep = "<PDFGraphicStateColor"
|
||||
if self.__gray is not None: rep += f' gray={self.__gray}'
|
||||
if self.__rgb is not None: rep += f' rgb=[{",".join(map(str, self.__rgb))}]'
|
||||
if self.__cmyk is not None: rep += f' cmyk=[{",".join(map(str, self.__cmyk))}]'
|
||||
if self.__custom is not None: rep += f' custom={self.__custom}'
|
||||
return rep + '/>'
|
||||
@@ -1,45 +0,0 @@
|
||||
from .PDFGraphicStateColor import PDFGraphicStateColor
|
||||
|
||||
class PDFGraphicState(object):
|
||||
|
||||
def __init__(self):
|
||||
self.linewidth = 0
|
||||
self.linecap = None
|
||||
self.linejoin = None
|
||||
self.miterlimit = None
|
||||
self.dash = None
|
||||
self.intent = None
|
||||
self.flatness = None
|
||||
|
||||
# stroking color
|
||||
self.scolor = PDFGraphicStateColor()
|
||||
|
||||
# non stroking color
|
||||
self.ncolor = PDFGraphicStateColor()
|
||||
|
||||
return
|
||||
|
||||
def copy(self):
|
||||
obj = PDFGraphicState()
|
||||
obj.linewidth = self.linewidth
|
||||
obj.linecap = self.linecap
|
||||
obj.linejoin = self.linejoin
|
||||
obj.miterlimit = self.miterlimit
|
||||
obj.dash = self.dash
|
||||
obj.intent = self.intent
|
||||
obj.flatness = self.flatness
|
||||
obj.scolor = self.scolor.copy()
|
||||
obj.ncolor = self.ncolor.copy()
|
||||
return obj
|
||||
|
||||
def __repr__(self):
|
||||
return ('<PDFGraphicState: linewidth=%r, linecap=%r, linejoin=%r, '
|
||||
' miterlimit=%r, dash=%r, intent=%r, flatness=%r, '
|
||||
' stroking color=%r, non stroking color=%r'
|
||||
'>' %
|
||||
(
|
||||
self.linewidth, self.linecap, self.linejoin,
|
||||
self.miterlimit, self.dash, self.intent, self.flatness,
|
||||
str(self.scolor), str(self.ncolor)
|
||||
)
|
||||
)
|
||||
@@ -1,137 +0,0 @@
|
||||
|
||||
import logging
|
||||
from ..utils import settings
|
||||
from .constants import LIT
|
||||
from .PDFStream import PDFObjectNotFound
|
||||
from .PDFStream import resolve1, int_value, list_value, dict_value
|
||||
from .PDFParser import PDFParser
|
||||
from .PDFDocument import PDFDocument, PDFTextExtractionNotAllowed
|
||||
|
||||
from ..utils import get_logger
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
# some predefined literals and keywords.
|
||||
LITERAL_PAGE = LIT('Page')
|
||||
LITERAL_PAGES = LIT('Pages')
|
||||
|
||||
## PDFPage
|
||||
##
|
||||
class PDFPage(object):
|
||||
|
||||
"""An object that holds the information about a page.
|
||||
|
||||
A PDFPage object is merely a convenience class that has a set
|
||||
of keys and values, which describe the properties of a page
|
||||
and point to its contents.
|
||||
|
||||
Attributes:
|
||||
doc: a PDFDocument object.
|
||||
pageid: any Python object that can uniquely identify the page.
|
||||
attrs: a dictionary of page attributes.
|
||||
contents: a list of PDFStream objects that represents the page content.
|
||||
lastmod: the last modified time of the page.
|
||||
resources: a list of resources used by the page.
|
||||
mediabox: the physical size of the page.
|
||||
cropbox: the crop rectangle of the page.
|
||||
rotate: the page rotation (in degree).
|
||||
annots: the page annotations.
|
||||
beads: a chain that represents natural reading order.
|
||||
"""
|
||||
|
||||
def __init__(self, doc: PDFDocument, pageid, attrs):
|
||||
"""Initialize a page object.
|
||||
|
||||
doc: a PDFDocument object.
|
||||
pageid: any Python object that can uniquely identify the page.
|
||||
attrs: a dictionary of page attributes.
|
||||
"""
|
||||
self.doc = doc
|
||||
self.pageid = pageid
|
||||
self.attrs = dict_value(attrs)
|
||||
self.lastmod = resolve1(self.attrs.get('LastModified'))
|
||||
self.resources = resolve1(self.attrs.get('Resources', dict()))
|
||||
self.mediabox = resolve1(self.attrs['MediaBox'])
|
||||
if 'CropBox' in self.attrs:
|
||||
self.cropbox = resolve1(self.attrs['CropBox'])
|
||||
else:
|
||||
self.cropbox = self.mediabox
|
||||
self.rotate = (int_value(self.attrs.get('Rotate', 0))+360) % 360
|
||||
self.annots = self.attrs.get('Annots')
|
||||
self.beads = self.attrs.get('B')
|
||||
if 'Contents' in self.attrs:
|
||||
contents = resolve1(self.attrs['Contents'])
|
||||
else:
|
||||
contents = []
|
||||
if not isinstance(contents, list):
|
||||
contents = [contents]
|
||||
self.contents = contents
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFPage: Resources=%r, MediaBox=%r>' % (self.resources, self.mediabox)
|
||||
|
||||
INHERITABLE_ATTRS = set(['Resources', 'MediaBox', 'CropBox', 'Rotate'])
|
||||
|
||||
@classmethod
|
||||
def create_pages(cls, document: PDFDocument):
|
||||
def search(obj, parent):
|
||||
if isinstance(obj, int):
|
||||
objid = obj
|
||||
tree = dict_value(document.getobj(objid)).copy()
|
||||
else:
|
||||
objid = obj.objid
|
||||
tree = dict_value(obj).copy()
|
||||
for (k, v) in parent.items():
|
||||
if k in cls.INHERITABLE_ATTRS and k not in tree:
|
||||
tree[k] = v
|
||||
|
||||
tree_type = tree.get('Type')
|
||||
if tree_type is None and not settings.STRICT: # See #64
|
||||
tree_type = tree.get('type')
|
||||
|
||||
if tree_type is LITERAL_PAGES and 'Kids' in tree:
|
||||
log.debug('Pages: Kids=%r', tree['Kids'])
|
||||
for c in list_value(tree['Kids']):
|
||||
for x in search(c, tree):
|
||||
yield x
|
||||
elif tree_type is LITERAL_PAGE:
|
||||
log.debug('Page: %r', tree)
|
||||
yield (objid, tree)
|
||||
|
||||
pages = False
|
||||
if 'Pages' in document.catalog:
|
||||
for (objid, tree) in search(document.catalog['Pages'], document.catalog):
|
||||
yield cls(document, objid, tree)
|
||||
pages = True
|
||||
if not pages:
|
||||
# fallback when /Pages is missing.
|
||||
for xref in document.xrefs:
|
||||
for objid in xref.get_objids():
|
||||
try:
|
||||
obj = document.getobj(objid)
|
||||
if isinstance(obj, dict) and obj.get('Type') is LITERAL_PAGE:
|
||||
yield cls(document, objid, obj)
|
||||
except PDFObjectNotFound:
|
||||
pass
|
||||
return
|
||||
|
||||
@classmethod
|
||||
def get_pages(cls, fp,
|
||||
pagenos=None, maxpages=0, password='',
|
||||
caching=True, check_extractable=True):
|
||||
# Create a PDF parser object associated with the file object.
|
||||
parser = PDFParser(fp)
|
||||
# Create a PDF document object that stores the document structure.
|
||||
doc = PDFDocument(parser, password=password, caching=caching)
|
||||
# Check if the document allows text extraction. If not, abort.
|
||||
if check_extractable and not doc.is_extractable:
|
||||
raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)
|
||||
# Process each page contained in the document.
|
||||
for (pageno, page) in enumerate(cls.create_pages(doc)):
|
||||
if pagenos and (pageno not in pagenos):
|
||||
continue
|
||||
yield page
|
||||
if maxpages and maxpages <= pageno+1:
|
||||
break
|
||||
return
|
||||
@@ -1,668 +0,0 @@
|
||||
import logging
|
||||
import operator
|
||||
|
||||
from collections import OrderedDict
|
||||
from typing import List
|
||||
|
||||
|
||||
from .PDFColorSpace import PDFColorSpace
|
||||
from .PDFColorSpace import PREDEFINED_COLORSPACE
|
||||
|
||||
from .cmapdb import CMapDB
|
||||
from .cmapdb import CMap
|
||||
|
||||
from .PDFStream import PDFException
|
||||
from .PDFStream.PDFObjRef import PDFObjRef
|
||||
from .PDFStream import resolve1, list_value, dict_value
|
||||
from .PDFStream import PDFStream
|
||||
|
||||
from .PSStackParser import PSEOF
|
||||
from .PSStackParser import PSKeyword
|
||||
from .PSStackParser import literal_name
|
||||
from .PSStackParser import keyword_name
|
||||
from .PSStackParser import PSStackParser
|
||||
from .PSStackParser import LIT
|
||||
|
||||
from .PDFGraphicState import PDFGraphicState
|
||||
from .PDFTextState import PDFTextState
|
||||
from .PDFContentParser import PDFContentParser
|
||||
|
||||
from ..utils import mult_matrix, MATRIX_IDENTITY
|
||||
from .constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
from .types import CurvePoint, CurvePath
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
## Exceptions
|
||||
##
|
||||
class PDFInterpreterError(PDFException):
|
||||
pass
|
||||
|
||||
class PDFPageInterpreter(object):
|
||||
|
||||
def __init__(self, rsrcmgr, device):
|
||||
self.rsrcmgr = rsrcmgr
|
||||
self.device = device
|
||||
self.curpath: List[CurvePath] = []
|
||||
return
|
||||
|
||||
def dup(self):
|
||||
return self.__class__(self.rsrcmgr, self.device)
|
||||
|
||||
# init_resources(resources):
|
||||
# Prepare the fonts and XObjects listed in the Resource attribute.
|
||||
def init_resources(self, resources):
|
||||
self.resources = resources
|
||||
self.fontmap = {}
|
||||
self.xobjmap = {}
|
||||
self.csmap:OrderedDict = PREDEFINED_COLORSPACE.copy()
|
||||
if not resources:
|
||||
return
|
||||
|
||||
def get_colorspace(spec):
|
||||
if isinstance(spec, list):
|
||||
name = literal_name(spec[0])
|
||||
else:
|
||||
name = literal_name(spec)
|
||||
if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, PDFStream.validated_stream(spec[1])['N'])
|
||||
elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, len(list_value(spec[1])))
|
||||
else:
|
||||
return PREDEFINED_COLORSPACE.get(name)
|
||||
|
||||
for (k, v) in iter(dict_value(resources).items()):
|
||||
log.debug('Resource: %r: %r', k, v)
|
||||
if k == 'Font':
|
||||
for (fontid, spec) in iter(dict_value(v).items()):
|
||||
objid = None
|
||||
if isinstance(spec, PDFObjRef):
|
||||
objid = spec.objid
|
||||
spec = dict_value(spec)
|
||||
self.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
|
||||
elif k == 'ColorSpace':
|
||||
for (csid, spec) in iter(dict_value(v).items()):
|
||||
self.csmap[csid] = get_colorspace(resolve1(spec))
|
||||
elif k == 'ProcSet':
|
||||
self.rsrcmgr.get_procset(list_value(v))
|
||||
elif k == 'XObject':
|
||||
for (xobjid, xobjstrm) in iter(dict_value(v).items()):
|
||||
self.xobjmap[xobjid] = xobjstrm
|
||||
return
|
||||
|
||||
# init_state(ctm)
|
||||
# Initialize the text and graphic states for rendering a page.
|
||||
def init_state(self, ctm):
|
||||
# gstack: stack for graphical states.
|
||||
self.gstack = []
|
||||
self.ctm = ctm
|
||||
self.device.set_ctm(self.ctm)
|
||||
self.textstate = PDFTextState()
|
||||
self.graphicstate = PDFGraphicState()
|
||||
self.curpath = []
|
||||
# argstack: stack for command arguments.
|
||||
self.argstack = []
|
||||
# set some global states.
|
||||
self.scs = self.ncs = None
|
||||
if self.csmap:
|
||||
self.scs = self.ncs = next(iter(self.csmap.values()))
|
||||
return
|
||||
|
||||
def push(self, obj):
|
||||
self.argstack.append(obj)
|
||||
return
|
||||
|
||||
def pop(self, n):
|
||||
if n == 0:
|
||||
return []
|
||||
x = self.argstack[-n:]
|
||||
self.argstack = self.argstack[:-n]
|
||||
return x
|
||||
|
||||
def get_current_state(self):
|
||||
return (self.ctm, self.textstate.copy(), self.graphicstate.copy())
|
||||
|
||||
def set_current_state(self, state):
|
||||
(self.ctm, self.textstate, self.graphicstate) = state
|
||||
self.device.set_ctm(self.ctm)
|
||||
return
|
||||
|
||||
# gsave
|
||||
def do_q(self):
|
||||
self.gstack.append(self.get_current_state())
|
||||
return
|
||||
|
||||
# grestore
|
||||
def do_Q(self):
|
||||
if self.gstack:
|
||||
self.set_current_state(self.gstack.pop())
|
||||
return
|
||||
|
||||
# concat-matrix
|
||||
def do_cm(self, a1, b1, c1, d1, e1, f1):
|
||||
self.ctm = mult_matrix((a1, b1, c1, d1, e1, f1), self.ctm)
|
||||
self.device.set_ctm(self.ctm)
|
||||
return
|
||||
|
||||
# setlinewidth
|
||||
def do_w(self, linewidth):
|
||||
self.graphicstate.linewidth = linewidth
|
||||
return
|
||||
|
||||
# setlinecap
|
||||
def do_J(self, linecap):
|
||||
self.graphicstate.linecap = linecap
|
||||
return
|
||||
|
||||
# setlinejoin
|
||||
def do_j(self, linejoin):
|
||||
self.graphicstate.linejoin = linejoin
|
||||
return
|
||||
|
||||
# setmiterlimit
|
||||
def do_M(self, miterlimit):
|
||||
self.graphicstate.miterlimit = miterlimit
|
||||
return
|
||||
|
||||
# setdash
|
||||
def do_d(self, dash, phase):
|
||||
self.graphicstate.dash = (dash, phase)
|
||||
return
|
||||
|
||||
# setintent
|
||||
def do_ri(self, intent):
|
||||
self.graphicstate.intent = intent
|
||||
return
|
||||
|
||||
# setflatness
|
||||
def do_i(self, flatness):
|
||||
self.graphicstate.flatness = flatness
|
||||
return
|
||||
|
||||
# load-gstate
|
||||
def do_gs(self, name):
|
||||
#XXX
|
||||
return
|
||||
|
||||
# moveto
|
||||
def do_m(self, x: float, y: float):
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y)))
|
||||
return
|
||||
|
||||
# lineto
|
||||
def do_l(self, x: float, y: float):
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y)))
|
||||
return
|
||||
|
||||
# curveto
|
||||
def do_c(self, x1: float, y1: float, x2: float, y2: float, x3: float, y3: float):
|
||||
self.curpath.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_BOTH_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
))
|
||||
return
|
||||
|
||||
# urveto
|
||||
def do_v(self, x2: float, y2: float, x3: float, y3: float):
|
||||
self.curpath.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_NEXT_TO,
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
))
|
||||
return
|
||||
|
||||
# rveto
|
||||
def do_y(self, x1: float, y1: float, x3: float, y3: float):
|
||||
self.curpath.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_FIRST_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x3, y3)
|
||||
))
|
||||
return
|
||||
|
||||
# closepath
|
||||
def do_h(self):
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
|
||||
return
|
||||
|
||||
# rectangle
|
||||
def do_re(self, x, y, w, h):
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y) ))
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y) ))
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y+h) ))
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y+h) ))
|
||||
self.curpath.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
|
||||
return
|
||||
|
||||
# stroke
|
||||
def do_S(self):
|
||||
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
|
||||
self.curpath = []
|
||||
return
|
||||
|
||||
# close-and-stroke
|
||||
def do_s(self):
|
||||
self.do_h()
|
||||
self.do_S()
|
||||
return
|
||||
|
||||
# fill
|
||||
def do_f(self):
|
||||
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
|
||||
self.curpath = []
|
||||
return
|
||||
# fill (obsolete)
|
||||
do_F = do_f
|
||||
|
||||
# fill-even-odd
|
||||
def do_f_a(self):
|
||||
self.device.paint_path(self.graphicstate.copy(), True, self.curpath)
|
||||
self.curpath = []
|
||||
return
|
||||
|
||||
# fill-and-stroke
|
||||
def do_B(self):
|
||||
self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
|
||||
self.curpath = []
|
||||
return
|
||||
|
||||
# fill-and-stroke-even-odd
|
||||
def do_B_a(self):
|
||||
self.device.paint_path(self.graphicstate.copy(), True, self.curpath)
|
||||
self.curpath = []
|
||||
return
|
||||
|
||||
# close-fill-and-stroke
|
||||
def do_b(self):
|
||||
self.do_h()
|
||||
self.do_B()
|
||||
return
|
||||
|
||||
# close-fill-and-stroke-even-odd
|
||||
def do_b_a(self):
|
||||
self.do_h()
|
||||
self.do_B_a()
|
||||
return
|
||||
|
||||
# close-only
|
||||
def do_n(self):
|
||||
self.curpath = []
|
||||
return
|
||||
|
||||
# clip
|
||||
def do_W(self):
|
||||
return
|
||||
|
||||
# clip-even-odd
|
||||
def do_W_a(self):
|
||||
return
|
||||
|
||||
# setcolorspace-stroking
|
||||
def do_CS(self, name):
|
||||
try:
|
||||
self.scs = self.csmap[literal_name(name)]
|
||||
except KeyError:
|
||||
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
|
||||
return
|
||||
|
||||
# setcolorspace-non-strokine
|
||||
def do_cs(self, name):
|
||||
try:
|
||||
self.ncs = self.csmap[literal_name(name)]
|
||||
except KeyError:
|
||||
raise PDFInterpreterError('Undefined ColorSpace: %r' % name)
|
||||
return
|
||||
|
||||
# setgray-stroking
|
||||
def do_G(self, gray):
|
||||
self.graphicstate.scolor.gray = gray
|
||||
#self.do_CS(LITERAL_DEVICE_GRAY)
|
||||
return
|
||||
|
||||
# setgray-non-stroking
|
||||
def do_g(self, gray):
|
||||
self.graphicstate.ncolor.gray = gray
|
||||
#self.do_cs(LITERAL_DEVICE_GRAY)
|
||||
return
|
||||
|
||||
# setrgb-stroking
|
||||
def do_RG(self, r, g, b):
|
||||
self.graphicstate.scolor.rgb = (r*255, g*255, b*255)
|
||||
#self.do_CS(LITERAL_DEVICE_RGB)
|
||||
return
|
||||
|
||||
# setrgb-non-stroking
|
||||
def do_rg(self, r, g, b):
|
||||
self.graphicstate.ncolor.rgb = (r*255, g*255, b*255)
|
||||
#self.do_cs(LITERAL_DEVICE_RGB)
|
||||
return
|
||||
|
||||
# setcmyk-stroking
|
||||
def do_K(self, c, m, y, k):
|
||||
self.graphicstate.scolor.cmyk = (c, m, y, k)
|
||||
#self.do_CS(LITERAL_DEVICE_CMYK)
|
||||
return
|
||||
|
||||
# setcmyk-non-stroking
|
||||
def do_k(self, c, m, y, k):
|
||||
self.graphicstate.ncolor.cmyk = (c, m, y, k)
|
||||
#self.do_cs(LITERAL_DEVICE_CMYK)
|
||||
return
|
||||
|
||||
def __process_ICCBased(self, n: int, components: list):
|
||||
if n == 1:
|
||||
return {
|
||||
"type": "gray",
|
||||
"definition": components
|
||||
}
|
||||
elif n == 3:
|
||||
return {
|
||||
"type": "rgb",
|
||||
"definition": components
|
||||
}
|
||||
elif n == 4:
|
||||
return {
|
||||
"type": "cmyk",
|
||||
"definition": components
|
||||
}
|
||||
raise PDFInterpreterError("Unsupported ICCBased color")
|
||||
|
||||
# setcolor
|
||||
def __process_scn(self):
|
||||
if self.ncs:
|
||||
n = self.ncs.ncomponents
|
||||
else:
|
||||
raise PDFInterpreterError('No colorspace specified!')
|
||||
components = self.pop(n)
|
||||
obj_id = components[0]
|
||||
if self.ncs.name == "ICCBased":
|
||||
return self.__process_ICCBased(n, components)
|
||||
if self.ncs.name not in self.resources.keys():
|
||||
log.warning("Color space not in resources")
|
||||
return {
|
||||
"type": self.ncs.name,
|
||||
"defintion": f"{n}|({','.join(map(str, components))})"
|
||||
}
|
||||
if obj_id.name not in self.resources[self.ncs.name].keys():
|
||||
raise PDFInterpreterError("Invalid Color reference")
|
||||
|
||||
color_data = self.resources[self.ncs.name][obj_id.name].resolve()
|
||||
|
||||
if isinstance(color_data, PDFStream):
|
||||
color_data = color_data.attrs
|
||||
|
||||
return {
|
||||
"type": self.ncs.name,
|
||||
"defintion": color_data
|
||||
}
|
||||
def do_SCN(self):
|
||||
processed_col = self.__process_scn()
|
||||
if processed_col['type'] == 'rgb':
|
||||
self.graphicstate.scolor.rgb = processed_col['definition']
|
||||
elif processed_col['type'] == 'gray':
|
||||
self.graphicstate.scolor.gray = processed_col['definition'][0]
|
||||
elif processed_col['type'] == 'cmyk':
|
||||
self.graphicstate.scolor.cmyk = processed_col['definition']
|
||||
else:
|
||||
self.graphicstate.scolor.custom = processed_col
|
||||
return
|
||||
|
||||
def do_scn(self):
|
||||
processed_col = self.__process_scn()
|
||||
if processed_col['type'] == 'rgb':
|
||||
self.graphicstate.ncolor.rgb = processed_col['definition']
|
||||
elif processed_col['type'] == 'gray':
|
||||
self.graphicstate.ncolor.gray = processed_col['definition'][0]
|
||||
elif processed_col['type'] == 'cmyk':
|
||||
self.graphicstate.ncolor.cmyk = processed_col['definition']
|
||||
else:
|
||||
self.graphicstate.ncolor.custom = processed_col
|
||||
return
|
||||
|
||||
def do_SC(self):
|
||||
self.do_SCN()
|
||||
return
|
||||
|
||||
def do_sc(self):
|
||||
self.do_scn()
|
||||
return
|
||||
|
||||
# sharing-name
|
||||
def do_sh(self, name):
|
||||
return
|
||||
|
||||
# begin-text
|
||||
def do_BT(self):
|
||||
self.textstate.reset()
|
||||
return
|
||||
|
||||
# end-text
|
||||
def do_ET(self):
|
||||
return
|
||||
|
||||
# begin-compat
|
||||
def do_BX(self):
|
||||
return
|
||||
|
||||
# end-compat
|
||||
def do_EX(self):
|
||||
return
|
||||
|
||||
# marked content operators
|
||||
def do_MP(self, tag):
|
||||
self.device.do_tag(tag)
|
||||
return
|
||||
|
||||
def do_DP(self, tag, props):
|
||||
self.device.do_tag(tag, props)
|
||||
return
|
||||
|
||||
def do_BMC(self, tag):
|
||||
self.device.begin_tag(tag)
|
||||
return
|
||||
|
||||
def do_BDC(self, tag, props):
|
||||
self.device.begin_tag(tag, props)
|
||||
return
|
||||
|
||||
def do_EMC(self):
|
||||
self.device.end_tag()
|
||||
return
|
||||
|
||||
# setcharspace
|
||||
def do_Tc(self, space):
|
||||
self.textstate.charspace = space
|
||||
return
|
||||
|
||||
# setwordspace
|
||||
def do_Tw(self, space):
|
||||
self.textstate.wordspace = space
|
||||
return
|
||||
|
||||
# def do_Th(self, )
|
||||
|
||||
# textscale
|
||||
def do_Tz(self, scale):
|
||||
self.textstate.scaling = scale
|
||||
return
|
||||
|
||||
# setleading
|
||||
def do_TL(self, leading):
|
||||
self.textstate.leading = -leading
|
||||
return
|
||||
|
||||
# selectfont
|
||||
def do_Tf(self, fontid, fontsize):
|
||||
try:
|
||||
self.textstate.font = self.fontmap[literal_name(fontid)]
|
||||
except KeyError:
|
||||
raise PDFInterpreterError('Undefined Font id: %r' % fontid)
|
||||
# self.textstate.font = self.rsrcmgr.get_font(None, {})
|
||||
self.textstate.fontsize = fontsize
|
||||
return
|
||||
|
||||
# setrendering
|
||||
def do_Tr(self, render):
|
||||
self.textstate.render = render
|
||||
return
|
||||
|
||||
# settextrise
|
||||
def do_Ts(self, rise):
|
||||
self.textstate.rise = rise
|
||||
return
|
||||
|
||||
# text-move
|
||||
def do_Td(self, tx, ty):
|
||||
(a, b, c, d, e, f) = self.textstate.matrix
|
||||
self.textstate.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
self.textstate.linematrix = [0, 0]
|
||||
return
|
||||
|
||||
# text-move
|
||||
def do_TD(self, tx, ty):
|
||||
(a, b, c, d, e, f) = self.textstate.matrix
|
||||
self.textstate.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
self.textstate.leading = ty
|
||||
self.textstate.linematrix = [0, 0]
|
||||
return
|
||||
|
||||
# textmatrix
|
||||
def do_Tm(self, a, b, c, d, e, f):
|
||||
self.textstate.matrix = (a, b, c, d, e, f)
|
||||
self.textstate.linematrix = [0, 0]
|
||||
return
|
||||
|
||||
# nextline
|
||||
def do_T_a(self):
|
||||
(a, b, c, d, e, f) = self.textstate.matrix
|
||||
self.textstate.matrix = (a, b, c, d, self.textstate.leading*c+e, self.textstate.leading*d+f)
|
||||
self.textstate.linematrix = [0, 0]
|
||||
return
|
||||
|
||||
# show-pos
|
||||
def do_TJ(self, seq: bytearray):
|
||||
if self.textstate.font is None:
|
||||
raise PDFInterpreterError('No font specified!')
|
||||
self.device.render_string(self.textstate.copy(), seq, self.ncs, self.graphicstate.copy())
|
||||
return
|
||||
|
||||
# show
|
||||
def do_Tj(self, s):
|
||||
self.do_TJ([s])
|
||||
return
|
||||
|
||||
# quote
|
||||
def do__q(self, s):
|
||||
self.do_T_a()
|
||||
self.do_TJ([s])
|
||||
return
|
||||
|
||||
# doublequote
|
||||
def do__w(self, aw, ac, s):
|
||||
self.do_Tw(aw)
|
||||
self.do_Tc(ac)
|
||||
self.do_TJ([s])
|
||||
return
|
||||
|
||||
# inline image
|
||||
def do_BI(self): # never called
|
||||
return
|
||||
|
||||
def do_ID(self): # never called
|
||||
return
|
||||
|
||||
def do_EI(self, obj: PDFStream):
|
||||
if 'W' in obj and 'H' in obj:
|
||||
iobjid = str(id(obj))
|
||||
self.device.begin_figure(iobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
|
||||
self.device.render_image(iobjid, obj)
|
||||
self.device.end_figure(iobjid)
|
||||
return
|
||||
|
||||
# invoke an XObject
|
||||
def do_Do(self, xobjid):
|
||||
xobjid = literal_name(xobjid)
|
||||
try:
|
||||
xobj = PDFStream.validated_stream(self.xobjmap[xobjid])
|
||||
except KeyError:
|
||||
raise PDFInterpreterError('Undefined xobject id: %r' % xobjid)
|
||||
log.info('Processing xobj: %r', xobj)
|
||||
subtype = xobj.get('Subtype')
|
||||
if subtype is LITERAL_FORM and 'BBox' in xobj:
|
||||
interpreter = self.dup()
|
||||
bbox = list_value(xobj['BBox'])
|
||||
matrix = list_value(xobj.get('Matrix', MATRIX_IDENTITY))
|
||||
# According to PDF reference 1.7 section 4.9.1, XObjects in
|
||||
# earlier PDFs (prior to v1.2) use the page's Resources entry
|
||||
# instead of having their own Resources entry.
|
||||
xobjres = xobj.get('Resources')
|
||||
resources = dict_value(xobjres) if xobjres else self.resources.copy()
|
||||
self.device.begin_figure(xobjid, bbox, matrix)
|
||||
interpreter.render_contents(resources, [xobj], ctm=mult_matrix(matrix, self.ctm))
|
||||
self.device.end_figure(xobjid)
|
||||
elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
|
||||
self.device.begin_figure(xobjid, (0, 0, 1, 1), MATRIX_IDENTITY)
|
||||
self.device.render_image(xobjid, xobj)
|
||||
self.device.end_figure(xobjid)
|
||||
else:
|
||||
# unsupported xobject type.
|
||||
pass
|
||||
return
|
||||
|
||||
def process_page(self, page):
|
||||
log.info('Processing page: %r', page)
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
if page.rotate == 90:
|
||||
ctm = (0, -1, 1, 0, -y0, x1)
|
||||
elif page.rotate == 180:
|
||||
ctm = (-1, 0, 0, -1, x1, y1)
|
||||
elif page.rotate == 270:
|
||||
ctm = (0, 1, -1, 0, y1, -x0)
|
||||
else:
|
||||
ctm = (1, 0, 0, 1, -x0, -y0)
|
||||
self.device.begin_page(page, ctm)
|
||||
self.render_contents(page.resources, page.contents, ctm=ctm)
|
||||
return self.device.end_page(page)
|
||||
|
||||
# render_contents(resources, streams, ctm)
|
||||
# Render the content streams.
|
||||
# This method may be called recursively.
|
||||
def render_contents(self, resources, streams, ctm=MATRIX_IDENTITY):
|
||||
log.info('render_contents: resources=%r, streams=%r, ctm=%r',
|
||||
resources, streams, ctm)
|
||||
self.init_resources(resources)
|
||||
self.init_state(ctm)
|
||||
self.execute(list_value(streams))
|
||||
return
|
||||
|
||||
def execute(self, streams):
|
||||
try:
|
||||
parser = PDFContentParser(streams)
|
||||
except PSEOF:
|
||||
# empty page
|
||||
return
|
||||
while True:
|
||||
try:
|
||||
(_, obj) = parser.nextobject()
|
||||
except PSEOF:
|
||||
break
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = 'do_%s' % name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if hasattr(self, method):
|
||||
func = getattr(self, method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(*args)
|
||||
else:
|
||||
log.debug('exec: %s', name)
|
||||
func()
|
||||
else:
|
||||
raise PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.push(obj)
|
||||
return
|
||||
@@ -1,178 +0,0 @@
|
||||
|
||||
import logging
|
||||
from io import BytesIO, TextIOWrapper
|
||||
from ..utils import settings
|
||||
|
||||
from .PSStackParser import PSStackParser
|
||||
from .PSStackParser import PSSyntaxError
|
||||
from .PSStackParser import PSEOF
|
||||
from .PSStackParser import KWD
|
||||
from .PDFStream import PDFException
|
||||
from .PDFStream import PDFStream
|
||||
from .PDFStream.PDFObjRef import PDFObjRef
|
||||
from .PDFStream import int_value
|
||||
from .PDFStream import dict_value
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
## Exceptions
|
||||
##
|
||||
class PDFSyntaxError(PDFException):
|
||||
pass
|
||||
|
||||
|
||||
## PDFParser
|
||||
##
|
||||
class PDFParser(PSStackParser):
|
||||
|
||||
"""
|
||||
PDFParser fetch PDF objects from a file stream.
|
||||
It can handle indirect references by referring to
|
||||
a PDF document set by set_document method.
|
||||
It also reads XRefs at the end of every PDF file.
|
||||
|
||||
Typical usage:
|
||||
parser = PDFParser(fp)
|
||||
parser.read_xref()
|
||||
parser.read_xref(fallback=True) # optional
|
||||
parser.set_document(doc)
|
||||
parser.seek(offset)
|
||||
parser.nextobject()
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self, fp: TextIOWrapper):
|
||||
PSStackParser.__init__(self, fp)
|
||||
self.doc = None
|
||||
self.fallback = False
|
||||
return
|
||||
|
||||
def set_document(self, doc):
|
||||
"""Associates the parser with a PDFDocument object."""
|
||||
self.doc = doc
|
||||
return
|
||||
|
||||
KEYWORD_R = KWD(b'R')
|
||||
KEYWORD_NULL = KWD(b'null')
|
||||
KEYWORD_ENDOBJ = KWD(b'endobj')
|
||||
KEYWORD_STREAM = KWD(b'stream')
|
||||
KEYWORD_XREF = KWD(b'xref')
|
||||
KEYWORD_STARTXREF = KWD(b'startxref')
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
"""Handles PDF-related keywords."""
|
||||
|
||||
if token in (self.KEYWORD_XREF, self.KEYWORD_STARTXREF):
|
||||
self.add_results(*self.pop(1))
|
||||
|
||||
elif token is self.KEYWORD_ENDOBJ:
|
||||
self.add_results(*self.pop(4))
|
||||
|
||||
elif token is self.KEYWORD_NULL:
|
||||
# null object
|
||||
self.push((pos, None))
|
||||
|
||||
elif token is self.KEYWORD_R:
|
||||
# reference to indirect object
|
||||
try:
|
||||
((_, objid), (_, genno)) = self.pop(2) # pylint: disable=E0632
|
||||
(objid, genno) = (int(objid), int(genno)) # pylint: disable=E0632
|
||||
obj = PDFObjRef(self.doc, objid, genno) # pylint: disable=E0632
|
||||
self.push((pos, obj))
|
||||
except PSSyntaxError:
|
||||
pass
|
||||
|
||||
elif token is self.KEYWORD_STREAM:
|
||||
# stream object
|
||||
((_, dic),) = self.pop(1) # pylint: disable=E0632
|
||||
dic = dict_value(dic)
|
||||
objlen = 0
|
||||
if not self.fallback:
|
||||
try:
|
||||
objlen = int_value(dic['Length'])
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('/Length is undefined: %r' % dic)
|
||||
self.seek(pos)
|
||||
try:
|
||||
(_, line) = self.nextline() # 'stream'
|
||||
except PSEOF:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('Unexpected EOF')
|
||||
return
|
||||
pos += len(line)
|
||||
self.fp.seek(pos)
|
||||
data = bytearray(self.fp.read(objlen))
|
||||
self.seek(pos+objlen)
|
||||
while 1:
|
||||
try:
|
||||
(_, line) = self.nextline()
|
||||
except PSEOF:
|
||||
if settings.STRICT:
|
||||
raise PDFSyntaxError('Unexpected EOF')
|
||||
break
|
||||
if b'endstream' in line:
|
||||
i = line.index(b'endstream')
|
||||
objlen += i
|
||||
if self.fallback:
|
||||
data += line[:i]
|
||||
break
|
||||
objlen += len(line)
|
||||
if self.fallback:
|
||||
data += line
|
||||
data = bytes(data)
|
||||
self.seek(pos+objlen)
|
||||
# XXX limit objlen not to exceed object boundary
|
||||
log.debug('Stream: pos=%d, objlen=%d, dic=%r, data=%r...', pos, objlen, dic, data[:10])
|
||||
obj = PDFStream(dic, data, self.doc.decipher)
|
||||
self.push((pos, obj))
|
||||
|
||||
else:
|
||||
# others
|
||||
self.push((pos, token))
|
||||
|
||||
return
|
||||
|
||||
|
||||
## PDFStreamParser
|
||||
##
|
||||
class PDFStreamParser(PDFParser):
|
||||
|
||||
"""
|
||||
PDFStreamParser is used to parse PDF content streams
|
||||
that is contained in each page and has instructions
|
||||
for rendering the page. A reference to a PDF document is
|
||||
needed because a PDF content stream can also have
|
||||
indirect references to other objects in the same document.
|
||||
"""
|
||||
|
||||
def __init__(self, data):
|
||||
PDFParser.__init__(self, BytesIO(data))
|
||||
return
|
||||
|
||||
def flush(self):
|
||||
self.add_results(*self.popall())
|
||||
return
|
||||
|
||||
KEYWORD_OBJ = KWD(b'obj')
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_R:
|
||||
# reference to indirect object
|
||||
try:
|
||||
((_, objid), (_, genno)) = self.pop(2) # pylint: disable=E0632
|
||||
(objid, genno) = (int(objid), int(genno))
|
||||
obj = PDFObjRef(self.doc, objid, genno)
|
||||
self.push((pos, obj))
|
||||
except PSSyntaxError:
|
||||
pass
|
||||
return
|
||||
elif token in (self.KEYWORD_OBJ, self.KEYWORD_ENDOBJ):
|
||||
if settings.STRICT:
|
||||
# See PDF Spec 3.4.6: Only the object values are stored in the
|
||||
# stream; the obj and endobj keywords are not used.
|
||||
raise PDFSyntaxError('Keyword endobj found in stream')
|
||||
return
|
||||
# others
|
||||
self.push((pos, token))
|
||||
return
|
||||
@@ -1,103 +0,0 @@
|
||||
import logging
|
||||
|
||||
from .cmapdb import CMapDB, CMap
|
||||
|
||||
from .PDFFont import PDFFontError
|
||||
from .PDFFont import PDFType1Font
|
||||
from .PDFFont import PDFTrueTypeFont
|
||||
from .PDFFont import PDFType3Font
|
||||
from .PDFFont import PDFCIDFont
|
||||
|
||||
from .PDFStream import PDFException
|
||||
from .PDFStream import resolve1, dict_value, list_value
|
||||
from .constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
|
||||
from .PSStackParser import literal_name, settings
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
## Exceptions
|
||||
##
|
||||
class PDFResourceError(PDFException):
|
||||
pass
|
||||
|
||||
class PDFInterpreterError(PDFException):
|
||||
pass
|
||||
|
||||
## Resource Manager
|
||||
##
|
||||
class PDFResourceManager(object):
|
||||
|
||||
"""Repository of shared resources.
|
||||
|
||||
ResourceManager facilitates reuse of shared resources
|
||||
such as fonts and images so that large objects are not
|
||||
allocated multiple times.
|
||||
"""
|
||||
|
||||
def __init__(self, caching=True):
|
||||
self.caching = caching
|
||||
self._cached_fonts = {}
|
||||
return
|
||||
|
||||
def get_procset(self, procs):
|
||||
for proc in procs:
|
||||
if proc is LITERAL_PDF:
|
||||
pass
|
||||
elif proc is LITERAL_TEXT:
|
||||
pass
|
||||
else:
|
||||
#raise PDFResourceError('ProcSet %r is not supported.' % proc)
|
||||
pass
|
||||
return
|
||||
|
||||
def get_cmap(self, cmapname, strict=False):
|
||||
try:
|
||||
return CMapDB.get_cmap(cmapname)
|
||||
except CMapDB.CMapNotFound:
|
||||
if strict:
|
||||
raise
|
||||
return CMap()
|
||||
|
||||
def get_font(self, objid: int, spec: dict):
|
||||
if objid and objid in self._cached_fonts:
|
||||
font = self._cached_fonts[objid]
|
||||
else:
|
||||
log.info('get_font: create: objid=%r, spec=%r', objid, spec)
|
||||
if settings.STRICT:
|
||||
if spec['Type'] is not LITERAL_FONT:
|
||||
raise PDFFontError('Type is not /Font')
|
||||
# Create a Font object.
|
||||
if 'Subtype' in spec:
|
||||
subtype = literal_name(spec['Subtype'])
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFontError('Font Subtype is not specified.')
|
||||
subtype = 'Type1'
|
||||
if subtype in ('Type1', 'MMType1'):
|
||||
# Type1 Font
|
||||
font = PDFType1Font(self, spec)
|
||||
elif subtype == 'TrueType':
|
||||
# TrueType Font
|
||||
font = PDFTrueTypeFont(self, spec)
|
||||
elif subtype == 'Type3':
|
||||
# Type3 Font
|
||||
font = PDFType3Font(self, spec)
|
||||
elif subtype in ('CIDFontType0', 'CIDFontType2'):
|
||||
# CID Font
|
||||
font = PDFCIDFont(self, spec)
|
||||
elif subtype == 'Type0':
|
||||
# Type0 Font
|
||||
dfonts = list_value(spec['DescendantFonts'])
|
||||
assert dfonts
|
||||
subspec = dict_value(dfonts[0]).copy()
|
||||
for k in ('Encoding', 'ToUnicode'):
|
||||
if k in spec:
|
||||
subspec[k] = resolve1(spec[k])
|
||||
font = self.get_font(None, subspec)
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFontError('Invalid Font spec: %r' % spec)
|
||||
font = PDFType1Font(self, spec) # this is so wrong!
|
||||
if objid and self.caching:
|
||||
self._cached_fonts[objid] = font
|
||||
return font
|
||||
@@ -1,32 +0,0 @@
|
||||
import zlib
|
||||
|
||||
from ..PSStackParser import PSException
|
||||
from ..PSStackParser import PSObject
|
||||
from ...utils import lzwdecode, settings
|
||||
|
||||
from .constants import * # pylint: disable=W0614
|
||||
from .types import * # pylint: disable=W0614
|
||||
from .exceptions import * # pylint: disable=W0614
|
||||
|
||||
## PDFObjRef
|
||||
##
|
||||
class PDFObjRef(PDFObject):
|
||||
|
||||
def __init__(self, doc, objid, a):
|
||||
if objid == 0:
|
||||
if settings.STRICT:
|
||||
raise PDFValueError('PDF object id cannot be 0.')
|
||||
self.doc = doc
|
||||
self.objid = objid
|
||||
#self.genno = genno # Never used.
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFObjRef:%d>' % (self.objid)
|
||||
|
||||
def resolve(self, default=None):
|
||||
try:
|
||||
return self.doc.getobj(self.objid)
|
||||
except PDFObjectNotFound:
|
||||
return default
|
||||
|
||||
@@ -1,144 +0,0 @@
|
||||
import zlib
|
||||
|
||||
from ...utils import lzwdecode
|
||||
from ...utils import ascii85decode, asciihexdecode, apply_png_predictor
|
||||
|
||||
from .constants import *
|
||||
from .types import *
|
||||
from .exceptions import *
|
||||
from .util import *
|
||||
from .ccitt import ccittfaxdecode
|
||||
|
||||
class PDFStream(PDFObject):
|
||||
|
||||
@classmethod
|
||||
def validated_stream(cls, x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, cls):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('PDFStream required: %r' % x)
|
||||
return PDFStream({}, b'')
|
||||
return x
|
||||
|
||||
def __init__(self, attrs, rawdata, decipher=None):
|
||||
assert isinstance(attrs, dict), str(type(attrs))
|
||||
self.attrs = attrs
|
||||
self.rawdata = rawdata
|
||||
self.decipher = decipher
|
||||
self.data = None
|
||||
self.objid = None
|
||||
self.genno = None
|
||||
return
|
||||
|
||||
def set_objid(self, objid, genno):
|
||||
self.objid = objid
|
||||
self.genno = genno
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
if self.data is None:
|
||||
assert self.rawdata is not None
|
||||
return '<PDFStream(%r): raw=%d, %r>' % (self.objid, len(self.rawdata), self.attrs)
|
||||
else:
|
||||
assert self.data is not None
|
||||
return '<PDFStream(%r): len=%d, %r>' % (self.objid, len(self.data), self.attrs)
|
||||
|
||||
def __contains__(self, name):
|
||||
return name in self.attrs
|
||||
|
||||
def __getitem__(self, name):
|
||||
return self.attrs[name]
|
||||
|
||||
def get(self, name, default=None):
|
||||
return self.attrs.get(name, default)
|
||||
|
||||
def get_any(self, names, default=None):
|
||||
for name in names:
|
||||
if name in self.attrs:
|
||||
return self.attrs[name]
|
||||
return default
|
||||
|
||||
def get_filters(self):
|
||||
filters = self.get_any(('F', 'Filter'))
|
||||
params = self.get_any(('DP', 'DecodeParms', 'FDecodeParms'), {})
|
||||
if not filters:
|
||||
return []
|
||||
if not isinstance(filters, list):
|
||||
filters = [filters]
|
||||
if not isinstance(params, list):
|
||||
# Make sure the parameters list is the same as filters.
|
||||
params = [params] * len(filters)
|
||||
if settings.STRICT and len(params) != len(filters):
|
||||
raise PDFException("Parameters len filter mismatch")
|
||||
# resolve filter if possible
|
||||
_filters = []
|
||||
for fltr in filters:
|
||||
if hasattr(fltr, 'resolve'):
|
||||
fltr = fltr.resolve()[0]
|
||||
_filters.append(fltr)
|
||||
return list(zip(_filters, params)) #solves https://github.com/pdfminer/pdfminer.six/issues/15
|
||||
|
||||
def decode(self):
|
||||
assert self.data is None and self.rawdata is not None, str((self.data, self.rawdata))
|
||||
data = self.rawdata
|
||||
if self.decipher:
|
||||
# Handle encryption
|
||||
data = self.decipher(self.objid, self.genno, data, self.attrs)
|
||||
filters = self.get_filters()
|
||||
if not filters:
|
||||
self.data = data
|
||||
self.rawdata = None
|
||||
return
|
||||
for (f,params) in filters:
|
||||
if f in LITERALS_FLATE_DECODE:
|
||||
# will get errors if the document is encrypted.
|
||||
try:
|
||||
data = zlib.decompress(data)
|
||||
except zlib.error as e:
|
||||
if settings.STRICT:
|
||||
raise PDFException('Invalid zlib bytes: %r, %r' % (e, data))
|
||||
data = b''
|
||||
elif f in LITERALS_LZW_DECODE:
|
||||
data = lzwdecode(data)
|
||||
elif f in LITERALS_ASCII85_DECODE:
|
||||
data = ascii85decode(data)
|
||||
elif f in LITERALS_ASCIIHEX_DECODE:
|
||||
data = asciihexdecode(data)
|
||||
elif f in LITERALS_RUNLENGTH_DECODE:
|
||||
data = rldecode(data)
|
||||
elif f in LITERALS_CCITTFAX_DECODE:
|
||||
data = ccittfaxdecode(data, params)
|
||||
elif f in LITERALS_DCT_DECODE:
|
||||
# This is probably a JPG stream - it does not need to be decoded twice.
|
||||
# Just return the stream to the user.
|
||||
pass
|
||||
elif f == LITERAL_CRYPT:
|
||||
# not yet..
|
||||
raise PDFNotImplementedError('/Crypt filter is unsupported')
|
||||
else:
|
||||
raise PDFNotImplementedError('Unsupported filter: %r' % f)
|
||||
# apply predictors
|
||||
if params and 'Predictor' in params:
|
||||
pred = int_value(params['Predictor'])
|
||||
if pred == 1:
|
||||
# no predictor
|
||||
pass
|
||||
elif 10 <= pred:
|
||||
# PNG predictor
|
||||
colors = int_value(params.get('Colors', 1))
|
||||
columns = int_value(params.get('Columns', 1))
|
||||
bitspercomponent = int_value(params.get('BitsPerComponent', 8))
|
||||
data = apply_png_predictor(pred, colors, columns, bitspercomponent, data)
|
||||
else:
|
||||
raise PDFNotImplementedError('Unsupported predictor: %r' % pred)
|
||||
self.data = data
|
||||
self.rawdata = None
|
||||
return
|
||||
|
||||
def get_data(self):
|
||||
if self.data is None:
|
||||
self.decode()
|
||||
return self.data
|
||||
|
||||
def get_rawdata(self):
|
||||
return self.rawdata
|
||||
@@ -1,604 +0,0 @@
|
||||
|
||||
# CCITT Fax decoder
|
||||
#
|
||||
# Bugs: uncompressed mode untested.
|
||||
#
|
||||
# cf.
|
||||
# ITU-T Recommendation T.4
|
||||
# "Standardization of Group 3 facsimile terminals for document transmission"
|
||||
# ITU-T Recommendation T.6
|
||||
# "FACSIMILE CODING SCHEMES AND CODING CONTROL FUNCTIONS FOR GROUP 4 FACSIMILE APPARATUS"
|
||||
|
||||
|
||||
import sys
|
||||
import array
|
||||
from abc import abstractmethod
|
||||
|
||||
def get_bytes(data):
|
||||
for byte in data:
|
||||
yield byte
|
||||
|
||||
|
||||
## BitParser
|
||||
##
|
||||
class BitParser(object):
|
||||
|
||||
def __init__(self):
|
||||
self._pos = 0
|
||||
self._state = None
|
||||
return
|
||||
|
||||
@classmethod
|
||||
def add(klass, root, v, bits):
|
||||
p = root
|
||||
b = None
|
||||
for i in range(len(bits)):
|
||||
if 0 < i:
|
||||
if p[b] is None:
|
||||
p[b] = [None, None]
|
||||
p = p[b]
|
||||
if bits[i] == '1':
|
||||
b = 1
|
||||
else:
|
||||
b = 0
|
||||
p[b] = v
|
||||
return
|
||||
|
||||
def feedbytes(self, data):
|
||||
for byte in get_bytes(data):
|
||||
for m in (128, 64, 32, 16, 8, 4, 2, 1):
|
||||
self._parse_bit(byte & m)
|
||||
return
|
||||
|
||||
def _parse_bit(self, x):
|
||||
if x:
|
||||
v = self._state[1]
|
||||
else:
|
||||
v = self._state[0]
|
||||
self._pos += 1
|
||||
if isinstance(v, list):
|
||||
self._state = v
|
||||
else:
|
||||
self._state = self._accept(v)
|
||||
return
|
||||
|
||||
@abstractmethod
|
||||
def _accept(self, v):
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
## CCITTG4Parser
|
||||
##
|
||||
class CCITTG4Parser(BitParser):
|
||||
|
||||
MODE = [None, None]
|
||||
BitParser.add(MODE, 0, '1')
|
||||
BitParser.add(MODE, +1, '011')
|
||||
BitParser.add(MODE, -1, '010')
|
||||
BitParser.add(MODE, 'h', '001')
|
||||
BitParser.add(MODE, 'p', '0001')
|
||||
BitParser.add(MODE, +2, '000011')
|
||||
BitParser.add(MODE, -2, '000010')
|
||||
BitParser.add(MODE, +3, '0000011')
|
||||
BitParser.add(MODE, -3, '0000010')
|
||||
BitParser.add(MODE, 'u', '0000001111')
|
||||
BitParser.add(MODE, 'x1', '0000001000')
|
||||
BitParser.add(MODE, 'x2', '0000001001')
|
||||
BitParser.add(MODE, 'x3', '0000001010')
|
||||
BitParser.add(MODE, 'x4', '0000001011')
|
||||
BitParser.add(MODE, 'x5', '0000001100')
|
||||
BitParser.add(MODE, 'x6', '0000001101')
|
||||
BitParser.add(MODE, 'x7', '0000001110')
|
||||
BitParser.add(MODE, 'e', '000000000001000000000001')
|
||||
|
||||
WHITE = [None, None]
|
||||
BitParser.add(WHITE, 0 , '00110101')
|
||||
BitParser.add(WHITE, 1 , '000111')
|
||||
BitParser.add(WHITE, 2 , '0111')
|
||||
BitParser.add(WHITE, 3 , '1000')
|
||||
BitParser.add(WHITE, 4 , '1011')
|
||||
BitParser.add(WHITE, 5 , '1100')
|
||||
BitParser.add(WHITE, 6 , '1110')
|
||||
BitParser.add(WHITE, 7 , '1111')
|
||||
BitParser.add(WHITE, 8 , '10011')
|
||||
BitParser.add(WHITE, 9 , '10100')
|
||||
BitParser.add(WHITE, 10 , '00111')
|
||||
BitParser.add(WHITE, 11 , '01000')
|
||||
BitParser.add(WHITE, 12 , '001000')
|
||||
BitParser.add(WHITE, 13 , '000011')
|
||||
BitParser.add(WHITE, 14 , '110100')
|
||||
BitParser.add(WHITE, 15 , '110101')
|
||||
BitParser.add(WHITE, 16 , '101010')
|
||||
BitParser.add(WHITE, 17 , '101011')
|
||||
BitParser.add(WHITE, 18 , '0100111')
|
||||
BitParser.add(WHITE, 19 , '0001100')
|
||||
BitParser.add(WHITE, 20 , '0001000')
|
||||
BitParser.add(WHITE, 21 , '0010111')
|
||||
BitParser.add(WHITE, 22 , '0000011')
|
||||
BitParser.add(WHITE, 23 , '0000100')
|
||||
BitParser.add(WHITE, 24 , '0101000')
|
||||
BitParser.add(WHITE, 25 , '0101011')
|
||||
BitParser.add(WHITE, 26 , '0010011')
|
||||
BitParser.add(WHITE, 27 , '0100100')
|
||||
BitParser.add(WHITE, 28 , '0011000')
|
||||
BitParser.add(WHITE, 29 , '00000010')
|
||||
BitParser.add(WHITE, 30 , '00000011')
|
||||
BitParser.add(WHITE, 31 , '00011010')
|
||||
BitParser.add(WHITE, 32 , '00011011')
|
||||
BitParser.add(WHITE, 33 , '00010010')
|
||||
BitParser.add(WHITE, 34 , '00010011')
|
||||
BitParser.add(WHITE, 35 , '00010100')
|
||||
BitParser.add(WHITE, 36 , '00010101')
|
||||
BitParser.add(WHITE, 37 , '00010110')
|
||||
BitParser.add(WHITE, 38 , '00010111')
|
||||
BitParser.add(WHITE, 39 , '00101000')
|
||||
BitParser.add(WHITE, 40 , '00101001')
|
||||
BitParser.add(WHITE, 41 , '00101010')
|
||||
BitParser.add(WHITE, 42 , '00101011')
|
||||
BitParser.add(WHITE, 43 , '00101100')
|
||||
BitParser.add(WHITE, 44 , '00101101')
|
||||
BitParser.add(WHITE, 45 , '00000100')
|
||||
BitParser.add(WHITE, 46 , '00000101')
|
||||
BitParser.add(WHITE, 47 , '00001010')
|
||||
BitParser.add(WHITE, 48 , '00001011')
|
||||
BitParser.add(WHITE, 49 , '01010010')
|
||||
BitParser.add(WHITE, 50 , '01010011')
|
||||
BitParser.add(WHITE, 51 , '01010100')
|
||||
BitParser.add(WHITE, 52 , '01010101')
|
||||
BitParser.add(WHITE, 53 , '00100100')
|
||||
BitParser.add(WHITE, 54 , '00100101')
|
||||
BitParser.add(WHITE, 55 , '01011000')
|
||||
BitParser.add(WHITE, 56 , '01011001')
|
||||
BitParser.add(WHITE, 57 , '01011010')
|
||||
BitParser.add(WHITE, 58 , '01011011')
|
||||
BitParser.add(WHITE, 59 , '01001010')
|
||||
BitParser.add(WHITE, 60 , '01001011')
|
||||
BitParser.add(WHITE, 61 , '00110010')
|
||||
BitParser.add(WHITE, 62 , '00110011')
|
||||
BitParser.add(WHITE, 63 , '00110100')
|
||||
BitParser.add(WHITE, 64 , '11011')
|
||||
BitParser.add(WHITE, 128 , '10010')
|
||||
BitParser.add(WHITE, 192 , '010111')
|
||||
BitParser.add(WHITE, 256 , '0110111')
|
||||
BitParser.add(WHITE, 320 , '00110110')
|
||||
BitParser.add(WHITE, 384 , '00110111')
|
||||
BitParser.add(WHITE, 448 , '01100100')
|
||||
BitParser.add(WHITE, 512 , '01100101')
|
||||
BitParser.add(WHITE, 576 , '01101000')
|
||||
BitParser.add(WHITE, 640 , '01100111')
|
||||
BitParser.add(WHITE, 704 , '011001100')
|
||||
BitParser.add(WHITE, 768 , '011001101')
|
||||
BitParser.add(WHITE, 832 , '011010010')
|
||||
BitParser.add(WHITE, 896 , '011010011')
|
||||
BitParser.add(WHITE, 960 , '011010100')
|
||||
BitParser.add(WHITE, 1024, '011010101')
|
||||
BitParser.add(WHITE, 1088, '011010110')
|
||||
BitParser.add(WHITE, 1152, '011010111')
|
||||
BitParser.add(WHITE, 1216, '011011000')
|
||||
BitParser.add(WHITE, 1280, '011011001')
|
||||
BitParser.add(WHITE, 1344, '011011010')
|
||||
BitParser.add(WHITE, 1408, '011011011')
|
||||
BitParser.add(WHITE, 1472, '010011000')
|
||||
BitParser.add(WHITE, 1536, '010011001')
|
||||
BitParser.add(WHITE, 1600, '010011010')
|
||||
BitParser.add(WHITE, 1664, '011000')
|
||||
BitParser.add(WHITE, 1728, '010011011')
|
||||
BitParser.add(WHITE, 1792, '00000001000')
|
||||
BitParser.add(WHITE, 1856, '00000001100')
|
||||
BitParser.add(WHITE, 1920, '00000001101')
|
||||
BitParser.add(WHITE, 1984, '000000010010')
|
||||
BitParser.add(WHITE, 2048, '000000010011')
|
||||
BitParser.add(WHITE, 2112, '000000010100')
|
||||
BitParser.add(WHITE, 2176, '000000010101')
|
||||
BitParser.add(WHITE, 2240, '000000010110')
|
||||
BitParser.add(WHITE, 2304, '000000010111')
|
||||
BitParser.add(WHITE, 2368, '000000011100')
|
||||
BitParser.add(WHITE, 2432, '000000011101')
|
||||
BitParser.add(WHITE, 2496, '000000011110')
|
||||
BitParser.add(WHITE, 2560, '000000011111')
|
||||
|
||||
BLACK = [None, None]
|
||||
BitParser.add(BLACK, 0 , '0000110111')
|
||||
BitParser.add(BLACK, 1 , '010')
|
||||
BitParser.add(BLACK, 2 , '11')
|
||||
BitParser.add(BLACK, 3 , '10')
|
||||
BitParser.add(BLACK, 4 , '011')
|
||||
BitParser.add(BLACK, 5 , '0011')
|
||||
BitParser.add(BLACK, 6 , '0010')
|
||||
BitParser.add(BLACK, 7 , '00011')
|
||||
BitParser.add(BLACK, 8 , '000101')
|
||||
BitParser.add(BLACK, 9 , '000100')
|
||||
BitParser.add(BLACK, 10 , '0000100')
|
||||
BitParser.add(BLACK, 11 , '0000101')
|
||||
BitParser.add(BLACK, 12 , '0000111')
|
||||
BitParser.add(BLACK, 13 , '00000100')
|
||||
BitParser.add(BLACK, 14 , '00000111')
|
||||
BitParser.add(BLACK, 15 , '000011000')
|
||||
BitParser.add(BLACK, 16 , '0000010111')
|
||||
BitParser.add(BLACK, 17 , '0000011000')
|
||||
BitParser.add(BLACK, 18 , '0000001000')
|
||||
BitParser.add(BLACK, 19 , '00001100111')
|
||||
BitParser.add(BLACK, 20 , '00001101000')
|
||||
BitParser.add(BLACK, 21 , '00001101100')
|
||||
BitParser.add(BLACK, 22 , '00000110111')
|
||||
BitParser.add(BLACK, 23 , '00000101000')
|
||||
BitParser.add(BLACK, 24 , '00000010111')
|
||||
BitParser.add(BLACK, 25 , '00000011000')
|
||||
BitParser.add(BLACK, 26 , '000011001010')
|
||||
BitParser.add(BLACK, 27 , '000011001011')
|
||||
BitParser.add(BLACK, 28 , '000011001100')
|
||||
BitParser.add(BLACK, 29 , '000011001101')
|
||||
BitParser.add(BLACK, 30 , '000001101000')
|
||||
BitParser.add(BLACK, 31 , '000001101001')
|
||||
BitParser.add(BLACK, 32 , '000001101010')
|
||||
BitParser.add(BLACK, 33 , '000001101011')
|
||||
BitParser.add(BLACK, 34 , '000011010010')
|
||||
BitParser.add(BLACK, 35 , '000011010011')
|
||||
BitParser.add(BLACK, 36 , '000011010100')
|
||||
BitParser.add(BLACK, 37 , '000011010101')
|
||||
BitParser.add(BLACK, 38 , '000011010110')
|
||||
BitParser.add(BLACK, 39 , '000011010111')
|
||||
BitParser.add(BLACK, 40 , '000001101100')
|
||||
BitParser.add(BLACK, 41 , '000001101101')
|
||||
BitParser.add(BLACK, 42 , '000011011010')
|
||||
BitParser.add(BLACK, 43 , '000011011011')
|
||||
BitParser.add(BLACK, 44 , '000001010100')
|
||||
BitParser.add(BLACK, 45 , '000001010101')
|
||||
BitParser.add(BLACK, 46 , '000001010110')
|
||||
BitParser.add(BLACK, 47 , '000001010111')
|
||||
BitParser.add(BLACK, 48 , '000001100100')
|
||||
BitParser.add(BLACK, 49 , '000001100101')
|
||||
BitParser.add(BLACK, 50 , '000001010010')
|
||||
BitParser.add(BLACK, 51 , '000001010011')
|
||||
BitParser.add(BLACK, 52 , '000000100100')
|
||||
BitParser.add(BLACK, 53 , '000000110111')
|
||||
BitParser.add(BLACK, 54 , '000000111000')
|
||||
BitParser.add(BLACK, 55 , '000000100111')
|
||||
BitParser.add(BLACK, 56 , '000000101000')
|
||||
BitParser.add(BLACK, 57 , '000001011000')
|
||||
BitParser.add(BLACK, 58 , '000001011001')
|
||||
BitParser.add(BLACK, 59 , '000000101011')
|
||||
BitParser.add(BLACK, 60 , '000000101100')
|
||||
BitParser.add(BLACK, 61 , '000001011010')
|
||||
BitParser.add(BLACK, 62 , '000001100110')
|
||||
BitParser.add(BLACK, 63 , '000001100111')
|
||||
BitParser.add(BLACK, 64 , '0000001111')
|
||||
BitParser.add(BLACK, 128 , '000011001000')
|
||||
BitParser.add(BLACK, 192 , '000011001001')
|
||||
BitParser.add(BLACK, 256 , '000001011011')
|
||||
BitParser.add(BLACK, 320 , '000000110011')
|
||||
BitParser.add(BLACK, 384 , '000000110100')
|
||||
BitParser.add(BLACK, 448 , '000000110101')
|
||||
BitParser.add(BLACK, 512 , '0000001101100')
|
||||
BitParser.add(BLACK, 576 , '0000001101101')
|
||||
BitParser.add(BLACK, 640 , '0000001001010')
|
||||
BitParser.add(BLACK, 704 , '0000001001011')
|
||||
BitParser.add(BLACK, 768 , '0000001001100')
|
||||
BitParser.add(BLACK, 832 , '0000001001101')
|
||||
BitParser.add(BLACK, 896 , '0000001110010')
|
||||
BitParser.add(BLACK, 960 , '0000001110011')
|
||||
BitParser.add(BLACK, 1024, '0000001110100')
|
||||
BitParser.add(BLACK, 1088, '0000001110101')
|
||||
BitParser.add(BLACK, 1152, '0000001110110')
|
||||
BitParser.add(BLACK, 1216, '0000001110111')
|
||||
BitParser.add(BLACK, 1280, '0000001010010')
|
||||
BitParser.add(BLACK, 1344, '0000001010011')
|
||||
BitParser.add(BLACK, 1408, '0000001010100')
|
||||
BitParser.add(BLACK, 1472, '0000001010101')
|
||||
BitParser.add(BLACK, 1536, '0000001011010')
|
||||
BitParser.add(BLACK, 1600, '0000001011011')
|
||||
BitParser.add(BLACK, 1664, '0000001100100')
|
||||
BitParser.add(BLACK, 1728, '0000001100101')
|
||||
BitParser.add(BLACK, 1792, '00000001000')
|
||||
BitParser.add(BLACK, 1856, '00000001100')
|
||||
BitParser.add(BLACK, 1920, '00000001101')
|
||||
BitParser.add(BLACK, 1984, '000000010010')
|
||||
BitParser.add(BLACK, 2048, '000000010011')
|
||||
BitParser.add(BLACK, 2112, '000000010100')
|
||||
BitParser.add(BLACK, 2176, '000000010101')
|
||||
BitParser.add(BLACK, 2240, '000000010110')
|
||||
BitParser.add(BLACK, 2304, '000000010111')
|
||||
BitParser.add(BLACK, 2368, '000000011100')
|
||||
BitParser.add(BLACK, 2432, '000000011101')
|
||||
BitParser.add(BLACK, 2496, '000000011110')
|
||||
BitParser.add(BLACK, 2560, '000000011111')
|
||||
|
||||
UNCOMPRESSED = [None, None]
|
||||
BitParser.add(UNCOMPRESSED, '1', '1')
|
||||
BitParser.add(UNCOMPRESSED, '01', '01')
|
||||
BitParser.add(UNCOMPRESSED, '001', '001')
|
||||
BitParser.add(UNCOMPRESSED, '0001', '0001')
|
||||
BitParser.add(UNCOMPRESSED, '00001', '00001')
|
||||
BitParser.add(UNCOMPRESSED, '00000', '000001')
|
||||
BitParser.add(UNCOMPRESSED, 'T00', '00000011')
|
||||
BitParser.add(UNCOMPRESSED, 'T10', '00000010')
|
||||
BitParser.add(UNCOMPRESSED, 'T000', '000000011')
|
||||
BitParser.add(UNCOMPRESSED, 'T100', '000000010')
|
||||
BitParser.add(UNCOMPRESSED, 'T0000', '0000000011')
|
||||
BitParser.add(UNCOMPRESSED, 'T1000', '0000000010')
|
||||
BitParser.add(UNCOMPRESSED, 'T00000', '00000000011')
|
||||
BitParser.add(UNCOMPRESSED, 'T10000', '00000000010')
|
||||
|
||||
class EOFB(Exception):
|
||||
pass
|
||||
|
||||
class InvalidData(Exception):
|
||||
pass
|
||||
|
||||
class ByteSkip(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, width, bytealign=False):
|
||||
BitParser.__init__(self)
|
||||
self.width = width
|
||||
self.bytealign = bytealign
|
||||
self.reset()
|
||||
return
|
||||
|
||||
def feedbytes(self, data):
|
||||
for byte in get_bytes(data):
|
||||
try:
|
||||
for m in (128, 64, 32, 16, 8, 4, 2, 1):
|
||||
self._parse_bit(byte & m)
|
||||
except self.ByteSkip:
|
||||
self._accept = self._parse_mode
|
||||
self._state = self.MODE
|
||||
except self.EOFB:
|
||||
break
|
||||
return
|
||||
|
||||
def _parse_mode(self, mode):
|
||||
if mode == 'p':
|
||||
self._do_pass()
|
||||
self._flush_line()
|
||||
return self.MODE
|
||||
elif mode == 'h':
|
||||
self._n1 = 0
|
||||
self._accept = self._parse_horiz1
|
||||
if self._color:
|
||||
return self.WHITE
|
||||
else:
|
||||
return self.BLACK
|
||||
elif mode == 'u':
|
||||
self._accept = self._parse_uncompressed
|
||||
return self.UNCOMPRESSED
|
||||
elif mode == 'e':
|
||||
raise self.EOFB
|
||||
elif isinstance(mode, int):
|
||||
self._do_vertical(mode)
|
||||
self._flush_line()
|
||||
return self.MODE
|
||||
else:
|
||||
raise self.InvalidData(mode)
|
||||
|
||||
def _parse_horiz1(self, n):
|
||||
if n is None:
|
||||
raise self.InvalidData
|
||||
self._n1 += n
|
||||
if n < 64:
|
||||
self._n2 = 0
|
||||
self._color = 1-self._color
|
||||
self._accept = self._parse_horiz2
|
||||
if self._color:
|
||||
return self.WHITE
|
||||
else:
|
||||
return self.BLACK
|
||||
|
||||
def _parse_horiz2(self, n):
|
||||
if n is None:
|
||||
raise self.InvalidData
|
||||
self._n2 += n
|
||||
if n < 64:
|
||||
self._color = 1-self._color
|
||||
self._accept = self._parse_mode
|
||||
self._do_horizontal(self._n1, self._n2)
|
||||
self._flush_line()
|
||||
return self.MODE
|
||||
elif self._color:
|
||||
return self.WHITE
|
||||
else:
|
||||
return self.BLACK
|
||||
|
||||
def _parse_uncompressed(self, bits):
|
||||
if not bits:
|
||||
raise self.InvalidData
|
||||
if bits.startswith('T'):
|
||||
self._accept = self._parse_mode
|
||||
self._color = int(bits[1])
|
||||
self._do_uncompressed(bits[2:])
|
||||
return self.MODE
|
||||
else:
|
||||
self._do_uncompressed(bits)
|
||||
return self.UNCOMPRESSED
|
||||
|
||||
def _get_bits(self):
|
||||
return ''.join(str(b) for b in self._curline[:self._curpos])
|
||||
|
||||
def _get_refline(self, i):
|
||||
if i < 0:
|
||||
return '[]'+''.join(str(b) for b in self._refline)
|
||||
elif len(self._refline) <= i:
|
||||
return ''.join(str(b) for b in self._refline)+'[]'
|
||||
else:
|
||||
return (''.join(str(b) for b in self._refline[:i]) +
|
||||
'['+str(self._refline[i])+']' +
|
||||
''.join(str(b) for b in self._refline[i+1:]))
|
||||
|
||||
def reset(self):
|
||||
self._y = 0
|
||||
self._curline = array.array('b', [1]*self.width)
|
||||
self._reset_line()
|
||||
self._accept = self._parse_mode
|
||||
self._state = self.MODE
|
||||
return
|
||||
|
||||
def output_line(self, y, bits):
|
||||
print (y, ''.join(str(b) for b in bits))
|
||||
return
|
||||
|
||||
def _reset_line(self):
|
||||
self._refline = self._curline
|
||||
self._curline = array.array('b', [1]*self.width)
|
||||
self._curpos = -1
|
||||
self._color = 1
|
||||
return
|
||||
|
||||
def _flush_line(self):
|
||||
if self.width <= self._curpos:
|
||||
self.output_line(self._y, self._curline)
|
||||
self._y += 1
|
||||
self._reset_line()
|
||||
if self.bytealign:
|
||||
raise self.ByteSkip
|
||||
return
|
||||
|
||||
def _do_vertical(self, dx):
|
||||
x1 = self._curpos+1
|
||||
while 1:
|
||||
if x1 == 0:
|
||||
if (self._color == 1 and self._refline[x1] != self._color):
|
||||
break
|
||||
elif x1 == len(self._refline):
|
||||
break
|
||||
elif (self._refline[x1-1] == self._color and
|
||||
self._refline[x1] != self._color):
|
||||
break
|
||||
x1 += 1
|
||||
x1 += dx
|
||||
x0 = max(0, self._curpos)
|
||||
x1 = max(0, min(self.width, x1))
|
||||
if x1 < x0:
|
||||
for x in range(x1, x0):
|
||||
self._curline[x] = self._color
|
||||
elif x0 < x1:
|
||||
for x in range(x0, x1):
|
||||
self._curline[x] = self._color
|
||||
self._curpos = x1
|
||||
self._color = 1-self._color
|
||||
return
|
||||
|
||||
def _do_pass(self):
|
||||
x1 = self._curpos+1
|
||||
while 1:
|
||||
if x1 == 0:
|
||||
if (self._color == 1 and self._refline[x1] != self._color):
|
||||
break
|
||||
elif x1 == len(self._refline):
|
||||
break
|
||||
elif (self._refline[x1-1] == self._color and
|
||||
self._refline[x1] != self._color):
|
||||
break
|
||||
x1 += 1
|
||||
while 1:
|
||||
if x1 == 0:
|
||||
if (self._color == 0 and self._refline[x1] == self._color):
|
||||
break
|
||||
elif x1 == len(self._refline):
|
||||
break
|
||||
elif (self._refline[x1-1] != self._color and
|
||||
self._refline[x1] == self._color):
|
||||
break
|
||||
x1 += 1
|
||||
for x in range(self._curpos, x1):
|
||||
self._curline[x] = self._color
|
||||
self._curpos = x1
|
||||
return
|
||||
|
||||
def _do_horizontal(self, n1, n2):
|
||||
if self._curpos < 0:
|
||||
self._curpos = 0
|
||||
x = self._curpos
|
||||
for _ in range(n1):
|
||||
if len(self._curline) <= x:
|
||||
break
|
||||
self._curline[x] = self._color
|
||||
x += 1
|
||||
for _ in range(n2):
|
||||
if len(self._curline) <= x:
|
||||
break
|
||||
self._curline[x] = 1-self._color
|
||||
x += 1
|
||||
self._curpos = x
|
||||
return
|
||||
|
||||
def _do_uncompressed(self, bits):
|
||||
for c in bits:
|
||||
self._curline[self._curpos] = int(c)
|
||||
self._curpos += 1
|
||||
self._flush_line()
|
||||
return
|
||||
|
||||
|
||||
|
||||
|
||||
class CCITTFaxDecoder(CCITTG4Parser):
|
||||
|
||||
def __init__(self, width, bytealign=False, reversed=False):
|
||||
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
|
||||
self.reversed = reversed
|
||||
self._buf = b''
|
||||
return
|
||||
|
||||
def close(self):
|
||||
return self._buf
|
||||
|
||||
def output_line(self, y, bits):
|
||||
bytes = array.array('B', [0]*((len(bits)+7)//8))
|
||||
if self.reversed:
|
||||
bits = [1-b for b in bits]
|
||||
for (i, b) in enumerate(bits):
|
||||
if b:
|
||||
bytes[i//8] += (128, 64, 32, 16, 8, 4, 2, 1)[i % 8]
|
||||
self._buf += bytes.tostring()
|
||||
return
|
||||
|
||||
|
||||
def ccittfaxdecode(data, params):
|
||||
K = params.get('K')
|
||||
cols = params.get('Columns')
|
||||
bytealign = params.get('EncodedByteAlign')
|
||||
reversed = params.get('BlackIs1')
|
||||
if K == -1:
|
||||
parser = CCITTFaxDecoder(cols, bytealign=bytealign, reversed=reversed)
|
||||
else:
|
||||
raise ValueError(K)
|
||||
parser.feedbytes(data)
|
||||
return parser.close()
|
||||
|
||||
|
||||
# test
|
||||
def main(argv):
|
||||
if not argv[1:]:
|
||||
import unittest
|
||||
return unittest.main()
|
||||
|
||||
class Parser(CCITTG4Parser):
|
||||
def __init__(self, width, bytealign=False):
|
||||
import pygame
|
||||
CCITTG4Parser.__init__(self, width, bytealign=bytealign)
|
||||
self.img = pygame.Surface((self.width, 1000))
|
||||
return
|
||||
|
||||
def output_line(self, y, bits):
|
||||
for (x, b) in enumerate(bits):
|
||||
if b:
|
||||
self.img.set_at((x, y), (255, 255, 255))
|
||||
else:
|
||||
self.img.set_at((x, y), (0, 0, 0))
|
||||
return
|
||||
|
||||
def close(self):
|
||||
import pygame
|
||||
pygame.image.save(self.img, 'out.bmp')
|
||||
return
|
||||
for path in argv[1:]:
|
||||
fp = open(path, 'rb')
|
||||
(_, _, _, w, _, _) = path.split('.')
|
||||
parser = Parser(int(w))
|
||||
parser.feedbytes(fp.read())
|
||||
parser.close()
|
||||
fp.close()
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
@@ -1,13 +0,0 @@
|
||||
from ...utils import settings
|
||||
from ..PSStackParser import LIT
|
||||
|
||||
LITERAL_CRYPT = LIT('Crypt')
|
||||
|
||||
# Abbreviation of Filter names in PDF 4.8.6. "Inline Images"
|
||||
LITERALS_FLATE_DECODE = (LIT('FlateDecode'), LIT('Fl'))
|
||||
LITERALS_LZW_DECODE = (LIT('LZWDecode'), LIT('LZW'))
|
||||
LITERALS_ASCII85_DECODE = (LIT('ASCII85Decode'), LIT('A85'))
|
||||
LITERALS_ASCIIHEX_DECODE = (LIT('ASCIIHexDecode'), LIT('AHx'))
|
||||
LITERALS_RUNLENGTH_DECODE = (LIT('RunLengthDecode'), LIT('RL'))
|
||||
LITERALS_CCITTFAX_DECODE = (LIT('CCITTFaxDecode'), LIT('CCF'))
|
||||
LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
|
||||
@@ -1,16 +0,0 @@
|
||||
from ..PSStackParser import PSException
|
||||
|
||||
class PDFException(PSException):
|
||||
pass
|
||||
|
||||
class PDFTypeError(PDFException):
|
||||
pass
|
||||
|
||||
class PDFValueError(PDFException):
|
||||
pass
|
||||
|
||||
class PDFObjectNotFound(PDFException):
|
||||
pass
|
||||
|
||||
class PDFNotImplementedError(PDFException):
|
||||
pass
|
||||
@@ -1,6 +0,0 @@
|
||||
from ..PSStackParser import PSObject
|
||||
|
||||
## PDF Objects
|
||||
##
|
||||
class PDFObject(PSObject):
|
||||
pass
|
||||
@@ -1,139 +0,0 @@
|
||||
from .PDFObjRef import PDFObjRef
|
||||
from ...utils import settings, isnumber, int2byte
|
||||
from .exceptions import PDFTypeError
|
||||
|
||||
from logging import getLogger
|
||||
|
||||
log = getLogger(__name__)
|
||||
|
||||
# resolve
|
||||
def resolve1(x, default=None):
|
||||
"""Resolves an object.
|
||||
|
||||
If this is an array or dictionary, it may still contains
|
||||
some indirect objects inside.
|
||||
"""
|
||||
while isinstance(x, PDFObjRef):
|
||||
x = x.resolve(default=default)
|
||||
return x
|
||||
|
||||
|
||||
def resolve_all(x, default=None):
|
||||
"""Recursively resolves the given object and all the internals.
|
||||
|
||||
Make sure there is no indirect reference within the nested object.
|
||||
This procedure might be slow.
|
||||
"""
|
||||
while isinstance(x, PDFObjRef):
|
||||
x = x.resolve(default=default)
|
||||
if isinstance(x, list):
|
||||
x = [resolve_all(v, default=default) for v in x]
|
||||
elif isinstance(x, dict):
|
||||
for (k, v) in x.iteritems():
|
||||
x[k] = resolve_all(v, default=default)
|
||||
return x
|
||||
|
||||
|
||||
def decipher_all(decipher, objid, genno, x):
|
||||
"""Recursively deciphers the given object.
|
||||
"""
|
||||
if isinstance(x, bytes):
|
||||
return decipher(objid, genno, x)
|
||||
if isinstance(x, list):
|
||||
x = [decipher_all(decipher, objid, genno, v) for v in x]
|
||||
elif isinstance(x, dict):
|
||||
for (k, v) in x.items():
|
||||
x[k] = decipher_all(decipher, objid, genno, v)
|
||||
return x
|
||||
# Type cheking
|
||||
def int_value(x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, int):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('Integer required: %r' % x)
|
||||
return 0
|
||||
return x
|
||||
|
||||
|
||||
def float_value(x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, float):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('Float required: %r' % x)
|
||||
return 0.0
|
||||
return x
|
||||
|
||||
|
||||
def num_value(x):
|
||||
x = resolve1(x)
|
||||
if not isnumber(x):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('Int or Float required: %r' % x)
|
||||
return 0
|
||||
return x
|
||||
|
||||
|
||||
def str_value(x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, bytes):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('String required: %r' % x)
|
||||
return ''
|
||||
return x
|
||||
|
||||
|
||||
def list_value(x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, (list, tuple)):
|
||||
if settings.STRICT:
|
||||
raise PDFTypeError('List required: %r' % x)
|
||||
return []
|
||||
return x
|
||||
|
||||
|
||||
def dict_value(x):
|
||||
x = resolve1(x)
|
||||
if not isinstance(x, dict):
|
||||
if settings.STRICT:
|
||||
log.error('PDFTypeError : Dict required: %r', x)
|
||||
raise PDFTypeError('Dict required: %r' % x)
|
||||
return {}
|
||||
return x
|
||||
|
||||
#
|
||||
# RunLength decoder (Adobe version) implementation based on PDF Reference
|
||||
# version 1.4 section 3.3.4.
|
||||
#
|
||||
# * public domain *
|
||||
#
|
||||
def rldecode(data):
|
||||
"""
|
||||
RunLength decoder (Adobe version) implementation based on PDF Reference
|
||||
version 1.4 section 3.3.4:
|
||||
The RunLengthDecode filter decodes data that has been encoded in a
|
||||
simple byte-oriented format based on run length. The encoded data
|
||||
is a sequence of runs, where each run consists of a length byte
|
||||
followed by 1 to 128 bytes of data. If the length byte is in the
|
||||
range 0 to 127, the following length + 1 (1 to 128) bytes are
|
||||
copied literally during decompression. If length is in the range
|
||||
129 to 255, the following single byte is to be copied 257 - length
|
||||
(2 to 128) times during decompression. A length value of 128
|
||||
denotes EOD.
|
||||
"""
|
||||
decoded = b''
|
||||
i = 0
|
||||
while i < len(data):
|
||||
length = data[i]
|
||||
if length == 128:
|
||||
break
|
||||
if length >= 0 and length < 128:
|
||||
for j in range(i+1,(i+1)+(length+1)):
|
||||
decoded+=int2byte(data[j])
|
||||
|
||||
i = (i+1) + (length+1)
|
||||
if length > 128:
|
||||
run = int2byte(data[i+1])*(257-length)
|
||||
decoded+=run
|
||||
i = (i+1) + 1
|
||||
return decoded
|
||||
|
||||
@@ -1,345 +0,0 @@
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
from io import TextIOWrapper
|
||||
from typing import List, Tuple, Union
|
||||
from ...utils import int2byte
|
||||
|
||||
from .exceptions import PSEOF
|
||||
from .constants import EOL, NONSPC, KWD, END_LITERAL, LIT, HEX
|
||||
from .constants import SPC, HEX_PAIR
|
||||
from .constants import END_NUMBER, END_STRING, END_KEYWORD, END_HEX_STRING
|
||||
from .constants import OCT_STRING, ESC_STRING, KEYWORD_DICT_BEGIN, KEYWORD_DICT_END
|
||||
from .types import PSLiteral
|
||||
|
||||
class PSBaseParser(object):
|
||||
|
||||
"""Most basic PostScript parser that performs only tokenization.
|
||||
"""
|
||||
BUFSIZ = 4096
|
||||
|
||||
def __init__(self, fp: TextIOWrapper):
|
||||
self.fp = fp
|
||||
self.seek(0)
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<%s: %r, bufpos=%d>' % (self.__class__.__name__, self.fp, self.bufpos)
|
||||
|
||||
def flush(self):
|
||||
return
|
||||
|
||||
def close(self):
|
||||
self.flush()
|
||||
return
|
||||
|
||||
def tell(self):
|
||||
return self.bufpos+self.charpos
|
||||
|
||||
def poll(self, pos=None, n=80):
|
||||
pos0 = self.fp.tell()
|
||||
if not pos:
|
||||
pos = self.bufpos+self.charpos
|
||||
self.fp.seek(pos)
|
||||
log.info('poll(%d): %r', pos, self.fp.read(n))
|
||||
self.fp.seek(pos0)
|
||||
return
|
||||
|
||||
def seek(self, pos: int):
|
||||
"""Seeks the parser to the given position.
|
||||
"""
|
||||
log.debug('seek: %r', pos)
|
||||
self.fp.seek(pos)
|
||||
# reset the status for nextline()
|
||||
self.bufpos: int = pos
|
||||
self.buf: bytes = b''
|
||||
self.charpos: int = 0
|
||||
# reset the status for nexttoken()
|
||||
self._current_parse_func = self._parse_main
|
||||
self._curtoken: bytes = b''
|
||||
self._curtokenpos: int = 0
|
||||
self._tokens: List[Tuple[int, Union[int, float, bool, str, bytes, PSLiteral]]] = []
|
||||
return
|
||||
|
||||
def fillbuf(self):
|
||||
if self.charpos < len(self.buf):
|
||||
return
|
||||
# fetch next chunk.
|
||||
self.bufpos = self.fp.tell()
|
||||
self.buf = self.fp.read(self.BUFSIZ)
|
||||
if not self.buf:
|
||||
raise PSEOF('Unexpected EOF')
|
||||
self.charpos = 0
|
||||
return
|
||||
|
||||
def nextline(self):
|
||||
"""Fetches a next line that ends either with \\r or \\n.
|
||||
"""
|
||||
linebuf = b''
|
||||
linepos = self.bufpos + self.charpos
|
||||
eol = False
|
||||
while 1:
|
||||
self.fillbuf()
|
||||
if eol:
|
||||
c = self.buf[self.charpos:self.charpos+1]
|
||||
# handle b'\r\n'
|
||||
if c == b'\n':
|
||||
linebuf += c
|
||||
self.charpos += 1
|
||||
break
|
||||
m = EOL.search(self.buf, self.charpos)
|
||||
if m:
|
||||
linebuf += self.buf[self.charpos:m.end(0)]
|
||||
self.charpos = m.end(0)
|
||||
if linebuf[-1:] == b'\r':
|
||||
eol = True
|
||||
else:
|
||||
break
|
||||
else:
|
||||
linebuf += self.buf[self.charpos:]
|
||||
self.charpos = len(self.buf)
|
||||
log.debug('nextline: %r, %r', linepos, linebuf)
|
||||
return (linepos, linebuf)
|
||||
|
||||
def revreadlines(self):
|
||||
"""Fetches a next line backword.
|
||||
|
||||
This is used to locate the trailers at the end of a file.
|
||||
"""
|
||||
self.fp.seek(0, 2)
|
||||
pos = self.fp.tell()
|
||||
buf = b''
|
||||
while 0 < pos:
|
||||
prevpos = pos
|
||||
pos = max(0, pos-self.BUFSIZ)
|
||||
self.fp.seek(pos)
|
||||
s = self.fp.read(prevpos-pos)
|
||||
if not s:
|
||||
break
|
||||
while 1:
|
||||
n = max(s.rfind(b'\r'), s.rfind(b'\n'))
|
||||
if n == -1:
|
||||
buf = s + buf
|
||||
break
|
||||
yield s[n:] + buf
|
||||
s = s[:n]
|
||||
buf = b''
|
||||
return
|
||||
|
||||
def _parse_main(self, s, i):
|
||||
m = NONSPC.search(s, i)
|
||||
if not m:
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
c = s[j:j+1]
|
||||
self._curtokenpos = self.bufpos+j
|
||||
if c == b'%':
|
||||
self._curtoken = b'%'
|
||||
self._current_parse_func = self._parse_comment
|
||||
return j+1
|
||||
elif c == b'/':
|
||||
self._curtoken = b''
|
||||
self._current_parse_func = self._parse_literal
|
||||
return j+1
|
||||
elif c in b'-+' or c.isdigit():
|
||||
self._curtoken = c
|
||||
self._current_parse_func = self._parse_number
|
||||
return j+1
|
||||
elif c == b'.':
|
||||
self._curtoken = c
|
||||
self._current_parse_func = self._parse_float
|
||||
return j+1
|
||||
elif c.isalpha():
|
||||
self._curtoken = c
|
||||
self._current_parse_func = self._parse_keyword
|
||||
return j+1
|
||||
elif c == b'(':
|
||||
self._curtoken = b''
|
||||
self.paren = 1
|
||||
self._current_parse_func = self._parse_string
|
||||
return j+1
|
||||
elif c == b'<':
|
||||
self._curtoken = b''
|
||||
self._current_parse_func = self._parse_wopen
|
||||
return j+1
|
||||
elif c == b'>':
|
||||
self._curtoken = b''
|
||||
self._current_parse_func = self._parse_wclose
|
||||
return j+1
|
||||
else:
|
||||
self._add_token(KWD(c))
|
||||
return j+1
|
||||
|
||||
def _add_token(self, obj):
|
||||
self._tokens.append((self._curtokenpos, obj))
|
||||
return
|
||||
|
||||
def _parse_comment(self, s, i):
|
||||
m = EOL.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
self._current_parse_func = self._parse_main
|
||||
# We ignore comments.
|
||||
#self._tokens.append(self._curtoken)
|
||||
return j
|
||||
|
||||
def _parse_literal(self, s, i):
|
||||
m = END_LITERAL.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
c = s[j:j+1]
|
||||
if c == b'#':
|
||||
self.hex = b''
|
||||
self._current_parse_func = self._parse_literal_hex
|
||||
return j+1
|
||||
try:
|
||||
self._curtoken=str(self._curtoken,'utf-8')
|
||||
except:
|
||||
pass
|
||||
self._add_token(LIT(self._curtoken))
|
||||
self._current_parse_func = self._parse_main
|
||||
return j
|
||||
|
||||
def _parse_literal_hex(self, s, i):
|
||||
c = s[i:i+1]
|
||||
if HEX.match(c) and len(self.hex) < 2:
|
||||
self.hex += c
|
||||
return i+1
|
||||
if self.hex:
|
||||
self._curtoken += int2byte(int(self.hex, 16))
|
||||
self._current_parse_func = self._parse_literal
|
||||
return i
|
||||
|
||||
def _parse_number(self, s, i):
|
||||
m = END_NUMBER.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
c = s[j:j+1]
|
||||
if c == b'.':
|
||||
self._curtoken += c
|
||||
self._current_parse_func = self._parse_float
|
||||
return j+1
|
||||
try:
|
||||
self._add_token(int(self._curtoken))
|
||||
except ValueError:
|
||||
pass
|
||||
self._current_parse_func = self._parse_main
|
||||
return j
|
||||
|
||||
def _parse_float(self, s, i):
|
||||
m = END_NUMBER.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
try:
|
||||
self._add_token(float(self._curtoken))
|
||||
except ValueError:
|
||||
pass
|
||||
self._current_parse_func = self._parse_main
|
||||
return j
|
||||
|
||||
def _parse_keyword(self, s, i):
|
||||
m = END_KEYWORD.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
if self._curtoken == b'true':
|
||||
token = True
|
||||
elif self._curtoken == b'false':
|
||||
token = False
|
||||
else:
|
||||
token = KWD(self._curtoken)
|
||||
self._add_token(token)
|
||||
self._current_parse_func = self._parse_main
|
||||
return j
|
||||
|
||||
def _parse_string(self, s, i):
|
||||
m = END_STRING.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
c = s[j:j+1]
|
||||
if c == b'\\':
|
||||
self.oct = b''
|
||||
self._current_parse_func = self._parse_string_1
|
||||
return j+1
|
||||
if c == b'(':
|
||||
self.paren += 1
|
||||
self._curtoken += c
|
||||
return j+1
|
||||
if c == b')':
|
||||
self.paren -= 1
|
||||
if self.paren: # WTF, they said balanced parens need no special treatment.
|
||||
self._curtoken += c
|
||||
return j+1
|
||||
self._add_token(self._curtoken)
|
||||
self._current_parse_func = self._parse_main
|
||||
return j+1
|
||||
|
||||
def _parse_string_1(self, s, i):
|
||||
c = s[i:i+1]
|
||||
if OCT_STRING.match(c) and len(self.oct) < 3:
|
||||
self.oct += c
|
||||
return i+1
|
||||
if self.oct:
|
||||
self._curtoken += int2byte(int(self.oct, 8))
|
||||
self._current_parse_func = self._parse_string
|
||||
return i
|
||||
if c in ESC_STRING:
|
||||
self._curtoken += int2byte(ESC_STRING[c])
|
||||
self._current_parse_func = self._parse_string
|
||||
return i+1
|
||||
|
||||
def _parse_wopen(self, s, i):
|
||||
c = s[i:i+1]
|
||||
if c == b'<':
|
||||
self._add_token(KEYWORD_DICT_BEGIN)
|
||||
self._current_parse_func = self._parse_main
|
||||
i += 1
|
||||
else:
|
||||
self._current_parse_func = self._parse_hexstring
|
||||
return i
|
||||
|
||||
def _parse_wclose(self, s, i):
|
||||
c = s[i:i+1]
|
||||
if c == b'>':
|
||||
self._add_token(KEYWORD_DICT_END)
|
||||
i += 1
|
||||
self._current_parse_func = self._parse_main
|
||||
return i
|
||||
|
||||
def _parse_hexstring(self, s, i):
|
||||
m = END_HEX_STRING.search(s, i)
|
||||
if not m:
|
||||
self._curtoken += s[i:]
|
||||
return len(s)
|
||||
j = m.start(0)
|
||||
self._curtoken += s[i:j]
|
||||
token = HEX_PAIR.sub(lambda m: int2byte(int(m.group(0), 16)),SPC.sub(b'', self._curtoken))
|
||||
self._add_token(token)
|
||||
self._current_parse_func = self._parse_main
|
||||
return j
|
||||
|
||||
def nexttoken(self):
|
||||
while not self._tokens:
|
||||
self.fillbuf()
|
||||
self.charpos = self._current_parse_func(self.buf, self.charpos)
|
||||
token = self._tokens.pop(0)
|
||||
log.debug('nexttoken: %r', token)
|
||||
return token
|
||||
@@ -1,168 +0,0 @@
|
||||
import logging
|
||||
from io import TextIOWrapper
|
||||
|
||||
from ...utils import choplist, settings
|
||||
|
||||
from .PSBaseParser import PSBaseParser
|
||||
from .types import *
|
||||
from .exceptions import *
|
||||
from .constants import *
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
def literal_name(x):
|
||||
if not isinstance(x, PSLiteral):
|
||||
if settings.STRICT:
|
||||
raise PSTypeError('Literal required: %r' % (x,))
|
||||
else:
|
||||
name=x
|
||||
else:
|
||||
name=x.name
|
||||
try:
|
||||
name = str(name,'utf-8')
|
||||
except:
|
||||
pass
|
||||
return name
|
||||
|
||||
def keyword_name(x):
|
||||
if not isinstance(x, PSKeyword):
|
||||
if settings.STRICT:
|
||||
raise PSTypeError('Keyword required: %r' % x)
|
||||
else:
|
||||
name=x
|
||||
else:
|
||||
name=x.name
|
||||
name = str(name,'utf-8','ignore')
|
||||
return name
|
||||
|
||||
|
||||
|
||||
class PSStackParser(PSBaseParser):
|
||||
|
||||
def __init__(self, fp: TextIOWrapper):
|
||||
PSBaseParser.__init__(self, fp)
|
||||
self.reset()
|
||||
return
|
||||
|
||||
def reset(self):
|
||||
self.context = []
|
||||
self.curtype = None
|
||||
self.curstack = []
|
||||
self.results = []
|
||||
return
|
||||
|
||||
def seek(self, pos):
|
||||
PSBaseParser.seek(self, pos)
|
||||
self.reset()
|
||||
return
|
||||
|
||||
def push(self, *objs):
|
||||
self.curstack.extend(objs)
|
||||
return
|
||||
|
||||
def pop(self, n):
|
||||
objs = self.curstack[-n:]
|
||||
self.curstack[-n:] = []
|
||||
return objs
|
||||
|
||||
def popall(self):
|
||||
objs = self.curstack
|
||||
self.curstack = []
|
||||
return objs
|
||||
|
||||
def add_results(self, *objs):
|
||||
try:
|
||||
log.debug('add_results: %r', objs)
|
||||
except:
|
||||
log.debug('add_results: (unprintable object)')
|
||||
self.results.extend(objs)
|
||||
return
|
||||
|
||||
def start_type(self, pos, type):
|
||||
self.context.append((pos, self.curtype, self.curstack))
|
||||
(self.curtype, self.curstack) = (type, [])
|
||||
log.debug('start_type: pos=%r, type=%r', pos, type)
|
||||
return
|
||||
|
||||
def end_type(self, type):
|
||||
if self.curtype != type:
|
||||
raise PSTypeError('Type mismatch: %r != %r' % (self.curtype, type))
|
||||
objs = [obj for (_, obj) in self.curstack]
|
||||
(pos, self.curtype, self.curstack) = self.context.pop()
|
||||
log.debug('end_type: pos=%r, type=%r, objs=%r', pos, type, objs)
|
||||
return (pos, objs)
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
return
|
||||
|
||||
def __iter__(self):
|
||||
while True:
|
||||
try:
|
||||
(_, obj) = self.nextobject()
|
||||
yield obj
|
||||
except PSEOF:
|
||||
break
|
||||
|
||||
def nextobject(self):
|
||||
"""Yields a list of objects.
|
||||
|
||||
Returns keywords, literals, strings, numbers, arrays and dictionaries.
|
||||
Arrays and dictionaries are represented as Python lists and dictionaries.
|
||||
"""
|
||||
while not self.results:
|
||||
(pos, token) = self.nexttoken()
|
||||
if isinstance(token, (int, float, bool, str, bytes, PSLiteral)):
|
||||
# normal token
|
||||
self.push((pos, token))
|
||||
elif token == KEYWORD_ARRAY_BEGIN:
|
||||
# begin array
|
||||
self.start_type(pos, 'a')
|
||||
elif token == KEYWORD_ARRAY_END:
|
||||
# end array
|
||||
try:
|
||||
self.push(self.end_type('a'))
|
||||
except PSTypeError:
|
||||
if settings.STRICT:
|
||||
raise
|
||||
elif token == KEYWORD_DICT_BEGIN:
|
||||
# begin dictionary
|
||||
self.start_type(pos, 'd')
|
||||
elif token == KEYWORD_DICT_END:
|
||||
# end dictionary
|
||||
try:
|
||||
(pos, objs) = self.end_type('d')
|
||||
if len(objs) % 2 != 0:
|
||||
raise PSSyntaxError('Invalid dictionary construct: %r' % objs)
|
||||
# construct a Python dictionary.
|
||||
d = dict((literal_name(k), v) for (k, v) in choplist(2, objs) if v is not None)
|
||||
self.push((pos, d))
|
||||
except PSTypeError:
|
||||
if settings.STRICT:
|
||||
raise
|
||||
elif token == KEYWORD_PROC_BEGIN:
|
||||
# begin proc
|
||||
self.start_type(pos, 'p')
|
||||
elif token == KEYWORD_PROC_END:
|
||||
# end proc
|
||||
try:
|
||||
self.push(self.end_type('p'))
|
||||
except PSTypeError:
|
||||
if settings.STRICT:
|
||||
raise
|
||||
elif isinstance(token,PSKeyword):
|
||||
log.debug('do_keyword: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
|
||||
self.do_keyword(pos, token)
|
||||
else:
|
||||
log.error('unknown token: pos=%r, token=%r, stack=%r', pos, token, self.curstack)
|
||||
self.do_keyword(pos, token)
|
||||
raise Exception("unknown tokens")
|
||||
if self.context:
|
||||
continue
|
||||
else:
|
||||
self.flush()
|
||||
obj = self.results.pop(0)
|
||||
try:
|
||||
log.debug('nextobject: %r', obj)
|
||||
except:
|
||||
log.debug('nextobject: (unprintable object)')
|
||||
return obj
|
||||
@@ -1,28 +0,0 @@
|
||||
import re
|
||||
from .types import PSLiteral, PSSymbolTable, PSKeyword
|
||||
|
||||
PSLiteralTable = PSSymbolTable(PSLiteral)
|
||||
PSKeywordTable = PSSymbolTable(PSKeyword)
|
||||
LIT = PSLiteralTable.intern
|
||||
KWD = PSKeywordTable.intern
|
||||
KEYWORD_PROC_BEGIN = KWD(b'{')
|
||||
KEYWORD_PROC_END = KWD(b'}')
|
||||
KEYWORD_ARRAY_BEGIN = KWD(b'[')
|
||||
KEYWORD_ARRAY_END = KWD(b']')
|
||||
KEYWORD_DICT_BEGIN = KWD(b'<<')
|
||||
KEYWORD_DICT_END = KWD(b'>>')
|
||||
|
||||
## PSBaseParser
|
||||
##
|
||||
EOL = re.compile(br'[\r\n]')
|
||||
SPC = re.compile(br'\s')
|
||||
NONSPC = re.compile(br'\S')
|
||||
HEX = re.compile(br'[0-9a-fA-F]')
|
||||
END_LITERAL = re.compile(br'[#/%\[\]()<>{}\s]')
|
||||
END_HEX_STRING = re.compile(br'[^\s0-9a-fA-F]')
|
||||
HEX_PAIR = re.compile(br'[0-9a-fA-F]{2}|.')
|
||||
END_NUMBER = re.compile(br'[^0-9]')
|
||||
END_KEYWORD = re.compile(br'[#/%\[\]()<>{}\s]')
|
||||
END_STRING = re.compile(br'[()\134]')
|
||||
OCT_STRING = re.compile(br'[0-7]')
|
||||
ESC_STRING = {b'b': 8, b't': 9, b'n': 10, b'f': 12, b'r': 13, b'(': 40, b')': 41, b'\\': 92}
|
||||
@@ -1,20 +0,0 @@
|
||||
## PS Exceptions
|
||||
##
|
||||
class PSException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class PSEOF(PSException):
|
||||
pass
|
||||
|
||||
|
||||
class PSSyntaxError(PSException):
|
||||
pass
|
||||
|
||||
|
||||
class PSTypeError(PSException):
|
||||
pass
|
||||
|
||||
|
||||
class PSValueError(PSException):
|
||||
pass
|
||||
@@ -1,80 +0,0 @@
|
||||
## Basic PostScript Types
|
||||
##
|
||||
from typing import Union
|
||||
|
||||
## PSObject
|
||||
##
|
||||
class PSObject(object):
|
||||
|
||||
"""Base class for all PS or PDF-related data types."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
## PSLiteral
|
||||
##
|
||||
class PSLiteral(PSObject):
|
||||
|
||||
"""A class that represents a PostScript literal.
|
||||
|
||||
Postscript literals are used as identifiers, such as
|
||||
variable names, property names and dictionary keys.
|
||||
Literals are case sensitive and denoted by a preceding
|
||||
slash sign (e.g. "/Name")
|
||||
|
||||
Note: Do not create an instance of PSLiteral directly.
|
||||
Always use PSLiteralTable.intern().
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
|
||||
def __repr__(self):
|
||||
name=self.name
|
||||
return '/%r' % name
|
||||
|
||||
|
||||
## PSKeyword
|
||||
##
|
||||
class PSKeyword(PSObject):
|
||||
|
||||
"""A class that represents a PostScript keyword.
|
||||
|
||||
PostScript keywords are a dozen of predefined words.
|
||||
Commands and directives in PostScript are expressed by keywords.
|
||||
They are also used to denote the content boundaries.
|
||||
|
||||
Note: Do not create an instance of PSKeyword directly.
|
||||
Always use PSKeywordTable.intern().
|
||||
"""
|
||||
|
||||
def __init__(self, name):
|
||||
self.name = name
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
name=self.name
|
||||
return '/%r' % name
|
||||
|
||||
|
||||
## PSSymbolTable
|
||||
##
|
||||
class PSSymbolTable(object):
|
||||
|
||||
"""A utility class for storing PSLiteral/PSKeyword objects.
|
||||
|
||||
Interned objects can be checked its identity with "is" operator.
|
||||
"""
|
||||
|
||||
def __init__(self, klass):
|
||||
self.dict = {}
|
||||
self.klass = klass
|
||||
return
|
||||
|
||||
def intern(self, name):
|
||||
if name in self.dict:
|
||||
lit = self.dict[name]
|
||||
else:
|
||||
lit = self.klass(name)
|
||||
self.dict[name] = lit
|
||||
return lit
|
||||
@@ -0,0 +1,78 @@
|
||||
from pdfmajor.parser.PDFPage import PDFPage
|
||||
from pdfmajor.parser.PDFStream import list_value
|
||||
from pdfmajor.parser.PDFContentParser import PDFContentParser
|
||||
from pdfmajor.parser.PSStackParser import PSKeyword
|
||||
from pdfmajor.parser.PSStackParser import keyword_name
|
||||
|
||||
from .commands import PDFStateStack, PDFCommands
|
||||
from .commands import LTXObject
|
||||
from .utils import init_resources
|
||||
from .XObjectInterpreter import XObjectInterpreter
|
||||
|
||||
class PageInterpreter:
|
||||
class PDFInterpreterError(Exception): pass
|
||||
|
||||
def __init__(self, page: PDFPage, page_num: int, font_cache: dict = {}):
|
||||
(x0, y0, x1, y1) = page.mediabox
|
||||
if page.rotate == 90:
|
||||
ctm = [0, -1, 1, 0, -y0, x1]
|
||||
elif page.rotate == 180:
|
||||
ctm = [-1, 0, 0, -1, x1, y1]
|
||||
elif page.rotate == 270:
|
||||
ctm = [0, 1, -1, 0, y1, -x0]
|
||||
else:
|
||||
ctm = [1, 0, 0, 1, -x0, -y0]
|
||||
|
||||
# Locals
|
||||
self.font_cache = font_cache
|
||||
self.page = page
|
||||
self.page_num = page_num
|
||||
|
||||
# Init State
|
||||
self.state: PDFStateStack = PDFStateStack()
|
||||
self.state.t_matrix = ctm
|
||||
self.state.resources = page.resources
|
||||
# set some global states.
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = None
|
||||
if self.state.colorspace_map:
|
||||
col_space = next(iter(self.state.colorspace_map.values()))
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = col_space
|
||||
# Init Resources
|
||||
init_resources(self.state, self.font_cache)
|
||||
|
||||
def __iter__(self):
|
||||
parser = PDFContentParser(list_value(self.page.contents))
|
||||
for obj in parser:
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if method in PDFCommands.commands.keys():
|
||||
func = PDFCommands.commands.get(method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.state.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(self.state, *args)
|
||||
else:
|
||||
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
|
||||
else:
|
||||
# log.debug('exec: %s', name)
|
||||
func(self.state)
|
||||
else:
|
||||
raise self.PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.state.argstack.append(obj)
|
||||
|
||||
for complete_item in self.state.complete_layout_items:
|
||||
if isinstance(complete_item, LTXObject):
|
||||
interpeter = XObjectInterpreter(
|
||||
streams=[complete_item.stream],
|
||||
resources=complete_item.resources,
|
||||
ctm=complete_item.t_matrix
|
||||
)
|
||||
for item in interpeter:
|
||||
complete_item.add(item)
|
||||
yield complete_item
|
||||
self.state.complete_layout_items = []
|
||||
def __repr__(self) -> str:
|
||||
return f"<Page:{self.page_num}/>"
|
||||
@@ -0,0 +1,70 @@
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFParser import PDFStream
|
||||
from pdfmajor.parser.PSStackParser import PSEOF
|
||||
from pdfmajor.parser.PSStackParser import PSKeyword
|
||||
from pdfmajor.parser.PSStackParser import keyword_name
|
||||
from pdfmajor.parser.PSStackParser import PSStackParser
|
||||
from pdfmajor.parser.constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
|
||||
|
||||
from pdfmajor.parser.PDFStream import resolve1, list_value, dict_value
|
||||
from pdfmajor.parser.PDFStream.PDFObjRef import PDFObjRef
|
||||
from pdfmajor.parser.PDFContentParser import PDFContentParser
|
||||
|
||||
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFCommands, LTXObject
|
||||
from .utils import init_resources
|
||||
|
||||
class XObjectInterpreter:
|
||||
|
||||
class PDFInterpreterError(Exception): pass
|
||||
|
||||
def __init__(self, streams, resources, ctm):
|
||||
self.streams = streams
|
||||
self.font_cache = {}
|
||||
|
||||
self.state = PDFStateStack()
|
||||
self.state.t_matrix = ctm
|
||||
self.state.resources = resources
|
||||
|
||||
# set some global states.
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = None
|
||||
if self.state.colorspace_map:
|
||||
col_space = next(iter(self.state.colorspace_map.values()))
|
||||
self.state.graphics.ncolor.color_space = self.state.graphics.scolor.color_space = col_space
|
||||
init_resources(self.state, self.font_cache)
|
||||
|
||||
def __iter__(self):
|
||||
parser = PDFContentParser(self.streams)
|
||||
for obj in parser:
|
||||
if isinstance(obj, PSKeyword):
|
||||
name = keyword_name(obj)
|
||||
method = name.replace('*', '_a').replace('"', '_w').replace("'", '_q')
|
||||
if method in PDFCommands.commands.keys():
|
||||
func = PDFCommands.commands.get(method)
|
||||
nargs = func.__code__.co_argcount-1
|
||||
if nargs:
|
||||
args = self.state.pop(nargs)
|
||||
if len(args) == nargs:
|
||||
func(self.state, *args)
|
||||
else:
|
||||
raise self.PDFInterpreterError(f"Invalid Number of Args provided {method}")
|
||||
else:
|
||||
# log.debug('exec: %s', name)
|
||||
func(self.state)
|
||||
else:
|
||||
raise self.PDFInterpreterError('Unknown operator: %r' % name)
|
||||
else:
|
||||
self.state.argstack.append(obj)
|
||||
|
||||
for complete_item in self.state.complete_layout_items:
|
||||
if isinstance(complete_item, LTXObject):
|
||||
interpeter = self.__class__(
|
||||
streams=[complete_item.stream],
|
||||
resources=complete_item.resources,
|
||||
ctm=complete_item.t_matrix
|
||||
)
|
||||
for item in interpeter:
|
||||
complete_item.add(item)
|
||||
yield complete_item
|
||||
self.state.complete_layout_items = []
|
||||
@@ -0,0 +1,72 @@
|
||||
from typing import List
|
||||
|
||||
from tqdm import tqdm
|
||||
|
||||
from ..parser.PDFPage import PDFPage
|
||||
from ..utils import set_log_level, get_logger, logging
|
||||
from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFCommands
|
||||
from .commands import LTCharBlock, LTChar
|
||||
from .commands import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .commands import LTImage
|
||||
from .commands import LTXObject
|
||||
from .PageInterpreter import PageInterpreter
|
||||
|
||||
log = get_logger(__name__)
|
||||
|
||||
class PDFInterpreter:
|
||||
class AccessError(Exception): pass
|
||||
|
||||
def __init__(self,
|
||||
input_file_path: str,
|
||||
maxpages: int = 0,
|
||||
caching: bool = True,
|
||||
check_extractable: bool = True,
|
||||
password: str = None,
|
||||
pagenos: List[int] = None,
|
||||
preload: bool = False,
|
||||
debug_level: int = logging.WARNING,
|
||||
):
|
||||
self.input_file_path = input_file_path
|
||||
self.maxpages = maxpages
|
||||
self.caching = caching
|
||||
self.check_extractable = check_extractable
|
||||
self.password = password
|
||||
self.pagenos = pagenos
|
||||
self.debug_level = debug_level
|
||||
self.__pages = []
|
||||
if preload:
|
||||
for _ in self.__load_pages():
|
||||
pass
|
||||
|
||||
def __load_pages(self):
|
||||
set_log_level(self.debug_level)
|
||||
log.info("Opening file...")
|
||||
with open(self.input_file_path, 'rb') as input_file:
|
||||
font_cache = {}
|
||||
log.info("Parsing file...")
|
||||
pages = PDFPage.get_pages(
|
||||
input_file,
|
||||
pagenos=self.pagenos,
|
||||
maxpages=self.maxpages,
|
||||
password=self.password,
|
||||
caching=self.caching,
|
||||
check_extractable=self.check_extractable
|
||||
)
|
||||
for page_num, page in tqdm(enumerate(pages), desc="Reading pages...", disable=self.debug_level > logging.INFO):
|
||||
self.__pages.append(PageInterpreter(page, page_num, font_cache))
|
||||
yield self.__pages[-1]
|
||||
log.info(f"Done Reading {len(self.__pages)} pages.")
|
||||
set_log_level(logging.WARNING)
|
||||
if len(self.__pages) == 0:
|
||||
raise self.AccessError("No pages found in pdf-file")
|
||||
|
||||
def __iter__(self):
|
||||
if len(self.__pages) > 0:
|
||||
set_log_level(self.debug_level)
|
||||
for page in tqdm(self.__pages, desc="Reading pages from store...", disable=self.debug_level > logging.INFO):
|
||||
yield page
|
||||
set_log_level(logging.WARNING)
|
||||
else:
|
||||
for page in self.__load_pages():
|
||||
yield page
|
||||
@@ -1,432 +0,0 @@
|
||||
|
||||
|
||||
""" Adobe character mapping (CMap) support.
|
||||
|
||||
CMaps provide the mapping between character codes and Unicode
|
||||
code-points to character ids (CIDs).
|
||||
|
||||
More information is available on the Adobe website:
|
||||
|
||||
http://opensource.adobe.com/wiki/display/cmap/CMap+Resources
|
||||
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import os.path
|
||||
import gzip
|
||||
import pickle as pickle
|
||||
import struct
|
||||
import logging
|
||||
|
||||
from ...utils import choplist, nunpack, name2unicode
|
||||
from ..PSStackParser import PSStackParser
|
||||
from ..PSStackParser import PSSyntaxError
|
||||
from ..PSStackParser import PSEOF
|
||||
from ..PSStackParser import PSLiteral
|
||||
from ..PSStackParser import KWD
|
||||
from ..PSStackParser import literal_name
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class CMapError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
## CMapBase
|
||||
##
|
||||
class CMapBase(object):
|
||||
|
||||
debug = 0
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.attrs = kwargs.copy()
|
||||
return
|
||||
|
||||
def is_vertical(self):
|
||||
return self.attrs.get('WMode', 0) != 0
|
||||
|
||||
def set_attr(self, k, v):
|
||||
self.attrs[k] = v
|
||||
return
|
||||
|
||||
def add_code2cid(self, code, cid):
|
||||
return
|
||||
|
||||
def add_cid2unichr(self, cid, code):
|
||||
return
|
||||
|
||||
def use_cmap(self, cmap):
|
||||
return
|
||||
|
||||
|
||||
## CMap
|
||||
##
|
||||
class CMap(CMapBase):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
CMapBase.__init__(self, **kwargs)
|
||||
self.code2cid = {}
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<CMap: %s>' % self.attrs.get('CMapName')
|
||||
|
||||
def use_cmap(self, cmap):
|
||||
assert isinstance(cmap, CMap), str(type(cmap))
|
||||
|
||||
def copy(dst, src):
|
||||
for (k, v) in src.iteritems():
|
||||
if isinstance(v, dict):
|
||||
d = {}
|
||||
dst[k] = d
|
||||
copy(d, v)
|
||||
else:
|
||||
dst[k] = v
|
||||
copy(self.code2cid, cmap.code2cid)
|
||||
return
|
||||
|
||||
def decode(self, code):
|
||||
log.debug('decode: %r, %r', self, code)
|
||||
d = self.code2cid
|
||||
for i in iter(code):
|
||||
if i in d:
|
||||
d = d[i]
|
||||
if isinstance(d, int):
|
||||
yield d
|
||||
d = self.code2cid
|
||||
else:
|
||||
d = self.code2cid
|
||||
return
|
||||
|
||||
def dump(self, out=sys.stdout, code2cid=None, code=None):
|
||||
if code2cid is None:
|
||||
code2cid = self.code2cid
|
||||
code = ()
|
||||
for (k, v) in sorted(code2cid.iteritems()):
|
||||
c = code+(k,)
|
||||
if isinstance(v, int):
|
||||
out.write('code %r = cid %d\n' % (c, v))
|
||||
else:
|
||||
self.dump(out=out, code2cid=v, code=c)
|
||||
return
|
||||
|
||||
|
||||
## IdentityCMap
|
||||
##
|
||||
class IdentityCMap(CMapBase):
|
||||
|
||||
def decode(self, code):
|
||||
n = len(code)//2
|
||||
if n:
|
||||
return struct.unpack('>%dH' % n, code)
|
||||
else:
|
||||
return ()
|
||||
|
||||
|
||||
## UnicodeMap
|
||||
##
|
||||
class UnicodeMap(CMapBase):
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
CMapBase.__init__(self, **kwargs)
|
||||
self.cid2unichr = {}
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<UnicodeMap: %s>' % self.attrs.get('CMapName')
|
||||
|
||||
def get_unichr(self, cid):
|
||||
log.debug('get_unichr: %r, %r', self, cid)
|
||||
return self.cid2unichr[cid]
|
||||
|
||||
def dump(self, out=sys.stdout):
|
||||
for (k, v) in sorted(self.cid2unichr.items()):
|
||||
out.write('cid %d = unicode %r\n' % (k, v))
|
||||
return
|
||||
|
||||
|
||||
## FileCMap
|
||||
##
|
||||
class FileCMap(CMap):
|
||||
|
||||
def add_code2cid(self, code, cid):
|
||||
assert isinstance(code, str) and isinstance(cid, int), str((type(code), type(cid)))
|
||||
d = self.code2cid
|
||||
for c in code[:-1]:
|
||||
c = ord(c)
|
||||
if c in d:
|
||||
d = d[c]
|
||||
else:
|
||||
t = {}
|
||||
d[c] = t
|
||||
d = t
|
||||
c = ord(code[-1])
|
||||
d[c] = cid
|
||||
return
|
||||
|
||||
|
||||
## FileUnicodeMap
|
||||
##
|
||||
class FileUnicodeMap(UnicodeMap):
|
||||
|
||||
def add_cid2unichr(self, cid, code):
|
||||
assert isinstance(cid, int), str(type(cid))
|
||||
if isinstance(code, PSLiteral):
|
||||
# Interpret as an Adobe glyph name.
|
||||
self.cid2unichr[cid] = name2unicode(code.name)
|
||||
elif isinstance(code, bytes):
|
||||
# Interpret as UTF-16BE.
|
||||
self.cid2unichr[cid] = code.decode('UTF-16BE', 'ignore')
|
||||
elif isinstance(code, int):
|
||||
self.cid2unichr[cid] = chr(code)
|
||||
else:
|
||||
raise TypeError(code)
|
||||
return
|
||||
|
||||
|
||||
## PyCMap
|
||||
##
|
||||
class PyCMap(CMap):
|
||||
|
||||
def __init__(self, name, module):
|
||||
CMap.__init__(self, CMapName=name)
|
||||
self.code2cid = module.CODE2CID
|
||||
if module.IS_VERTICAL:
|
||||
self.attrs['WMode'] = 1
|
||||
return
|
||||
|
||||
|
||||
## PyUnicodeMap
|
||||
##
|
||||
class PyUnicodeMap(UnicodeMap):
|
||||
|
||||
def __init__(self, name, module, vertical):
|
||||
UnicodeMap.__init__(self, CMapName=name)
|
||||
if vertical:
|
||||
self.cid2unichr = module.CID2UNICHR_V
|
||||
self.attrs['WMode'] = 1
|
||||
else:
|
||||
self.cid2unichr = module.CID2UNICHR_H
|
||||
return
|
||||
|
||||
|
||||
## CMapDB
|
||||
##
|
||||
class CMapDB(object):
|
||||
|
||||
_cmap_cache = {}
|
||||
_umap_cache = {}
|
||||
|
||||
class CMapNotFound(CMapError):
|
||||
pass
|
||||
|
||||
@classmethod
|
||||
def _load_data(klass, name):
|
||||
name = name.replace("\0", "")
|
||||
filename = '%s.pickle.gz' % name
|
||||
log.info('loading: %r', name)
|
||||
cmap_paths = (os.environ.get('CMAP_PATH', '/usr/share/pdfminer/'),
|
||||
os.path.join(os.path.dirname(__file__), 'cmap'),)
|
||||
for directory in cmap_paths:
|
||||
path = os.path.join(directory, filename)
|
||||
if os.path.exists(path):
|
||||
gzfile = gzip.open(path)
|
||||
try:
|
||||
return type(str(name), (), pickle.loads(gzfile.read()))
|
||||
finally:
|
||||
gzfile.close()
|
||||
else:
|
||||
raise CMapDB.CMapNotFound(name)
|
||||
|
||||
@classmethod
|
||||
def get_cmap(klass, name):
|
||||
if name == 'Identity-H':
|
||||
return IdentityCMap(WMode=0)
|
||||
elif name == 'Identity-V':
|
||||
return IdentityCMap(WMode=1)
|
||||
try:
|
||||
return klass._cmap_cache[name]
|
||||
except KeyError:
|
||||
pass
|
||||
data = klass._load_data(name)
|
||||
klass._cmap_cache[name] = cmap = PyCMap(name, data)
|
||||
return cmap
|
||||
|
||||
@classmethod
|
||||
def get_unicode_map(klass, name, vertical=False):
|
||||
try:
|
||||
return klass._umap_cache[name][vertical]
|
||||
except KeyError:
|
||||
pass
|
||||
data = klass._load_data('to-unicode-%s' % name)
|
||||
klass._umap_cache[name] = umaps = [PyUnicodeMap(name, data, v) for v in (False, True)]
|
||||
return umaps[vertical]
|
||||
|
||||
|
||||
## CMapParser
|
||||
##
|
||||
class CMapParser(PSStackParser):
|
||||
|
||||
def __init__(self, cmap, fp):
|
||||
PSStackParser.__init__(self, fp)
|
||||
self.cmap = cmap
|
||||
# some ToUnicode maps don't have "begincmap" keyword.
|
||||
self._in_cmap = True
|
||||
return
|
||||
|
||||
def run(self):
|
||||
try:
|
||||
self.nextobject()
|
||||
except PSEOF:
|
||||
pass
|
||||
return
|
||||
|
||||
KEYWORD_BEGINCMAP = KWD(b'begincmap')
|
||||
KEYWORD_ENDCMAP = KWD(b'endcmap')
|
||||
KEYWORD_USECMAP = KWD(b'usecmap')
|
||||
KEYWORD_DEF = KWD(b'def')
|
||||
KEYWORD_BEGINCODESPACERANGE = KWD(b'begincodespacerange')
|
||||
KEYWORD_ENDCODESPACERANGE = KWD(b'endcodespacerange')
|
||||
KEYWORD_BEGINCIDRANGE = KWD(b'begincidrange')
|
||||
KEYWORD_ENDCIDRANGE = KWD(b'endcidrange')
|
||||
KEYWORD_BEGINCIDCHAR = KWD(b'begincidchar')
|
||||
KEYWORD_ENDCIDCHAR = KWD(b'endcidchar')
|
||||
KEYWORD_BEGINBFRANGE = KWD(b'beginbfrange')
|
||||
KEYWORD_ENDBFRANGE = KWD(b'endbfrange')
|
||||
KEYWORD_BEGINBFCHAR = KWD(b'beginbfchar')
|
||||
KEYWORD_ENDBFCHAR = KWD(b'endbfchar')
|
||||
KEYWORD_BEGINNOTDEFRANGE = KWD(b'beginnotdefrange')
|
||||
KEYWORD_ENDNOTDEFRANGE = KWD(b'endnotdefrange')
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_BEGINCMAP:
|
||||
self._in_cmap = True
|
||||
self.popall()
|
||||
return
|
||||
elif token is self.KEYWORD_ENDCMAP:
|
||||
self._in_cmap = False
|
||||
return
|
||||
if not self._in_cmap:
|
||||
return
|
||||
#
|
||||
if token is self.KEYWORD_DEF:
|
||||
try:
|
||||
((_, k), (_, v)) = self.pop(2)
|
||||
self.cmap.set_attr(literal_name(k), v)
|
||||
except PSSyntaxError:
|
||||
pass
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_USECMAP:
|
||||
try:
|
||||
((_, cmapname),) = self.pop(1)
|
||||
self.cmap.use_cmap(CMapDB.get_cmap(literal_name(cmapname)))
|
||||
except PSSyntaxError:
|
||||
pass
|
||||
except CMapDB.CMapNotFound:
|
||||
pass
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINCODESPACERANGE:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDCODESPACERANGE:
|
||||
self.popall()
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINCIDRANGE:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDCIDRANGE:
|
||||
objs = [obj for (__, obj) in self.popall()]
|
||||
for (s, e, cid) in choplist(3, objs):
|
||||
if (not isinstance(s, str) or not isinstance(e, str) or
|
||||
not isinstance(cid, int) or len(s) != len(e)):
|
||||
continue
|
||||
sprefix = s[:-4]
|
||||
eprefix = e[:-4]
|
||||
if sprefix != eprefix:
|
||||
continue
|
||||
svar = s[-4:]
|
||||
evar = e[-4:]
|
||||
s1 = nunpack(svar)
|
||||
e1 = nunpack(evar)
|
||||
vlen = len(svar)
|
||||
#assert s1 <= e1, str((s1, e1))
|
||||
for i in range(e1-s1+1):
|
||||
x = sprefix+struct.pack('>L', s1+i)[-vlen:]
|
||||
self.cmap.add_code2cid(x, cid+i)
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINCIDCHAR:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDCIDCHAR:
|
||||
objs = [obj for (__, obj) in self.popall()]
|
||||
for (cid, code) in choplist(2, objs):
|
||||
if isinstance(code, str) and isinstance(cid, str):
|
||||
self.cmap.add_code2cid(code, nunpack(cid))
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINBFRANGE:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDBFRANGE:
|
||||
objs = [obj for (__, obj) in self.popall()]
|
||||
for (s, e, code) in choplist(3, objs):
|
||||
if (not isinstance(s, bytes) or not isinstance(e, bytes) or
|
||||
len(s) != len(e)):
|
||||
continue
|
||||
s1 = nunpack(s)
|
||||
e1 = nunpack(e)
|
||||
#assert s1 <= e1, str((s1, e1))
|
||||
if isinstance(code, list):
|
||||
for i in range(e1-s1+1):
|
||||
self.cmap.add_cid2unichr(s1+i, code[i])
|
||||
else:
|
||||
var = code[-4:]
|
||||
base = nunpack(var)
|
||||
prefix = code[:-4]
|
||||
vlen = len(var)
|
||||
for i in range(e1-s1+1):
|
||||
x = prefix+struct.pack('>L', base+i)[-vlen:]
|
||||
self.cmap.add_cid2unichr(s1+i, x)
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINBFCHAR:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDBFCHAR:
|
||||
objs = [obj for (__, obj) in self.popall()]
|
||||
for (cid, code) in choplist(2, objs):
|
||||
if isinstance(cid, bytes) and isinstance(code, bytes):
|
||||
self.cmap.add_cid2unichr(nunpack(cid), code)
|
||||
return
|
||||
|
||||
if token is self.KEYWORD_BEGINNOTDEFRANGE:
|
||||
self.popall()
|
||||
return
|
||||
if token is self.KEYWORD_ENDNOTDEFRANGE:
|
||||
self.popall()
|
||||
return
|
||||
|
||||
self.push((pos, token))
|
||||
return
|
||||
|
||||
|
||||
# test
|
||||
def main(argv):
|
||||
args = argv[1:]
|
||||
for fname in args:
|
||||
fp = open(fname, 'rb')
|
||||
cmap = FileUnicodeMap()
|
||||
#cmap = FileCMap()
|
||||
CMapParser(cmap, fp).run()
|
||||
fp.close()
|
||||
cmap.dump()
|
||||
return
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main(sys.argv))
|
||||
@@ -0,0 +1,30 @@
|
||||
from typing import List
|
||||
|
||||
from pdfmajor.utils import MATRIX_IDENTITY, mult_matrix
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
|
||||
from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
from .state import PDFStateStack, PDFColorSpace
|
||||
from .state.Curves import CurveMethod, CurvePath, CurvePoint
|
||||
|
||||
class PDFCommands:
|
||||
commands = {}
|
||||
class InvalidOperation(Exception): pass
|
||||
class RepeatedCommand(Exception): pass
|
||||
|
||||
@classmethod
|
||||
def __add(cls, cmd_name: str, cmd: callable):
|
||||
if not (cmd_name in cls.commands.keys()):
|
||||
cls.commands[cmd_name] = cmd
|
||||
return cmd
|
||||
raise cls.RepeatedCommand(cmd_name)
|
||||
|
||||
@classmethod
|
||||
def add(cls, *cmd_names: List[str]):
|
||||
def wrap(cmd: callable):
|
||||
for cmd_name in cmd_names:
|
||||
cls.__add(cmd_name, cmd)
|
||||
return cmd
|
||||
return wrap
|
||||
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
from .commands import PDFCommands
|
||||
from .state import PDFStateStack, PDFGraphicState, PDFTextState
|
||||
from .state.PDFGraphicState import PDFColor, PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .state import PDFFont, get_font
|
||||
from .state import LTCharBlock, LTChar
|
||||
from .state import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .state import LTImage
|
||||
from .state import LTXObject
|
||||
from .state import LTItem, LTComponent, LTContainer
|
||||
@@ -0,0 +1,485 @@
|
||||
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFStream import PDFStream, list_value, dict_value
|
||||
from pdfmajor.parser.constants import LITERAL_FORM, LITERAL_IMAGE
|
||||
|
||||
from .PDFCommands import PDFCommands
|
||||
from .state import PDFStateStack, PDFGraphicState
|
||||
from .state.Curves import CurveMethod, CurvePath, CurvePoint
|
||||
# from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
|
||||
from .state import make_char_block, make_curve, make_image, make_xobject
|
||||
|
||||
# No Support
|
||||
@PDFCommands.add('W','W_a', 'sh', 'ET', 'BX', 'EX' ,'BI', 'ID', 'gs')
|
||||
def do_no_support(stack: PDFStateStack) -> PDFStateStack:
|
||||
return stack
|
||||
|
||||
# marked content operators
|
||||
@PDFCommands.add('MP', 'DP')
|
||||
def do_MP(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.do_tag(tag)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('BMC', 'BDC')
|
||||
def do_BMC(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.begin_tag(tag)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('EMC')
|
||||
def do_EMC(stack: PDFStateStack) -> PDFStateStack:
|
||||
# self.device.end_tag(tag)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('q')
|
||||
def do_q(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.graphicstack.append(stack.graphics)
|
||||
stack.graphics = PDFGraphicState()
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('Q')
|
||||
def do_Q(stack: PDFStateStack) -> PDFStateStack:
|
||||
if len(stack.graphicstack) > 0:
|
||||
stack.graphicstate = stack.graphicstack.pop()
|
||||
return stack
|
||||
|
||||
# concat-matrix
|
||||
@PDFCommands.add('cm')
|
||||
def do_cm(stack: PDFStateStack, a1, b1, c1, d1, e1, f1) -> PDFStateStack:
|
||||
stack.t_matrix = mult_matrix((a1, b1, c1, d1, e1, f1), stack.t_matrix)
|
||||
return stack
|
||||
|
||||
# setlinewidth
|
||||
@PDFCommands.add('w')
|
||||
def do_w(stack: PDFStateStack, linewidth) -> PDFStateStack:
|
||||
stack.graphics.linewidth = linewidth
|
||||
return stack
|
||||
|
||||
# setlinecap
|
||||
@PDFCommands.add('J')
|
||||
def do_J(stack: PDFStateStack, linecap) -> PDFStateStack:
|
||||
stack.graphics.linecap = linecap
|
||||
return stack
|
||||
|
||||
# setlinejoin
|
||||
@PDFCommands.add('j')
|
||||
def do_j(stack: PDFStateStack, linejoin) -> PDFStateStack:
|
||||
stack.graphics.linejoin = linejoin
|
||||
return stack
|
||||
|
||||
# setmiterlimit
|
||||
@PDFCommands.add('M')
|
||||
def do_M(stack: PDFStateStack, miterlimit) -> PDFStateStack:
|
||||
stack.graphics.miterlimit = miterlimit
|
||||
return stack
|
||||
|
||||
# setdash
|
||||
@PDFCommands.add('d')
|
||||
def do_d(stack: PDFStateStack, dash, phase) -> PDFStateStack:
|
||||
stack.graphics.dash = (dash, phase)
|
||||
return stack
|
||||
|
||||
# setintent
|
||||
@PDFCommands.add('ri')
|
||||
def do_ri(stack: PDFStateStack, intent) -> PDFStateStack:
|
||||
stack.graphics.intent = intent
|
||||
return stack
|
||||
|
||||
# setflatness
|
||||
@PDFCommands.add('i')
|
||||
def do_i(stack: PDFStateStack, flatness) -> PDFStateStack:
|
||||
stack.graphics.flatness = flatness
|
||||
return stack
|
||||
|
||||
# moveto
|
||||
@PDFCommands.add('m')
|
||||
def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.MOVE_TO,
|
||||
CurvePoint(x, y)
|
||||
))
|
||||
return stack
|
||||
|
||||
# lineto
|
||||
@PDFCommands.add('l')
|
||||
def do_l(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.LINE_TO,
|
||||
CurvePoint(x, y)
|
||||
))
|
||||
return stack
|
||||
|
||||
# curveto
|
||||
@PDFCommands.add('c')
|
||||
def do_c(stack: PDFStateStack,
|
||||
x1: float, y1: float,
|
||||
x2: float, y2: float,
|
||||
x3: float, y3: float ) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_BOTH_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
))
|
||||
return stack
|
||||
|
||||
# urveto
|
||||
@PDFCommands.add('v')
|
||||
def do_v(stack: PDFStateStack,
|
||||
x2: float, y2: float,
|
||||
x3: float, y3: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_NEXT_TO,
|
||||
CurvePoint(x2, y2),
|
||||
CurvePoint(x3, y3),
|
||||
))
|
||||
return stack
|
||||
|
||||
# rveto
|
||||
@PDFCommands.add('y')
|
||||
def do_y(stack: PDFStateStack,
|
||||
x1: float, y1: float,
|
||||
x3: float, y3: float) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CURVE_FIRST_TO,
|
||||
CurvePoint(x1, y1),
|
||||
CurvePoint(x3, y3)
|
||||
))
|
||||
return stack
|
||||
|
||||
# closepath
|
||||
@PDFCommands.add('h')
|
||||
def do_h(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
|
||||
return stack
|
||||
|
||||
# rectangle
|
||||
@PDFCommands.add('re')
|
||||
def do_re(stack: PDFStateStack, x, y, w, h) -> PDFStateStack:
|
||||
stack.curvestacks.extend([
|
||||
CurvePath(CurvePath.METHOD.MOVE_TO, CurvePoint(x, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x+w, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(x, y+h) ),
|
||||
CurvePath(CurvePath.METHOD.CLOSE_PATH)
|
||||
])
|
||||
return stack
|
||||
|
||||
# stroke, fill
|
||||
@PDFCommands.add('S', 'f', 'F', 'B')
|
||||
def do_complete_path(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
False,
|
||||
stack.curvestacks
|
||||
))
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
# sroke, fill-even-odd
|
||||
@PDFCommands.add('f_a', 'B_a')
|
||||
def do_complete_path_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
True,
|
||||
stack.curvestacks
|
||||
))
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
# close-and-stroke (do_h, do_S)
|
||||
@PDFCommands.add('s', 'b')
|
||||
def do_close_complete(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
False,
|
||||
stack.curvestacks
|
||||
))
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
# close-fill-and-stroke-even-odd
|
||||
@PDFCommands.add('b_a')
|
||||
def do_close_complete_evenodd(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks.append(CurvePath(
|
||||
CurvePath.METHOD.CLOSE_PATH
|
||||
))
|
||||
stack.complete_layout_items.append(make_curve(
|
||||
stack.t_matrix,
|
||||
stack.graphics.copy(),
|
||||
True,
|
||||
stack.curvestacks
|
||||
))
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
# close-only (empty path)
|
||||
@PDFCommands.add('n')
|
||||
def do_n(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.curvestacks = []
|
||||
return stack
|
||||
|
||||
|
||||
# setcolorspace-stroking
|
||||
@PDFCommands.add('CS')
|
||||
def do_CS(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
try:
|
||||
stack.graphics.set_stroke_color(stack.colorspace_map[literal_name(name)])
|
||||
return stack
|
||||
# stack.set_scolor()
|
||||
except KeyError:
|
||||
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name)
|
||||
|
||||
# setcolorspace-non-strokine
|
||||
@PDFCommands.add('cs')
|
||||
def do_cs(stack: PDFStateStack, name: str) -> PDFStateStack:
|
||||
try:
|
||||
stack.graphics.set_nostroke_color(stack.colorspace_map[literal_name(name)])
|
||||
return stack
|
||||
# stack.set_scolor()
|
||||
except KeyError:
|
||||
raise PDFCommands.InvalidOperation('Undefined ColorSpace: %r' % name)
|
||||
|
||||
# setgray-stroking
|
||||
@PDFCommands.add('G')
|
||||
def do_G(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceGray'],
|
||||
gray
|
||||
)
|
||||
return stack
|
||||
|
||||
# setgray-non-stroking
|
||||
@PDFCommands.add('g')
|
||||
def do_g(stack: PDFStateStack, gray: float) -> PDFStateStack:
|
||||
stack.graphics.set_nostroke_color(
|
||||
stack.colorspace_map['DeviceGray'],
|
||||
gray
|
||||
)
|
||||
return stack
|
||||
|
||||
|
||||
# setrgb-stroking
|
||||
@PDFCommands.add('RG')
|
||||
def do_RG(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceRGB'],
|
||||
round(r*255),
|
||||
round(g*255),
|
||||
round(b*255)
|
||||
)
|
||||
return stack
|
||||
|
||||
# setrgb-non-stroking
|
||||
@PDFCommands.add('rg')
|
||||
def do_rg(stack: PDFStateStack, r, g, b) -> PDFStateStack:
|
||||
stack.graphics.set_nostroke_color(
|
||||
stack.colorspace_map['DeviceRGB'],
|
||||
round(r*255),
|
||||
round(g*255),
|
||||
round(b*255)
|
||||
)
|
||||
return stack
|
||||
|
||||
# setcmyk-stroking
|
||||
@PDFCommands.add('K')
|
||||
def do_K(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceCMYK'],
|
||||
c, m, y, k
|
||||
)
|
||||
return stack
|
||||
|
||||
# setcmyk-non-stroking
|
||||
@PDFCommands.add('k')
|
||||
def do_k(stack: PDFStateStack, c, m, y, k) -> PDFStateStack:
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.colorspace_map['DeviceCMYK'],
|
||||
c, m, y, k
|
||||
)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('SCN','SC')
|
||||
def do_SCN(stack: PDFStateStack) -> PDFStateStack:
|
||||
if stack.graphics.scolor.color_space is None:
|
||||
raise PDFCommands.InvalidOperation('No colorspace specified!')
|
||||
components = stack.pop(stack.graphics.scolor.color_space.ncomponents)
|
||||
stack.graphics.set_stroke_color(
|
||||
stack.graphics.scolor.color_space,
|
||||
*components
|
||||
)
|
||||
return stack
|
||||
|
||||
@PDFCommands.add('scn','sc')
|
||||
def do_scn(stack: PDFStateStack) -> PDFStateStack:
|
||||
if stack.graphics.ncolor.color_space is None:
|
||||
raise PDFCommands.InvalidOperation('No colorspace specified!')
|
||||
components = stack.pop(stack.graphics.ncolor.color_space.ncomponents)
|
||||
stack.graphics.set_nostroke_color(
|
||||
stack.graphics.ncolor.color_space,
|
||||
*components
|
||||
)
|
||||
return stack
|
||||
|
||||
# begin-text
|
||||
@PDFCommands.add('BT')
|
||||
def do_BT(stack: PDFStateStack) -> PDFStateStack:
|
||||
stack.text.matrix = MATRIX_IDENTITY
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# setcharspace
|
||||
@PDFCommands.add('Tc')
|
||||
def do_Tc(stack: PDFStateStack, charspace) -> PDFStateStack:
|
||||
stack.text.charspace = charspace
|
||||
return stack
|
||||
|
||||
# setwordspace
|
||||
@PDFCommands.add('Tw')
|
||||
def do_Tw(stack: PDFStateStack, wordspace) -> PDFStateStack:
|
||||
stack.text.wordspace = wordspace
|
||||
return stack
|
||||
|
||||
# textscale
|
||||
@PDFCommands.add('Tz')
|
||||
def do_Tz(stack: PDFStateStack, scaling) -> PDFStateStack:
|
||||
stack.text.scaling = scaling
|
||||
return stack
|
||||
|
||||
# setleading
|
||||
@PDFCommands.add('TL')
|
||||
def do_TL(stack: PDFStateStack, leading) -> PDFStateStack:
|
||||
stack.text.leading = -leading
|
||||
return stack
|
||||
|
||||
# selectfont
|
||||
@PDFCommands.add('Tf')
|
||||
def do_Tf(stack: PDFStateStack, fontid, fontsize) -> PDFStateStack:
|
||||
stack.text.fontsize = fontsize
|
||||
try:
|
||||
stack.text.font = stack.fontmap[literal_name(fontid)]
|
||||
return stack
|
||||
except KeyError:
|
||||
raise PDFCommands.InvalidOperation('Undefined Font id: %r' % fontid)
|
||||
|
||||
# setrendering
|
||||
@PDFCommands.add('Tr')
|
||||
def do_Tr(stack: PDFStateStack, render) -> PDFStateStack:
|
||||
stack.text.render = render
|
||||
return stack
|
||||
|
||||
# settextrise
|
||||
@PDFCommands.add('Ts')
|
||||
def do_Ts(stack: PDFStateStack, rise) -> PDFStateStack:
|
||||
stack.text.rise = rise
|
||||
return stack
|
||||
|
||||
# text-move
|
||||
@PDFCommands.add('Td')
|
||||
def do_Td(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# text-move
|
||||
@PDFCommands.add('TD')
|
||||
def do_TD(stack: PDFStateStack, tx, ty) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, tx*a+ty*c+e, tx*b+ty*d+f)
|
||||
stack.text.leading = ty
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# textmatrix
|
||||
@PDFCommands.add('Tm')
|
||||
def do_Tm(stack: PDFStateStack, a, b, c, d, e, f) -> PDFStateStack:
|
||||
stack.text.matrix = (a, b, c, d, e, f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# nextline
|
||||
@PDFCommands.add('T_a')
|
||||
def do_T_a(stack: PDFStateStack) -> PDFStateStack:
|
||||
(a, b, c, d, e, f) = stack.text.matrix
|
||||
stack.text.matrix = (a, b, c, d, stack.text.leading*c+e, stack.text.leading*d+f)
|
||||
stack.text.linematrix = [0, 0]
|
||||
return stack
|
||||
|
||||
# show-pos
|
||||
@PDFCommands.add('TJ')
|
||||
def do_TJ(stack: PDFStateStack, seq: bytearray) -> PDFStateStack:
|
||||
if stack.text.font is None:
|
||||
raise PDFCommands.InvalidOperation("No Font Specified")
|
||||
stack.complete_layout_items.append(make_char_block(
|
||||
seq,
|
||||
stack.t_matrix,
|
||||
stack.text.copy(),
|
||||
stack.graphics.ncolor.copy()
|
||||
))
|
||||
return stack
|
||||
|
||||
# show
|
||||
@PDFCommands.add('Tj')
|
||||
def do_Tj(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return do_TJ(stack, [b])
|
||||
|
||||
# quote
|
||||
@PDFCommands.add('_q')
|
||||
def do__q(stack: PDFStateStack, b: bytes) -> PDFStateStack:
|
||||
return do_TJ(do_T_a(stack), [b])
|
||||
|
||||
# doublequote
|
||||
@PDFCommands.add('_w')
|
||||
def do__w(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
|
||||
stack.text.wordspace = wordspace
|
||||
stack.text.charspace = charspace
|
||||
return do_TJ(stack, [b])
|
||||
|
||||
@PDFCommands.add('EI')
|
||||
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
|
||||
if 'W' in obj and 'H' in obj:
|
||||
stack.complete_layout_items.append(make_image(
|
||||
obj, stack.ctm
|
||||
))
|
||||
return stack
|
||||
|
||||
# invoke an XObject
|
||||
@PDFCommands.add('Do')
|
||||
def do_Do(stack: PDFStateStack, xobjid) -> PDFStateStack:
|
||||
xobjid = literal_name(xobjid)
|
||||
try:
|
||||
xobj = PDFStream.validated_stream(stack.xobjmap[xobjid])
|
||||
except KeyError:
|
||||
raise PDFCommands.InvalidOperation('Undefined xobject id: %r' % xobjid)
|
||||
# log.info('Processing xobj: %r', xobj)
|
||||
subtype = xobj.get('Subtype')
|
||||
if subtype is LITERAL_FORM and 'BBox' in xobj:
|
||||
bbox = list_value(xobj['BBox'])
|
||||
matrix = list_value(xobj.get('Matrix', MATRIX_IDENTITY))
|
||||
# According to PDF reference 1.7 section 4.9.1, XObjects in
|
||||
# earlier PDFs (prior to v1.2) use the page's Resources entry
|
||||
# instead of having their own Resources entry.
|
||||
xobjres = xobj.get('Resources')
|
||||
resources = dict_value(xobjres) if xobjres else stack.resources.copy()
|
||||
stack.complete_layout_items.append(make_xobject(
|
||||
obj=xobj,
|
||||
bbox=bbox,
|
||||
ctm=stack.t_matrix,
|
||||
matrix=matrix,
|
||||
resources=resources
|
||||
))
|
||||
elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
|
||||
stack.complete_layout_items.append(make_image(
|
||||
obj, stack.ctm
|
||||
))
|
||||
else:
|
||||
# unsupported xobject type.
|
||||
pass
|
||||
return stack
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,16 @@
|
||||
from typing import List
|
||||
from .PDFColorSpace import PDFColorSpace
|
||||
|
||||
class PDFColor:
|
||||
def __init__(self, color_space: PDFColorSpace, *values: List[float]):
|
||||
self.color_space: PDFColorSpace = color_space
|
||||
self.values: list = values
|
||||
|
||||
def copy(self):
|
||||
return self.__class__(
|
||||
self.color_space,
|
||||
*self.values
|
||||
)
|
||||
|
||||
def __repr__(self):
|
||||
return f"<PDFColor space={self.color_space} values={self.values}/>"
|
||||
+2
-10
@@ -1,12 +1,4 @@
|
||||
import collections
|
||||
from .PSStackParser import LIT
|
||||
|
||||
## PDFColorSpace
|
||||
##
|
||||
LITERAL_DEVICE_GRAY = LIT('DeviceGray')
|
||||
LITERAL_DEVICE_RGB = LIT('DeviceRGB')
|
||||
LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')
|
||||
|
||||
from collections import OrderedDict
|
||||
|
||||
class PDFColorSpace(object):
|
||||
|
||||
@@ -19,7 +11,7 @@ class PDFColorSpace(object):
|
||||
return '<PDFColorSpace: %s, ncomponents=%d>' % (self.name, self.ncomponents)
|
||||
|
||||
|
||||
PREDEFINED_COLORSPACE = collections.OrderedDict()
|
||||
PREDEFINED_COLORSPACE = OrderedDict()
|
||||
|
||||
for (name, n) in [
|
||||
('DeviceGray', 1), # default value first
|
||||
@@ -0,0 +1,49 @@
|
||||
from .PDFColor import PDFColor, PDFColorSpace
|
||||
from .PDFColorSpace import PREDEFINED_COLORSPACE
|
||||
|
||||
class PDFGraphicState(object):
|
||||
class InvalidOperation(Exception): pass
|
||||
|
||||
def __init__(self):
|
||||
self.linewidth = 0
|
||||
self.linecap = None
|
||||
self.linejoin = None
|
||||
self.miterlimit = None
|
||||
self.dash = None
|
||||
self.intent = None
|
||||
self.flatness = None
|
||||
self.scolor:PDFColor = PDFColor(None)
|
||||
self.ncolor:PDFColor = PDFColor(None)
|
||||
|
||||
def set_stroke_color(self, colspace: PDFColorSpace, *values):
|
||||
if self.scolor is None:
|
||||
self.scolor = PDFColor(colspace, *values)
|
||||
|
||||
def set_nostroke_color(self, colspace: PDFColorSpace, *values):
|
||||
if self.ncolor is None:
|
||||
self.ncolor = PDFColor(colspace, *values)
|
||||
|
||||
def copy(self):
|
||||
obj = PDFGraphicState()
|
||||
obj.linewidth = self.linewidth
|
||||
obj.linecap = self.linecap
|
||||
obj.linejoin = self.linejoin
|
||||
obj.miterlimit = self.miterlimit
|
||||
obj.dash = self.dash
|
||||
obj.intent = self.intent
|
||||
obj.flatness = self.flatness
|
||||
obj.scolor = self.scolor.copy() if self.scolor is not None else None
|
||||
obj.ncolor = self.ncolor.copy() if self.ncolor is not None else None
|
||||
return obj
|
||||
|
||||
def __repr__(self):
|
||||
return ('<PDFGraphicState linewidth=%r, linecap=%r, linejoin=%r, '
|
||||
' miterlimit=%r, dash=%r, intent=%r, flatness=%r, '
|
||||
' stroking color=%r, non stroking color=%r'
|
||||
'>' %
|
||||
(
|
||||
self.linewidth, self.linecap, self.linejoin,
|
||||
self.miterlimit, self.dash, self.intent, self.flatness,
|
||||
str(self.scolor), str(self.ncolor)
|
||||
)
|
||||
)
|
||||
@@ -0,0 +1,6 @@
|
||||
from pdfmajor.parser.PDFStream import PDFStream
|
||||
from ._base import PDFItem
|
||||
|
||||
class PDFImage(PDFItem):
|
||||
def __init__(self, stream: PDFStream):
|
||||
self.stream = stream
|
||||
@@ -0,0 +1,12 @@
|
||||
from typing import List
|
||||
|
||||
from ..PDFGraphicState import PDFGraphicState, PDFColorSpace
|
||||
from ..Curves import CurvePath
|
||||
|
||||
from ._base import PDFItem
|
||||
|
||||
class PDFShape(PDFItem):
|
||||
def __init__(self, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
self.gstate = gstate
|
||||
self.evenodd = evenodd
|
||||
self.paths = paths
|
||||
+32
-79
@@ -1,96 +1,51 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from typing import List
|
||||
|
||||
from .PDFFont import PDFUnicodeNotDefined
|
||||
from pdfmajor.utils import mult_matrix, isnumber, apply_matrix_pt, translate_matrix
|
||||
from pdfmajor.utils import Bbox
|
||||
|
||||
from ..utils import isnumber, apply_matrix_pt
|
||||
from ..utils import Bbox, mult_matrix, translate_matrix
|
||||
from ..PDFTextState import PDFTextState
|
||||
from ..PDFGraphicState import PDFGraphicState, PDFColorSpace
|
||||
from ._base import PDFItem
|
||||
|
||||
from .PDFResourceManager import PDFResourceManager
|
||||
from .PDFGraphicState import PDFGraphicState
|
||||
from .PDFTextState import PDFTextState
|
||||
from .PDFColorSpace import PDFColorSpace
|
||||
from .PDFFont import PDFFont
|
||||
|
||||
## PDFDevice
|
||||
##
|
||||
class PDFDevice(object):
|
||||
|
||||
def __init__(self, rsrcmgr: PDFResourceManager):
|
||||
self.rsrcmgr = rsrcmgr
|
||||
self.ctm = None
|
||||
return
|
||||
|
||||
class Char:
|
||||
def __init__(self, char: str, bbox: Bbox):
|
||||
self.char = char
|
||||
self.bbox = bbox
|
||||
def __str__(self):
|
||||
return '"%s" %s' % (self.char, self.bbox)
|
||||
def __repr__(self):
|
||||
return '<PDFDevice>'
|
||||
return f"<Char:{str(self)}/>"
|
||||
|
||||
def __enter__(self):
|
||||
return self
|
||||
class PDFText(PDFItem):
|
||||
def __init__(self, chars: List[Char], textstate: PDFTextState, graphicstate: PDFGraphicState):
|
||||
self.chars = chars
|
||||
self.tstate = textstate
|
||||
self.gstate = graphicstate
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.close()
|
||||
|
||||
def close(self):
|
||||
return
|
||||
|
||||
def set_ctm(self, ctm):
|
||||
self.ctm = ctm
|
||||
return
|
||||
|
||||
def begin_tag(self, tag, props=None):
|
||||
return
|
||||
|
||||
def end_tag(self):
|
||||
return
|
||||
|
||||
def do_tag(self, tag, props=None):
|
||||
return
|
||||
|
||||
def begin_page(self, page, ctm):
|
||||
return
|
||||
|
||||
def end_page(self, page):
|
||||
return
|
||||
|
||||
def begin_figure(self, name, bbox: Bbox, matrix):
|
||||
return
|
||||
|
||||
def end_figure(self, name):
|
||||
return
|
||||
|
||||
def paint_path(self, graphicstate: PDFGraphicState, stroke, fill, evenodd, path):
|
||||
return
|
||||
|
||||
def render_image(self, name, stream):
|
||||
return
|
||||
|
||||
def render_string(self, textstate, seq, ncs, graphicstate: PDFGraphicState):
|
||||
return
|
||||
|
||||
|
||||
## PDFTextDevice
|
||||
##
|
||||
class PDFTextDevice(PDFDevice):
|
||||
|
||||
def render_string(self, textstate: PDFTextState, seq: bytearray, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
|
||||
textstate.matrix = mult_matrix(textstate.matrix, self.ctm)
|
||||
@classmethod
|
||||
def make(cls, seq: bytearray, ctm, textstate: PDFTextState, graphicstate: PDFGraphicState):
|
||||
textstate.matrix = mult_matrix(textstate.matrix, ctm)
|
||||
textstate.scaling = textstate.scaling * .01
|
||||
textstate.charspace = textstate.charspace * textstate.scaling
|
||||
textstate.wordspace = textstate.wordspace * textstate.scaling
|
||||
ncs = graphicstate.ncolor.color_space
|
||||
|
||||
if textstate.font.is_multibyte():
|
||||
textstate.wordspace = 0
|
||||
dxscale = .001 * textstate.fontsize * textstate.scaling
|
||||
|
||||
if textstate.font.is_vertical():
|
||||
self.__render_string_along(1,
|
||||
return cls.__render_string_along(1,
|
||||
seq, textstate, dxscale, ncs, graphicstate
|
||||
)
|
||||
else:
|
||||
self.__render_string_along(0,
|
||||
return cls.__render_string_along(0,
|
||||
seq, textstate, dxscale, ncs, graphicstate
|
||||
)
|
||||
|
||||
def __render_string_along(self, idx: int, seq: bytearray,
|
||||
@classmethod
|
||||
def __render_string_along(cls, idx: int, seq: bytearray,
|
||||
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
|
||||
needcharspace = False
|
||||
char_meta_datas = []
|
||||
@@ -107,19 +62,20 @@ class PDFTextDevice(PDFDevice):
|
||||
assert isinstance(text, str), str(type(text))
|
||||
|
||||
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
|
||||
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
|
||||
adv, bbox = cls.__compute_char_bbox(matrix, cid, textstate)
|
||||
textstate.linematrix[idx] += adv
|
||||
|
||||
char_meta_datas.append(
|
||||
( text, bbox, )
|
||||
Char( text, Bbox.from_points(bbox), )
|
||||
)
|
||||
if cid == 32 and textstate.wordspace:
|
||||
textstate.linematrix[idx] += textstate.wordspace
|
||||
needcharspace = True
|
||||
|
||||
self.render_char_box(char_meta_datas, textstate, graphicstate, ncs)
|
||||
return PDFText(char_meta_datas, textstate, graphicstate)
|
||||
|
||||
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
|
||||
@classmethod
|
||||
def __compute_char_bbox(cls, matrix, char_id: int, textstate: PDFTextState):
|
||||
font = textstate.font
|
||||
fontsize = textstate.fontsize
|
||||
rise = textstate.rise
|
||||
@@ -157,7 +113,4 @@ class PDFTextDevice(PDFDevice):
|
||||
if y1 < y0:
|
||||
(y0, y1) = (y1, y0)
|
||||
|
||||
return (adv, ((x0, y0), (x1, y1)))
|
||||
|
||||
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
||||
pass
|
||||
return (adv, ((x0, y0), (x1, y1)))
|
||||
@@ -0,0 +1,9 @@
|
||||
from ._base import PDFItem
|
||||
|
||||
class PDFXObject(PDFItem):
|
||||
def __init__(self, xobj_strem, bbox, resources, ctm):
|
||||
self.stream = xobj_strem
|
||||
self.bbox = bbox
|
||||
self.resources = resources
|
||||
self.ctm = ctm
|
||||
self.children = []
|
||||
@@ -0,0 +1,100 @@
|
||||
from pdfmajor.utils import mult_matrix, isnumber, apply_matrix_pt, translate_matrix
|
||||
|
||||
from ..PDFTextState import PDFTextState
|
||||
from ..PDFGraphicState import PDFGraphicState, PDFColorSpace
|
||||
from .PDFItem import PDFText
|
||||
from .Char import Char, Bbox
|
||||
|
||||
class TextDecoder:
|
||||
|
||||
@classmethod
|
||||
def make_pdftext(cls, seq: bytearray, ctm, textstate: PDFTextState, graphicstate: PDFGraphicState) -> PDFText:
|
||||
textstate.matrix = mult_matrix(textstate.matrix, ctm)
|
||||
textstate.scaling = textstate.scaling * .01
|
||||
textstate.charspace = textstate.charspace * textstate.scaling
|
||||
textstate.wordspace = textstate.wordspace * textstate.scaling
|
||||
ncs = graphicstate.ncolor.color_space
|
||||
|
||||
if textstate.font.is_multibyte():
|
||||
textstate.wordspace = 0
|
||||
dxscale = .001 * textstate.fontsize * textstate.scaling
|
||||
|
||||
if textstate.font.is_vertical():
|
||||
return cls.__render_string_along(1,
|
||||
seq, textstate, dxscale, ncs, graphicstate
|
||||
)
|
||||
else:
|
||||
return cls.__render_string_along(0,
|
||||
seq, textstate, dxscale, ncs, graphicstate
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def __render_string_along(cls, idx: int, seq: bytearray,
|
||||
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState) -> PDFText:
|
||||
needcharspace = False
|
||||
char_meta_datas = []
|
||||
for obj in seq:
|
||||
if isnumber(obj):
|
||||
textstate.linematrix[idx] -= obj*dxscale
|
||||
needcharspace = True
|
||||
else:
|
||||
for cid in textstate.font.decode(obj):
|
||||
if needcharspace:
|
||||
textstate.linematrix[idx] += textstate.charspace
|
||||
|
||||
text = textstate.font.to_unichr(cid)
|
||||
assert isinstance(text, str), str(type(text))
|
||||
|
||||
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
|
||||
adv, bbox = cls.__compute_char_bbox(matrix, cid, textstate)
|
||||
textstate.linematrix[idx] += adv
|
||||
|
||||
char_meta_datas.append(
|
||||
Char( text, Bbox.from_points(bbox), )
|
||||
)
|
||||
if cid == 32 and textstate.wordspace:
|
||||
textstate.linematrix[idx] += textstate.wordspace
|
||||
needcharspace = True
|
||||
|
||||
return PDFText(char_meta_datas, textstate, graphicstate)
|
||||
|
||||
@classmethod
|
||||
def __compute_char_bbox(cls, matrix, char_id: int, textstate: PDFTextState):
|
||||
font = textstate.font
|
||||
fontsize = textstate.fontsize
|
||||
rise = textstate.rise
|
||||
scaling = textstate.scaling
|
||||
|
||||
textwidth = font.char_width(char_id)
|
||||
textdisp = font.char_disp(char_id)
|
||||
adv = textwidth * fontsize * scaling
|
||||
|
||||
# compute the boundary rectangle.
|
||||
if font.is_vertical():
|
||||
# vertical
|
||||
width = font.get_width() * fontsize
|
||||
(vx, vy) = textdisp
|
||||
if vx is None:
|
||||
vx = width * 0.5
|
||||
else:
|
||||
vx = vx * fontsize * .001
|
||||
vy = (1000 - vy) * fontsize * .001
|
||||
tx = -vx
|
||||
ty = vy + rise
|
||||
bll = (tx, ty+adv)
|
||||
bur = (tx+width, ty)
|
||||
else:
|
||||
# horizontal
|
||||
height = font.get_height() * fontsize
|
||||
descent = font.get_descent() * fontsize
|
||||
ty = descent + rise
|
||||
bll = (0, ty)
|
||||
bur = (adv, ty+height)
|
||||
(x0, y0) = apply_matrix_pt(matrix, bll)
|
||||
(x1, y1) = apply_matrix_pt(matrix, bur)
|
||||
if x1 < x0:
|
||||
(x0, x1) = (x1, x0)
|
||||
if y1 < y0:
|
||||
(y0, y1) = (y1, y0)
|
||||
|
||||
return (adv, ((x0, y0), (x1, y1)))
|
||||
@@ -0,0 +1,5 @@
|
||||
from ._base import PDFItem
|
||||
from .PDFShape import PDFShape
|
||||
from .PDFImage import PDFImage
|
||||
from .PDFText import PDFText
|
||||
from .PDFXObject import PDFXObject
|
||||
@@ -0,0 +1,6 @@
|
||||
class PDFItem:
|
||||
def __repr__(self) -> str:
|
||||
return f"""<{self.__class__.__name__} {' '.join([
|
||||
f'{name}="{val}"'
|
||||
for name, val in vars(self).items()
|
||||
])}/>"""
|
||||
@@ -0,0 +1,246 @@
|
||||
## CFFFont
|
||||
## (Format specified in Adobe Technical Note: #5176
|
||||
## "The Compact Font Format Specification")
|
||||
##
|
||||
import struct
|
||||
from io import BytesIO
|
||||
from pdfmajor.utils import nunpack
|
||||
|
||||
NIBBLES = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'e-', None, '-')
|
||||
|
||||
def getdict(data):
|
||||
d = {}
|
||||
fp = BytesIO(data)
|
||||
stack = []
|
||||
while 1:
|
||||
c = fp.read(1)
|
||||
if not c:
|
||||
break
|
||||
b0 = ord(c)
|
||||
if b0 <= 21:
|
||||
d[b0] = stack
|
||||
stack = []
|
||||
continue
|
||||
if b0 == 30:
|
||||
s = ''
|
||||
loop = True
|
||||
while loop:
|
||||
b = ord(fp.read(1))
|
||||
for n in (b >> 4, b & 15):
|
||||
if n == 15:
|
||||
loop = False
|
||||
else:
|
||||
s += NIBBLES[n]
|
||||
value = float(s)
|
||||
elif 32 <= b0 and b0 <= 246:
|
||||
value = b0-139
|
||||
else:
|
||||
b1 = ord(fp.read(1))
|
||||
if 247 <= b0 and b0 <= 250:
|
||||
value = ((b0-247) << 8)+b1+108
|
||||
elif 251 <= b0 and b0 <= 254:
|
||||
value = -((b0-251) << 8)-b1-108
|
||||
else:
|
||||
b2 = ord(fp.read(1))
|
||||
if 128 <= b1:
|
||||
b1 -= 256
|
||||
if b0 == 28:
|
||||
value = b1 << 8 | b2
|
||||
else:
|
||||
value = b1 << 24 | b2 << 16 | struct.unpack('>H', fp.read(2))[0]
|
||||
stack.append(value)
|
||||
return d
|
||||
|
||||
|
||||
class CFFFont(object):
|
||||
|
||||
STANDARD_STRINGS = (
|
||||
'.notdef', 'space', 'exclam', 'quotedbl', 'numbersign',
|
||||
'dollar', 'percent', 'ampersand', 'quoteright', 'parenleft',
|
||||
'parenright', 'asterisk', 'plus', 'comma', 'hyphen', 'period',
|
||||
'slash', 'zero', 'one', 'two', 'three', 'four', 'five', 'six',
|
||||
'seven', 'eight', 'nine', 'colon', 'semicolon', 'less', 'equal',
|
||||
'greater', 'question', 'at', 'A', 'B', 'C', 'D', 'E', 'F', 'G',
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
|
||||
'U', 'V', 'W', 'X', 'Y', 'Z', 'bracketleft', 'backslash',
|
||||
'bracketright', 'asciicircum', 'underscore', 'quoteleft', 'a',
|
||||
'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||
'braceleft', 'bar', 'braceright', 'asciitilde', 'exclamdown',
|
||||
'cent', 'sterling', 'fraction', 'yen', 'florin', 'section',
|
||||
'currency', 'quotesingle', 'quotedblleft', 'guillemotleft',
|
||||
'guilsinglleft', 'guilsinglright', 'fi', 'fl', 'endash',
|
||||
'dagger', 'daggerdbl', 'periodcentered', 'paragraph', 'bullet',
|
||||
'quotesinglbase', 'quotedblbase', 'quotedblright',
|
||||
'guillemotright', 'ellipsis', 'perthousand', 'questiondown',
|
||||
'grave', 'acute', 'circumflex', 'tilde', 'macron', 'breve',
|
||||
'dotaccent', 'dieresis', 'ring', 'cedilla', 'hungarumlaut',
|
||||
'ogonek', 'caron', 'emdash', 'AE', 'ordfeminine', 'Lslash',
|
||||
'Oslash', 'OE', 'ordmasculine', 'ae', 'dotlessi', 'lslash',
|
||||
'oslash', 'oe', 'germandbls', 'onesuperior', 'logicalnot', 'mu',
|
||||
'trademark', 'Eth', 'onehalf', 'plusminus', 'Thorn',
|
||||
'onequarter', 'divide', 'brokenbar', 'degree', 'thorn',
|
||||
'threequarters', 'twosuperior', 'registered', 'minus', 'eth',
|
||||
'multiply', 'threesuperior', 'copyright', 'Aacute',
|
||||
'Acircumflex', 'Adieresis', 'Agrave', 'Aring', 'Atilde',
|
||||
'Ccedilla', 'Eacute', 'Ecircumflex', 'Edieresis', 'Egrave',
|
||||
'Iacute', 'Icircumflex', 'Idieresis', 'Igrave', 'Ntilde',
|
||||
'Oacute', 'Ocircumflex', 'Odieresis', 'Ograve', 'Otilde',
|
||||
'Scaron', 'Uacute', 'Ucircumflex', 'Udieresis', 'Ugrave',
|
||||
'Yacute', 'Ydieresis', 'Zcaron', 'aacute', 'acircumflex',
|
||||
'adieresis', 'agrave', 'aring', 'atilde', 'ccedilla', 'eacute',
|
||||
'ecircumflex', 'edieresis', 'egrave', 'iacute', 'icircumflex',
|
||||
'idieresis', 'igrave', 'ntilde', 'oacute', 'ocircumflex',
|
||||
'odieresis', 'ograve', 'otilde', 'scaron', 'uacute',
|
||||
'ucircumflex', 'udieresis', 'ugrave', 'yacute', 'ydieresis',
|
||||
'zcaron', 'exclamsmall', 'Hungarumlautsmall', 'dollaroldstyle',
|
||||
'dollarsuperior', 'ampersandsmall', 'Acutesmall',
|
||||
'parenleftsuperior', 'parenrightsuperior', 'twodotenleader',
|
||||
'onedotenleader', 'zerooldstyle', 'oneoldstyle', 'twooldstyle',
|
||||
'threeoldstyle', 'fouroldstyle', 'fiveoldstyle', 'sixoldstyle',
|
||||
'sevenoldstyle', 'eightoldstyle', 'nineoldstyle',
|
||||
'commasuperior', 'threequartersemdash', 'periodsuperior',
|
||||
'questionsmall', 'asuperior', 'bsuperior', 'centsuperior',
|
||||
'dsuperior', 'esuperior', 'isuperior', 'lsuperior', 'msuperior',
|
||||
'nsuperior', 'osuperior', 'rsuperior', 'ssuperior', 'tsuperior',
|
||||
'ff', 'ffi', 'ffl', 'parenleftinferior', 'parenrightinferior',
|
||||
'Circumflexsmall', 'hyphensuperior', 'Gravesmall', 'Asmall',
|
||||
'Bsmall', 'Csmall', 'Dsmall', 'Esmall', 'Fsmall', 'Gsmall',
|
||||
'Hsmall', 'Ismall', 'Jsmall', 'Ksmall', 'Lsmall', 'Msmall',
|
||||
'Nsmall', 'Osmall', 'Psmall', 'Qsmall', 'Rsmall', 'Ssmall',
|
||||
'Tsmall', 'Usmall', 'Vsmall', 'Wsmall', 'Xsmall', 'Ysmall',
|
||||
'Zsmall', 'colonmonetary', 'onefitted', 'rupiah', 'Tildesmall',
|
||||
'exclamdownsmall', 'centoldstyle', 'Lslashsmall', 'Scaronsmall',
|
||||
'Zcaronsmall', 'Dieresissmall', 'Brevesmall', 'Caronsmall',
|
||||
'Dotaccentsmall', 'Macronsmall', 'figuredash', 'hypheninferior',
|
||||
'Ogoneksmall', 'Ringsmall', 'Cedillasmall', 'questiondownsmall',
|
||||
'oneeighth', 'threeeighths', 'fiveeighths', 'seveneighths',
|
||||
'onethird', 'twothirds', 'zerosuperior', 'foursuperior',
|
||||
'fivesuperior', 'sixsuperior', 'sevensuperior', 'eightsuperior',
|
||||
'ninesuperior', 'zeroinferior', 'oneinferior', 'twoinferior',
|
||||
'threeinferior', 'fourinferior', 'fiveinferior', 'sixinferior',
|
||||
'seveninferior', 'eightinferior', 'nineinferior',
|
||||
'centinferior', 'dollarinferior', 'periodinferior',
|
||||
'commainferior', 'Agravesmall', 'Aacutesmall',
|
||||
'Acircumflexsmall', 'Atildesmall', 'Adieresissmall',
|
||||
'Aringsmall', 'AEsmall', 'Ccedillasmall', 'Egravesmall',
|
||||
'Eacutesmall', 'Ecircumflexsmall', 'Edieresissmall',
|
||||
'Igravesmall', 'Iacutesmall', 'Icircumflexsmall',
|
||||
'Idieresissmall', 'Ethsmall', 'Ntildesmall', 'Ogravesmall',
|
||||
'Oacutesmall', 'Ocircumflexsmall', 'Otildesmall',
|
||||
'Odieresissmall', 'OEsmall', 'Oslashsmall', 'Ugravesmall',
|
||||
'Uacutesmall', 'Ucircumflexsmall', 'Udieresissmall',
|
||||
'Yacutesmall', 'Thornsmall', 'Ydieresissmall', '001.000',
|
||||
'001.001', '001.002', '001.003', 'Black', 'Bold', 'Book',
|
||||
'Light', 'Medium', 'Regular', 'Roman', 'Semibold',
|
||||
)
|
||||
|
||||
class INDEX(object):
|
||||
|
||||
def __init__(self, fp):
|
||||
self.fp = fp
|
||||
self.offsets = []
|
||||
(count, offsize) = struct.unpack('>HB', self.fp.read(3))
|
||||
for _ in range(count+1):
|
||||
self.offsets.append(nunpack(self.fp.read(offsize)))
|
||||
self.base = self.fp.tell()-1
|
||||
self.fp.seek(self.base+self.offsets[-1])
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<INDEX: size=%d>' % len(self)
|
||||
|
||||
def __len__(self): # pylint: disable=E0303
|
||||
return len(self.offsets)-1
|
||||
|
||||
def __getitem__(self, i):
|
||||
self.fp.seek(self.base+self.offsets[i])
|
||||
return self.fp.read(self.offsets[i+1]-self.offsets[i])
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self[i] for i in range(len(self)))
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
# Header
|
||||
(_major, _minor, hdrsize, _) = struct.unpack('BBBB', self.fp.read(4))
|
||||
self.fp.read(hdrsize-4)
|
||||
# Name INDEX
|
||||
self.name_index = self.INDEX(self.fp)
|
||||
# Top DICT INDEX
|
||||
self.dict_index = self.INDEX(self.fp)
|
||||
# String INDEX
|
||||
self.string_index = self.INDEX(self.fp)
|
||||
# Global Subr INDEX
|
||||
self.subr_index = self.INDEX(self.fp)
|
||||
# Top DICT DATA
|
||||
self.top_dict = getdict(self.dict_index[0])
|
||||
(charset_pos,) = self.top_dict.get(15, [0])
|
||||
(encoding_pos,) = self.top_dict.get(16, [0])
|
||||
(charstring_pos,) = self.top_dict.get(17, [0])
|
||||
# CharStrings
|
||||
self.fp.seek(charstring_pos)
|
||||
self.charstring = self.INDEX(self.fp)
|
||||
self.nglyphs = len(self.charstring)
|
||||
# Encodings
|
||||
self.code2gid = {}
|
||||
self.gid2code = {}
|
||||
self.fp.seek(encoding_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
for (code, gid) in enumerate(struct.unpack('B'*n, self.fp.read(n))):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
code = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
self.code2gid[code] = gid
|
||||
self.gid2code[gid] = code
|
||||
code += 1
|
||||
else:
|
||||
raise ValueError('unsupported encoding format: %r' % format)
|
||||
# Charsets
|
||||
self.name2gid = {}
|
||||
self.gid2name = {}
|
||||
self.fp.seek(charset_pos)
|
||||
format = self.fp.read(1)
|
||||
if format == b'\x00':
|
||||
# Format 0
|
||||
n = self.nglyphs-1
|
||||
for (gid, sid) in enumerate(struct.unpack('>'+'H'*n, self.fp.read(2*n))):
|
||||
gid += 1
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
elif format == b'\x01':
|
||||
# Format 1
|
||||
(n,) = struct.unpack('B', self.fp.read(1))
|
||||
sid = 0
|
||||
for _ in range(n):
|
||||
(first, nleft) = struct.unpack('BB', self.fp.read(2))
|
||||
for gid in range(first, first+nleft+1):
|
||||
name = self.getstr(sid)
|
||||
self.name2gid[name] = gid
|
||||
self.gid2name[gid] = name
|
||||
sid += 1
|
||||
elif format == b'\x02':
|
||||
# Format 2
|
||||
assert False, str(('Unhandled', format))
|
||||
else:
|
||||
raise ValueError('unsupported charset format: %r' % format)
|
||||
#print self.code2gid
|
||||
#print self.name2gid
|
||||
#assert 0
|
||||
return
|
||||
|
||||
def getstr(self, sid):
|
||||
if sid < len(self.STANDARD_STRINGS):
|
||||
return self.STANDARD_STRINGS[sid]
|
||||
return self.string_index[sid-len(self.STANDARD_STRINGS)]
|
||||
@@ -0,0 +1,140 @@
|
||||
# PDFFont
|
||||
from abc import abstractmethod
|
||||
from io import BytesIO
|
||||
|
||||
from pdfmajor.parser.EncodingDB import EncodingDB
|
||||
from pdfmajor.parser.PSStackParser import LIT
|
||||
from pdfmajor.parser.PSStackParser import PSLiteral
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFStream import resolve1
|
||||
from pdfmajor.parser.PDFStream import int_value
|
||||
from pdfmajor.parser.PDFStream import num_value
|
||||
from pdfmajor.parser.PDFStream import list_value
|
||||
from pdfmajor.parser.PDFStream import PDFException
|
||||
from pdfmajor.parser.PDFStream import PDFStream
|
||||
from pdfmajor.parser.cmapdb import FileUnicodeMap
|
||||
from pdfmajor.parser.cmapdb import CMapParser
|
||||
|
||||
## Literals
|
||||
##
|
||||
LITERAL_STANDARD_ENCODING = LIT('StandardEncoding')
|
||||
LITERAL_TYPE1C = LIT('Type1C')
|
||||
|
||||
|
||||
class PDFFont(object):
|
||||
class PDFFontError(PDFException): pass
|
||||
class PDFUnicodeNotDefined(PDFFontError): pass
|
||||
|
||||
def __init__(self, descriptor, widths, default_width=None):
|
||||
self.descriptor = descriptor
|
||||
self.widths = widths
|
||||
self.fontname = resolve1(descriptor.get('FontName', 'unknown'))
|
||||
if isinstance(self.fontname, PSLiteral):
|
||||
self.fontname = literal_name(self.fontname)
|
||||
self.flags = int_value(descriptor.get('Flags', 0))
|
||||
self.ascent = num_value(descriptor.get('Ascent', 0))
|
||||
self.descent = num_value(descriptor.get('Descent', 0))
|
||||
self.italic_angle = num_value(descriptor.get('ItalicAngle', 0))
|
||||
self.default_width = default_width or num_value(descriptor.get('MissingWidth', 0))
|
||||
self.leading = num_value(descriptor.get('Leading', 0))
|
||||
self.bbox = list_value(descriptor.get('FontBBox', (0, 0, 0, 0)))
|
||||
self.hscale = self.vscale = .001
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFFont>'
|
||||
|
||||
def is_vertical(self):
|
||||
return False
|
||||
|
||||
def is_multibyte(self):
|
||||
return False
|
||||
|
||||
def decode(self, bytes):
|
||||
return bytearray(bytes) # map(ord, bytes)
|
||||
|
||||
def get_ascent(self):
|
||||
return self.ascent * self.vscale
|
||||
|
||||
def get_descent(self):
|
||||
return self.descent * self.vscale
|
||||
|
||||
def get_width(self):
|
||||
w = self.bbox[2]-self.bbox[0]
|
||||
if w == 0:
|
||||
w = -self.default_width
|
||||
return w * self.hscale
|
||||
|
||||
def get_height(self):
|
||||
h = self.bbox[3]-self.bbox[1]
|
||||
if h == 0:
|
||||
h = self.ascent - self.descent
|
||||
return h * self.vscale
|
||||
|
||||
def char_width(self, cid):
|
||||
try:
|
||||
return self.widths[cid] * self.hscale
|
||||
except KeyError:
|
||||
try:
|
||||
return self.widths[self.to_unichr(cid)] * self.hscale
|
||||
except (KeyError, self.PDFUnicodeNotDefined):
|
||||
return self.default_width * self.hscale
|
||||
|
||||
def char_disp(self, cid):
|
||||
return 0
|
||||
|
||||
def string_width(self, s):
|
||||
return sum(self.char_width(cid) for cid in self.decode(s))
|
||||
|
||||
@abstractmethod
|
||||
def to_unichr(self, cid):
|
||||
raise NotImplementedError
|
||||
|
||||
@property
|
||||
def font_weight(self):
|
||||
if 'FontWeight' in self.descriptor.keys():
|
||||
return self.descriptor['FontWeight']
|
||||
elif 'bold' in self.fontname.lower():
|
||||
return 'bold'
|
||||
else:
|
||||
return None
|
||||
|
||||
@property
|
||||
def is_bold(self):
|
||||
return self.font_weight == 'bold' or self.font_weight > 549
|
||||
|
||||
# PDFSimpleFont
|
||||
class PDFSimpleFont(PDFFont):
|
||||
|
||||
def __init__(self, descriptor, widths, spec):
|
||||
# Font encoding is specified either by a name of
|
||||
# built-in encoding or a dictionary that describes
|
||||
# the differences.
|
||||
if 'Encoding' in spec:
|
||||
encoding = resolve1(spec['Encoding'])
|
||||
else:
|
||||
encoding = LITERAL_STANDARD_ENCODING
|
||||
if isinstance(encoding, dict):
|
||||
name = literal_name(encoding.get('BaseEncoding', LITERAL_STANDARD_ENCODING))
|
||||
diff = list_value(encoding.get('Differences', []))
|
||||
self.cid2unicode = EncodingDB.get_encoding(name, diff)
|
||||
else:
|
||||
self.cid2unicode = EncodingDB.get_encoding(literal_name(encoding))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
PDFFont.__init__(self, descriptor, widths)
|
||||
return
|
||||
|
||||
def to_unichr(self, cid):
|
||||
if self.unicode_map:
|
||||
try:
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
pass
|
||||
try:
|
||||
return self.cid2unicode[cid]
|
||||
except KeyError:
|
||||
raise self.PDFUnicodeNotDefined(None, cid)
|
||||
@@ -0,0 +1,89 @@
|
||||
## TrueTypeFont
|
||||
##
|
||||
import struct
|
||||
from pdfmajor.parser.cmapdb import FileUnicodeMap
|
||||
|
||||
class TrueTypeFont(object):
|
||||
|
||||
class CMapNotFound(Exception):
|
||||
pass
|
||||
|
||||
def __init__(self, name, fp):
|
||||
self.name = name
|
||||
self.fp = fp
|
||||
self.tables = {}
|
||||
self.fonttype = fp.read(4)
|
||||
try:
|
||||
(ntables, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
for _ in range(ntables):
|
||||
(name, _, offset, length) = struct.unpack('>4sLLL', fp.read(16))
|
||||
self.tables[name] = (offset, length)
|
||||
except struct.error:
|
||||
# Do not fail if there are not enough bytes to read. Even for
|
||||
# corrupted PDFs we would like to get as much information as
|
||||
# possible, so continue.
|
||||
pass
|
||||
return
|
||||
|
||||
def create_unicode_map(self):
|
||||
if 'cmap' not in self.tables:
|
||||
raise TrueTypeFont.CMapNotFound
|
||||
(base_offset, _) = self.tables['cmap']
|
||||
fp = self.fp
|
||||
fp.seek(base_offset)
|
||||
(_, nsubtables) = struct.unpack('>HH', fp.read(4))
|
||||
subtables = []
|
||||
for i in range(nsubtables):
|
||||
subtables.append(struct.unpack('>HHL', fp.read(8)))
|
||||
char2gid = {}
|
||||
# Only supports subtable type 0, 2 and 4.
|
||||
for (_1, _2, st_offset) in subtables:
|
||||
fp.seek(base_offset+st_offset)
|
||||
(fmttype, _, _) = struct.unpack('>HHH', fp.read(6))
|
||||
if fmttype == 0:
|
||||
char2gid.update(enumerate(struct.unpack('>256B', fp.read(256))))
|
||||
elif fmttype == 2:
|
||||
subheaderkeys = struct.unpack('>256H', fp.read(512))
|
||||
firstbytes = [0]*8192
|
||||
for (i, k) in enumerate(subheaderkeys):
|
||||
firstbytes[k//8] = i
|
||||
nhdrs = max(subheaderkeys)//8 + 1
|
||||
hdrs = []
|
||||
for i in range(nhdrs):
|
||||
(firstcode, entcount, delta, offset) = struct.unpack('>HHhH', fp.read(8))
|
||||
hdrs.append((i, firstcode, entcount, delta, fp.tell()-2+offset))
|
||||
for (i, firstcode, entcount, delta, pos) in hdrs:
|
||||
if not entcount:
|
||||
continue
|
||||
first = firstcode + (firstbytes[i] << 8)
|
||||
fp.seek(pos)
|
||||
for c in range(entcount):
|
||||
gid = struct.unpack('>H', fp.read(2))
|
||||
if gid:
|
||||
gid += delta
|
||||
char2gid[first+c] = gid
|
||||
elif fmttype == 4:
|
||||
(segcount, _1, _2, _3) = struct.unpack('>HHHH', fp.read(8))
|
||||
segcount //= 2
|
||||
ecs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
fp.read(2)
|
||||
scs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
idds = struct.unpack('>%dh' % segcount, fp.read(2*segcount))
|
||||
pos = fp.tell()
|
||||
idrs = struct.unpack('>%dH' % segcount, fp.read(2*segcount))
|
||||
for (ec, sc, idd, idr) in zip(ecs, scs, idds, idrs):
|
||||
if idr:
|
||||
fp.seek(pos+idr)
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (struct.unpack('>H', fp.read(2))[0] + idd) & 0xffff
|
||||
else:
|
||||
for c in range(sc, ec+1):
|
||||
char2gid[c] = (c + idd) & 0xffff
|
||||
else:
|
||||
assert False, str(('Unhandled', fmttype))
|
||||
# create unicode map
|
||||
unicode_map = FileUnicodeMap()
|
||||
for (char, gid) in char2gid.items():
|
||||
unicode_map.add_cid2unichr(gid, char)
|
||||
return unicode_map
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from pdfmajor.parser.PSStackParser import PSStackParser
|
||||
from pdfmajor.parser.PSStackParser import PSEOF, KWD
|
||||
from pdfmajor.parser.PSStackParser import PSLiteral
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.EncodingDB import name2unicode
|
||||
|
||||
## Type1FontHeaderParser
|
||||
##
|
||||
class Type1FontHeaderParser(PSStackParser):
|
||||
|
||||
KEYWORD_BEGIN = KWD(b'begin')
|
||||
KEYWORD_END = KWD(b'end')
|
||||
KEYWORD_DEF = KWD(b'def')
|
||||
KEYWORD_PUT = KWD(b'put')
|
||||
KEYWORD_DICT = KWD(b'dict')
|
||||
KEYWORD_ARRAY = KWD(b'array')
|
||||
KEYWORD_READONLY = KWD(b'readonly')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
KEYWORD_FOR = KWD(b'for')
|
||||
|
||||
def __init__(self, data):
|
||||
PSStackParser.__init__(self, data)
|
||||
self._cid2unicode = {}
|
||||
return
|
||||
|
||||
def get_encoding(self):
|
||||
while 1:
|
||||
try:
|
||||
(cid, name) = self.nextobject()
|
||||
except PSEOF:
|
||||
break
|
||||
try:
|
||||
self._cid2unicode[cid] = name2unicode(name)
|
||||
except KeyError:
|
||||
pass
|
||||
return self._cid2unicode
|
||||
|
||||
def do_keyword(self, pos, token):
|
||||
if token is self.KEYWORD_PUT:
|
||||
((_, key), (_, value)) = self.pop(2) # pylint: disable=E0632
|
||||
if (isinstance(key, int) and
|
||||
isinstance(value, PSLiteral)):
|
||||
self.add_results((key, literal_name(value)))
|
||||
return
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
from pdfmajor.utils import get_logger, settings
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFStream import list_value
|
||||
from pdfmajor.parser.PDFStream import dict_value
|
||||
from pdfmajor.parser.PDFStream import resolve1
|
||||
from pdfmajor.parser.constants import LITERAL_FONT
|
||||
|
||||
from .fonts import PDFType1Font, PDFTrueTypeFont, PDFType3Font, PDFCIDFont, PDFFont
|
||||
|
||||
log = get_logger(__file__)
|
||||
|
||||
def get_font(objid: int, spec: dict, cached_fonts: dict = {}):
|
||||
if objid and objid in cached_fonts:
|
||||
font = cached_fonts[objid]
|
||||
else:
|
||||
log.debug('get_font: create: objid=%r, spec=%r', objid, spec)
|
||||
if settings.STRICT:
|
||||
if spec['Type'] is not LITERAL_FONT:
|
||||
raise PDFFont.PDFFontError('Type is not /Font')
|
||||
# Create a Font object.
|
||||
if 'Subtype' in spec:
|
||||
subtype = literal_name(spec['Subtype'])
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFont.PDFFontError('Font Subtype is not specified.')
|
||||
subtype = 'Type1'
|
||||
if subtype in ('Type1', 'MMType1'):
|
||||
# Type1 Font
|
||||
font = PDFType1Font(spec)
|
||||
elif subtype == 'TrueType':
|
||||
# TrueType Font
|
||||
font = PDFTrueTypeFont(spec)
|
||||
elif subtype == 'Type3':
|
||||
# Type3 Font
|
||||
font = PDFType3Font(spec)
|
||||
elif subtype in ('CIDFontType0', 'CIDFontType2'):
|
||||
# CID Font
|
||||
font = PDFCIDFont(spec)
|
||||
elif subtype == 'Type0':
|
||||
# Type0 Font
|
||||
dfonts = list_value(spec['DescendantFonts'])
|
||||
assert dfonts
|
||||
subspec = dict_value(dfonts[0]).copy()
|
||||
for k in ('Encoding', 'ToUnicode'):
|
||||
if k in spec:
|
||||
subspec[k] = resolve1(spec[k])
|
||||
font = get_font(None, subspec, cached_fonts)
|
||||
else:
|
||||
if settings.STRICT:
|
||||
raise PDFFont.PDFFontError('Invalid Font spec: %r' % spec)
|
||||
font = PDFType1Font(spec) # this is so wrong!
|
||||
if objid:
|
||||
cached_fonts[objid] = font
|
||||
return font
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,174 @@
|
||||
from io import BytesIO
|
||||
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.PDFStream import int_value
|
||||
from pdfmajor.parser.PDFStream import num_value
|
||||
from pdfmajor.parser.PDFStream import list_value
|
||||
from pdfmajor.parser.PDFStream import dict_value
|
||||
from pdfmajor.parser.PDFStream import PDFStream
|
||||
from pdfmajor.parser.PDFStream import resolve1
|
||||
from pdfmajor.parser.cmapdb import CMap, CMapDB, CMapParser
|
||||
from pdfmajor.parser.cmapdb import FileUnicodeMap
|
||||
from pdfmajor.utils import settings, apply_matrix_norm
|
||||
|
||||
from .PDFFont import PDFFont, PDFSimpleFont
|
||||
from .util import FontMetricsDB, get_widths, get_widths2
|
||||
from .Type1FontHeaderParser import Type1FontHeaderParser
|
||||
from .TrueTypeFont import TrueTypeFont
|
||||
|
||||
# PDFType1Font
|
||||
class PDFType1Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, spec):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if settings.STRICT:
|
||||
raise self.PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
try:
|
||||
(descriptor, widths) = FontMetricsDB.get_metrics(self.basefont)
|
||||
except KeyError:
|
||||
descriptor = dict_value(spec.get('FontDescriptor', {}))
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 255))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
if 'Encoding' not in spec and 'FontFile' in descriptor:
|
||||
# try to recover the missing encoding info from the font file.
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile'))
|
||||
length1 = int_value(self.fontfile['Length1'])
|
||||
data = self.fontfile.get_data()[:length1]
|
||||
parser = Type1FontHeaderParser(BytesIO(data))
|
||||
self.cid2unicode = parser.get_encoding()
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType1Font: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFTrueTypeFont
|
||||
class PDFTrueTypeFont(PDFType1Font):
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFTrueTypeFont: basefont=%r>' % self.basefont
|
||||
|
||||
|
||||
# PDFType3Font
|
||||
class PDFType3Font(PDFSimpleFont):
|
||||
|
||||
def __init__(self, spec):
|
||||
firstchar = int_value(spec.get('FirstChar', 0))
|
||||
#lastchar = int_value(spec.get('LastChar', 0))
|
||||
widths = list_value(spec.get('Widths', [0]*256))
|
||||
widths = dict((i+firstchar, w) for (i, w) in enumerate(widths))
|
||||
if 'FontDescriptor' in spec:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
else:
|
||||
descriptor = {'Ascent': 0, 'Descent': 0,
|
||||
'FontBBox': spec['FontBBox']}
|
||||
PDFSimpleFont.__init__(self, descriptor, widths, spec)
|
||||
self.matrix = tuple(list_value(spec.get('FontMatrix')))
|
||||
(_, self.descent, _, self.ascent) = self.bbox
|
||||
(self.hscale, self.vscale) = apply_matrix_norm(self.matrix, (1, 1))
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFType3Font>'
|
||||
|
||||
|
||||
# PDFCIDFont
|
||||
class PDFCIDFont(PDFFont):
|
||||
|
||||
def __init__(self, spec, strict=settings.STRICT):
|
||||
try:
|
||||
self.basefont = literal_name(spec['BaseFont'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('BaseFont is missing')
|
||||
self.basefont = 'unknown'
|
||||
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"))
|
||||
try:
|
||||
name = literal_name(spec['Encoding'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('Encoding is unspecified')
|
||||
name = 'unknown'
|
||||
try:
|
||||
self.cmap = CMapDB.get_cmap(name)
|
||||
except CMapDB.CMapNotFound as e:
|
||||
if strict:
|
||||
raise self.PDFFontError(e)
|
||||
self.cmap = CMap()
|
||||
try:
|
||||
descriptor = dict_value(spec['FontDescriptor'])
|
||||
except KeyError:
|
||||
if strict:
|
||||
raise self.PDFFontError('FontDescriptor is missing')
|
||||
descriptor = {}
|
||||
ttf = None
|
||||
if 'FontFile2' in descriptor:
|
||||
self.fontfile = PDFStream.validated_stream(descriptor.get('FontFile2'))
|
||||
ttf = TrueTypeFont(self.basefont,
|
||||
BytesIO(self.fontfile.get_data()))
|
||||
self.unicode_map = None
|
||||
if 'ToUnicode' in spec:
|
||||
strm = PDFStream.validated_stream(spec['ToUnicode'])
|
||||
self.unicode_map = FileUnicodeMap()
|
||||
CMapParser(self.unicode_map, BytesIO(strm.get_data())).run()
|
||||
elif self.cidcoding in ('Adobe-Identity', 'Adobe-UCS'):
|
||||
if ttf:
|
||||
try:
|
||||
self.unicode_map = ttf.create_unicode_map()
|
||||
except TrueTypeFont.CMapNotFound:
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
self.unicode_map = CMapDB.get_unicode_map(self.cidcoding, self.cmap.is_vertical())
|
||||
except CMapDB.CMapNotFound as e:
|
||||
pass
|
||||
|
||||
self.vertical = self.cmap.is_vertical()
|
||||
if self.vertical:
|
||||
# writing mode: vertical
|
||||
widths = get_widths2(list_value(spec.get('W2', [])))
|
||||
self.disps = dict((cid, (vx, vy)) for (cid, (_, (vx, vy))) in iter(widths.items()))
|
||||
(vy, w) = spec.get('DW2', [880, -1000])
|
||||
self.default_disp = (None, vy)
|
||||
widths = dict((cid, w) for (cid, (w, _)) in iter(widths.items()))
|
||||
default_width = w
|
||||
else:
|
||||
# writing mode: horizontal
|
||||
self.disps = {}
|
||||
self.default_disp = 0
|
||||
widths = get_widths(list_value(spec.get('W', [])))
|
||||
default_width = spec.get('DW', 1000)
|
||||
PDFFont.__init__(self, descriptor, widths, default_width=default_width)
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return '<PDFCIDFont: basefont=%r, cidcoding=%r>' % (self.basefont, self.cidcoding)
|
||||
|
||||
def is_vertical(self):
|
||||
return self.vertical
|
||||
|
||||
def is_multibyte(self):
|
||||
return True
|
||||
|
||||
def decode(self, bytes):
|
||||
return self.cmap.decode(bytes)
|
||||
|
||||
def char_disp(self, cid):
|
||||
"Returns an integer for horizontal fonts, a tuple for vertical fonts."
|
||||
return self.disps.get(cid, self.default_disp)
|
||||
|
||||
def to_unichr(self, cid):
|
||||
try:
|
||||
if not self.unicode_map:
|
||||
raise KeyError(cid)
|
||||
return self.unicode_map.get_unichr(cid)
|
||||
except KeyError:
|
||||
raise self.PDFUnicodeNotDefined(self.cidcoding, cid)
|
||||
@@ -0,0 +1,57 @@
|
||||
from pdfmajor.utils import FONT_METRICS, choplist, isnumber
|
||||
|
||||
|
||||
def get_widths(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, w) in enumerate(v):
|
||||
widths[char1+i] = w
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 3:
|
||||
(char1, char2, w) = r
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = w
|
||||
r = []
|
||||
return widths
|
||||
assert get_widths([1]) == {}
|
||||
assert get_widths([1,2,3]) == {1:3, 2:3}
|
||||
assert get_widths([1,[2,3],6,[7,8]]) == {1:2,2:3, 6:7,7:8}
|
||||
|
||||
def get_widths2(seq):
|
||||
widths = {}
|
||||
r = []
|
||||
for v in seq:
|
||||
if isinstance(v, list):
|
||||
if r:
|
||||
char1 = r[-1]
|
||||
for (i, (w, vx, vy)) in enumerate(choplist(3, v)):
|
||||
widths[char1+i] = (w, (vx, vy))
|
||||
r = []
|
||||
elif isnumber(v):
|
||||
r.append(v)
|
||||
if len(r) == 5:
|
||||
(char1, char2, w, vx, vy) = r # pylint: disable=E0632
|
||||
for i in range(char1, char2+1):
|
||||
widths[i] = (w, (vx, vy))
|
||||
r = []
|
||||
return widths
|
||||
assert get_widths2([1]) == {}
|
||||
assert get_widths2([1,2,3,4,5]) == {1:(3, (4,5)), 2:(3, (4,5))}
|
||||
assert get_widths2([1,[2,3,4,5],6,[7,8,9]]) == {1:(2, (3,4)), 6:(7, (8,9))}
|
||||
|
||||
|
||||
## FontMetricsDB
|
||||
##
|
||||
class FontMetricsDB(object):
|
||||
|
||||
@classmethod
|
||||
def get_metrics(klass, fontname):
|
||||
return FONT_METRICS[fontname]
|
||||
|
||||
|
||||
+8
-13
@@ -1,11 +1,10 @@
|
||||
from .PDFFont import PDFFont
|
||||
from ..utils import MATRIX_IDENTITY
|
||||
|
||||
from .PDFFont import PDFFont, get_font
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
|
||||
## PDFTextState
|
||||
##
|
||||
class PDFTextState(object):
|
||||
|
||||
|
||||
def __init__(self):
|
||||
self.font: PDFFont = None
|
||||
self.fontsize: int = 0
|
||||
@@ -15,10 +14,8 @@ class PDFTextState(object):
|
||||
self.leading: int = 0
|
||||
self.render: int = 0
|
||||
self.rise: int = 0
|
||||
self.reset()
|
||||
# self.matrix is set
|
||||
# self.linematrix is set
|
||||
return
|
||||
self.matrix = MATRIX_IDENTITY
|
||||
self.linematrix = [0, 0]
|
||||
|
||||
def __repr__(self):
|
||||
return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%r, '
|
||||
@@ -29,7 +26,7 @@ class PDFTextState(object):
|
||||
self.matrix, self.linematrix))
|
||||
|
||||
def copy(self):
|
||||
obj = PDFTextState()
|
||||
obj = self.__class__()
|
||||
obj.font = self.font
|
||||
obj.fontsize = self.fontsize
|
||||
obj.charspace = self.charspace
|
||||
@@ -42,12 +39,10 @@ class PDFTextState(object):
|
||||
obj.linematrix = self.linematrix
|
||||
return obj
|
||||
|
||||
def reset(self):
|
||||
def reset_matrices(self):
|
||||
self.matrix = MATRIX_IDENTITY
|
||||
self.linematrix = [0, 0]
|
||||
return
|
||||
|
||||
@property
|
||||
def is_upright(self):
|
||||
(a, b, c, d, _, _) = self.matrix
|
||||
return (0 < a*d*self.scaling and b*c <= 0)
|
||||
return (0 < self.matrix[:,0].prod()*self.scaling and self.matrix[0,1:].prod() <= 0)
|
||||
@@ -0,0 +1,39 @@
|
||||
from typing import List
|
||||
from pdfmajor.utils import MATRIX_IDENTITY
|
||||
|
||||
from .PDFGraphicState import PDFGraphicState
|
||||
from .PDFGraphicState.PDFColor import PDFColor
|
||||
from .PDFGraphicState.PDFColorSpace import PREDEFINED_COLORSPACE, OrderedDict, PDFColorSpace
|
||||
|
||||
from .PDFTextState import PDFTextState, PDFFont, get_font
|
||||
|
||||
from .Curves import CurvePath
|
||||
|
||||
from .layout import LTItem, LTComponent, LTContainer
|
||||
from .layout import LTCharBlock, LTChar
|
||||
from .layout import LTCurve, LTLine, LTHorizontalLine, LTVerticalLine, LTRect
|
||||
from .layout import LTXObject
|
||||
from .layout import LTImage
|
||||
|
||||
from .layout import make_char_block, make_curve, make_image, make_xobject
|
||||
|
||||
class PDFStateStack:
|
||||
def __init__(self):
|
||||
self.t_matrix = MATRIX_IDENTITY
|
||||
self.text = PDFTextState()
|
||||
self.graphics = PDFGraphicState()
|
||||
self.graphicstack : List[PDFGraphicState] = []
|
||||
self.curvestacks: List[CurvePath] = []
|
||||
self.argstack: List[bytes] = []
|
||||
self.complete_layout_items : List[LTItem] = []
|
||||
self.colorspace_map: OrderedDict = PREDEFINED_COLORSPACE.copy()
|
||||
self.fontmap = {}
|
||||
self.xobjmap = {}
|
||||
self.resources = {}
|
||||
|
||||
def pop(self, n: int) -> bytearray:
|
||||
if n == 0:
|
||||
return []
|
||||
x = self.argstack[-n:]
|
||||
self.argstack = self.argstack[:-n]
|
||||
return x
|
||||
@@ -0,0 +1,46 @@
|
||||
from ._base import LTComponent
|
||||
from pdfmajor.utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox
|
||||
from ..PDFGraphicState import PDFColor
|
||||
from ..PDFTextState import PDFTextState, PDFFont
|
||||
|
||||
## LTChar
|
||||
##
|
||||
class LTChar(LTComponent):
|
||||
|
||||
def __init__(self,
|
||||
bbox: Bbox,
|
||||
char: str,
|
||||
textstate: PDFTextState,
|
||||
color: PDFColor
|
||||
):
|
||||
self._text = char
|
||||
self.textstate = textstate
|
||||
self.color = color
|
||||
LTComponent.__init__(self, bbox)
|
||||
if self.textstate.font.is_vertical():
|
||||
self.size = self.width
|
||||
else:
|
||||
self.size = self.height
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return (
|
||||
'<%s %s font=%r text=%r>' %
|
||||
(
|
||||
self.__class__.__name__,
|
||||
str(self.bbox),
|
||||
self.fontname,
|
||||
self.get_text()
|
||||
)
|
||||
)
|
||||
|
||||
def get_text(self):
|
||||
return self._text
|
||||
|
||||
@property
|
||||
def font(self):
|
||||
return self.textstate.font
|
||||
|
||||
@property
|
||||
def fontname(self) -> str:
|
||||
return self.font.fontname
|
||||
@@ -0,0 +1,66 @@
|
||||
from typing import List
|
||||
|
||||
from ._base import LTContainer
|
||||
from .LTChar import LTChar
|
||||
|
||||
from pdfmajor.utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox, Point
|
||||
from ..PDFGraphicState import PDFColor
|
||||
from ..PDFTextState import PDFFont
|
||||
from ..PDFTextState import PDFTextState
|
||||
|
||||
## LTCharBlock
|
||||
##
|
||||
class LTCharBlock(LTContainer):
|
||||
|
||||
def __init__(self,
|
||||
chars: list, # [str, List[Point]]
|
||||
textstate: PDFTextState,
|
||||
color: PDFColor
|
||||
):
|
||||
c, ((x0, y0), (x1, y1)) = chars[0]
|
||||
ltchars = [
|
||||
LTChar(
|
||||
bbox=Bbox(x0, y0, x1, y1),
|
||||
char= c,
|
||||
textstate=textstate,
|
||||
color=color
|
||||
)
|
||||
]
|
||||
|
||||
for c, pts in chars[1:]:
|
||||
((nx0, ny0), (nx1, ny1)) = pts
|
||||
x0 = min([ x0, x1, nx0, nx1 ])
|
||||
x1 = max([ x0, x1, nx0, nx1 ])
|
||||
y1 = max([ y0, y1, ny0, ny1 ])
|
||||
y0 = min([ y0, y1, ny0, ny1 ])
|
||||
ltchars.append(LTChar(
|
||||
bbox=Bbox(nx0, ny0, nx1, ny1),
|
||||
char= c,
|
||||
textstate=textstate,
|
||||
color=color
|
||||
))
|
||||
|
||||
LTContainer.__init__(self, Bbox(x0, y0, x1, y1), ltchars)
|
||||
|
||||
self.textstate = textstate
|
||||
self.color = color
|
||||
|
||||
if self.textstate.font.is_vertical():
|
||||
self.size = self.width
|
||||
else:
|
||||
self.size = self.height
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return f"""<{self.__class__.__name__} font="{self.fontname}" text="{self.get_text()}"/>"""
|
||||
|
||||
def get_text(self):
|
||||
return "".join([ c.get_text() for c in self ])
|
||||
|
||||
@property
|
||||
def font(self):
|
||||
return self.textstate.font
|
||||
|
||||
@property
|
||||
def fontname(self) -> str:
|
||||
return self.textstate.font.fontname
|
||||
@@ -0,0 +1,45 @@
|
||||
|
||||
from typing import List, Tuple
|
||||
|
||||
from pdfmajor.utils import Bbox
|
||||
from ..PDFGraphicState import PDFColor
|
||||
from ..Curves import CurvePath
|
||||
|
||||
from ._base import LTComponent, Bbox
|
||||
|
||||
class LTCurve(LTComponent):
|
||||
|
||||
def __init__(self,
|
||||
linewidth: float,
|
||||
paths: List[CurvePath],
|
||||
bbox: Bbox,
|
||||
stroke: PDFColor,
|
||||
fill: PDFColor,
|
||||
evenodd: bool,
|
||||
):
|
||||
LTComponent.__init__(self, bbox)
|
||||
self.paths = paths
|
||||
self.linewidth = linewidth
|
||||
self.evenodd = evenodd
|
||||
self.stroke = stroke
|
||||
self.fill = fill
|
||||
|
||||
## LTLine
|
||||
##
|
||||
class LTLine(LTCurve):
|
||||
pass
|
||||
|
||||
## LTHorizontalLine
|
||||
##
|
||||
class LTHorizontalLine(LTLine):
|
||||
pass
|
||||
|
||||
## LTHorizontalLine
|
||||
##
|
||||
class LTVerticalLine(LTLine):
|
||||
pass
|
||||
|
||||
## LTRect
|
||||
##
|
||||
class LTRect(LTCurve):
|
||||
pass
|
||||
@@ -0,0 +1,25 @@
|
||||
from pdfmajor.utils import Bbox
|
||||
from pdfmajor.parser.PDFStream import PDFStream
|
||||
from ._base import LTComponent
|
||||
|
||||
## LTImage
|
||||
##
|
||||
class LTImage(LTComponent):
|
||||
|
||||
def __init__(self, name: str, stream: PDFStream, bbox: Bbox):
|
||||
LTComponent.__init__(self, bbox)
|
||||
self.name = name
|
||||
self.stream = stream
|
||||
self.srcsize = (stream.get_any(('W', 'Width')),
|
||||
stream.get_any(('H', 'Height')))
|
||||
self.imagemask = stream.get_any(('IM', 'ImageMask'))
|
||||
self.bits = stream.get_any(('BPC', 'BitsPerComponent'), 1)
|
||||
self.colorspace = stream.get_any(('CS', 'ColorSpace'))
|
||||
if not isinstance(self.colorspace, list):
|
||||
self.colorspace = [self.colorspace]
|
||||
return
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s(%s) %s %r>' %
|
||||
(self.__class__.__name__, self.name,
|
||||
str(self.bbox), self.srcsize))
|
||||
@@ -0,0 +1,22 @@
|
||||
from typing import List
|
||||
|
||||
from pdfmajor.utils import Bbox, Point, matrix2str
|
||||
from pdfmajor.parser.PDFStream import PDFStream
|
||||
|
||||
from ._base import LTContainer
|
||||
|
||||
## LTXObject
|
||||
##
|
||||
class LTXObject(LTContainer):
|
||||
|
||||
def __init__(self, name: str, bbox: Bbox, xobj_stream: PDFStream, resources: dict, t_matrix: List[Point] ):
|
||||
LTContainer.__init__(self, bbox)
|
||||
self.name = name
|
||||
self.stream = xobj_stream
|
||||
self.t_matrix = t_matrix
|
||||
self.resources = resources
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s(%s) %s matrix=%s>' %
|
||||
(self.__class__.__name__, self.name,
|
||||
str(self.bbox), matrix2str(self.t_matrix)))
|
||||
@@ -0,0 +1,133 @@
|
||||
from typing import List
|
||||
from pdfmajor.utils import apply_matrix_pt, INF, Bbox, Point, MATRIX_IDENTITY, mult_matrix
|
||||
|
||||
from ..Curves import CurvePath
|
||||
from ..PDFGraphicState import PDFGraphicState, PDFColor
|
||||
from ..PDFTextState import PDFTextState
|
||||
|
||||
from ._base import LTItem, LTComponent, LTContainer
|
||||
from .LTCharBlock import LTCharBlock
|
||||
from .LTCurves import LTCurve, LTLine, LTHorizontalLine, LTRect, LTVerticalLine
|
||||
from .LTImage import LTImage
|
||||
from .LTXObject import LTXObject
|
||||
from .utils.textdecoder import decode_text_seq
|
||||
|
||||
|
||||
def make_char_block(seq: bytearray, ctm: tuple, textstate: PDFTextState, color: PDFColor):
|
||||
(char_meta_datas, textstate, color) = decode_text_seq(seq, ctm, textstate, color)
|
||||
return LTCharBlock(
|
||||
chars=char_meta_datas,
|
||||
color=color,
|
||||
textstate=textstate
|
||||
)
|
||||
|
||||
def make_curve(ctm: tuple, gstate: PDFGraphicState, evenodd: bool, paths: List[CurvePath]):
|
||||
shape = ''.join(x.method.value for x in paths)
|
||||
if shape == 'ml':
|
||||
# horizontal/vertical line
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
(x0, y0) = apply_matrix_pt(ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(ctm, (pt1.x, pt1.y))
|
||||
if x0 == x1:
|
||||
return LTVerticalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
)
|
||||
elif y0 == y1:
|
||||
return LTHorizontalLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
)
|
||||
else:
|
||||
return LTLine(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox( x0, y0, x1, y1 ),
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
evenodd=evenodd,
|
||||
)
|
||||
elif shape == 'mlllh':
|
||||
# rectangle
|
||||
pt0 = paths[0].points[0]
|
||||
pt1 = paths[1].points[0]
|
||||
pt2 = paths[2].points[0]
|
||||
pt3 = paths[3].points[0]
|
||||
(x0, y0) = apply_matrix_pt(ctm, (pt0.x, pt0.y))
|
||||
(x1, y1) = apply_matrix_pt(ctm, (pt1.x, pt1.y))
|
||||
(x2, y2) = apply_matrix_pt(ctm, (pt2.x, pt2.y))
|
||||
(x3, y3) = apply_matrix_pt(ctm, (pt3.x, pt3.y))
|
||||
if ((x0 == x1 and y1 == y2 and x2 == x3 and y3 == y0) or
|
||||
(y0 == y1 and x1 == x2 and y2 == y3 and x3 == x0)):
|
||||
return LTRect(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(
|
||||
x0=min(x0,x1,x2,x3),
|
||||
x1=max(x0,x1,x2,x3),
|
||||
y0=min(y0,y1,y2,y3),
|
||||
y1=max(y0,y1,y2,y3),
|
||||
),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor,
|
||||
)
|
||||
else:
|
||||
(x0, y0, x1, y1) = (INF, INF, -INF, -INF)
|
||||
for path in paths:
|
||||
if path.method in [CurvePath.METHOD.MOVE_TO, CurvePath.METHOD.LINE_TO]:
|
||||
x0 = min(x0, path.points[0].x)
|
||||
y0 = min(y0, path.points[0].y)
|
||||
x1 = max(x1, path.points[0].x)
|
||||
y1 = max(y1, path.points[0].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_BOTH_TO]:
|
||||
x0 = min(x0, path.points[2].x)
|
||||
y0 = min(y0, path.points[2].y)
|
||||
x1 = max(x1, path.points[2].x)
|
||||
y1 = max(y1, path.points[2].y)
|
||||
elif path.method in [CurvePath.METHOD.CURVE_NEXT_TO, CurvePath.METHOD.CURVE_FIRST_TO]:
|
||||
x0 = min(x0, path.points[1].x)
|
||||
y0 = min(y0, path.points[1].y)
|
||||
x1 = max(x1, path.points[1].x)
|
||||
y1 = max(y1, path.points[1].y)
|
||||
return LTCurve(
|
||||
linewidth=gstate.linewidth,
|
||||
paths=paths,
|
||||
bbox=Bbox(x0,y0,x1,y1),
|
||||
evenodd=evenodd,
|
||||
stroke=gstate.scolor,
|
||||
fill=gstate.ncolor
|
||||
)
|
||||
|
||||
def make_image(obj, ctm):
|
||||
matrix = mult_matrix(MATRIX_IDENTITY, ctm)
|
||||
return LTImage(
|
||||
name=str(id(obj)),
|
||||
stream=obj,
|
||||
bbox=Bbox.from_points([
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((0, 0), (1, 0), (0, 1), (1, 1))
|
||||
])
|
||||
)
|
||||
|
||||
def make_xobject(obj, bbox, ctm, matrix, resources):
|
||||
(x, y, w, h) = bbox
|
||||
return LTXObject(
|
||||
name=str(id(obj)),
|
||||
bbox=Bbox.from_points([
|
||||
apply_matrix_pt(matrix, (p, q))
|
||||
for (p, q) in ((x, y), (x+w, y), (x, y+h), (x+w, y+h))
|
||||
]),
|
||||
xobj_stream=obj,
|
||||
resources=resources,
|
||||
t_matrix = ctm
|
||||
)
|
||||
@@ -0,0 +1,115 @@
|
||||
from typing import List
|
||||
from pdfmajor.utils import Bbox
|
||||
|
||||
## LTItem
|
||||
##
|
||||
class LTItem(object):
|
||||
pass
|
||||
|
||||
## LTComponent
|
||||
##
|
||||
class LTComponent(LTItem):
|
||||
def __init__(self, bbox: Bbox):
|
||||
if isinstance(bbox, Bbox):
|
||||
self.bbox = bbox
|
||||
else:
|
||||
raise Exception("Invalid BBox")
|
||||
|
||||
def __repr__(self):
|
||||
return ('<%s %s>' %
|
||||
(self.__class__.__name__, str(self.bbox)))
|
||||
|
||||
# Disable comparison.
|
||||
def __lt__(self, _):
|
||||
raise ValueError
|
||||
def __le__(self, _):
|
||||
raise ValueError
|
||||
def __gt__(self, _):
|
||||
raise ValueError
|
||||
def __ge__(self, _):
|
||||
raise ValueError
|
||||
|
||||
@property
|
||||
def width(self): return self.bbox.width
|
||||
|
||||
@property
|
||||
def height(self): return self.bbox.height
|
||||
|
||||
@property
|
||||
def x0(self): return self.bbox.x0
|
||||
|
||||
@property
|
||||
def x1(self): return self.bbox.x1
|
||||
|
||||
@property
|
||||
def y0(self): return self.bbox.y0
|
||||
|
||||
@property
|
||||
def y1(self): return self.bbox.y1
|
||||
|
||||
def is_empty(self):
|
||||
return self.width <= 0 or self.height <= 0
|
||||
|
||||
def is_hoverlap(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
return obj.x0 <= self.x1 and self.x0 <= obj.x1
|
||||
|
||||
def hdistance(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
if self.is_hoverlap(obj):
|
||||
return 0
|
||||
else:
|
||||
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
|
||||
|
||||
def hoverlap(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
if self.is_hoverlap(obj):
|
||||
return min(abs(self.x0-obj.x1), abs(self.x1-obj.x0))
|
||||
else:
|
||||
return 0
|
||||
|
||||
def is_voverlap(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
return obj.y0 <= self.y1 and self.y0 <= obj.y1
|
||||
|
||||
def vdistance(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
if self.is_voverlap(obj):
|
||||
return 0
|
||||
else:
|
||||
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
|
||||
|
||||
def voverlap(self, obj):
|
||||
assert isinstance(obj, LTComponent), str(type(obj))
|
||||
if self.is_voverlap(obj):
|
||||
return min(abs(self.y0-obj.y1), abs(self.y1-obj.y0))
|
||||
else:
|
||||
return 0
|
||||
|
||||
## LTContainer
|
||||
##
|
||||
class LTContainer(LTComponent):
|
||||
|
||||
def __init__(self, bbox: Bbox, objs: List[LTItem] = []):
|
||||
LTComponent.__init__(self, bbox)
|
||||
self._objs: List[LTItem] = objs
|
||||
return
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._objs)
|
||||
|
||||
def __len__(self) -> int:
|
||||
return len(self._objs)
|
||||
|
||||
def add(self, obj: LTItem):
|
||||
self._objs.append(obj)
|
||||
return
|
||||
|
||||
def extend(self, objs: List[LTItem]):
|
||||
for obj in objs:
|
||||
self.add(obj)
|
||||
return
|
||||
|
||||
|
||||
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,90 @@
|
||||
from pdfmajor.utils import mult_matrix, translate_matrix, Bbox, apply_matrix_pt, isnumber
|
||||
from ....state import PDFTextState, PDFColor
|
||||
|
||||
def decode_text_seq(seq: bytearray, ctm: tuple, textstate: PDFTextState, color: PDFColor):
|
||||
textstate.matrix = mult_matrix(textstate.matrix, ctm)
|
||||
textstate.scaling = textstate.scaling * .01
|
||||
textstate.charspace = textstate.charspace * textstate.scaling
|
||||
textstate.wordspace = textstate.wordspace * textstate.scaling
|
||||
|
||||
if textstate.font.is_multibyte():
|
||||
textstate.wordspace = 0
|
||||
dxscale = .001 * textstate.fontsize * textstate.scaling
|
||||
|
||||
if textstate.font.is_vertical():
|
||||
return __render_string_along(1,
|
||||
seq, textstate, dxscale, color
|
||||
)
|
||||
else:
|
||||
return __render_string_along(0,
|
||||
seq, textstate, dxscale, color
|
||||
)
|
||||
|
||||
def __render_string_along(idx: int, seq: bytearray,
|
||||
textstate: PDFTextState, dxscale: float, color: PDFColor):
|
||||
needcharspace = False
|
||||
char_meta_datas = []
|
||||
for obj in seq:
|
||||
if isnumber(obj):
|
||||
textstate.linematrix[idx] -= obj*dxscale
|
||||
needcharspace = True
|
||||
else:
|
||||
for cid in textstate.font.decode(obj):
|
||||
if needcharspace:
|
||||
textstate.linematrix[idx] += textstate.charspace
|
||||
|
||||
text = textstate.font.to_unichr(cid)
|
||||
assert isinstance(text, str), str(type(text))
|
||||
|
||||
matrix = translate_matrix(textstate.matrix, textstate.linematrix)
|
||||
adv, bbox = __compute_char_bbox(matrix, cid, textstate)
|
||||
textstate.linematrix[idx] += adv
|
||||
|
||||
char_meta_datas.append(
|
||||
( text, bbox )
|
||||
)
|
||||
if cid == 32 and textstate.wordspace:
|
||||
textstate.linematrix[idx] += textstate.wordspace
|
||||
needcharspace = True
|
||||
|
||||
return (char_meta_datas, textstate, color)
|
||||
|
||||
def __compute_char_bbox(matrix, char_id: int, textstate: PDFTextState):
|
||||
font = textstate.font
|
||||
fontsize = textstate.fontsize
|
||||
rise = textstate.rise
|
||||
scaling = textstate.scaling
|
||||
|
||||
textwidth = font.char_width(char_id)
|
||||
textdisp = font.char_disp(char_id)
|
||||
adv = textwidth * fontsize * scaling
|
||||
|
||||
# compute the boundary rectangle.
|
||||
if font.is_vertical():
|
||||
# vertical
|
||||
width = font.get_width() * fontsize
|
||||
(vx, vy) = textdisp
|
||||
if vx is None:
|
||||
vx = width * 0.5
|
||||
else:
|
||||
vx = vx * fontsize * .001
|
||||
vy = (1000 - vy) * fontsize * .001
|
||||
tx = -vx
|
||||
ty = vy + rise
|
||||
bll = (tx, ty+adv)
|
||||
bur = (tx+width, ty)
|
||||
else:
|
||||
# horizontal
|
||||
height = font.get_height() * fontsize
|
||||
descent = font.get_descent() * fontsize
|
||||
ty = descent + rise
|
||||
bll = (0, ty)
|
||||
bur = (adv, ty+height)
|
||||
(x0, y0) = apply_matrix_pt(matrix, bll)
|
||||
(x1, y1) = apply_matrix_pt(matrix, bur)
|
||||
if x1 < x0:
|
||||
(x0, x1) = (x1, x0)
|
||||
if y1 < y0:
|
||||
(y0, y1) = (y1, y0)
|
||||
|
||||
return (adv, ((x0, y0), (x1, y1)))
|
||||
@@ -1,35 +0,0 @@
|
||||
|
||||
from .PSStackParser import LIT
|
||||
|
||||
## Constants
|
||||
##
|
||||
LITERAL_PDF = LIT("PDF")
|
||||
LITERAL_TEXT = LIT("Text")
|
||||
LITERAL_FONT = LIT("Font")
|
||||
LITERAL_FORM = LIT("Form")
|
||||
LITERAL_IMAGE = LIT("Image")
|
||||
|
||||
COMMON_FONT_ATTRIBUTES = [
|
||||
"Type",
|
||||
"FontName",
|
||||
"FontFamily",
|
||||
"FontStretch",
|
||||
"FontWeight",
|
||||
"Flags",
|
||||
"FontBBox",
|
||||
"ItalicAngle",
|
||||
"Ascent",
|
||||
"Descent",
|
||||
"Leading",
|
||||
"CapHeight",
|
||||
"XHeight",
|
||||
"StemV",
|
||||
"StemH",
|
||||
"AvgWidth",
|
||||
"MaxWidth",
|
||||
"MissingWidth",
|
||||
# "FontFile",
|
||||
# "FontFile2",
|
||||
# "FontFile3",
|
||||
"Charset",
|
||||
]
|
||||
@@ -0,0 +1,50 @@
|
||||
from pdfmajor.parser.PDFParser import PDFStream
|
||||
from pdfmajor.parser.PDFStream import list_value, dict_value, resolve1
|
||||
from pdfmajor.parser.PDFStream.PDFObjRef import PDFObjRef
|
||||
from pdfmajor.parser.PSStackParser import literal_name
|
||||
from pdfmajor.parser.constants import LITERAL_PDF, LITERAL_TEXT
|
||||
|
||||
from .commands import PDFColorSpace, PREDEFINED_COLORSPACE
|
||||
from .commands import PDFStateStack, get_font
|
||||
|
||||
def get_colorspace(spec):
|
||||
if isinstance(spec, list):
|
||||
name = literal_name(spec[0])
|
||||
else:
|
||||
name = literal_name(spec)
|
||||
if name == 'ICCBased' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, PDFStream.validated_stream(spec[1])['N'])
|
||||
elif name == 'DeviceN' and isinstance(spec, list) and 2 <= len(spec):
|
||||
return PDFColorSpace(name, len(list_value(spec[1])))
|
||||
else:
|
||||
return PREDEFINED_COLORSPACE.get(name)
|
||||
|
||||
def get_procset(procs):
|
||||
for proc in procs:
|
||||
if proc is LITERAL_PDF:
|
||||
pass
|
||||
elif proc is LITERAL_TEXT:
|
||||
pass
|
||||
else:
|
||||
#raise PDFResourceError('ProcSet %r is not supported.' % proc)
|
||||
pass
|
||||
return
|
||||
|
||||
def init_resources(state: PDFStateStack, font_cache: dict = {}):
|
||||
for (k, v) in iter(dict_value(state.resources).items()):
|
||||
# log.debug('Resource: %r: %r', k, v)
|
||||
if k == 'Font':
|
||||
for (fontid, spec) in iter(dict_value(v).items()):
|
||||
objid = None
|
||||
if isinstance(spec, PDFObjRef):
|
||||
objid = spec.objid
|
||||
spec = dict_value(spec)
|
||||
state.fontmap[fontid] = get_font(objid, spec, font_cache)
|
||||
elif k == 'ColorSpace':
|
||||
for (csid, spec) in iter(dict_value(v).items()):
|
||||
state.colorspace_map[csid] = get_colorspace(resolve1(spec))
|
||||
elif k == 'ProcSet':
|
||||
get_procset(list_value(v))
|
||||
elif k == 'XObject':
|
||||
for (xobjid, xobjstrm) in iter(dict_value(v).items()):
|
||||
state.xobjmap[xobjid] = xobjstrm
|
||||
Reference in new issue
Block a user