- better abstraction for char parsing

This commit is contained in:
ari committed 2018-10-28 14:37:44 -04:00
1 parent ba8c025d38
commit 9fb0efd510
5 files changed
+98 -111

No files matched your search

+6 -1
View File
@@ -45,4 +45,9 @@ class PDFTextState(object):
def reset(self):
self.matrix = MATRIX_IDENTITY
self.linematrix = (0, 0)
return
return
@property
def is_upright(self):
(a, b, c, d, _, _) = self.matrix
return (0 < a*d*self.scaling and b*c <= 0)
+51 -45
View File
@@ -2,7 +2,7 @@
from .PDFFont import PDFUnicodeNotDefined
from ..utils import isnumber
from ..utils import isnumber, apply_matrix_pt
from ..utils import Bbox, mult_matrix, translate_matrix
from .PDFResourceManager import PDFResourceManager
@@ -103,59 +103,65 @@ class PDFTextDevice(PDFDevice):
if needcharspace:
pos_copy[idx] += textstate.charspace
# text = font.to_unichr(cid)
# assert isinstance(text, str), str(type(text))
text = textstate.font.to_unichr(cid)
assert isinstance(text, str), str(type(text))
# textwidth = font.char_width(cid)
# textdisp = font.char_disp(cid)
matrix = translate_matrix(textstate.matrix, pos_copy)
adv, bbox = self.__compute_char_bbox(matrix, cid, textstate)
pos_copy[idx] += adv
pos_copy[idx] += self.render_char(
translate_matrix(textstate.matrix, pos_copy),
textstate.font,
textstate.fontsize,
textstate.scaling,
textstate.rise,
cid,
ncs,
graphicstate
self.render_char(
text,
bbox,
textstate,
graphicstate,
ncs
)
if cid == 32 and textstate.wordspace:
pos_copy[idx] += textstate.wordspace
needcharspace = True
return pos_copy
def __compute_char_bbox(self, font: PDFFont):
pass
def __compute_char_bbox(self, matrix, char_id: int, textstate: PDFTextState):
font = textstate.font
fontsize = textstate.fontsize
rise = textstate.rise
scaling = textstate.scaling
textwidth = font.char_width(char_id)
textdisp = font.char_disp(char_id)
adv = textwidth * fontsize * scaling
# compute the boundary rectangle.
# if font.is_vertical():
# # vertical
# width = font.get_width() * fontsize
# (vx, vy) = textdisp
# if vx is None:
# vx = width * 0.5
# else:
# vx = vx * fontsize * .001
# vy = (1000 - vy) * fontsize * .001
# tx = -vx
# ty = vy + rise
# bll = (tx, ty+self.adv)
# bur = (tx+width, ty)
# else:
# # horizontal
# height = font.get_height() * fontsize
# descent = font.get_descent() * fontsize
# ty = descent + rise
# bll = (0, ty)
# bur = (self.adv, ty+height)
# (a, b, c, d, _, _) = self.matrix
# self.upright = (0 < a*d*scaling and b*c <= 0)
# (x0, y0) = apply_matrix_pt(self.matrix, bll)
# (x1, y1) = apply_matrix_pt(self.matrix, bur)
# if x1 < x0:
# (x0, x1) = (x1, x0)
# if y1 < y0:
# (y0, y1) = (y1, y0)
if font.is_vertical():
# vertical
width = font.get_width() * fontsize
(vx, vy) = textdisp
if vx is None:
vx = width * 0.5
else:
vx = vx * fontsize * .001
vy = (1000 - vy) * fontsize * .001
tx = -vx
ty = vy + rise
bll = (tx, ty+adv)
bur = (tx+width, ty)
else:
# horizontal
height = font.get_height() * fontsize
descent = font.get_descent() * fontsize
ty = descent + rise
bll = (0, ty)
bur = (adv, ty+height)
(x0, y0) = apply_matrix_pt(matrix, bll)
(x1, y1) = apply_matrix_pt(matrix, bur)
if x1 < x0:
(x0, x1) = (x1, x0)
if y1 < y0:
(y0, y1) = (y1, y0)
return (adv, ((x0, y0), (x1, y1)))
def render_char(self, matrix, font, fontsize, scaling, rise, cid, ncs, graphicstate: PDFGraphicState):
def render_char(self, char: str, bbox: tuple, textstate: PDFTextState, graphicstate: PDFGraphicState, ncs: PDFColorSpace):
return NotImplementedError
+11 -45
View File
@@ -3,69 +3,35 @@ from ..utils import apply_matrix_norm, apply_matrix_pt, matrix2str, Bbox
from ..interpreter.PDFGraphicState import PDFGraphicState
from ..interpreter.PDFFont import PDFFont
from ..interpreter.PDFColorSpace import PDFColorSpace
from ..interpreter.PDFTextState import PDFTextState
## LTChar
##
class LTChar(LTComponent):
def __init__(self,
matrix,
font: PDFFont,
fontsize: float,
scaling: float,
rise: float,
text: str,
textwidth: float,
textdisp: tuple,
bbox: Bbox,
char: str,
textstate: PDFTextState,
ncs: PDFColorSpace,
graphicstate: PDFGraphicState
):
self._text = text
self.matrix = matrix
self.font = font
self._text = char
self.font = textstate.font
self.textstate = textstate
self.ncs = ncs
self.graphicstate = graphicstate
self.adv = textwidth * fontsize * scaling
# compute the boundary rectangle.
if font.is_vertical():
# vertical
width = font.get_width() * fontsize
(vx, vy) = textdisp
if vx is None:
vx = width * 0.5
else:
vx = vx * fontsize * .001
vy = (1000 - vy) * fontsize * .001
tx = -vx
ty = vy + rise
bll = (tx, ty+self.adv)
bur = (tx+width, ty)
else:
# horizontal
height = font.get_height() * fontsize
descent = font.get_descent() * fontsize
ty = descent + rise
bll = (0, ty)
bur = (self.adv, ty+height)
(a, b, c, d, _, _) = self.matrix
self.upright = (0 < a*d*scaling and b*c <= 0)
(x0, y0) = apply_matrix_pt(self.matrix, bll)
(x1, y1) = apply_matrix_pt(self.matrix, bur)
if x1 < x0:
(x0, x1) = (x1, x0)
if y1 < y0:
(y0, y1) = (y1, y0)
LTComponent.__init__(self, Bbox(x0, y0, x1, y1))
if font.is_vertical():
LTComponent.__init__(self, bbox)
if self.font.is_vertical():
self.size = self.width
else:
self.size = self.height
return
def __repr__(self):
return ('<%s %s matrix=%s font=%r adv=%s text=%r>' %
return ('<%s %s matrix=%s font=%r text=%r>' %
(self.__class__.__name__, str(self.bbox),
matrix2str(self.matrix), self.fontname, self.adv,
matrix2str(self.matrix), self.fontname,
self.get_text()))
def get_text(self):
+19
View File
@@ -0,0 +1,19 @@
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 -20
View File
@@ -19,6 +19,8 @@ from ..interpreter.PDFFont import PDFUnicodeNotDefined
from ..interpreter.PDFResourceManager import PDFResourceManager
from ..interpreter.PDFGraphicState import PDFGraphicState, PDFGraphicStateColor
from ..interpreter.PDFdevice import PDFTextDevice
from ..interpreter.PDFTextState import PDFTextState
from ..interpreter.PDFColorSpace import PDFColorSpace
from ..interpreter.PDFPage import PDFPage
from ..interpreter.PDFStream import PDFStream
from ..interpreter.types import CurvePath
@@ -164,8 +166,6 @@ class PDFLayoutAnalyzer(PDFTextDevice):
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,
@@ -175,24 +175,15 @@ class PDFLayoutAnalyzer(PDFTextDevice):
fill=gstate.ncolor
))
def render_char(self, matrix: List[Point], font, fontsize, scaling, rise, cid, ncs, graphicstate):
try:
text = font.to_unichr(cid)
assert isinstance(text, str), str(type(text))
except PDFUnicodeNotDefined:
text = self.handle_undefined_char(font, cid)
textwidth = font.char_width(cid)
textdisp = font.char_disp(cid)
item = LTChar(
matrix,
font, fontsize,
scaling,
rise, text,
textwidth, textdisp,
ncs, graphicstate
)
self.cur_item.add(item)
return item.adv
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,
textstate=textstate,
ncs=ncs,
graphicstate=graphicstate
))
def handle_undefined_char(self, font: str, cid: int):
log.info('undefined: %r, %r', font, cid)