- moved parser files from old interpeter into parser folder

- renamed interpreter_v2 to just interpreter
- removed legacy code
This commit is contained in:
ari committed 2018-12-20 10:41:10 -05:00
1 parent 487f9b6c08
commit 3ba0b95001
81 files changed
+138 -2565

No files matched your search

-36
View File
@@ -1,36 +0,0 @@
import re
import os
from typing import List
from io import BytesIO, StringIO, TextIOWrapper
from tqdm import tqdm
from ..interpreter.PDFPage import PDFPage
from ..interpreter_v2 import PDFPageInterpreter
from ..imagewriter import ImageWriter
from ..utils import set_log_level, get_logger, logging
log = get_logger(__name__)
def extract_items_from_pdf(
input_file_path: str,
maxpages: int = 0,
password: str = None,
caching: bool = True,
check_extractable: bool = True,
pagenos: List[int] = None,
debug_level: int = logging.WARNING,
):
set_log_level(debug_level)
log.debug("Creating resources....")
interpreter = PDFPageInterpreter()
log.debug("Reading pages....")
with open(input_file_path, 'rb') as input_file:
pages = PDFPage.get_pages(input_file, pagenos=pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=check_extractable)
page_waiter = tqdm(enumerate(pages), disable=debug_level > logging.INFO)
for _, page in page_waiter:
page_waiter.set_description("Processing pdf...")
yield interpreter.process_page(page)
page_waiter.close()
-755
View File
@@ -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)
)
)
-668
View File
@@ -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
-103
View File
@@ -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
-53
View File
@@ -1,53 +0,0 @@
from .PDFFont import PDFFont
from ..utils import MATRIX_IDENTITY
## PDFTextState
##
class PDFTextState(object):
def __init__(self):
self.font: PDFFont = None
self.fontsize: int = 0
self.charspace: int = 0
self.wordspace: int = 0
self.scaling: int = 100
self.leading: int = 0
self.render: int = 0
self.rise: int = 0
self.reset()
# self.matrix is set
# self.linematrix is set
return
def __repr__(self):
return ('<PDFTextState: font=%r, fontsize=%r, charspace=%r, wordspace=%r, '
' scaling=%r, leading=%r, render=%r, rise=%r, '
' matrix=%r, linematrix=%r>' %
(self.font, self.fontsize, self.charspace, self.wordspace,
self.scaling, self.leading, self.render, self.rise,
self.matrix, self.linematrix))
def copy(self):
obj = PDFTextState()
obj.font = self.font
obj.fontsize = self.fontsize
obj.charspace = self.charspace
obj.wordspace = self.wordspace
obj.scaling = self.scaling
obj.leading = self.leading
obj.render = self.render
obj.rise = self.rise
obj.matrix = self.matrix
obj.linematrix = self.linematrix
return obj
def reset(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)
-163
View File
@@ -1,163 +0,0 @@
# -*- coding: utf-8 -*-
from .PDFFont import PDFUnicodeNotDefined
from ..utils import isnumber, apply_matrix_pt
from ..utils import Bbox, mult_matrix, translate_matrix
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
def __repr__(self):
return '<PDFDevice>'
def __enter__(self):
return self
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)
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():
self.__render_string_along(1,
seq, textstate, dxscale, ncs, graphicstate
)
else:
self.__render_string_along(0,
seq, textstate, dxscale, ncs, graphicstate
)
def __render_string_along(self, idx: int, seq: bytearray,
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
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 = self.__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
self.render_char_box(char_meta_datas, textstate, graphicstate, ncs)
def __compute_char_bbox(self, 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)))
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
pass
@@ -1,8 +1,8 @@
from pdfmajor.interpreter.PDFPage import PDFPage
from pdfmajor.interpreter.PDFStream import list_value
from pdfmajor.interpreter.PDFContentParser import PDFContentParser
from pdfmajor.interpreter.PSStackParser import PSKeyword
from pdfmajor.interpreter.PSStackParser import keyword_name
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
@@ -1,16 +1,15 @@
from pdfmajor.utils import MATRIX_IDENTITY
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFParser import PDFStream
from pdfmajor.interpreter.PSStackParser import PSEOF
from pdfmajor.interpreter.PSStackParser import PSKeyword
from pdfmajor.interpreter.PSStackParser import keyword_name
from pdfmajor.interpreter.PSStackParser import PSStackParser
from pdfmajor.interpreter.PDFResourceManager import PDFResourceManager
from pdfmajor.interpreter.constants import LITERAL_PDF, LITERAL_TEXT, LITERAL_FONT
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.interpreter.PDFStream import resolve1, list_value, dict_value
from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
from pdfmajor.interpreter.PDFContentParser import PDFContentParser
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
+72
View File
@@ -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,9 +1,9 @@
from typing import List
from pdfmajor.utils import MATRIX_IDENTITY, mult_matrix
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFStream import PDFStream, list_value, dict_value
from pdfmajor.interpreter.constants import LITERAL_FORM, LITERAL_IMAGE
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
@@ -1,7 +1,7 @@
from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY, apply_matrix_pt
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFStream import PDFStream, list_value, dict_value
from pdfmajor.interpreter.constants import LITERAL_FORM, LITERAL_IMAGE
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
@@ -1,4 +1,4 @@
from pdfmajor.interpreter.PDFStream import PDFStream
from pdfmajor.parser.PDFStream import PDFStream
from ._base import PDFItem
class PDFImage(PDFItem):
@@ -2,18 +2,18 @@
from abc import abstractmethod
from io import BytesIO
from pdfmajor.interpreter.EncodingDB import EncodingDB
from pdfmajor.interpreter.PSStackParser import LIT
from pdfmajor.interpreter.PSStackParser import PSLiteral
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFStream import resolve1
from pdfmajor.interpreter.PDFStream import int_value
from pdfmajor.interpreter.PDFStream import num_value
from pdfmajor.interpreter.PDFStream import list_value
from pdfmajor.interpreter.PDFStream import PDFException
from pdfmajor.interpreter.PDFStream import PDFStream
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
from pdfmajor.interpreter.cmapdb import CMapParser
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
##
@@ -1,7 +1,7 @@
## TrueTypeFont
##
import struct
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
from pdfmajor.parser.cmapdb import FileUnicodeMap
class TrueTypeFont(object):
@@ -1,8 +1,8 @@
from pdfmajor.interpreter.PSStackParser import PSStackParser
from pdfmajor.interpreter.PSStackParser import PSEOF, KWD
from pdfmajor.interpreter.PSStackParser import PSLiteral
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.EncodingDB import name2unicode
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
##
@@ -1,9 +1,9 @@
from pdfmajor.utils import get_logger, settings
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFStream import list_value
from pdfmajor.interpreter.PDFStream import dict_value
from pdfmajor.interpreter.PDFStream import resolve1
from pdfmajor.interpreter.constants import LITERAL_FONT
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
@@ -1,14 +1,14 @@
from io import BytesIO
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.PDFStream import int_value
from pdfmajor.interpreter.PDFStream import num_value
from pdfmajor.interpreter.PDFStream import list_value
from pdfmajor.interpreter.PDFStream import dict_value
from pdfmajor.interpreter.PDFStream import PDFStream
from pdfmajor.interpreter.PDFStream import resolve1
from pdfmajor.interpreter.cmapdb import CMap, CMapDB, CMapParser
from pdfmajor.interpreter.cmapdb import FileUnicodeMap
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
@@ -1,5 +1,5 @@
from pdfmajor.utils import Bbox
from pdfmajor.interpreter.PDFStream import PDFStream
from pdfmajor.parser.PDFStream import PDFStream
from ._base import LTComponent
## LTImage
@@ -1,7 +1,7 @@
from typing import List
from pdfmajor.utils import Bbox, Point, matrix2str
from pdfmajor.interpreter.PDFStream import PDFStream
from pdfmajor.parser.PDFStream import PDFStream
from ._base import LTContainer
@@ -1,8 +1,8 @@
from pdfmajor.interpreter.PDFParser import PDFStream
from pdfmajor.interpreter.PDFStream import list_value, dict_value, resolve1
from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
from pdfmajor.interpreter.PSStackParser import literal_name
from pdfmajor.interpreter.constants import LITERAL_PDF, LITERAL_TEXT
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
-72
View File
@@ -1,72 +0,0 @@
from typing import List
from tqdm import tqdm
from pdfmajor.interpreter.PDFPage import PDFPage
from pdfmajor.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
-47
View File
@@ -1,47 +0,0 @@
from ._base import LTComponent
from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox
from ..interpreter.PDFGraphicState import PDFGraphicState
from ..interpreter.PDFFont import PDFFont
from ..interpreter.PDFColorSpace import PDFColorSpace
from ..interpreter.PDFTextState import PDFTextState
## LTChar
##
class LTChar(LTComponent):
def __init__(self,
bbox: Bbox,
char: str,
textstate: PDFTextState,
ncs: PDFColorSpace,
graphicstate: PDFGraphicState
):
self._text = char
self.font = textstate.font
self.textstate = textstate
self.ncs = ncs
self.graphicstate = graphicstate
LTComponent.__init__(self, bbox)
if self.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 fontname(self) -> str:
return self.font.fontname
-68
View File
@@ -1,68 +0,0 @@
from typing import List
from ._base import LTContainer
from .LTChar import LTChar
from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox, Point
from ..interpreter.PDFGraphicState import PDFGraphicState
from ..interpreter.PDFFont import PDFFont
from ..interpreter.PDFColorSpace import PDFColorSpace
from ..interpreter.PDFTextState import PDFTextState
## LTCharBlock
##
class LTCharBlock(LTContainer):
def __init__(self,
chars: list, # [str, List[Point]]
textstate: PDFTextState,
ncs: PDFColorSpace,
graphicstate: PDFGraphicState
):
((x0, y0), (x1, y1)) = chars[0][1]
ltchars = [
LTChar(
bbox=Bbox(x0, y0, x1, y1),
char= chars[0][0],
textstate=textstate,
ncs=ncs,
graphicstate=graphicstate
)
]
for char in chars[1:]:
((nx0, ny0), (nx1, ny1)) = char[1]
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= char[0],
textstate=textstate,
ncs=ncs,
graphicstate=graphicstate
))
LTContainer.__init__(self, Bbox(x0, y0, x1, y1), ltchars)
self.font = textstate.font
self.textstate = textstate
self.ncs = ncs
self.graphicstate = graphicstate
if self.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 fontname(self) -> str:
return self.font.fontname
-45
View File
@@ -1,45 +0,0 @@
from typing import List, Tuple
from ..utils import Bbox
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
from ..interpreter.types import CurvePath
from ._base import LTComponent, Bbox
class LTCurve(LTComponent):
def __init__(self,
linewidth: float,
paths: List[CurvePath],
bbox: Bbox,
stroke: PDFGraphicStateColor,
fill: PDFGraphicStateColor,
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
-18
View File
@@ -1,18 +0,0 @@
from typing import List
from ._base import LTContainer
from ..utils import Bbox, Point, matrix2str
## LTFigure
##
class LTFigure(LTContainer):
def __init__(self, name: str, pts: List[Point], matrix):
LTContainer.__init__(self, Bbox.from_points(pts))
self.name = name
self.matrix = matrix
def __repr__(self):
return ('<%s(%s) %s matrix=%s>' %
(self.__class__.__name__, self.name,
str(self.bbox), matrix2str(self.matrix)))
-25
View File
@@ -1,25 +0,0 @@
from ..utils import Bbox
from ..interpreter.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))
-20
View File
@@ -1,20 +0,0 @@
from ._base import LTContainer
from ..utils import Bbox
class LTPage(LTContainer):
"""A class reprensting an extracted page from the pdf
Arguments:
pageid {int} -- page number
bbox {Bbox} -- Boundary Box
rotate {float} -- rotation of the page (default: {0})
"""
def __init__(self, pageid: int, bbox: Bbox, rotate: float=0):
LTContainer.__init__(self, bbox)
self.pageid = pageid
self.rotate = rotate
def __repr__(self):
return ('<%s(%r) %s rotate=%r>' %
(self.__class__.__name__, self.pageid,
str(self.bbox), self.rotate))
-189
View File
@@ -1,189 +0,0 @@
import logging
import re
import io
from abc import abstractmethod
from typing import Tuple, List
from ._base import LTItem
from .LTPage import LTPage
from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
from .LTFigure import LTFigure
from .LTImage import LTImage
from .LTChar import LTChar
from .LTCharBlock import LTCharBlock
from ..utils import Point, Bbox, INF
from ..utils import apply_matrix_pt, mult_matrix
from ..interpreter.PDFFont import PDFUnicodeNotDefined
from ..interpreter.PDFResourceManager import PDFResourceManager
from ..interpreter.PDFGraphicState import PDFGraphicState, PDFGraphicStateColor
from ..interpreter.PDFdevice import PDFTextDevice
from ..interpreter.PDFTextState import PDFTextState
from ..interpreter.PDFColorSpace import PDFColorSpace
from ..interpreter.PDFPage import PDFPage
from ..interpreter.PDFStream import PDFStream
from ..interpreter.types import CurvePath
log = logging.getLogger(__name__)
## PDFLayoutAnalyzer
##
class PDFLayoutAnalyzer(PDFTextDevice):
def __init__(self, rsrcmgr: PDFResourceManager, pageno: int=1):
PDFTextDevice.__init__(self, rsrcmgr)
self.pageno = pageno
self._stack = []
return
def begin_page(self, page: PDFPage, ctm: Tuple[float]):
(x0, y0, x1, y1) = page.mediabox
(x0, y0) = apply_matrix_pt(ctm, (x0, y0))
(x1, y1) = apply_matrix_pt(ctm, (x1, y1))
self.cur_item = LTPage(
self.pageno,
Bbox( 0, 0, abs(x0-x1), abs(y0-y1) )
)
return
def end_page(self, page: PDFPage):
assert not self._stack, str(len(self._stack))
assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
# self.cur_item.analyze()
self.pageno += 1
self.receive_layout(self.cur_item)
return
def begin_figure(self, name: str, bbox: List[Point], matrix: List[Point]):
self._stack.append(self.cur_item)
matrix = mult_matrix(matrix, self.ctm)
(x, y, w, h) = bbox
self.cur_item = LTFigure(name, [
apply_matrix_pt(matrix, (p, q))
for (p, q) in ((x, y), (x+w, y), (x, y+h), (x+w, y+h))
], matrix)
def end_figure(self, name: str):
fig = self.cur_item
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
self.cur_item = self._stack.pop()
self.cur_item.add(fig)
return
def render_image(self, name: str, stream: PDFStream):
assert isinstance(self.cur_item, LTFigure), str(type(self.cur_item))
item = LTImage(
name,
stream,
Bbox(
self.cur_item.x0, self.cur_item.y0,
self.cur_item.x1, self.cur_item.y1
)
)
self.cur_item.add(item)
return
def paint_path(self, 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(self.ctm, (pt0.x, pt0.y))
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
if x0 == x1:
self.cur_item.add(LTVerticalLine(
linewidth=gstate.linewidth,
paths=paths,
bbox=Bbox( x0, y0, x1, y1 ),
stroke=gstate.scolor,
fill=gstate.ncolor,
evenodd=evenodd,
))
elif y0 == y1:
self.cur_item.add(LTHorizontalLine(
linewidth=gstate.linewidth,
paths=paths,
bbox=Bbox( x0, y0, x1, y1 ),
stroke=gstate.scolor,
fill=gstate.ncolor,
evenodd=evenodd,
))
else:
self.cur_item.add(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(self.ctm, (pt0.x, pt0.y))
(x1, y1) = apply_matrix_pt(self.ctm, (pt1.x, pt1.y))
(x2, y2) = apply_matrix_pt(self.ctm, (pt2.x, pt2.y))
(x3, y3) = apply_matrix_pt(self.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)):
self.cur_item.add(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)
self.cur_item.add(LTCurve(
linewidth=gstate.linewidth,
paths=paths,
bbox=Bbox(x0,y0,x1,y1),
evenodd=evenodd,
stroke=gstate.scolor,
fill=gstate.ncolor
))
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
self.cur_item.add(LTCharBlock(
chars=chars,
textstate=textstate,
ncs=ncs,
graphicstate=graphicstate
))
@abstractmethod
def receive_layout(self, ltpage: LTPage):
raise NotImplementedError
-115
View File
@@ -1,115 +0,0 @@
from typing import List
from ..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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View File
Whitespace-only changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
+5 -6
View File
@@ -1,15 +1,14 @@
import os
# from pdfmajor.extractor import extract_items_from_pdf
from pdfmajor.interpreter_v2 import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter_v2 import PageInterpreter
from pdfmajor.interpreter_v2.commands import LTItem
from pdfmajor.interpreter_v2.commands import LTCharBlock, LTChar
from pdfmajor.interpreter import LTCharBlock, PDFInterpreter, logging
from pdfmajor.interpreter import PageInterpreter
from pdfmajor.interpreter.commands import LTItem
from pdfmajor.interpreter.commands import LTCharBlock, LTChar
CUR_PATH = os.path.dirname(os.path.dirname(__file__))
FILE_NAME = os.path.join(
CUR_PATH,
"../" if CUR_PATH == "" else CUR_PATH,
"samples/pdf/tables.pdf"
)