- char-block introduced as a primary data source
This commit is contained in:
1 parent
b23df00569
commit
3375cd42ea
9 files changed
+153
-96
No files matched your search
@@ -9,7 +9,7 @@ from ..layouts import LTItem
|
|||||||
from ..layouts import LTCurve, LTLine, LTRect, CurvePath
|
from ..layouts import LTCurve, LTLine, LTRect, CurvePath
|
||||||
from ..layouts import LTFigure
|
from ..layouts import LTFigure
|
||||||
from ..layouts import LTImage
|
from ..layouts import LTImage
|
||||||
from ..layouts import LTChar
|
from ..layouts import LTChar, LTCharBlock
|
||||||
from ..utils import enc
|
from ..utils import enc
|
||||||
|
|
||||||
from .PDFConverter import PDFConverter
|
from .PDFConverter import PDFConverter
|
||||||
@@ -125,52 +125,43 @@ 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',
|
|
||||||
# 'height': f'{item.height}px',
|
|
||||||
}
|
}
|
||||||
# if isinstance(item, LTRect):
|
|
||||||
# self.place_elm_close('div', {'class': 'curve rect'}, {**css,
|
|
||||||
# "border-color": get_color(item.stroke),
|
|
||||||
# "background-color": get_color(item.fill),
|
|
||||||
# '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 = ""
|
path = ""
|
||||||
svg_attr = {
|
svg_attr = {
|
||||||
'class': 'curve',
|
'class': 'curve',
|
||||||
'z-index': 2
|
'z-index': 2
|
||||||
}
|
}
|
||||||
|
|
||||||
if item.height > 1:
|
if item.height > 0:
|
||||||
dx = (item.width/item.height)/(item.x1-item.x0)
|
|
||||||
dy = 1.0/item.height
|
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 item.height > 0:
|
|
||||||
dx = (item.width/item.height)/(item.x1-item.x0)
|
dx = (item.width/item.height)/(item.x1-item.x0)
|
||||||
dy = 1.0/item.height
|
|
||||||
svg_attr.update({
|
svg_attr.update({
|
||||||
"viewBox": f"0 0 {item.width/item.height} 1",
|
"viewBox": "0 0 %s %s" % ( item.width/item.height, 1 )
|
||||||
'width': f'{item.width}px',
|
|
||||||
'height': f'1px',
|
|
||||||
})
|
})
|
||||||
else:
|
else:
|
||||||
dx = 1.0/(item.x1-item.x0)
|
dx = 1.0/(item.x1-item.x0)
|
||||||
dy = 1.0
|
dy = 1.0
|
||||||
svg_attr.update({
|
svg_attr.update({
|
||||||
"viewBox": "0 0 1 1",
|
"viewBox": "0 0 1 1"
|
||||||
'width': f'{item.width}px',
|
})
|
||||||
|
|
||||||
|
if item.height > 1:
|
||||||
|
svg_attr.update({
|
||||||
|
'height': f'{item.height}px',
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
svg_attr.update({
|
||||||
'height': '1px',
|
'height': '1px',
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if item.width > 1:
|
||||||
|
svg_attr.update({
|
||||||
|
'width': f'{item.width}px',
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
svg_attr.update({
|
||||||
|
'width': '1px',
|
||||||
|
})
|
||||||
|
|
||||||
for p in item.paths:
|
for p in item.paths:
|
||||||
if p.method == CurvePath.METHOD.MOVE_TO:
|
if p.method == CurvePath.METHOD.MOVE_TO:
|
||||||
@@ -220,6 +211,18 @@ class HTMLConverter(PDFConverter):
|
|||||||
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
|
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
|
||||||
}):
|
}):
|
||||||
self.write(item.get_text())
|
self.write(item.get_text())
|
||||||
|
elif isinstance(item, LTCharBlock):
|
||||||
|
with self.place_elm_with_child('span', { 'class': 'char-block' }, {
|
||||||
|
'font-size': f'{item.size}px',
|
||||||
|
'font-family': item.fontname,
|
||||||
|
'font-weight': 'bold' if 'bold' in item.fontname.lower() else '',
|
||||||
|
'color': get_color(item.graphicstate.ncolor),
|
||||||
|
'position': 'absolute',
|
||||||
|
'left': f'{item.x0}',
|
||||||
|
'bottom': f'{item.y0}',
|
||||||
|
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
|
||||||
|
}):
|
||||||
|
self.write(item.get_text())
|
||||||
render(ltpage)
|
render(ltpage)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from typing import Dict
|
from typing import Dict, Union
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
@@ -12,7 +12,7 @@ from ..layouts import LTLine
|
|||||||
from ..layouts import LTRect
|
from ..layouts import LTRect
|
||||||
from ..layouts import LTFigure
|
from ..layouts import LTFigure
|
||||||
from ..layouts import LTImage
|
from ..layouts import LTImage
|
||||||
from ..layouts import LTChar
|
from ..layouts import LTChar, LTCharBlock
|
||||||
from ..utils import enc
|
from ..utils import enc
|
||||||
|
|
||||||
from .PDFConverter import PDFConverter
|
from .PDFConverter import PDFConverter
|
||||||
@@ -110,19 +110,6 @@ class JSONConverter(PDFConverter):
|
|||||||
"y1": item.y1,
|
"y1": item.y1,
|
||||||
})
|
})
|
||||||
|
|
||||||
def place_text(self, char: LTChar):
|
|
||||||
self.place_elm_close('char', {
|
|
||||||
'font-size': char.size,
|
|
||||||
'font-family': char.fontname,
|
|
||||||
"x0": char.x0,
|
|
||||||
"x1": char.x1,
|
|
||||||
"y0": char.y0,
|
|
||||||
"y1": char.y1,
|
|
||||||
"stroke-color": get_color(char.graphicstate.scolor),
|
|
||||||
"fill-color": get_color(char.graphicstate.ncolor),
|
|
||||||
"text": char.get_text()
|
|
||||||
})
|
|
||||||
|
|
||||||
def render_curve(self, item: LTCurve):
|
def render_curve(self, item: LTCurve):
|
||||||
attr = {
|
attr = {
|
||||||
"x0": item.x0,
|
"x0": item.x0,
|
||||||
@@ -166,7 +153,29 @@ class JSONConverter(PDFConverter):
|
|||||||
elif isinstance(item, LTImage):
|
elif isinstance(item, LTImage):
|
||||||
self.place_image(item)
|
self.place_image(item)
|
||||||
elif isinstance(item, LTChar):
|
elif isinstance(item, LTChar):
|
||||||
self.place_text(item)
|
self.place_elm_close('char', {
|
||||||
|
'font-size': item.size,
|
||||||
|
'font-family': item.fontname,
|
||||||
|
"x0": item.x0,
|
||||||
|
"x1": item.x1,
|
||||||
|
"y0": item.y0,
|
||||||
|
"y1": item.y1,
|
||||||
|
"stroke-color": get_color(item.graphicstate.scolor),
|
||||||
|
"fill-color": get_color(item.graphicstate.ncolor),
|
||||||
|
"text": item.get_text()
|
||||||
|
})
|
||||||
|
elif isinstance(item, LTCharBlock):
|
||||||
|
self.place_elm_close('char-block', {
|
||||||
|
'font-size': item.size,
|
||||||
|
'font-family': item.fontname,
|
||||||
|
"x0": item.x0,
|
||||||
|
"x1": item.x1,
|
||||||
|
"y0": item.y0,
|
||||||
|
"y1": item.y1,
|
||||||
|
"stroke-color": get_color(item.graphicstate.scolor),
|
||||||
|
"fill-color": get_color(item.graphicstate.ncolor),
|
||||||
|
"text": item.get_text()
|
||||||
|
})
|
||||||
render(ltpage)
|
render(ltpage)
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from ..layouts import LTLine
|
|||||||
from ..layouts import LTRect
|
from ..layouts import LTRect
|
||||||
from ..layouts import LTFigure
|
from ..layouts import LTFigure
|
||||||
from ..layouts import LTImage
|
from ..layouts import LTImage
|
||||||
from ..layouts import LTChar
|
from ..layouts import LTChar, LTCharBlock
|
||||||
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
|
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
|
||||||
from ..utils import enc
|
from ..utils import enc
|
||||||
|
|
||||||
@@ -47,6 +47,6 @@ class TextConverter(PDFConverter):
|
|||||||
elif isinstance(item, LTFigure):
|
elif isinstance(item, LTFigure):
|
||||||
for child in item:
|
for child in item:
|
||||||
render(child)
|
render(child)
|
||||||
elif isinstance(item, LTChar):
|
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
|
||||||
self.place_text(item)
|
self.place_text(item)
|
||||||
render(ltpage)
|
render(ltpage)
|
||||||
@@ -113,6 +113,9 @@ class XMLConverter(PDFConverter):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def place_text(self, char: LTChar):
|
def place_text(self, char: LTChar):
|
||||||
|
tag_name = 'char'
|
||||||
|
if isinstance(char, LTCharBlock):
|
||||||
|
tag_name = 'char-block'
|
||||||
attr = {
|
attr = {
|
||||||
'size': char.size,
|
'size': char.size,
|
||||||
"color": get_color(char.graphicstate.ncolor),
|
"color": get_color(char.graphicstate.ncolor),
|
||||||
@@ -124,7 +127,7 @@ class XMLConverter(PDFConverter):
|
|||||||
for key, value in char.font.descriptor.items():
|
for key, value in char.font.descriptor.items():
|
||||||
if key != "Type" and "FontFile" not in key:
|
if key != "Type" and "FontFile" not in key:
|
||||||
attr["font:" + key] = value
|
attr["font:" + key] = value
|
||||||
with self.place_elm_with_child('char', attr, no_additional_char=True):
|
with self.place_elm_with_child(tag_name, attr, no_additional_char=True):
|
||||||
self.write(char.get_text(), lineend='', deep_space="")
|
self.write(char.get_text(), lineend='', deep_space="")
|
||||||
|
|
||||||
def render_curve(self, item: LTCurve):
|
def render_curve(self, item: LTCurve):
|
||||||
@@ -161,7 +164,7 @@ class XMLConverter(PDFConverter):
|
|||||||
render(child)
|
render(child)
|
||||||
elif isinstance(item, LTImage):
|
elif isinstance(item, LTImage):
|
||||||
self.place_image(item)
|
self.place_image(item)
|
||||||
elif isinstance(item, LTChar):
|
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
|
||||||
self.place_text(item)
|
self.place_text(item)
|
||||||
render(ltpage)
|
render(ltpage)
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,7 @@ class PDFTextDevice(PDFDevice):
|
|||||||
def __render_string_along(self, idx: int, seq: bytearray,
|
def __render_string_along(self, idx: int, seq: bytearray,
|
||||||
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
|
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
|
||||||
needcharspace = False
|
needcharspace = False
|
||||||
|
char_meta_datas = []
|
||||||
for obj in seq:
|
for obj in seq:
|
||||||
if isnumber(obj):
|
if isnumber(obj):
|
||||||
textstate.linematrix[idx] -= obj*dxscale
|
textstate.linematrix[idx] -= obj*dxscale
|
||||||
@@ -109,16 +110,14 @@ class PDFTextDevice(PDFDevice):
|
|||||||
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
|
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
|
||||||
textstate.linematrix[idx] += adv
|
textstate.linematrix[idx] += adv
|
||||||
|
|
||||||
self.render_char(
|
char_meta_datas.append(
|
||||||
text,
|
( text, bbox, )
|
||||||
bbox,
|
|
||||||
textstate,
|
|
||||||
graphicstate,
|
|
||||||
ncs
|
|
||||||
)
|
)
|
||||||
if cid == 32 and textstate.wordspace:
|
if cid == 32 and textstate.wordspace:
|
||||||
textstate.linematrix[idx] += textstate.wordspace
|
textstate.linematrix[idx] += textstate.wordspace
|
||||||
needcharspace = True
|
needcharspace = True
|
||||||
|
|
||||||
|
self.render_char_box(char_meta_datas, textstate, graphicstate, ncs)
|
||||||
|
|
||||||
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
|
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
|
||||||
font = textstate.font
|
font = textstate.font
|
||||||
@@ -160,6 +159,5 @@ class PDFTextDevice(PDFDevice):
|
|||||||
|
|
||||||
return (adv, ((x0, y0), (x1, y1)))
|
return (adv, ((x0, y0), (x1, y1)))
|
||||||
|
|
||||||
|
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
||||||
def render_char(self, char: str, bbox: tuple, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
pass
|
||||||
return NotImplementedError
|
|
||||||
@@ -29,18 +29,19 @@ class LTChar(LTComponent):
|
|||||||
return
|
return
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return ('<%s %s matrix=%s font=%r text=%r>' %
|
return (
|
||||||
(self.__class__.__name__, str(self.bbox),
|
'<%s %s font=%r text=%r>' %
|
||||||
matrix2str(self.matrix), self.fontname,
|
(
|
||||||
self.get_text()))
|
self.__class__.__name__,
|
||||||
|
str(self.bbox),
|
||||||
|
self.fontname,
|
||||||
|
self.get_text()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def get_text(self):
|
def get_text(self):
|
||||||
return self._text
|
return self._text
|
||||||
|
|
||||||
def is_compatible(self, obj):
|
|
||||||
"""Returns True if two characters can coexist in the same line."""
|
|
||||||
return True
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def fontname(self) -> str:
|
def fontname(self) -> str:
|
||||||
return self.font.fontname
|
return self.font.fontname
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
from typing import List
|
||||||
|
|
||||||
|
from ._base import LTComponent
|
||||||
|
from .LTChar import LTChar
|
||||||
|
|
||||||
|
from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox, Point
|
||||||
|
from ..interpreter.PDFGraphicState import PDFGraphicState
|
||||||
|
from ..interpreter.PDFFont import PDFFont
|
||||||
|
from ..interpreter.PDFColorSpace import PDFColorSpace
|
||||||
|
from ..interpreter.PDFTextState import PDFTextState
|
||||||
|
|
||||||
|
## LTCharBlock
|
||||||
|
##
|
||||||
|
class LTCharBlock(LTComponent):
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
chars: List[Point],
|
||||||
|
textstate: PDFTextState,
|
||||||
|
ncs: PDFColorSpace,
|
||||||
|
graphicstate: PDFGraphicState
|
||||||
|
):
|
||||||
|
((x0, y0), (x1, y1)) = chars[0][1]
|
||||||
|
self.chars = [
|
||||||
|
LTChar(
|
||||||
|
bbox=Bbox(x0, y0, x1, y1),
|
||||||
|
char= chars[0][0],
|
||||||
|
textstate=textstate,
|
||||||
|
ncs=ncs,
|
||||||
|
graphicstate=graphicstate
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
for char in chars[1:]:
|
||||||
|
((nx0, ny0), (nx1, ny1)) = char[1]
|
||||||
|
x0 = min([ x0, x1, nx0, nx1 ])
|
||||||
|
x1 = max([ x0, x1, nx0, nx1 ])
|
||||||
|
y1 = max([ y0, y1, ny0, ny1 ])
|
||||||
|
y0 = min([ y0, y1, ny0, ny1 ])
|
||||||
|
self.chars.append(LTChar(
|
||||||
|
bbox=Bbox(nx0, ny0, nx1, ny1),
|
||||||
|
char= char[0],
|
||||||
|
textstate=textstate,
|
||||||
|
ncs=ncs,
|
||||||
|
graphicstate=graphicstate
|
||||||
|
))
|
||||||
|
|
||||||
|
LTComponent.__init__(self, Bbox(x0, y0, x1, y1))
|
||||||
|
|
||||||
|
self.font = textstate.font
|
||||||
|
self.textstate = textstate
|
||||||
|
self.ncs = ncs
|
||||||
|
self.graphicstate = graphicstate
|
||||||
|
|
||||||
|
if self.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.chars ])
|
||||||
|
|
||||||
|
@property
|
||||||
|
def fontname(self) -> str:
|
||||||
|
return self.font.fontname
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
from ._base import LTComponent
|
|
||||||
from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox
|
|
||||||
from ..interpreter.PDFGraphicState import PDFGraphicState
|
|
||||||
from ..interpreter.PDFFont import PDFFont
|
|
||||||
from ..interpreter.PDFTextState import PDFTextState
|
|
||||||
from ..interpreter.PDFColorSpace import PDFColorSpace
|
|
||||||
|
|
||||||
## LTString
|
|
||||||
##
|
|
||||||
class LTString(LTComponent):
|
|
||||||
|
|
||||||
def __init__(self,
|
|
||||||
textstate: PDFTextState,
|
|
||||||
seq: bytearray,
|
|
||||||
ncs: PDFColorSpace,
|
|
||||||
graphicstate: PDFGraphicState
|
|
||||||
):
|
|
||||||
|
|
||||||
LTComponent.__init__(self, Bbox(x0, y0, x1, y1))
|
|
||||||
@@ -11,6 +11,7 @@ from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
|
|||||||
from .LTFigure import LTFigure
|
from .LTFigure import LTFigure
|
||||||
from .LTImage import LTImage
|
from .LTImage import LTImage
|
||||||
from .LTChar import LTChar
|
from .LTChar import LTChar
|
||||||
|
from .LTCharBlock import LTCharBlock
|
||||||
|
|
||||||
from ..utils import Point, Bbox, INF
|
from ..utils import Point, Bbox, INF
|
||||||
from ..utils import apply_matrix_pt, mult_matrix
|
from ..utils import apply_matrix_pt, mult_matrix
|
||||||
@@ -175,21 +176,14 @@ class PDFLayoutAnalyzer(PDFTextDevice):
|
|||||||
fill=gstate.ncolor
|
fill=gstate.ncolor
|
||||||
))
|
))
|
||||||
|
|
||||||
def render_char(self, char: str, bbox: tuple, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
|
||||||
((x0, y0), (x1, y1)) = bbox
|
self.cur_item.add(LTCharBlock(
|
||||||
self.cur_item.add(LTChar(
|
chars=chars,
|
||||||
bbox=Bbox(x0, y0, x1, y1),
|
|
||||||
char=char,
|
|
||||||
textstate=textstate,
|
textstate=textstate,
|
||||||
ncs=ncs,
|
ncs=ncs,
|
||||||
graphicstate=graphicstate
|
graphicstate=graphicstate
|
||||||
))
|
))
|
||||||
|
|
||||||
def handle_undefined_char(self, font: str, cid: int):
|
|
||||||
log.info('undefined: %r, %r', font, cid)
|
|
||||||
return '(cid:%d)' % cid
|
|
||||||
|
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def receive_layout(self, ltpage: LTPage):
|
def receive_layout(self, ltpage: LTPage):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
Reference in new issue
Block a user