def set_djvu_text(ctx, filename, text): doc = ctx.new_document(decode.FileUri(filename)) doc.decoding_job.wait() page = doc.pages[0] w, h = page.width, page.height data = sexpr.Expression([sexpr.Symbol('page'), 0, 0, w - 1, h - 1, text]) script = 'select 1; set-txt; ' + data.as_string() ret = subprocess.run(['djvused', '-s', '-e', script, filename]) if ret.returncode != 0: raise ChildProcessError('Failed to create DJVU text layer')
def sexpr(self): children = [ child.sexpr if isinstance(child, Zone) else child for child in self.children if not isinstance(child, Space) ] or [''] x0, y0, x1, y1 = self.bbox if x0 > x1: x0, x1 = x1, x0 elif x0 == x1: x1 += 1 if y0 > y1: y0, y1 = y1, y0 elif y0 == y1: y1 += 1 assert x0 < x1 assert y0 < y1 return sexpr.Expression([self.type, x0, y0, x1, y1] + children)
for k in range(i, j): bbox.update(zones[k].bbox) last_word = Zone(type=const.TEXT_ZONE_WORD, bbox=bbox) words += [last_word] if details > TEXT_DETAILS_CHARACTER: last_word += [subtext] else: last_word += [ Zone(type=const.TEXT_ZONE_CHARACTER, bbox=(x0, y0, x1, y1), children=[ch]) for k in range(i, j) for (x0, y0, x1, y1), ch in [(zones[k].bbox, text[k])] ] i = j return words try: sexpr.Expression(0).as_string(escape_unicode=True) except TypeError: # python-djvulibre << 0.4 def print_sexpr(expr, file, width=None): return expr.print_into(file, width=width) else: # python-djvulibre >= 0.4 def print_sexpr(expr, file, width=None): return expr.print_into(file, width=width, escape_unicode=False) # vim:ts=4 sts=4 sw=4 et
def sexpr(self): children = [ child.sexpr if isinstance(child, Zone) else child for child in self.children if not isinstance(child, Space) ] or [''] return sexpr.Expression([self.type] + list(self.bbox) + children)