- better html curve drawing

This commit is contained in:
ari committed 2018-10-27 19:51:58 -04:00
1 parent 3302082586
commit 39280476e2
1 file changed
+57 -36
+57 -36
View File
@@ -125,47 +125,66 @@ class HTMLConverter(PDFConverter):
'position': 'absolute', 'position': 'absolute',
'left': f'{item.x0}px', 'left': f'{item.x0}px',
'bottom': f'{item.y0}px', 'bottom': f'{item.y0}px',
'width': f'{item.width}px', # 'width': f'{item.width}px',
'height': f'{item.height}px', # 'height': f'{item.height}px',
} }
if isinstance(item, LTRect): # if isinstance(item, LTRect):
self.place_elm_close('div', {'class': 'curve rect'}, {**css, # self.place_elm_close('div', {'class': 'curve rect'}, {**css,
"border-color": get_color(item.stroke), # "border-color": get_color(item.stroke),
"background-color": get_color(item.fill), # "background-color": get_color(item.fill),
'z-index': 0 # 'z-index': 0
# })
# 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),
# 'z-index': 0
# })
# else:
path = ""
svg_attr = {
'class': 'curve',
'z-index': 2
}
if item.height > 1:
dx = (item.width/item.height)/(item.x1-item.x0)
dy = 1.0/item.height
svg_attr.update({
"viewBox": f"0 0 {item.width/item.height} 1",
'width': f'{item.width}px',
'height': f'{item.height}px',
}) })
elif isinstance(item, LTLine): elif item.height > 0:
self.place_elm_close('div', {'class': 'curve line'}, {**css, dx = (item.width/item.height)/(item.x1-item.x0)
"background-color": get_color(item.fill), dy = 1.0/item.height
"border-color": get_color(item.stroke), svg_attr.update({
'z-index': 0 "viewBox": f"0 0 {item.width/item.height} 1",
'width': f'{item.width}px',
'height': f'1px',
}) })
else: else:
path = "" dx = 1.0/(item.x1-item.x0)
dy = 1.0
svg_attr.update({
"viewBox": "0 0 1 1",
'width': f'{item.width}px',
'height': '1px',
})
for p in item.paths:
dx = (item.width/item.height)/(item.x1-item.x0) if p.method == CurvePath.METHOD.MOVE_TO:
dy = 1.0/(item.y1-item.y0) 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:
for p in item.paths: 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.MOVE_TO: if p.method == CurvePath.METHOD.CLOSE_PATH:
path += f'M{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {(item.height- (p.y-item.y0) )*dy}" for p in p.points )} ' path += 'Z'
if p.method == CurvePath.METHOD.LINE_TO: with self.place_elm_with_child('svg', svg_attr, css):
path += f'L{" ".join( f"{(item.width - (p.x-item.x0) )*dx} {( item.height - (p.y-item.y0) )*dy}" for p in p.points )} ' self.place_elm_close('path', {
if p.method == CurvePath.METHOD.CLOSE_PATH: "d": path,
path += 'Z' 'stroke': get_color(item.stroke),
with self.place_elm_with_child('svg', { 'fill': get_color(item.fill),
'class': 'curve', })
"viewBox": f"0 0 {item.width/item.height} 1",
'width': f'{item.width}px',
'height': f'{item.height}px',
'z-index': 2
}, 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 receive_layout(self, ltpage: LTPage):
def render(item: LTItem): def render(item: LTItem):
@@ -193,10 +212,12 @@ class HTMLConverter(PDFConverter):
with self.place_elm_with_child('span', { 'class': 'char' }, { with self.place_elm_with_child('span', { 'class': 'char' }, {
'font-size': f'{item.size}px', 'font-size': f'{item.size}px',
'font-family': item.fontname, 'font-family': item.fontname,
'font-weight': 'bold' if 'bold' in item.fontname.lower() else '',
'color': get_color(item.graphicstate.ncolor), 'color': get_color(item.graphicstate.ncolor),
'position': 'absolute', 'position': 'absolute',
'left': f'{item.x0}', 'left': f'{item.x0}',
'bottom': f'{item.y0}', 'bottom': f'{item.y0}',
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
}): }):
self.write(item.get_text()) self.write(item.get_text())
render(ltpage) render(ltpage)