- better curve extraction

- better color detection
- better curve/color visualization
This commit is contained in:
ari committed 2018-10-17 11:23:52 -04:00
1 parent cdf036ad0e
commit 5f2eeb7855
23 files changed
+2264 -935

No files matched your search

+3 -1
View File
@@ -8,4 +8,6 @@ docs/_build
tests/*.xml
tests/*.txt
.idea/
.vscode
.vscode
samples/output
samples/pdfmajor
+38 -5
View File
@@ -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}',
+4 -2
View File
@@ -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):
-1
View File
@@ -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
+4 -2
View File
@@ -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):
+3 -3
View File
@@ -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):
+8 -7
View File
@@ -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
+40 -24
View File
@@ -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:
+2 -2
View File
@@ -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
+1 -1
View File
@@ -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:
+10 -8
View File
@@ -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
+16 -34
View File
@@ -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
@@ -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
@@ -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
@@ -1,5 +1,6 @@
## Basic PostScript Types
##
from typing import Union
## PSObject
##
+27
View File
@@ -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'<CurvePoint ({self.x}, {self.y})/>'
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'<CurvePath {self.method} path=[{",".join(map(repr, self.points))}]/>'
+18 -18
View File
@@ -3,43 +3,43 @@ from typing import List, Tuple
from ..utils import Bbox
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
from ._base import LTComponent
from ..interpreter.types import CurvePath
from ._base import LTComponent, Bbox
class LTCurve(LTComponent):
def __init__(self,
linewidth: float,
pts: List[Tuple[float]],
paths: List[CurvePath],
bbox: Bbox,
stroke: PDFGraphicStateColor,
fill: PDFGraphicStateColor,
evenodd: bool,
):
LTComponent.__init__(self, Bbox.from_points(pts))
self.pts = pts
LTComponent.__init__(self, bbox)
self.paths = paths
self.linewidth = linewidth
self.evenodd = evenodd
self.stroke = stroke
self.fill = fill
def get_pts(self):
return ','.join('%.3f,%.3f' % p for p in self.pts)
## LTLine
##
class LTLine(LTCurve):
pass
def __init__(self, linewidth: float, p0:Tuple[float], p1:Tuple[float], stroke = PDFGraphicStateColor(), fill = PDFGraphicStateColor(), evenodd = False):
LTCurve.__init__(self,
linewidth=linewidth,
pts=[p0, p1],
stroke=stroke,
fill=fill,
evenodd=evenodd
)
## LTHorizontalLine
##
class LTHorizontalLine(LTLine):
pass
## LTHorizontalLine
##
class LTVerticalLine(LTLine):
pass
## LTRect
##
class LTRect(LTCurve):
def __init__(self, linewidth: float, pts: List[Tuple[float]], stroke = PDFGraphicStateColor(), fill = PDFGraphicStateColor(), evenodd = False):
LTCurve.__init__(self, linewidth, pts, stroke, fill, evenodd)
pass
+76 -40
View File
@@ -7,12 +7,12 @@ from typing import Tuple, List
from ._base import LTItem
from .LTPage import LTPage
from .LTCurves import LTCurve, LTLine, LTRect
from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
from .LTFigure import LTFigure
from .LTImage import LTImage
from .LTChar import LTChar
from ..utils import Point, Bbox
from ..utils import Point, Bbox, INF
from ..utils import apply_matrix_pt, mult_matrix
from ..interpreter.PDFFont import PDFUnicodeNotDefined
@@ -21,6 +21,7 @@ from ..interpreter.PDFGraphicState import PDFGraphicState, PDFGraphicStateColor
from ..interpreter.PDFdevice import PDFTextDevice
from ..interpreter.PDFPage import PDFPage
from ..interpreter.PDFStream import PDFStream
from ..interpreter.types import CurvePath
log = logging.getLogger(__name__)
@@ -85,59 +86,94 @@ class PDFLayoutAnalyzer(PDFTextDevice):
self.cur_item.add(item)
return
def paint_path(self, gstate: PDFGraphicState, evenodd: bool, path: list):
shape = ''.join(x[0] for x in path)
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
(_, x0, y0) = path[0]
(_, x1, y1) = path[1]
(x0, y0) = apply_matrix_pt(self.ctm, (x0, y0))
(x1, y1) = apply_matrix_pt(self.ctm, (x1, y1))
if x0 == x1 or y0 == y1:
self.cur_item.add(LTLine(
gstate.linewidth,
(x0, y0), (x1, y1),
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,
))
return
if shape == 'mlllh':
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
(_, x0, y0) = path[0]
(_, x1, y1) = path[1]
(_, x2, y2) = path[2]
(_, x3, y3) = path[3]
(x0, y0) = apply_matrix_pt(self.ctm, (x0, y0))
(x1, y1) = apply_matrix_pt(self.ctm, (x1, y1))
(x2, y2) = apply_matrix_pt(self.ctm, (x2, y2))
(x3, y3) = apply_matrix_pt(self.ctm, (x3, y3))
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,
pts=[
(x0, y0), (x0, y2),
(x2, y0), (x2, y2)
],
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,
))
return
# other shapes
pts = []
for p in path:
for i in range(1, len(p), 2):
pts.append(apply_matrix_pt(self.ctm, (p[i], p[i+1])))
self.cur_item.add(LTCurve(
linewidth=gstate.linewidth,
pts=pts,
evenodd=evenodd,
stroke=gstate.scolor,
fill=gstate.ncolor
))
return
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)
# for p in path:
# paths.append(apply_matrix_pt(self.ctm, (p.x, p.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(self, matrix: List[Point], font, fontsize, scaling, rise, cid, ncs, graphicstate):
try:
+4 -1
View File
@@ -1,12 +1,13 @@
import re
import os
from typing import List
from pdfmajor.converters import convert_file
def rel_path(*arg: List[str]) -> str:
return os.path.join(os.path.dirname(__file__), *arg)
EXT = 'html'
EXT = 'xml'
INPUT_FOLDER = rel_path("./pdf")
OUTPUT_FOLDER = rel_path(f"./output/{EXT}")
@@ -14,6 +15,8 @@ if not os.path.exists(OUTPUT_FOLDER):
os.makedirs(OUTPUT_FOLDER)
files = os.listdir(INPUT_FOLDER)
# files = ['colors.pdf']
# files = ['bar-charts.pdf']
TOTAL = len(files)
for idx, file_name in enumerate(files):
File diff suppressed because it is too large. Load diff
+24 -24
View File
@@ -7,68 +7,68 @@
</head>
<body>
<div class="page" style="width: 612px;height: 792px">
<div class="curve" style="position: absolute;left: 56.7px;bottom: 722.65px;width: 34.14999999999999px;height: 12.649999999999977px;border-color: none;background-color: rgb(0,0,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 56.8;bottom: 722.6080000000001">
<div class="curve rect" style="position: absolute;left: 56.7px;bottom: 722.65px;width: 34.14999999999999px;height: 12.649999999999977px;border-color: none;background-color: rgb(255,241.99755000000002,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 56.8;bottom: 722.6080000000001">
Y
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 64.3;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 64.3;bottom: 722.6080000000001">
e
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 69.604;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 69.604;bottom: 722.6080000000001">
l
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 72.904;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 72.904;bottom: 722.6080000000001">
l
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 76.204;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 76.204;bottom: 722.6080000000001">
o
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 82.204;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 82.204;bottom: 722.6080000000001">
w
</span>
<div class="curve" style="position: absolute;left: 90.9px;bottom: 722.65px;width: 19.25px;height: 12.649999999999977px;border-color: none;background-color: rgb(0,0,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 91.0;bottom: 722.6080000000001">
<div class="curve rect" style="position: absolute;left: 90.9px;bottom: 722.65px;width: 19.25px;height: 12.649999999999977px;border-color: none;background-color: rgb(255,63.9999,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 91.0;bottom: 722.6080000000001">
R
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 98.992;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 98.992;bottom: 722.6080000000001">
e
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 104.29599999999999;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 104.29599999999999;bottom: 722.6080000000001">
d
</span>
<div class="curve" style="position: absolute;left: 110.2px;bottom: 722.65px;width: 29.249999999999986px;height: 12.649999999999977px;border-color: none;background-color: rgb(0,0,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 110.3;bottom: 722.6080000000001">
<div class="curve rect" style="position: absolute;left: 110.2px;bottom: 722.65px;width: 29.249999999999986px;height: 12.649999999999977px;border-color: none;background-color: rgb(128.9994,211.99935000000002,25.9998)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 110.3;bottom: 722.6080000000001">
G
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 118.988;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 118.988;bottom: 722.6080000000001">
r
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 122.984;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 122.984;bottom: 722.6080000000001">
e
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 128.288;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 128.288;bottom: 722.6080000000001">
e
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 133.59199999999998;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 133.59199999999998;bottom: 722.6080000000001">
n
</span>
<div class="curve" style="position: absolute;left: 139.5px;bottom: 722.65px;width: 40.650000000000006px;height: 12.649999999999977px;border-color: none;background-color: rgb(0,0,0)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 139.6;bottom: 722.6080000000001">
<div class="curve rect" style="position: absolute;left: 139.5px;bottom: 722.65px;width: 40.650000000000006px;height: 12.649999999999977px;border-color: none;background-color: rgb(255,181.9986,107.9976)"></div>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 139.6;bottom: 722.6080000000001">
R
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 147.59199999999998;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 147.59199999999998;bottom: 722.6080000000001">
a
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 152.896;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 152.896;bottom: 722.6080000000001">
n
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 158.896;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 158.896;bottom: 722.6080000000001">
d
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 164.896;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 164.896;bottom: 722.6080000000001">
o
</span>
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: none;position: absolute;left: 170.896;bottom: 722.6080000000001">
<span class="char" style="font-size: 15.408000000000015px;font-family: BAAAAA+LiberationSerif;color: rgb(0,0,0);position: absolute;left: 170.896;bottom: 722.6080000000001">
m
</span>
</div>
Whitespace-only changes.
File diff suppressed because it is too large. Load diff