tesseract  4.00.00dev
renderer.cpp
Go to the documentation of this file.
1 // File: renderer.cpp
3 // Description: Rendering interface to inject into TessBaseAPI
4 //
5 // (C) Copyright 2011, Google Inc.
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
17 
18 #ifdef HAVE_CONFIG_H
19 #include "config_auto.h"
20 #endif
21 
22 #include <string.h>
23 #include <memory> // std::unique_ptr
24 #include "baseapi.h"
25 #include "genericvector.h"
26 #include "renderer.h"
27 
28 namespace tesseract {
29 
30 /**********************************************************************
31  * Base Renderer interface implementation
32  **********************************************************************/
34  const char* extension)
35  : file_extension_(extension),
36  title_(""), imagenum_(-1),
37  fout_(stdout),
38  next_(NULL),
39  happy_(true) {
40  if (strcmp(outputbase, "-") && strcmp(outputbase, "stdout")) {
41  STRING outfile = STRING(outputbase) + STRING(".") + STRING(file_extension_);
42  fout_ = fopen(outfile.string(), "wb");
43  if (fout_ == NULL) {
44  happy_ = false;
45  }
46  }
47 }
48 
50  if (fout_ != nullptr) {
51  if (fout_ != stdout)
52  fclose(fout_);
53  else
54  clearerr(fout_);
55  }
56  delete next_;
57 }
58 
60  if (next == NULL) return;
61 
62  TessResultRenderer* remainder = next_;
63  next_ = next;
64  if (remainder) {
65  while (next->next_ != NULL) {
66  next = next->next_;
67  }
68  next->next_ = remainder;
69  }
70 }
71 
73  if (!happy_) return false;
74  title_ = title;
75  imagenum_ = -1;
76  bool ok = BeginDocumentHandler();
77  if (next_) {
78  ok = next_->BeginDocument(title) && ok;
79  }
80  return ok;
81 }
82 
84  if (!happy_) return false;
85  ++imagenum_;
86  bool ok = AddImageHandler(api);
87  if (next_) {
88  ok = next_->AddImage(api) && ok;
89  }
90  return ok;
91 }
92 
94  if (!happy_) return false;
95  bool ok = EndDocumentHandler();
96  if (next_) {
97  ok = next_->EndDocument() && ok;
98  }
99  return ok;
100 }
101 
102 void TessResultRenderer::AppendString(const char* s) {
103  AppendData(s, strlen(s));
104 }
105 
106 void TessResultRenderer::AppendData(const char* s, int len) {
107  int n = fwrite(s, 1, len, fout_);
108  if (n != len) happy_ = false;
109 }
110 
112  return happy_;
113 }
114 
116  return happy_;
117 }
118 
119 
120 /**********************************************************************
121  * UTF8 Text Renderer interface implementation
122  **********************************************************************/
123 TessTextRenderer::TessTextRenderer(const char *outputbase)
124  : TessResultRenderer(outputbase, "txt") {
125 }
126 
128  const std::unique_ptr<const char[]> utf8(api->GetUTF8Text());
129  if (utf8 == NULL) {
130  return false;
131  }
132 
133  AppendString(utf8.get());
134 
135  const char* pageSeparator = api->GetStringVariable("page_separator");
136  if (pageSeparator != nullptr && *pageSeparator != '\0') {
137  AppendString(pageSeparator);
138  }
139 
140  return true;
141 }
142 
143 /**********************************************************************
144  * HOcr Text Renderer interface implementation
145  **********************************************************************/
146 TessHOcrRenderer::TessHOcrRenderer(const char *outputbase)
147  : TessResultRenderer(outputbase, "hocr") {
148  font_info_ = false;
149 }
150 
151 TessHOcrRenderer::TessHOcrRenderer(const char *outputbase, bool font_info)
152  : TessResultRenderer(outputbase, "hocr") {
153  font_info_ = font_info;
154 }
155 
157  AppendString(
158  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
159  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
160  " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
161  "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" "
162  "lang=\"en\">\n <head>\n <title>");
163  AppendString(title());
164  AppendString(
165  "</title>\n"
166  "<meta http-equiv=\"Content-Type\" content=\"text/html;"
167  "charset=utf-8\" />\n"
168  " <meta name='ocr-system' content='tesseract " TESSERACT_VERSION_STR
169  "' />\n"
170  " <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par"
171  " ocr_line ocrx_word");
172  if (font_info_)
173  AppendString(
174  " ocrp_lang ocrp_dir ocrp_font ocrp_fsize ocrp_wconf");
175  AppendString(
176  "'/>\n"
177  "</head>\n<body>\n");
178 
179  return true;
180 }
181 
183  AppendString(" </body>\n</html>\n");
184 
185  return true;
186 }
187 
189  const std::unique_ptr<const char[]> hocr(api->GetHOCRText(imagenum()));
190  if (hocr == NULL) return false;
191 
192  AppendString(hocr.get());
193 
194  return true;
195 }
196 
197 /**********************************************************************
198  * TSV Text Renderer interface implementation
199  **********************************************************************/
200 TessTsvRenderer::TessTsvRenderer(const char* outputbase)
201  : TessResultRenderer(outputbase, "tsv") {
202  font_info_ = false;
203 }
204 
205 TessTsvRenderer::TessTsvRenderer(const char* outputbase, bool font_info)
206  : TessResultRenderer(outputbase, "tsv") {
207  font_info_ = font_info;
208 }
209 
211  // Output TSV column headings
212  AppendString(
213  "level\tpage_num\tblock_num\tpar_num\tline_num\tword_"
214  "num\tleft\ttop\twidth\theight\tconf\ttext\n");
215  return true;
216 }
217 
218 bool TessTsvRenderer::EndDocumentHandler() { return true; }
219 
221  const std::unique_ptr<const char[]> tsv(api->GetTSVText(imagenum()));
222  if (tsv == NULL) return false;
223 
224  AppendString(tsv.get());
225 
226  return true;
227 }
228 
229 /**********************************************************************
230  * UNLV Text Renderer interface implementation
231  **********************************************************************/
232 TessUnlvRenderer::TessUnlvRenderer(const char *outputbase)
233  : TessResultRenderer(outputbase, "unlv") {
234 }
235 
237  const std::unique_ptr<const char[]> unlv(api->GetUNLVText());
238  if (unlv == NULL) return false;
239 
240  AppendString(unlv.get());
241 
242  return true;
243 }
244 
245 /**********************************************************************
246  * BoxText Renderer interface implementation
247  **********************************************************************/
249  : TessResultRenderer(outputbase, "box") {
250 }
251 
253  const std::unique_ptr<const char[]> text(api->GetBoxText(imagenum()));
254  if (text == NULL) return false;
255 
256  AppendString(text.get());
257 
258  return true;
259 }
260 
261 /**********************************************************************
262  * Osd Text Renderer interface implementation
263  **********************************************************************/
264 TessOsdRenderer::TessOsdRenderer(const char* outputbase)
265  : TessResultRenderer(outputbase, "osd") {}
266 
268  char* osd = api->GetOsdText(imagenum());
269  if (osd == NULL) return false;
270 
271  AppendString(osd);
272  delete[] osd;
273 
274  return true;
275 }
276 
277 } // namespace tesseract
bool AddImage(TessBaseAPI *api)
Definition: renderer.cpp:83
virtual bool EndDocumentHandler()
Definition: renderer.cpp:115
TessHOcrRenderer(const char *outputbase, bool font_info)
Definition: renderer.cpp:151
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:210
char * GetTSVText(int page_number)
Definition: baseapi.cpp:1599
virtual bool EndDocumentHandler()
Definition: renderer.cpp:182
TessOsdRenderer(const char *outputbase)
Definition: renderer.cpp:264
TessTextRenderer(const char *outputbase)
Definition: renderer.cpp:123
char * GetBoxText(int page_number)
Definition: baseapi.cpp:1727
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:220
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:267
TessBoxTextRenderer(const char *outputbase)
Definition: renderer.cpp:248
TessUnlvRenderer(const char *outputbase)
Definition: renderer.cpp:232
const char * string() const
Definition: strngs.cpp:198
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:236
Definition: strngs.h:45
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:188
TessResultRenderer(const char *outputbase, const char *extension)
Definition: renderer.cpp:33
const char * title() const
Definition: renderer.h:81
void insert(TessResultRenderer *next)
Definition: renderer.cpp:59
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:111
bool BeginDocument(const char *title)
Definition: renderer.cpp:72
char * GetOsdText(int page_number)
Definition: baseapi.cpp:1922
TessTsvRenderer(const char *outputbase, bool font_info)
Definition: renderer.cpp:205
const char * GetStringVariable(const char *name) const
Definition: baseapi.cpp:300
virtual bool EndDocumentHandler()
Definition: renderer.cpp:218
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:252
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:156
void AppendString(const char *s)
Definition: renderer.cpp:102
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:127
void AppendData(const char *s, int len)
Definition: renderer.cpp:106
TessResultRenderer * next()
Definition: renderer.h:55
char * GetHOCRText(ETEXT_DESC *monitor, int page_number)
Definition: baseapi.cpp:1427
#define TESSERACT_VERSION_STR
Definition: version.h:8
virtual bool AddImageHandler(TessBaseAPI *api)=0