tesseract  4.1.1
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  * TSV Text Renderer interface implementation
144  **********************************************************************/
145 TessTsvRenderer::TessTsvRenderer(const char* outputbase)
146  : TessResultRenderer(outputbase, "tsv") {
147  font_info_ = false;
148 }
149 
150 TessTsvRenderer::TessTsvRenderer(const char* outputbase, bool font_info)
151  : TessResultRenderer(outputbase, "tsv") {
152  font_info_ = font_info;
153 }
154 
156  // Output TSV column headings
157  AppendString(
158  "level\tpage_num\tblock_num\tpar_num\tline_num\tword_"
159  "num\tleft\ttop\twidth\theight\tconf\ttext\n");
160  return true;
161 }
162 
163 bool TessTsvRenderer::EndDocumentHandler() { return true; }
164 
166  const std::unique_ptr<const char[]> tsv(api->GetTSVText(imagenum()));
167  if (tsv == nullptr) return false;
168 
169  AppendString(tsv.get());
170 
171  return true;
172 }
173 
174 /**********************************************************************
175  * UNLV Text Renderer interface implementation
176  **********************************************************************/
177 TessUnlvRenderer::TessUnlvRenderer(const char *outputbase)
178  : TessResultRenderer(outputbase, "unlv") {
179 }
180 
182  const std::unique_ptr<const char[]> unlv(api->GetUNLVText());
183  if (unlv == nullptr) return false;
184 
185  AppendString(unlv.get());
186 
187  return true;
188 }
189 
190 /**********************************************************************
191  * BoxText Renderer interface implementation
192  **********************************************************************/
194  : TessResultRenderer(outputbase, "box") {
195 }
196 
198  const std::unique_ptr<const char[]> text(api->GetBoxText(imagenum()));
199  if (text == nullptr) return false;
200 
201  AppendString(text.get());
202 
203  return true;
204 }
205 
206 #ifndef DISABLED_LEGACY_ENGINE
207 
208 /**********************************************************************
209  * Osd Text Renderer interface implementation
210  **********************************************************************/
211 TessOsdRenderer::TessOsdRenderer(const char* outputbase)
212  : TessResultRenderer(outputbase, "osd") {}
213 
215  char* osd = api->GetOsdText(imagenum());
216  if (osd == nullptr) return false;
217 
218  AppendString(osd);
219  delete[] osd;
220 
221  return true;
222 }
223 
224 #endif // ndef DISABLED_LEGACY_ENGINE
225 
226 } // namespace tesseract
TessTsvRenderer(const char *outputbase, bool font_info)
Definition: renderer.cpp:150
const char * title() const
Definition: renderer.h:88
bool EndDocumentHandler() override
Definition: renderer.cpp:163
void insert(TessResultRenderer *next)
Definition: renderer.cpp:59
void AppendString(const char *s)
Definition: renderer.cpp:102
virtual bool BeginDocumentHandler()
Definition: renderer.cpp:110
const char * GetStringVariable(const char *name) const
Definition: baseapi.cpp:314
bool Serialize(FILE *fp, const char *data, size_t n)
Definition: serialis.cpp:60
bool BeginDocument(const char *title)
Definition: renderer.cpp:72
char * GetOsdText(int page_number)
Definition: baseapi.cpp:1717
TessBoxTextRenderer(const char *outputbase)
Definition: renderer.cpp:193
TessOsdRenderer(const char *outputbase)
Definition: renderer.cpp:211
const char * string() const
Definition: strngs.cpp:194
TessResultRenderer * next()
Definition: renderer.h:58
bool AddImageHandler(TessBaseAPI *api) override
Definition: renderer.cpp:214
char * GetBoxText(int page_number)
Definition: baseapi.cpp:1520
TessResultRenderer(const char *outputbase, const char *extension)
Definition: renderer.cpp:33
virtual bool EndDocumentHandler()
Definition: renderer.cpp:114
bool AddImageHandler(TessBaseAPI *api) override
Definition: renderer.cpp:126
char * GetTSVText(int page_number)
Definition: baseapi.cpp:1383
TessUnlvRenderer(const char *outputbase)
Definition: renderer.cpp:177
Definition: strngs.h:45
bool BeginDocumentHandler() override
Definition: renderer.cpp:155
bool AddImageHandler(TessBaseAPI *api) override
Definition: renderer.cpp:181
bool AddImageHandler(TessBaseAPI *api) override
Definition: renderer.cpp:165
bool AddImage(TessBaseAPI *api)
Definition: renderer.cpp:83
TessTextRenderer(const char *outputbase)
Definition: renderer.cpp:122
virtual bool AddImageHandler(TessBaseAPI *api)=0
void AppendData(const char *s, int len)
Definition: renderer.cpp:106
bool AddImageHandler(TessBaseAPI *api) override
Definition: renderer.cpp:197