tesseract  4.0.0-1-g2a2b
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 <cstring>
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_(nullptr),
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_ == nullptr) {
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 == nullptr) return;
61 
62  TessResultRenderer* remainder = next_;
63  next_ = next;
64  if (remainder) {
65  while (next->next_ != nullptr) {
66  next = next->next_;
67  }
68  next->next_ = remainder;
69  }
70 }
71 
72 bool TessResultRenderer::BeginDocument(const char* title) {
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  if (!tesseract::Serialize(fout_, s, len)) happy_ = false;
108 }
109 
111  return happy_;
112 }
113 
115  return happy_;
116 }
117 
118 
119 /**********************************************************************
120  * UTF8 Text Renderer interface implementation
121  **********************************************************************/
122 TessTextRenderer::TessTextRenderer(const char *outputbase)
123  : TessResultRenderer(outputbase, "txt") {
124 }
125 
127  const std::unique_ptr<const char[]> utf8(api->GetUTF8Text());
128  if (utf8 == nullptr) {
129  return false;
130  }
131 
132  AppendString(utf8.get());
133 
134  const char* pageSeparator = api->GetStringVariable("page_separator");
135  if (pageSeparator != nullptr && *pageSeparator != '\0') {
136  AppendString(pageSeparator);
137  }
138 
139  return true;
140 }
141 
142 /**********************************************************************
143  * HOcr Text Renderer interface implementation
144  **********************************************************************/
145 TessHOcrRenderer::TessHOcrRenderer(const char *outputbase)
146  : TessResultRenderer(outputbase, "hocr") {
147  font_info_ = false;
148 }
149 
150 TessHOcrRenderer::TessHOcrRenderer(const char *outputbase, bool font_info)
151  : TessResultRenderer(outputbase, "hocr") {
152  font_info_ = font_info;
153 }
154 
156  AppendString(
157  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
158  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n"
159  " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
160  "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" "
161  "lang=\"en\">\n <head>\n <title>");
162  AppendString(title());
163  AppendString(
164  "</title>\n"
165  "<meta http-equiv=\"Content-Type\" content=\"text/html;"
166  "charset=utf-8\" />\n"
167  " <meta name='ocr-system' content='tesseract " PACKAGE_VERSION
168  "' />\n"
169  " <meta name='ocr-capabilities' content='ocr_page ocr_carea ocr_par"
170  " ocr_line ocrx_word ocrp_wconf");
171  if (font_info_)
172  AppendString(
173  " ocrp_lang ocrp_dir ocrp_font ocrp_fsize");
174  AppendString(
175  "'/>\n"
176  "</head>\n<body>\n");
177 
178  return true;
179 }
180 
182  AppendString(" </body>\n</html>\n");
183 
184  return true;
185 }
186 
188  const std::unique_ptr<const char[]> hocr(api->GetHOCRText(imagenum()));
189  if (hocr == nullptr) return false;
190 
191  AppendString(hocr.get());
192 
193  return true;
194 }
195 
196 /**********************************************************************
197  * TSV Text Renderer interface implementation
198  **********************************************************************/
199 TessTsvRenderer::TessTsvRenderer(const char* outputbase)
200  : TessResultRenderer(outputbase, "tsv") {
201  font_info_ = false;
202 }
203 
204 TessTsvRenderer::TessTsvRenderer(const char* outputbase, bool font_info)
205  : TessResultRenderer(outputbase, "tsv") {
206  font_info_ = font_info;
207 }
208 
210  // Output TSV column headings
211  AppendString(
212  "level\tpage_num\tblock_num\tpar_num\tline_num\tword_"
213  "num\tleft\ttop\twidth\theight\tconf\ttext\n");
214  return true;
215 }
216 
217 bool TessTsvRenderer::EndDocumentHandler() { return true; }
218 
220  const std::unique_ptr<const char[]> tsv(api->GetTSVText(imagenum()));
221  if (tsv == nullptr) return false;
222 
223  AppendString(tsv.get());
224 
225  return true;
226 }
227 
228 /**********************************************************************
229  * UNLV Text Renderer interface implementation
230  **********************************************************************/
231 TessUnlvRenderer::TessUnlvRenderer(const char *outputbase)
232  : TessResultRenderer(outputbase, "unlv") {
233 }
234 
236  const std::unique_ptr<const char[]> unlv(api->GetUNLVText());
237  if (unlv == nullptr) return false;
238 
239  AppendString(unlv.get());
240 
241  return true;
242 }
243 
244 /**********************************************************************
245  * BoxText Renderer interface implementation
246  **********************************************************************/
248  : TessResultRenderer(outputbase, "box") {
249 }
250 
252  const std::unique_ptr<const char[]> text(api->GetBoxText(imagenum()));
253  if (text == nullptr) return false;
254 
255  AppendString(text.get());
256 
257  return true;
258 }
259 
260 #ifndef DISABLED_LEGACY_ENGINE
261 
262 /**********************************************************************
263  * Osd Text Renderer interface implementation
264  **********************************************************************/
265 TessOsdRenderer::TessOsdRenderer(const char* outputbase)
266  : TessResultRenderer(outputbase, "osd") {}
267 
269  char* osd = api->GetOsdText(imagenum());
270  if (osd == nullptr) return false;
271 
272  AppendString(osd);
273  delete[] osd;
274 
275  return true;
276 }
277 
278 #endif // ndef DISABLED_LEGACY_ENGINE
279 
280 } // namespace tesseract
TessResultRenderer * next()
Definition: renderer.h:55
char * GetHOCRText(ETEXT_DESC *monitor, int page_number)
Definition: baseapi.cpp:1485
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:126
char * GetTSVText(int page_number)
Definition: baseapi.cpp:1707
const char * string() const
Definition: strngs.cpp:196
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:155
TessBoxTextRenderer(const char *outputbase)
Definition: renderer.cpp:247
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:59
const char * GetStringVariable(const char *name) const
Definition: baseapi.cpp:321
char * GetBoxText(int page_number)
Definition: baseapi.cpp:1844
virtual bool AddImageHandler(TessBaseAPI *api)=0
bool BeginDocument(const char *title)
Definition: renderer.cpp:72
TessTextRenderer(const char *outputbase)
Definition: renderer.cpp:122
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:268
virtual bool EndDocumentHandler()
Definition: renderer.cpp:181
TessUnlvRenderer(const char *outputbase)
Definition: renderer.cpp:231
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:219
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:209
void AppendData(const char *s, int len)
Definition: renderer.cpp:106
bool AddImage(TessBaseAPI *api)
Definition: renderer.cpp:83
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:235
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:251
const char * title() const
Definition: renderer.h:81
TessResultRenderer(const char *outputbase, const char *extension)
Definition: renderer.cpp:33
TessOsdRenderer(const char *outputbase)
Definition: renderer.cpp:265
void insert(TessResultRenderer *next)
Definition: renderer.cpp:59
void AppendString(const char *s)
Definition: renderer.cpp:102
Definition: strngs.h:45
virtual bool AddImageHandler(TessBaseAPI *api)
Definition: renderer.cpp:187
virtual bool EndDocumentHandler()
Definition: renderer.cpp:217
virtual bool EndDocumentHandler()
Definition: renderer.cpp:114
TessHOcrRenderer(const char *outputbase, bool font_info)
Definition: renderer.cpp:150
char * GetOsdText(int page_number)
Definition: baseapi.cpp:2041
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:110
TessTsvRenderer(const char *outputbase, bool font_info)
Definition: renderer.cpp:204