diff --git a/pdfmajor/extractor/__init__.py b/pdfmajor/extractor/__init__.py
index a77bce9..50e423d 100644
--- a/pdfmajor/extractor/__init__.py
+++ b/pdfmajor/extractor/__init__.py
@@ -9,6 +9,7 @@ from tqdm import tqdm
from ..interpreter.PDFResourceManager import PDFResourceManager
from ..interpreter.PDFPage import PDFPage
from ..interpreter.PDFPageInterpreter import PDFPageInterpreter
+from ..interpreter_v2 import PDFPageInterpreter as PDFPageInterpreterV2
from ..imagewriter import ImageWriter
from ..utils import set_log_level, get_logger, logging
from .PDFExtractor import PDFExctractor
@@ -43,4 +44,29 @@ def extract_items_from_pdf(
page_waiter.set_description("Processing pdf...")
yield interpreter.process_page(page)
page_waiter.close()
- device.close()
\ No newline at end of file
+ device.close()
+
+
+def v2_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....")
+ rsrcmgr = PDFResourceManager(caching=caching)
+ interpreter = PDFPageInterpreterV2(rsrcmgr)
+ 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()
\ No newline at end of file
diff --git a/pdfmajor/interpreter/PSStackParser/__init__.py b/pdfmajor/interpreter/PSStackParser/__init__.py
index e78a18d..d41e5fe 100644
--- a/pdfmajor/interpreter/PSStackParser/__init__.py
+++ b/pdfmajor/interpreter/PSStackParser/__init__.py
@@ -95,6 +95,14 @@ class PSStackParser(PSBaseParser):
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.
diff --git a/pdfmajor/interpreter_v2/__init__.py b/pdfmajor/interpreter_v2/__init__.py
new file mode 100644
index 0000000..5bdd975
--- /dev/null
+++ b/pdfmajor/interpreter_v2/__init__.py
@@ -0,0 +1,124 @@
+from pdfmajor.utils import MATRIX_IDENTITY
+from pdfmajor.interpreter.PSStackParser import literal_name
+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.PDFParser import PDFStream
+
+from pdfmajor.interpreter.PDFStream import resolve1, list_value, dict_value
+from pdfmajor.interpreter.PDFStream.PDFObjRef import PDFObjRef
+from pdfmajor.interpreter.PDFContentParser import PDFContentParser
+
+from .commands.state import PDFStateStack, PDFColorSpace, PREDEFINED_COLORSPACE
+from .commands import PDFCommands
+from .commands.state.PDFItem import PDFXObject, PDFImage, PDFItem, PDFShape, PDFText
+
+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)
+
+class PDFPageInterpreter:
+
+ class PDFInterpreterError(Exception): pass
+
+ def __init__(self, rsrcmgr):
+ self.rsrcmgr = rsrcmgr
+ self.state: PDFStateStack = PDFStateStack()
+
+ # init_resources(resources):
+ # Prepare the fonts and XObjects listed in the Resource attribute.
+ def init_resources(self, resources):
+ self.state.resources = resources
+ for (k, v) in iter(dict_value(self.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)
+ self.state.fontmap[fontid] = self.rsrcmgr.get_font(objid, spec)
+ elif k == 'ColorSpace':
+ for (csid, spec) in iter(dict_value(v).items()):
+ self.state.colorspace_map[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.state.xobjmap[xobjid] = xobjstrm
+
+
+ # init_state(ctm)
+ # Initialize the text and graphic states for rendering a page.
+ def init_state(self, ctm):
+ self.state = PDFStateStack()
+ self.state.t_matrix = ctm
+ # 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
+
+ # render_contents(resources, streams, ctm)
+ # Render the content streams.
+ # This method may be called recursively.
+ def render_contents(self, resources, streams, ctm=MATRIX_IDENTITY):
+ self.init_state(ctm)
+ self.init_resources(resources)
+ return self.execute(list_value(streams))
+
+ def process_page(self, 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]
+ return self.render_contents(page.resources, page.contents, ctm=ctm)
+
+ def execute(self, streams):
+ parser = PDFContentParser(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_items:
+ if isinstance(complete_item, PDFXObject):
+ interpeter = self.__class__(self.rsrcmgr)
+ for item in interpeter.render_contents(
+ streams=[complete_item.stream],
+ resources=complete_item.resources,
+ ctm=complete_item.ctm
+ ):
+ complete_item.children.append(item)
+ yield complete_item
+ self.state.complete_items = []
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/PDFCommands.py b/pdfmajor/interpreter_v2/commands/PDFCommands.py
new file mode 100644
index 0000000..d7801ed
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/PDFCommands.py
@@ -0,0 +1,30 @@
+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 ..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
+
+
diff --git a/pdfmajor/interpreter_v2/commands/__init__.py b/pdfmajor/interpreter_v2/commands/__init__.py
new file mode 100644
index 0000000..23e16ae
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/__init__.py
@@ -0,0 +1,4 @@
+from .commands import PDFCommands
+from .state import PDFStateStack, PDFGraphicState, PDFTextState
+from .state.PDFGraphicState import PDFColor, PDFColorSpace, PREDEFINED_COLORSPACE
+from .state.PDFTextState import PDFFont
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/commands.py b/pdfmajor/interpreter_v2/commands/commands.py
new file mode 100644
index 0000000..3c3ade6
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/commands.py
@@ -0,0 +1,472 @@
+from pdfmajor.utils import mult_matrix, MATRIX_IDENTITY
+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 .PDFCommands import PDFCommands
+from .state import PDFStateStack, PDFGraphicState
+from .state.Curves import CurveMethod, CurvePath, CurvePoint
+from .state.PDFItem import PDFImage, PDFShape, PDFText, PDFXObject
+
+# 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, tag) -> PDFStateStack:
+ # self.device.do_tag(tag)
+ return stack
+
+@PDFCommands.add('BMC', 'BDC')
+def do_BMC(stack: PDFStateStack, tag) -> PDFStateStack:
+ # self.device.begin_tag(tag)
+ return stack
+
+@PDFCommands.add('EMC')
+def do_EMC(stack: PDFStateStack, tag) -> 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_items.append(PDFShape(
+ 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_items.append(PDFShape(
+ 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_items.append(PDFShape(
+ 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_items.append(PDFShape(
+ 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_items.append(PDFText.make(
+ seq,
+ stack.t_matrix,
+ stack.text.copy(),
+ stack.graphics.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_items.append(PDFImage(obj))
+ 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_items.append(PDFXObject(
+ xobj, bbox, resources, mult_matrix(stack.t_matrix, matrix)
+ ))
+ elif subtype is LITERAL_IMAGE and 'Width' in xobj and 'Height' in xobj:
+ stack.complete_items.append(PDFImage(obj))
+ else:
+ # unsupported xobject type.
+ pass
+ return stack
+
diff --git a/pdfmajor/interpreter_v2/commands/state/Curves.py b/pdfmajor/interpreter_v2/commands/state/Curves.py
new file mode 100644
index 0000000..ba9f9d5
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/Curves.py
@@ -0,0 +1,27 @@
+from enum import Enum
+from typing import List
+
+class CurvePoint:
+ def __init__(self, x: float, y: float):
+ self.x = x
+ self.y = y
+
+ def __repr__(self) -> str:
+ return f''
+
+class CurveMethod(Enum):
+ MOVE_TO='m'
+ LINE_TO='l'
+ CURVE_BOTH_TO='c'
+ CURVE_NEXT_TO='v'
+ CURVE_FIRST_TO='y'
+ CLOSE_PATH='h'
+
+class CurvePath:
+ METHOD = CurveMethod
+ def __init__(self, method: CurveMethod, *points: List[CurvePoint]):
+ self.method = method
+ self.points = points
+
+ def __repr__(self) -> str:
+ return f''
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColor.py b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColor.py
new file mode 100644
index 0000000..c773786
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColor.py
@@ -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""
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColorSpace.py b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColorSpace.py
new file mode 100644
index 0000000..5bac22c
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/PDFColorSpace.py
@@ -0,0 +1,27 @@
+from collections import OrderedDict
+
+class PDFColorSpace(object):
+
+ def __init__(self, name: str, ncomponents: int):
+ self.name: str = name
+ self.ncomponents: int = ncomponents
+ return
+
+ def __repr__(self):
+ return '' % (self.name, self.ncomponents)
+
+
+PREDEFINED_COLORSPACE = OrderedDict()
+
+for (name, n) in [
+ ('DeviceGray', 1), # default value first
+ ('CalRGB', 3),
+ ('CalGray', 1),
+ ('Lab', 3),
+ ('DeviceRGB', 3),
+ ('DeviceCMYK', 4),
+ ('Separation', 1),
+ ('Indexed', 1),
+ ('Pattern', 1),
+]:
+ PREDEFINED_COLORSPACE[name]=PDFColorSpace(name, n)
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/__init__.py b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/__init__.py
new file mode 100644
index 0000000..94de01f
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFGraphicState/__init__.py
@@ -0,0 +1,48 @@
+from .PDFColor import PDFColor, PDFColorSpace
+
+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 ('' %
+ (
+ self.linewidth, self.linecap, self.linejoin,
+ self.miterlimit, self.dash, self.intent, self.flatness,
+ str(self.scolor), str(self.ncolor)
+ )
+ )
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFImage.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFImage.py
new file mode 100644
index 0000000..f9e4f98
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFImage.py
@@ -0,0 +1,6 @@
+from pdfmajor.interpreter.PDFStream import PDFStream
+from ._base import PDFItem
+
+class PDFImage(PDFItem):
+ def __init__(self, stream: PDFStream):
+ self.stream = stream
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFShape.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFShape.py
new file mode 100644
index 0000000..27ac3f4
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFShape.py
@@ -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
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFText.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFText.py
new file mode 100644
index 0000000..6b5489a
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFText.py
@@ -0,0 +1,116 @@
+from typing import List
+
+from pdfmajor.utils import mult_matrix, isnumber, apply_matrix_pt, translate_matrix
+from pdfmajor.utils import Bbox
+
+from ..PDFTextState import PDFTextState
+from ..PDFGraphicState import PDFGraphicState, PDFColorSpace
+from ._base import PDFItem
+
+
+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 f""
+
+class PDFText(PDFItem):
+ def __init__(self, chars: List[Char], textstate: PDFTextState, graphicstate: PDFGraphicState):
+ self.chars = chars
+ self.tstate = textstate
+ self.gstate = graphicstate
+
+ @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():
+ 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):
+ 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)))
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFXObject.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFXObject.py
new file mode 100644
index 0000000..2a8cabc
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/PDFXObject.py
@@ -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 = []
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/TextDecoder.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/TextDecoder.py
new file mode 100644
index 0000000..e67fd72
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/TextDecoder.py
@@ -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)))
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/__init__.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/__init__.py
new file mode 100644
index 0000000..64cd126
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/__init__.py
@@ -0,0 +1,5 @@
+from ._base import PDFItem
+from .PDFShape import PDFShape
+from .PDFImage import PDFImage
+from .PDFText import PDFText
+from .PDFXObject import PDFXObject
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFItem/_base.py b/pdfmajor/interpreter_v2/commands/state/PDFItem/_base.py
new file mode 100644
index 0000000..9dae6e6
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFItem/_base.py
@@ -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()
+ ])}/>"""
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFTextState/PDFFont.py b/pdfmajor/interpreter_v2/commands/state/PDFTextState/PDFFont.py
new file mode 100644
index 0000000..7369eb7
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFTextState/PDFFont.py
@@ -0,0 +1,755 @@
+
+import sys
+import struct
+from io import BytesIO
+from abc import abstractmethod
+
+from pdfmajor.interpreter.cmapdb import CMapDB
+from pdfmajor.interpreter.cmapdb import CMapParser
+from pdfmajor.interpreter.cmapdb import FileUnicodeMap
+from pdfmajor.interpreter.cmapdb import CMap
+from pdfmajor.interpreter.EncodingDB import EncodingDB, name2unicode
+from pdfmajor.interpreter.PSStackParser import PSStackParser
+from pdfmajor.interpreter.PSStackParser import PSEOF
+from pdfmajor.interpreter.PSStackParser import LIT
+from pdfmajor.interpreter.PSStackParser import KWD
+from pdfmajor.interpreter.PSStackParser import PSLiteral
+from pdfmajor.interpreter.PSStackParser import literal_name
+from pdfmajor.interpreter.PDFStream import PDFException
+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 dict_value
+from pdfmajor.interpreter.PDFStream import PDFStream
+from pdfmajor.interpreter.constants import COMMON_FONT_ATTRIBUTES
+from pdfmajor.utils import settings, FONT_METRICS, apply_matrix_norm, nunpack, 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]
+
+
+## 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 '' % 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 ''
+
+ 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 '' % self.basefont
+
+
+# PDFTrueTypeFont
+class PDFTrueTypeFont(PDFType1Font):
+
+ def __repr__(self):
+ return '' % 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 ''
+
+
+# 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 '' % (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))
diff --git a/pdfmajor/interpreter_v2/commands/state/PDFTextState/__init__.py b/pdfmajor/interpreter_v2/commands/state/PDFTextState/__init__.py
new file mode 100644
index 0000000..6c93e2a
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/PDFTextState/__init__.py
@@ -0,0 +1,48 @@
+from .PDFFont import PDFFont
+from pdfmajor.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.matrix = MATRIX_IDENTITY
+ self.linematrix = [0, 0]
+
+ def __repr__(self):
+ return ('' %
+ (self.font, self.fontsize, self.charspace, self.wordspace,
+ self.scaling, self.leading, self.render, self.rise,
+ self.matrix, self.linematrix))
+
+ def copy(self):
+ obj = self.__class__()
+ 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_matrices(self):
+ self.matrix = MATRIX_IDENTITY
+ self.linematrix = [0, 0]
+
+ @property
+ def is_upright(self):
+ return (0 < self.matrix[:,0].prod()*self.scaling and self.matrix[0,1:].prod() <= 0)
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/__init__.py b/pdfmajor/interpreter_v2/commands/state/__init__.py
new file mode 100644
index 0000000..f371f30
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/__init__.py
@@ -0,0 +1,32 @@
+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
+
+from .Curves import CurvePath
+from .PDFItem import PDFItem, PDFImage, PDFShape, PDFXObject, PDFText
+
+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_items : List[PDFItem] = []
+ 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
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTChar.py b/pdfmajor/interpreter_v2/commands/state/layout/LTChar.py
new file mode 100644
index 0000000..9d10980
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTChar.py
@@ -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
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTCharBlock.py b/pdfmajor/interpreter_v2/commands/state/layout/LTCharBlock.py
new file mode 100644
index 0000000..a58c4aa
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTCharBlock.py
@@ -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
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTCurves.py b/pdfmajor/interpreter_v2/commands/state/layout/LTCurves.py
new file mode 100644
index 0000000..afb61f5
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTCurves.py
@@ -0,0 +1,45 @@
+
+from typing import List, Tuple
+
+from pdfmajor.utils import Bbox
+from ..state.PDFGraphicState import PDFGraphicStateColor
+from ..state.Curves 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
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTFigure.py b/pdfmajor/interpreter_v2/commands/state/layout/LTFigure.py
new file mode 100644
index 0000000..974d3e5
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTFigure.py
@@ -0,0 +1,18 @@
+from typing import List
+
+from ._base import LTContainer
+from pdfmajor.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)))
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTImage.py b/pdfmajor/interpreter_v2/commands/state/layout/LTImage.py
new file mode 100644
index 0000000..c7ea194
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTImage.py
@@ -0,0 +1,25 @@
+from pdfmajor.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))
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/LTPage.py b/pdfmajor/interpreter_v2/commands/state/layout/LTPage.py
new file mode 100644
index 0000000..11ea8b1
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/LTPage.py
@@ -0,0 +1,20 @@
+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))
\ No newline at end of file
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/PageLayoutAnalyzer.py b/pdfmajor/interpreter_v2/commands/state/layout/PageLayoutAnalyzer.py
new file mode 100644
index 0000000..c2d0a8a
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/PageLayoutAnalyzer.py
@@ -0,0 +1,178 @@
+import logging
+import re
+import io
+
+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 pdfmajor.utils import Point, Bbox, INF
+from pdfmajor.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 PDFPageLayoutAnalyzer:
+
+
+ def __init__(self, page: PDFPage, ctm: Tuple[float], pageno: int=1):
+ self.page = page
+ self.pageno = pageno
+ self._stack = []
+ self.ctm = ctm
+ (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) )
+ )
+
+ def done(self):
+ assert not self._stack, str(len(self._stack))
+ assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
+ return self.cur_item
+
+ 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
+ ))
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/__init__.py b/pdfmajor/interpreter_v2/commands/state/layout/__init__.py
new file mode 100644
index 0000000..854277a
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/__init__.py
@@ -0,0 +1,107 @@
+from typing import List
+from pdfmajor.utils import apply_matrix_pt, INF, Bbox
+
+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, LTHorizontalLine, LTRect, LTVerticalLine
+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_figure(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
+ )
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/_base.py b/pdfmajor/interpreter_v2/commands/state/layout/_base.py
new file mode 100644
index 0000000..76c3c1d
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/_base.py
@@ -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
+
+
+
+
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/utils/__init__.py b/pdfmajor/interpreter_v2/commands/state/layout/utils/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/pdfmajor/interpreter_v2/commands/state/layout/utils/textdecoder.py b/pdfmajor/interpreter_v2/commands/state/layout/utils/textdecoder.py
new file mode 100644
index 0000000..870a7c0
--- /dev/null
+++ b/pdfmajor/interpreter_v2/commands/state/layout/utils/textdecoder.py
@@ -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.from_points(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)))
\ No newline at end of file
diff --git a/pdfmajor/layouts/LTCharBlock.py b/pdfmajor/layouts/LTCharBlock.py
index f49520a..0b87736 100644
--- a/pdfmajor/layouts/LTCharBlock.py
+++ b/pdfmajor/layouts/LTCharBlock.py
@@ -14,7 +14,7 @@ from ..interpreter.PDFTextState import PDFTextState
class LTCharBlock(LTContainer):
def __init__(self,
- chars: List[Point],
+ chars: list, # [str, List[Point]]
textstate: PDFTextState,
ncs: PDFColorSpace,
graphicstate: PDFGraphicState
diff --git a/pdfmajor/layouts/PDFLayoutMaker.py b/pdfmajor/layouts/PDFLayoutMaker.py
new file mode 100644
index 0000000..97bae9a
--- /dev/null
+++ b/pdfmajor/layouts/PDFLayoutMaker.py
@@ -0,0 +1,178 @@
+import logging
+import re
+import io
+
+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 PDFPageLayoutAnalyzer:
+
+
+ def __init__(self, page: PDFPage, ctm: Tuple[float], pageno: int=1):
+ self.page = page
+ self.pageno = pageno
+ self._stack = []
+ self.ctm = ctm
+ (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) )
+ )
+
+ def done(self):
+ assert not self._stack, str(len(self._stack))
+ assert isinstance(self.cur_item, LTPage), str(type(self.cur_item))
+ return self.cur_item
+
+ 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
+ ))
diff --git a/tests/pdfmajor b/tests/pdfmajor
new file mode 120000
index 0000000..0b558f8
--- /dev/null
+++ b/tests/pdfmajor
@@ -0,0 +1 @@
+../pdfmajor
\ No newline at end of file
diff --git a/tests/test_extractor.py b/tests/test_extractor.py
new file mode 100644
index 0000000..a8325a4
--- /dev/null
+++ b/tests/test_extractor.py
@@ -0,0 +1,19 @@
+import os
+
+from pdfmajor.extractor import v2_extract_items_from_pdf
+from pdfmajor.interpreter_v2 import PDFText
+
+CUR_PATH = os.path.dirname(os.path.dirname(__file__))
+CUR_PATH = os.path.dirname(os.getcwd())
+
+FILE_NAME = os.path.join(
+ CUR_PATH,
+ "samples/pdf/fonts.pdf"
+)
+
+for page in v2_extract_items_from_pdf(FILE_NAME):
+ for item in page:
+ if isinstance(item, PDFText):
+ print(item.chars)
+ else:
+ print(item)
\ No newline at end of file