diff --git a/.gitignore b/.gitignore
index 4873f85..c44034b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -8,4 +8,6 @@ docs/_build
tests/*.xml
tests/*.txt
.idea/
-.vscode
\ No newline at end of file
+.vscode
+samples/output
+samples/pdfmajor
\ No newline at end of file
diff --git a/pdfmajor/converters/HTMLConverter.py b/pdfmajor/converters/HTMLConverter.py
index 57c8421..62e0ee6 100644
--- a/pdfmajor/converters/HTMLConverter.py
+++ b/pdfmajor/converters/HTMLConverter.py
@@ -6,7 +6,7 @@ from contextlib import contextmanager
from ..layouts import LTPage
from ..layouts import LTItem
-from ..layouts import LTCurve
+from ..layouts import LTCurve, LTLine, LTRect, CurvePath
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar
@@ -127,10 +127,43 @@ class HTMLConverter(PDFConverter):
'bottom': f'{item.y0}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
- "border-color": get_color(item.stroke),
- "background-color": get_color(item.fill),
}
- self.place_elm_close('div', {'class': 'curve'}, css)
+ if isinstance(item, LTRect):
+ pass
+ # self.place_elm_close('div', {'class': 'curve rect'}, {**css,
+ # "border-color": get_color(item.stroke),
+ # "background-color": get_color(item.fill),
+ # })
+ elif isinstance(item, LTLine):
+ self.place_elm_close('div', {'class': 'curve line'}, {**css,
+ "background-color": get_color(item.fill),
+ "border-color": get_color(item.stroke),
+ })
+ else:
+ path = ""
+
+
+ dx = (item.width/item.height)/(item.x1-item.x0)
+ dy = 1.0/(item.y1-item.y0)
+
+ for p in item.paths:
+ if p.method == CurvePath.METHOD.MOVE_TO:
+ path += f'M{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {(item.height- (p.y-item.y0) )*dy}" for p in p.points )} '
+ if p.method == CurvePath.METHOD.LINE_TO:
+ path += f'L{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {( item.height - (p.y-item.y0) )*dy}" for p in p.points )} '
+ if p.method == CurvePath.METHOD.CLOSE_PATH:
+ path += 'Z'
+ with self.place_elm_with_child('svg', {
+ 'class': 'curve',
+ "viewBox": f"0 0 {item.width/item.height} 1",
+ 'width': f'{item.width}px',
+ 'height': f'{item.height}px',
+ }, css):
+ self.place_elm_close('path', {
+ "d": path,
+ 'stroke': get_color(item.stroke),
+ 'fill': get_color(item.fill),
+ })
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
@@ -158,7 +191,7 @@ class HTMLConverter(PDFConverter):
with self.place_elm_with_child('span', { 'class': 'char' }, {
'font-size': f'{item.size}px',
'font-family': item.fontname,
- 'color': get_color(item.graphicstate.scolor),
+ 'color': get_color(item.graphicstate.ncolor),
'position': 'absolute',
'left': f'{item.x0}',
'bottom': f'{item.y0}',
diff --git a/pdfmajor/converters/JSONConverter.py b/pdfmajor/converters/JSONConverter.py
index c06c744..620cfb9 100644
--- a/pdfmajor/converters/JSONConverter.py
+++ b/pdfmajor/converters/JSONConverter.py
@@ -138,8 +138,10 @@ class JSONConverter(PDFConverter):
return self.place_elm_close('line', attr)
with self.place_elm_with_child('curve', attr):
- for x, y in item.pts:
- self.place_elm_close('point', { 'x': x, 'y': y })
+ for path in item.paths:
+ with self.place_elm_with_child('path', { "type": path.method.name }):
+ for point in path.points:
+ self.place_elm_close('point', { 'x': point.x, 'y': point.y })
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
diff --git a/pdfmajor/converters/PDFConverter.py b/pdfmajor/converters/PDFConverter.py
index 0f9b592..080cff1 100644
--- a/pdfmajor/converters/PDFConverter.py
+++ b/pdfmajor/converters/PDFConverter.py
@@ -50,7 +50,6 @@ class PDFConverter(PDFLayoutAnalyzer):
log.debug("Reading pages....")
pages = PDFPage.get_pages(input_file, pagenos=pagenos, maxpages=maxpages, password=password, caching=caching, check_extractable=check_extractable)
for idx, page in enumerate(pages):
- print(f"processing pages page #{idx}")
interpreter.process_page(page)
device.close()
return output_file
diff --git a/pdfmajor/converters/XMLConverter.py b/pdfmajor/converters/XMLConverter.py
index feaaa70..a9a0da7 100644
--- a/pdfmajor/converters/XMLConverter.py
+++ b/pdfmajor/converters/XMLConverter.py
@@ -135,8 +135,10 @@ class XMLConverter(PDFConverter):
return self.place_elm_close('line', attr)
with self.place_elm_with_child('curve', attr):
- for x, y in item.pts:
- self.place_elm_close('point', { 'x': x, 'y': y })
+ for path in item.paths:
+ with self.place_elm_with_child('path', { "type": path.method.name }):
+ for point in path.points:
+ self.place_elm_close('point', { 'x': point.x, 'y': point.y })
def receive_layout(self, ltpage: LTPage):
def render(item: LTItem):
diff --git a/pdfmajor/interpreter/PDFColorSpace.py b/pdfmajor/interpreter/PDFColorSpace.py
index 80368ad..0ce2482 100644
--- a/pdfmajor/interpreter/PDFColorSpace.py
+++ b/pdfmajor/interpreter/PDFColorSpace.py
@@ -10,9 +10,9 @@ LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')
class PDFColorSpace(object):
- def __init__(self, name, ncomponents):
- self.name = name
- self.ncomponents = ncomponents
+ def __init__(self, name: str, ncomponents: int):
+ self.name: str = name
+ self.ncomponents: int = ncomponents
return
def __repr__(self):
diff --git a/pdfmajor/interpreter/PDFPage.py b/pdfmajor/interpreter/PDFPage.py
index 95df3c5..e6098ea 100644
--- a/pdfmajor/interpreter/PDFPage.py
+++ b/pdfmajor/interpreter/PDFPage.py
@@ -37,7 +37,7 @@ class PDFPage(object):
beads: a chain that represents natural reading order.
"""
- def __init__(self, doc, pageid, attrs):
+ def __init__(self, doc: PDFDocument, pageid, attrs):
"""Initialize a page object.
doc: a PDFDocument object.
@@ -72,7 +72,7 @@ class PDFPage(object):
INHERITABLE_ATTRS = set(['Resources', 'MediaBox', 'CropBox', 'Rotate'])
@classmethod
- def create_pages(klass, document):
+ def create_pages(cls, document):
def search(obj, parent):
if isinstance(obj, int):
objid = obj
@@ -81,7 +81,7 @@ class PDFPage(object):
objid = obj.objid
tree = dict_value(obj).copy()
for (k, v) in parent.items():
- if k in klass.INHERITABLE_ATTRS and k not in tree:
+ if k in cls.INHERITABLE_ATTRS and k not in tree:
tree[k] = v
tree_type = tree.get('Type')
@@ -96,10 +96,11 @@ class PDFPage(object):
elif tree_type is LITERAL_PAGE:
log.info('Page: %r', tree)
yield (objid, tree)
+
pages = False
if 'Pages' in document.catalog:
for (objid, tree) in search(document.catalog['Pages'], document.catalog):
- yield klass(document, objid, tree)
+ yield cls(document, objid, tree)
pages = True
if not pages:
# fallback when /Pages is missing.
@@ -108,13 +109,13 @@ class PDFPage(object):
try:
obj = document.getobj(objid)
if isinstance(obj, dict) and obj.get('Type') is LITERAL_PAGE:
- yield klass(document, objid, obj)
+ yield cls(document, objid, obj)
except PDFObjectNotFound:
pass
return
@classmethod
- def get_pages(klass, fp,
+ def get_pages(cls, fp,
pagenos=None, maxpages=0, password='',
caching=True, check_extractable=True):
# Create a PDF parser object associated with the file object.
@@ -125,7 +126,7 @@ class PDFPage(object):
if check_extractable and not doc.is_extractable:
raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)
# Process each page contained in the document.
- for (pageno, page) in enumerate(klass.create_pages(doc)):
+ for (pageno, page) in enumerate(cls.create_pages(doc)):
if pagenos and (pageno not in pagenos):
continue
yield page
diff --git a/pdfmajor/interpreter/PDFPageInterpreter.py b/pdfmajor/interpreter/PDFPageInterpreter.py
index ce2847f..ee78321 100644
--- a/pdfmajor/interpreter/PDFPageInterpreter.py
+++ b/pdfmajor/interpreter/PDFPageInterpreter.py
@@ -1,6 +1,8 @@
import logging
import operator
+from typing import List
+
from .PDFColorSpace import PDFColorSpace
from .PDFColorSpace import PREDEFINED_COLORSPACE
@@ -26,6 +28,7 @@ from .PDFContentParser import PDFContentParser
from ..utils import settings, mult_matrix, MATRIX_IDENTITY
from .constants import LITERAL_FORM, LITERAL_IMAGE
+from .types import CurvePoint, CurvePath
log = logging.getLogger(__name__)
@@ -39,6 +42,7 @@ class PDFPageInterpreter(object):
def __init__(self, rsrcmgr, device):
self.rsrcmgr = rsrcmgr
self.device = device
+ self.curpath: List[CurvePath] = []
return
def dup(self):
@@ -180,47 +184,60 @@ class PDFPageInterpreter(object):
return
# moveto
- def do_m(self, x, y):
- self.curpath.append(('m', x, y))
+ 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, y):
- self.curpath.append(('l', x, y))
+ 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, y1, x2, y2, x3, y3):
- self.curpath.append(('c', x1, y1, x2, y2, x3, y3))
+ 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, y2, x3, y3):
- self.curpath.append(('v', x2, y2, x3, y3))
+ 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, y1, x3, y3):
- self.curpath.append(('y', x1, y1, x3, y3))
+ 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(('h',))
+ self.curpath.append(CurvePath(CurvePath.METHOD.CLOSE_PATH))
return
# rectangle
def do_re(self, x, y, w, h):
- self.curpath.append(('m', x, y))
- self.curpath.append(('l', x+w, y))
- self.curpath.append(('l', x+w, y+h))
- self.curpath.append(('l', x, y+h))
- self.curpath.append(('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, False, self.curpath)
+ self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
self.curpath = []
return
@@ -232,7 +249,7 @@ class PDFPageInterpreter(object):
# fill
def do_f(self):
- self.device.paint_path(self.graphicstate, False, self.curpath)
+ self.device.paint_path(self.graphicstate.copy(), False, self.curpath)
self.curpath = []
return
# fill (obsolete)
@@ -240,19 +257,19 @@ class PDFPageInterpreter(object):
# fill-even-odd
def do_f_a(self):
- self.device.paint_path(self.graphicstate, True, self.curpath)
+ 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, False, self.curpath)
+ 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, True, self.curpath)
+ self.device.paint_path(self.graphicstate.copy(), True, self.curpath)
self.curpath = []
return
@@ -343,7 +360,7 @@ class PDFPageInterpreter(object):
if settings.STRICT:
raise PDFInterpreterError('No colorspace specified!')
n = 1
- self.graphicstate.scolor = self.pop(n)
+ self.pop(n)
return
def do_scn(self):
@@ -353,7 +370,7 @@ class PDFPageInterpreter(object):
if settings.STRICT:
raise PDFInterpreterError('No colorspace specified!')
n = 1
- self.graphicstate.ncolor = self.pop(n)
+ self.pop(n)
return
def do_SC(self):
@@ -595,7 +612,6 @@ class PDFPageInterpreter(object):
nargs = func.__code__.co_argcount-1
if nargs:
args = self.pop(nargs)
- log.debug('exec: %s %r', name, args)
if len(args) == nargs:
func(*args)
else:
diff --git a/pdfmajor/interpreter/PDFParser.py b/pdfmajor/interpreter/PDFParser.py
index 65bed41..cdcf219 100644
--- a/pdfmajor/interpreter/PDFParser.py
+++ b/pdfmajor/interpreter/PDFParser.py
@@ -1,6 +1,6 @@
import logging
-from io import BytesIO
+from io import BytesIO, TextIOWrapper
from ..utils import settings
from .PSStackParser import PSStackParser
@@ -42,7 +42,7 @@ class PDFParser(PSStackParser):
"""
- def __init__(self, fp):
+ def __init__(self, fp: TextIOWrapper):
PSStackParser.__init__(self, fp)
self.doc = None
self.fallback = False
diff --git a/pdfmajor/interpreter/PDFResourceManager.py b/pdfmajor/interpreter/PDFResourceManager.py
index c4a0ae5..4a74d4b 100644
--- a/pdfmajor/interpreter/PDFResourceManager.py
+++ b/pdfmajor/interpreter/PDFResourceManager.py
@@ -58,7 +58,7 @@ class PDFResourceManager(object):
raise
return CMap()
- def get_font(self, objid, spec):
+ def get_font(self, objid: int, spec: dict):
if objid and objid in self._cached_fonts:
font = self._cached_fonts[objid]
else:
diff --git a/pdfmajor/interpreter/PDFTextState.py b/pdfmajor/interpreter/PDFTextState.py
index 9830273..6dcbc37 100644
--- a/pdfmajor/interpreter/PDFTextState.py
+++ b/pdfmajor/interpreter/PDFTextState.py
@@ -1,18 +1,20 @@
+from .PDFFont import PDFFont
from ..utils import MATRIX_IDENTITY
+
## PDFTextState
##
class PDFTextState(object):
def __init__(self):
- self.font = None
- self.fontsize = 0
- self.charspace = 0
- self.wordspace = 0
- self.scaling = 100
- self.leading = 0
- self.render = 0
- self.rise = 0
+ 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
diff --git a/pdfmajor/interpreter/PDFdevice.py b/pdfmajor/interpreter/PDFdevice.py
index d2111b9..0e137fd 100644
--- a/pdfmajor/interpreter/PDFdevice.py
+++ b/pdfmajor/interpreter/PDFdevice.py
@@ -7,6 +7,8 @@ from ..utils import Bbox, mult_matrix, translate_matrix
from .PDFResourceManager import PDFResourceManager
from .PDFGraphicState import PDFGraphicState
+from .PDFTextState import PDFTextState
+from .PDFColorSpace import PDFColorSpace
## PDFDevice
##
@@ -68,7 +70,7 @@ class PDFDevice(object):
##
class PDFTextDevice(PDFDevice):
- def render_string(self, textstate, seq, ncs, graphicstate: PDFGraphicState):
+ def render_string(self, textstate: PDFTextState, seq: bytearray, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
matrix = mult_matrix(textstate.matrix, self.ctm)
font = textstate.font
fontsize = textstate.fontsize
@@ -76,60 +78,40 @@ class PDFTextDevice(PDFDevice):
charspace = textstate.charspace * scaling
wordspace = textstate.wordspace * scaling
rise = textstate.rise
+
if font.is_multibyte():
wordspace = 0
dxscale = .001 * fontsize * scaling
- if font.is_vertical():
- textstate.linematrix = self.render_string_vertical(
+
+ if textstate.font.is_vertical():
+ textstate.linematrix = self.__render_string_along(1,
seq, matrix, textstate.linematrix, font, fontsize,
scaling, charspace, wordspace, rise, dxscale, ncs, graphicstate)
else:
- textstate.linematrix = self.render_string_horizontal(
+ textstate.linematrix = self.__render_string_along(0,
seq, matrix, textstate.linematrix, font, fontsize,
scaling, charspace, wordspace, rise, dxscale, ncs, graphicstate)
- return
- def render_string_horizontal(self, seq, matrix, pos,
+ def __render_string_along(self, idx: int, seq: bytearray, matrix: list, pos: tuple,
font, fontsize, scaling, charspace, wordspace,
rise, dxscale, ncs, graphicstate: PDFGraphicState):
- (x, y) = pos
+ pos_copy = [*pos]
needcharspace = False
for obj in seq:
if isnumber(obj):
- x -= obj*dxscale
+ pos_copy[idx] -= obj*dxscale
needcharspace = True
else:
for cid in font.decode(obj):
if needcharspace:
- x += charspace
- x += self.render_char(translate_matrix(matrix, (x, y)),
+ pos_copy[idx] += charspace
+ pos_copy[idx] += self.render_char(translate_matrix(matrix, pos_copy),
font, fontsize, scaling, rise, cid,
ncs, graphicstate)
if cid == 32 and wordspace:
- x += wordspace
+ pos_copy[idx] += wordspace
needcharspace = True
- return (x, y)
-
- def render_string_vertical(self, seq, matrix, pos,
- font, fontsize, scaling, charspace, wordspace,
- rise, dxscale, ncs, graphicstate: PDFGraphicState):
- (x, y) = pos
- needcharspace = False
- for obj in seq:
- if isnumber(obj):
- y -= obj*dxscale
- needcharspace = True
- else:
- for cid in font.decode(obj):
- if needcharspace:
- y += charspace
- y += self.render_char(translate_matrix(matrix, (x, y)),
- font, fontsize, scaling, rise, cid,
- ncs, graphicstate)
- if cid == 32 and wordspace:
- y += wordspace
- needcharspace = True
- return (x, y)
+ return pos_copy
def render_char(self, matrix, font, fontsize, scaling, rise, cid, ncs, graphicstate: PDFGraphicState):
- return 0
+ return NotImplementedError
diff --git a/pdfmajor/interpreter/PSStackParser/PSBaseParser.py b/pdfmajor/interpreter/PSStackParser/PSBaseParser.py
index a206b42..2ce6d92 100644
--- a/pdfmajor/interpreter/PSStackParser/PSBaseParser.py
+++ b/pdfmajor/interpreter/PSStackParser/PSBaseParser.py
@@ -1,6 +1,9 @@
import logging
+
log = logging.getLogger(__name__)
+from io import TextIOWrapper
+from typing import List, Tuple, Union
from ...utils import int2byte
from .exceptions import PSEOF
@@ -8,6 +11,7 @@ from .constants import EOL, NONSPC, KWD, END_LITERAL, LIT, HEX
from .constants import SPC, HEX_PAIR
from .constants import END_NUMBER, END_STRING, END_KEYWORD, END_HEX_STRING
from .constants import OCT_STRING, ESC_STRING, KEYWORD_DICT_BEGIN, KEYWORD_DICT_END
+from .types import PSLiteral
class PSBaseParser(object):
@@ -15,7 +19,7 @@ class PSBaseParser(object):
"""
BUFSIZ = 4096
- def __init__(self, fp):
+ def __init__(self, fp: TextIOWrapper):
self.fp = fp
self.seek(0)
return
@@ -42,20 +46,20 @@ class PSBaseParser(object):
self.fp.seek(pos0)
return
- def seek(self, pos):
+ def seek(self, pos: int):
"""Seeks the parser to the given position.
"""
log.debug('seek: %r', pos)
self.fp.seek(pos)
# reset the status for nextline()
- self.bufpos = pos
- self.buf = b''
- self.charpos = 0
+ self.bufpos: int = pos
+ self.buf: bytes = b''
+ self.charpos: int = 0
# reset the status for nexttoken()
- self._parse1 = self._parse_main
- self._curtoken = b''
- self._curtokenpos = 0
- self._tokens = []
+ self._current_parse_func = self._parse_main
+ self._curtoken: bytes = b''
+ self._curtokenpos: int = 0
+ self._tokens: List[Tuple[int, Union[int, float, bool, str, bytes, PSLiteral]]] = []
return
def fillbuf(self):
@@ -133,36 +137,36 @@ class PSBaseParser(object):
self._curtokenpos = self.bufpos+j
if c == b'%':
self._curtoken = b'%'
- self._parse1 = self._parse_comment
+ self._current_parse_func = self._parse_comment
return j+1
elif c == b'/':
self._curtoken = b''
- self._parse1 = self._parse_literal
+ self._current_parse_func = self._parse_literal
return j+1
elif c in b'-+' or c.isdigit():
self._curtoken = c
- self._parse1 = self._parse_number
+ self._current_parse_func = self._parse_number
return j+1
elif c == b'.':
self._curtoken = c
- self._parse1 = self._parse_float
+ self._current_parse_func = self._parse_float
return j+1
elif c.isalpha():
self._curtoken = c
- self._parse1 = self._parse_keyword
+ self._current_parse_func = self._parse_keyword
return j+1
elif c == b'(':
self._curtoken = b''
self.paren = 1
- self._parse1 = self._parse_string
+ self._current_parse_func = self._parse_string
return j+1
elif c == b'<':
self._curtoken = b''
- self._parse1 = self._parse_wopen
+ self._current_parse_func = self._parse_wopen
return j+1
elif c == b'>':
self._curtoken = b''
- self._parse1 = self._parse_wclose
+ self._current_parse_func = self._parse_wclose
return j+1
else:
self._add_token(KWD(c))
@@ -179,7 +183,7 @@ class PSBaseParser(object):
return len(s)
j = m.start(0)
self._curtoken += s[i:j]
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
# We ignore comments.
#self._tokens.append(self._curtoken)
return j
@@ -194,14 +198,14 @@ class PSBaseParser(object):
c = s[j:j+1]
if c == b'#':
self.hex = b''
- self._parse1 = self._parse_literal_hex
+ self._current_parse_func = self._parse_literal_hex
return j+1
try:
self._curtoken=str(self._curtoken,'utf-8')
except:
pass
self._add_token(LIT(self._curtoken))
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j
def _parse_literal_hex(self, s, i):
@@ -211,7 +215,7 @@ class PSBaseParser(object):
return i+1
if self.hex:
self._curtoken += int2byte(int(self.hex, 16))
- self._parse1 = self._parse_literal
+ self._current_parse_func = self._parse_literal
return i
def _parse_number(self, s, i):
@@ -224,13 +228,13 @@ class PSBaseParser(object):
c = s[j:j+1]
if c == b'.':
self._curtoken += c
- self._parse1 = self._parse_float
+ self._current_parse_func = self._parse_float
return j+1
try:
self._add_token(int(self._curtoken))
except ValueError:
pass
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j
def _parse_float(self, s, i):
@@ -244,7 +248,7 @@ class PSBaseParser(object):
self._add_token(float(self._curtoken))
except ValueError:
pass
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j
def _parse_keyword(self, s, i):
@@ -261,7 +265,7 @@ class PSBaseParser(object):
else:
token = KWD(self._curtoken)
self._add_token(token)
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j
def _parse_string(self, s, i):
@@ -274,7 +278,7 @@ class PSBaseParser(object):
c = s[j:j+1]
if c == b'\\':
self.oct = b''
- self._parse1 = self._parse_string_1
+ self._current_parse_func = self._parse_string_1
return j+1
if c == b'(':
self.paren += 1
@@ -286,7 +290,7 @@ class PSBaseParser(object):
self._curtoken += c
return j+1
self._add_token(self._curtoken)
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j+1
def _parse_string_1(self, s, i):
@@ -296,21 +300,21 @@ class PSBaseParser(object):
return i+1
if self.oct:
self._curtoken += int2byte(int(self.oct, 8))
- self._parse1 = self._parse_string
+ self._current_parse_func = self._parse_string
return i
if c in ESC_STRING:
self._curtoken += int2byte(ESC_STRING[c])
- self._parse1 = self._parse_string
+ self._current_parse_func = self._parse_string
return i+1
def _parse_wopen(self, s, i):
c = s[i:i+1]
if c == b'<':
self._add_token(KEYWORD_DICT_BEGIN)
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
i += 1
else:
- self._parse1 = self._parse_hexstring
+ self._current_parse_func = self._parse_hexstring
return i
def _parse_wclose(self, s, i):
@@ -318,7 +322,7 @@ class PSBaseParser(object):
if c == b'>':
self._add_token(KEYWORD_DICT_END)
i += 1
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return i
def _parse_hexstring(self, s, i):
@@ -330,13 +334,13 @@ class PSBaseParser(object):
self._curtoken += s[i:j]
token = HEX_PAIR.sub(lambda m: int2byte(int(m.group(0), 16)),SPC.sub(b'', self._curtoken))
self._add_token(token)
- self._parse1 = self._parse_main
+ self._current_parse_func = self._parse_main
return j
def nexttoken(self):
while not self._tokens:
self.fillbuf()
- self.charpos = self._parse1(self.buf, self.charpos)
+ self.charpos = self._current_parse_func(self.buf, self.charpos)
token = self._tokens.pop(0)
log.debug('nexttoken: %r', token)
return token
diff --git a/pdfmajor/interpreter/PSStackParser/__init__.py b/pdfmajor/interpreter/PSStackParser/__init__.py
index b91d5f7..e78a18d 100644
--- a/pdfmajor/interpreter/PSStackParser/__init__.py
+++ b/pdfmajor/interpreter/PSStackParser/__init__.py
@@ -1,4 +1,5 @@
import logging
+from io import TextIOWrapper
from ...utils import choplist, settings
@@ -38,7 +39,7 @@ def keyword_name(x):
class PSStackParser(PSBaseParser):
- def __init__(self, fp):
+ def __init__(self, fp: TextIOWrapper):
PSBaseParser.__init__(self, fp)
self.reset()
return
diff --git a/pdfmajor/interpreter/PSStackParser/types.py b/pdfmajor/interpreter/PSStackParser/types.py
index 7fd274b..38fae37 100644
--- a/pdfmajor/interpreter/PSStackParser/types.py
+++ b/pdfmajor/interpreter/PSStackParser/types.py
@@ -1,5 +1,6 @@
## Basic PostScript Types
##
+from typing import Union
## PSObject
##
diff --git a/pdfmajor/interpreter/types.py b/pdfmajor/interpreter/types.py
new file mode 100644
index 0000000..ba9f9d5
--- /dev/null
+++ b/pdfmajor/interpreter/types.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'