- slightly more accurate shape detection

This commit is contained in:
ari committed 2019-01-16 16:55:30 -05:00
1 parent b36f6bc907
commit 0345ca01ba
7 files changed
+142 -39

No files matched your search

+113 -19
View File
@@ -43,19 +43,21 @@ def convert_to_html(
render_page(html, page)
def render_page(html: HTMLMaker, ltpage: PageInterpreter):
def render(item: LTItem):
def render(item: LTItem, block = True):
if isinstance(item, LTCurve):
render_curve(html, item)
render_curve(html, ltpage, item)
elif isinstance(item, LTXObject):
with html.elm('div', { "class": 'xobject' }, {
'position': 'absolute',
# 'position': 'absolute',
'left': f'{item.x0}px',
'right': f'{item.x1}px',
'bottom': f'{item.y0}px',
'top': f'{item.y1}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}):
for child in item:
render(child)
render(child, False)
elif isinstance(item, LTImage):
name = item.name
# if html.imagewriter is not None:
@@ -68,12 +70,12 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
})
elif isinstance(item, LTChar):
with html.elm('span', { 'class': 'char' }, {
'width': item.width,
'min-width': item.width,
'max-width': item.width,
'height': item.height,
'min-height': item.height,
'max-height': item.height,
'width': str(item.width) + "px",
'min-width': str(item.width) + "px",
'max-width': str(item.width) + "px",
'height': str(item.height) + "px",
'min-height': str(item.height) + "px",
'max-height': str(item.height) + "px",
}):
html.write(item.get_text())
elif isinstance(item, LTTextBlock):
@@ -89,12 +91,12 @@ def render_page(html: HTMLMaker, ltpage: PageInterpreter):
'font-family': item.fontname,
'font-weight': item.font.font_weight,
'color': get_color(item.color, default="black"),
'width': item.width,
'min-width': item.width,
'max-width': item.width,
'height': item.height,
'min-height': item.height,
'max-height': item.height,
'width': str(item.width) + "px",
'min-width': str(item.width) + "px",
'max-width': str(item.width) + "px",
'height': str(item.height) + "px",
'min-height': str(item.height) + "px",
'max-height': str(item.height) + "px",
'transform': f'skew({item.font.italic_angle}deg, 0deg)',
'text-align': 'center',
'display': 'flex',
@@ -125,11 +127,101 @@ def get_color(col: PDFColor, default: str = ""):
else:
return default
def render_curve(html: HTMLMaker, item: LTCurve):
def render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
path, svg_attr = create_svg_path(item)
stroke_col = get_color(item.stroke)
fill_col = get_color(item.fill)
path_css = {}
if fill_col:
path_css['fill'] = fill_col
if stroke_col:
path_css['stroke'] = stroke_col
with html.elm('svg', svg_attr, css={
'border': '1px solid black',
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{ltpage.height - item.y0}px',
'top': f'{ltpage.height - item.y1}px',
'right': f'{item.x1}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}) as svg:
html.singleton('path', {
"d": path,
**path_css
}, {
**path_css
})
def create_svg_path(item: LTCurve):
css = {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
}
path = ""
svg_attr = {
'class': 'curve',
'z-index': 2
}
if item.height > 0:
dy = 1.0/item.height
dx = (item.width/item.height)/(item.x1-item.x0)
svg_attr.update({
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
})
else:
dx = 1.0/(item.x1-item.x0)
dy = 1.0
svg_attr.update({
"viewBox": "0 0 1 1"
})
if item.height > 1:
svg_attr.update({
'height': f'{item.height}px',
})
else:
svg_attr.update({
'height': '1px',
})
if item.width > 1:
svg_attr.update({
'width': f'{item.width}px',
})
else:
svg_attr.update({
'width': '1px',
})
def pX(val: float) -> float:
return ( val-item.x0 )*dx
def pY(val: float) -> float:
return ( item.height- (val-item.y0) )*dy
for p in item.paths:
if p.method == CurvePath.METHOD.MOVE_TO:
path += f'M{" ".join( f"{pX(p.x)} {pY(p.y)}" for p in p.points )} '
elif p.method == CurvePath.METHOD.LINE_TO:
path += f'L{" ".join( f"{pX(p.x)} {pY(p.y)}" for p in p.points )} '
elif p.method == CurvePath.METHOD.CLOSE_PATH:
path += 'Z'
elif p.method == CurvePath.METHOD.CURVE_BOTH_TO:
path += f'C{" ".join( f"{pX(pt.x)} {pY(pt.y)}" for pt in p.points )} '
return path, svg_attr
def __render_curve(html: HTMLMaker, ltpage: PageInterpreter, item: LTCurve):
css = {
'position': 'absolute',
'left': f'{item.x0}px',
'bottom': f'{item.y0}px',
'top': f'{item.y1}px',
'right': f'{item.x1}px',
'width': f'{item.width}px',
'height': f'{item.height}px',
}
@@ -172,10 +264,12 @@ def render_curve(html: HTMLMaker, item: LTCurve):
for p in item.paths:
if p.method == CurvePath.METHOD.MOVE_TO:
path += f'M{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
if p.method == CurvePath.METHOD.LINE_TO:
elif p.method == CurvePath.METHOD.LINE_TO:
path += f'L{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
if p.method == CurvePath.METHOD.CLOSE_PATH:
elif p.method == CurvePath.METHOD.CLOSE_PATH:
path += 'Z'
elif p.method == CurvePath.METHOD.CURVE_BOTH_TO:
path += f'C{" ".join( f"{round(dx*pt.x, ROUND_VAL)} {round(dy*pt.y, ROUND_VAL)}" for pt in p.points )} '
stroke_col = get_color(item.stroke)
fill_col = get_color(item.fill)
+2 -3
View File
@@ -56,7 +56,6 @@ class HTMLMaker:
self.write(f"<{tag_name} {attrs} />", lineend=lineend, deep_space=deep_space)
yield
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None):
with self.elm(tag_name, attrs, css, singleton=True):
def singleton(self, tag_name: str, attrs: dict = None, css: dict = None, singleton: bool = True):
with self.elm(tag_name, attrs, css, singleton=singleton):
pass
@@ -20,8 +20,6 @@ log = get_logger('process_command_stream')
class CommandProcessorError(Exception): pass
def process_command_stream(streams: List[PDFStream], font_cache: dict = None, state: PDFStateStack = None):
if state is None:
state = PDFStateStack()
if font_cache is None:
font_cache = {}
parser = PDFContentParser(streams)
+14 -14
View File
@@ -116,7 +116,7 @@ def do_i(stack: PDFStateStack, flatness) -> PDFStateStack:
def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
stack.curvestacks.append(CurvePath(
CurvePath.METHOD.MOVE_TO,
CurvePoint(x, y)
CurvePoint(stack.t_matrix, x, y)
))
return stack
@@ -125,7 +125,7 @@ def do_m(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
def do_l(stack: PDFStateStack, x: float, y: float) -> PDFStateStack:
stack.curvestacks.append(CurvePath(
CurvePath.METHOD.LINE_TO,
CurvePoint(x, y)
CurvePoint(stack.t_matrix, x, y)
))
return stack
@@ -137,9 +137,9 @@ def do_c(stack: PDFStateStack,
x3: float, y3: float ) -> PDFStateStack:
stack.curvestacks.append(CurvePath(
CurvePath.METHOD.CURVE_BOTH_TO,
CurvePoint(x1, y1),
CurvePoint(x2, y2),
CurvePoint(x3, y3),
CurvePoint(stack.t_matrix, x1, y1),
CurvePoint(stack.t_matrix, x2, y2),
CurvePoint(stack.t_matrix, x3, y3),
))
return stack
@@ -150,8 +150,8 @@ def do_v(stack: PDFStateStack,
x3: float, y3: float) -> PDFStateStack:
stack.curvestacks.append(CurvePath(
CurvePath.METHOD.CURVE_NEXT_TO,
CurvePoint(x2, y2),
CurvePoint(x3, y3),
CurvePoint(stack.t_matrix, x2, y2),
CurvePoint(stack.t_matrix, x3, y3),
))
return stack
@@ -162,8 +162,8 @@ def do_y(stack: PDFStateStack,
x3: float, y3: float) -> PDFStateStack:
stack.curvestacks.append(CurvePath(
CurvePath.METHOD.CURVE_FIRST_TO,
CurvePoint(x1, y1),
CurvePoint(x3, y3)
CurvePoint(stack.t_matrix, x1, y1),
CurvePoint(stack.t_matrix, x3, y3)
))
return stack
@@ -177,10 +177,10 @@ def do_h(stack: PDFStateStack) -> PDFStateStack:
@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.MOVE_TO, CurvePoint(stack.t_matrix, x, y) ),
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y) ),
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x+w, y+h) ),
CurvePath(CurvePath.METHOD.LINE_TO, CurvePoint(stack.t_matrix, x, y+h) ),
CurvePath(CurvePath.METHOD.CLOSE_PATH)
])
return stack
@@ -476,7 +476,7 @@ def do__w(stack: PDFStateStack, wordspace, charspace, b) -> PDFStateStack:
def do_EI(stack: PDFStateStack, obj) -> PDFStateStack:
if 'W' in obj and 'H' in obj:
stack.complete_layout_items.append(make_image(
obj, stack.ctm
obj, stack.t_matrix
))
return stack
@@ -1,8 +1,14 @@
from enum import Enum
from typing import List
from pdfmajor.utils import apply_matrix_pt
class CurvePoint:
def __init__(self, x: float, y: float):
def __init__(self, ctm: tuple, x: float, y: float):
# old = (x,y)
# (x, y) = apply_matrix_pt(ctm, (x, y))
# if old != (x,y):
# print("CurvePoint", ctm, old)
# print(" >", x, y)
self.x = x
self.y = y
+6
View File
@@ -13,6 +13,12 @@ class Bbox:
self.x1 = x1
self.y0 = y0
self.y1 = y1
if (x0 is not None) and (x1 is not None):
self.x0 = min([x0,x1])
self.x1 = max([x0,x1])
if (y0 is not None) and (y1 is not None):
self.y0 = min([y0,y1])
self.y1 = max([y0,y1])
@property
def height(self) -> float:
Binary file not shown.