1# © 2020 James R. Barlow: github.com/jbarlow83
2#
3# Permission is hereby granted, free of charge, to any person obtaining a
4# copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish,
7# distribute, sublicense, and/or sell copies of the Software, and to
8# permit persons to whom the Software is furnished to do so, subject to
9# the following conditions:
10#
11# The above copyright notice and this permission notice shall be included
12# in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22"""Tesseract no-op plugin
23
24To quickly run tests where getting OCR output is not necessary.
25
26In 'hocr' mode, create a .hocr file that specifies no text found.
27
28In 'pdf' mode, convert the image to PDF using another program.
29
30In orientation check mode, report the orientation is upright.
31"""
32
33import pikepdf
34from PIL import Image
35
36from ocrmypdf import OcrEngine, OrientationConfidence, hookimpl
37
38HOCR_TEMPLATE = '''<?xml version="1.0" encoding="UTF-8"?>
39<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
40    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
41<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
42 <head>
43  <title></title>
44  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
45  <meta name='ocr-system' content='tesseract 4.0.0' />
46  <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par ocr_line ocrx_word'/>
47 </head>
48 <body>
49  <div class='ocr_page' id='page_1' title='image "x.tif"; bbox 0 0 {0} {1}; ppageno 0'>
50   <div class='ocr_carea' id='block_1_1' title="bbox 0 1 {0} {1}">
51    <p class='ocr_par' dir='ltr' id='par_1' title="bbox 0 1 {0} {1}">
52     <span class='ocr_line' id='line_1' title="bbox 0 1 {0} {1}"><span class='ocrx_word' id='word_1' title="bbox 0 1 {0} {1}"> </span>
53     </span>
54    </p>
55   </div>
56  </div>
57 </body>
58</html>'''
59
60
61class NoopOcrEngine(OcrEngine):
62    @staticmethod
63    def version():
64        return '4.0.0'
65
66    @staticmethod
67    def creator_tag(options):
68        tag = '-PDF' if options.pdf_renderer == 'sandwich' else ''
69        return f"NO-OP {tag} {NoopOcrEngine.version()}"
70
71    def __str__(self):
72        return f"NO-OP {NoopOcrEngine.version()}"
73
74    @staticmethod
75    def languages(options):
76        return {'eng'}
77
78    @staticmethod
79    def get_orientation(input_file, options):
80        return OrientationConfidence(angle=0, confidence=0.0)
81
82    @staticmethod
83    def generate_hocr(input_file, output_hocr, output_text, options):
84        with Image.open(input_file) as im, open(
85            output_hocr, 'w', encoding='utf-8'
86        ) as f:
87            w, h = im.size
88            f.write(HOCR_TEMPLATE.format(str(w), str(h)))
89        with open(output_text, 'w') as f:
90            f.write('')
91
92    @staticmethod
93    def generate_pdf(input_file, output_pdf, output_text, options):
94        with Image.open(input_file) as im:
95            dpi = im.info['dpi']
96            pagesize = im.size[0] / dpi[0], im.size[1] / dpi[1]
97        ptsize = pagesize[0] * 72, pagesize[1] * 72
98        pdf = pikepdf.new()
99        pdf.add_blank_page(page_size=ptsize)
100        pdf.save(output_pdf, static_id=True)
101        output_text.write_text('')
102
103
104@hookimpl
105def get_ocr_engine():
106    return NoopOcrEngine()
107