Make text API more like an accessor
This commit is contained in:
@@ -445,23 +445,21 @@ class PikepdfCanvas:
|
||||
class PikepdfText:
|
||||
"""Text object for rendering text on a pikepdf canvas."""
|
||||
|
||||
def __init__(self, x=0, y=0, direction=TextDirection.LTR):
|
||||
def __init__(self, direction=TextDirection.LTR):
|
||||
self._cs = ContentStreamBuilder()
|
||||
self._cs.begin_text()
|
||||
self._p0 = (x, y)
|
||||
self._direction = direction
|
||||
|
||||
def set_font(self, font, size):
|
||||
def font(self, font, size):
|
||||
self._cs.set_text_font(Name("/f-0-0"), size)
|
||||
return self
|
||||
|
||||
def set_render_mode(self, mode):
|
||||
def render_mode(self, mode):
|
||||
self._cs.set_text_rendering(mode)
|
||||
return self
|
||||
|
||||
def set_text_transform(self, matrix: Matrix):
|
||||
def text_transform(self, matrix: Matrix):
|
||||
self._cs.set_text_matrix(matrix)
|
||||
self._p0 = (matrix.e, matrix.f)
|
||||
return self
|
||||
|
||||
def show(self, text: str):
|
||||
@@ -473,13 +471,10 @@ class PikepdfText:
|
||||
self._cs.end_marked_content()
|
||||
return self
|
||||
|
||||
def set_horiz_scale(self, scale):
|
||||
def horiz_scale(self, scale):
|
||||
self._cs.set_text_horizontal_scaling(scale)
|
||||
return self
|
||||
|
||||
def get_start_of_line(self):
|
||||
return self._p0
|
||||
|
||||
def move_cursor(self, x, y):
|
||||
self._cs.move_cursor(x, y)
|
||||
return self
|
||||
|
||||
@@ -272,9 +272,9 @@ class HocrTransform:
|
||||
# on a sloped baseline and the edge of the bounding box.
|
||||
line_box_height = abs(line_box.height) / cos(angle)
|
||||
fontsize = line_box_height + intercept
|
||||
text.set_font(fontname, fontsize)
|
||||
text.font(fontname, fontsize)
|
||||
if invisible_text or True:
|
||||
text.set_render_mode(3) # Invisible (indicates OCR text)
|
||||
text.render_mode(3) # Invisible (indicates OCR text)
|
||||
|
||||
self._debug_draw_baseline(
|
||||
canvas, line_matrix.inverse().transform(line_box), 0
|
||||
@@ -325,8 +325,8 @@ class HocrTransform:
|
||||
|
||||
# If this word is 0 units wide, our best bet seems to be to suppress this text
|
||||
if font_width > 0:
|
||||
text.set_text_transform(Matrix(1, 0, 0, 1, box.llx, 0))
|
||||
text.set_horiz_scale(100 * box.width / font_width)
|
||||
text.text_transform(Matrix(1, 0, 0, 1, box.llx, 0))
|
||||
text.horiz_scale(100 * box.width / font_width)
|
||||
text.show(elemtxt)
|
||||
|
||||
# Get coordinates of the next word (if there is one)
|
||||
@@ -342,14 +342,12 @@ class HocrTransform:
|
||||
next_box = line_matrix.inverse().transform(hocr_next_box)
|
||||
if text_direction == TextDirection.LTR:
|
||||
space_box = Rectangle(box.urx, box.lly, next_box.llx, next_box.ury)
|
||||
self._debug_draw_space_bbox(canvas, space_box)
|
||||
text.set_text_transform(Matrix(1, 0, 0, 1, space_box.llx, 0))
|
||||
elif text_direction == TextDirection.RTL:
|
||||
space_box = Rectangle(next_box.urx, box.lly, box.llx, next_box.ury)
|
||||
self._debug_draw_space_bbox(canvas, space_box)
|
||||
text.set_text_transform(Matrix(1, 0, 0, 1, space_box.llx, 0))
|
||||
self._debug_draw_space_bbox(canvas, space_box)
|
||||
text.text_transform(Matrix(1, 0, 0, 1, space_box.llx, 0))
|
||||
space_width = canvas.string_width(' ', fontname, fontsize)
|
||||
text.set_horiz_scale(100 * space_box.width / space_width)
|
||||
text.horiz_scale(100 * space_box.width / space_width)
|
||||
text.show(' ')
|
||||
|
||||
def _debug_draw_paragraph_boxes(self, canvas: Canvas, color=CYAN):
|
||||
|
||||
Reference in New Issue
Block a user