- char-block introduced as a primary data source

This commit is contained in:
ari committed 2018-10-28 21:31:44 -04:00
1 parent b23df00569
commit 3375cd42ea
9 files changed
+153 -96

No files matched your search

+33 -30
View File
@@ -9,7 +9,7 @@ from ..layouts import LTItem
from ..layouts import LTCurve, LTLine, LTRect, CurvePath
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar
from ..layouts import LTChar, LTCharBlock
from ..utils import enc
from .PDFConverter import PDFConverter
@@ -125,52 +125,43 @@ class HTMLConverter(PDFConverter):
'position': 'absolute',
'left': f'{item.x0}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 = ""
svg_attr = {
'class': 'curve',
'z-index': 2
}
if item.height > 1:
dx = (item.width/item.height)/(item.x1-item.x0)
if item.height > 0:
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)
dy = 1.0/item.height
svg_attr.update({
"viewBox": f"0 0 {item.width/item.height} 1",
'width': f'{item.width}px',
'height': f'1px',
"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",
'width': f'{item.width}px',
"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',
})
for p in item.paths:
if p.method == CurvePath.METHOD.MOVE_TO:
@@ -220,6 +211,18 @@ class HTMLConverter(PDFConverter):
'transform': f'skew({item.font.italic_angle}deg, 0deg)'
}):
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)
def close(self):
+25 -16
View File
@@ -1,6 +1,6 @@
import logging
from typing import Dict
from typing import Dict, Union
from io import BytesIO
from contextlib import contextmanager
@@ -12,7 +12,7 @@ from ..layouts import LTLine
from ..layouts import LTRect
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar
from ..layouts import LTChar, LTCharBlock
from ..utils import enc
from .PDFConverter import PDFConverter
@@ -110,19 +110,6 @@ class JSONConverter(PDFConverter):
"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):
attr = {
"x0": item.x0,
@@ -166,7 +153,29 @@ class JSONConverter(PDFConverter):
elif isinstance(item, LTImage):
self.place_image(item)
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)
def close(self):
+2 -2
View File
@@ -11,7 +11,7 @@ from ..layouts import LTLine
from ..layouts import LTRect
from ..layouts import LTFigure
from ..layouts import LTImage
from ..layouts import LTChar
from ..layouts import LTChar, LTCharBlock
from ..interpreter.PDFGraphicState import PDFGraphicStateColor
from ..utils import enc
@@ -47,6 +47,6 @@ class TextConverter(PDFConverter):
elif isinstance(item, LTFigure):
for child in item:
render(child)
elif isinstance(item, LTChar):
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
self.place_text(item)
render(ltpage)
+5 -2
View File
@@ -113,6 +113,9 @@ class XMLConverter(PDFConverter):
})
def place_text(self, char: LTChar):
tag_name = 'char'
if isinstance(char, LTCharBlock):
tag_name = 'char-block'
attr = {
'size': char.size,
"color": get_color(char.graphicstate.ncolor),
@@ -124,7 +127,7 @@ class XMLConverter(PDFConverter):
for key, value in char.font.descriptor.items():
if key != "Type" and "FontFile" not in key:
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="")
def render_curve(self, item: LTCurve):
@@ -161,7 +164,7 @@ class XMLConverter(PDFConverter):
render(child)
elif isinstance(item, LTImage):
self.place_image(item)
elif isinstance(item, LTChar):
elif isinstance(item, LTChar) or isinstance(item, LTCharBlock):
self.place_text(item)
render(ltpage)
+7 -9
View File
@@ -93,6 +93,7 @@ class PDFTextDevice(PDFDevice):
def __render_string_along(self, idx: int, seq: bytearray,
textstate: PDFTextState, dxscale: float, ncs: PDFColorSpace, graphicstate: PDFGraphicState):
needcharspace = False
char_meta_datas = []
for obj in seq:
if isnumber(obj):
textstate.linematrix[idx] -= obj*dxscale
@@ -109,16 +110,14 @@ class PDFTextDevice(PDFDevice):
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
textstate.linematrix[idx] += adv
self.render_char(
text,
bbox,
textstate,
graphicstate,
ncs
char_meta_datas.append(
( text, bbox, )
)
if cid == 32 and textstate.wordspace:
textstate.linematrix[idx] += textstate.wordspace
needcharspace = True
self.render_char_box(char_meta_datas, textstate, graphicstate, ncs)
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
font = textstate.font
@@ -160,6 +159,5 @@ class PDFTextDevice(PDFDevice):
return (adv, ((x0, y0), (x1, y1)))
def render_char(self, char: str, bbox: tuple, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
return NotImplementedError
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
pass
+9 -8
View File
@@ -29,18 +29,19 @@ class LTChar(LTComponent):
return
def __repr__(self):
return ('<%s %s matrix=%s font=%r text=%r>' %
(self.__class__.__name__, str(self.bbox),
matrix2str(self.matrix), self.fontname,
self.get_text()))
return (
'<%s %s font=%r text=%r>' %
(
self.__class__.__name__,
str(self.bbox),
self.fontname,
self.get_text()
)
)
def get_text(self):
return self._text
def is_compatible(self, obj):
"""Returns True if two characters can coexist in the same line."""
return True
@property
def fontname(self) -> str:
return self.font.fontname
+68
View File
@@ -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
-19
View File
@@ -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))
+4 -10
View File
@@ -11,6 +11,7 @@ from .LTCurves import LTCurve, LTLine, LTRect, LTHorizontalLine, LTVerticalLine
from .LTFigure import LTFigure
from .LTImage import LTImage
from .LTChar import LTChar
from .LTCharBlock import LTCharBlock
from ..utils import Point, Bbox, INF
from ..utils import apply_matrix_pt, mult_matrix
@@ -175,21 +176,14 @@ class PDFLayoutAnalyzer(PDFTextDevice):
fill=gstate.ncolor
))
def render_char(self, char: str, bbox: tuple, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
((x0, y0), (x1, y1)) = bbox
self.cur_item.add(LTChar(
bbox=Bbox(x0, y0, x1, y1),
char=char,
def render_char_box(self, chars: list, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
self.cur_item.add(LTCharBlock(
chars=chars,
textstate=textstate,
ncs=ncs,
graphicstate=graphicstate
))
def handle_undefined_char(self, font: str, cid: int):
log.info('undefined: %r, %r', font, cid)
return '(cid:%d)' % cid
@abstractmethod
def receive_layout(self, ltpage: LTPage):
raise NotImplementedError