tesseract  4.0.0-1-g2a2b
renderer.h
Go to the documentation of this file.
1 // File: renderer.h
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 #ifndef TESSERACT_API_RENDERER_H_
19 #define TESSERACT_API_RENDERER_H_
20 
21 // To avoid collision with other typenames include the ABSOLUTE MINIMUM
22 // complexity of includes here. Use forward declarations wherever possible
23 // and hide includes of complex types in baseapi.cpp.
24 #include <string> // for std::string
25 #include "genericvector.h"
26 #include "platform.h"
27 
28 namespace tesseract {
29 
30 class TessBaseAPI;
31 
46  public:
47  virtual ~TessResultRenderer();
48 
49  // Takes ownership of pointer so must be new'd instance.
50  // Renderers aren't ordered, but appends the sequences of next parameter
51  // and existing next(). The renderers should be unique across both lists.
52  void insert(TessResultRenderer* next);
53 
54  // Returns the next renderer or nullptr.
55  TessResultRenderer* next() { return next_; }
56 
62  bool BeginDocument(const char* title);
63 
72  bool AddImage(TessBaseAPI* api);
73 
78  bool EndDocument();
79 
80  const char* file_extension() const { return file_extension_; }
81  const char* title() const { return title_.c_str(); }
82 
83  // Is everything fine? Otherwise something went wrong.
84  bool happy() { return happy_; }
85 
95  int imagenum() const { return imagenum_; }
96 
97  protected:
108  TessResultRenderer(const char *outputbase,
109  const char* extension);
110 
111  // Hook for specialized handling in BeginDocument()
112  virtual bool BeginDocumentHandler();
113 
114  // This must be overridden to render the OCR'd results
115  virtual bool AddImageHandler(TessBaseAPI* api) = 0;
116 
117  // Hook for specialized handling in EndDocument()
118  virtual bool EndDocumentHandler();
119 
120  // Renderers can call this to append '\0' terminated strings into
121  // the output string returned by GetOutput.
122  // This method will grow the output buffer if needed.
123  void AppendString(const char* s);
124 
125  // Renderers can call this to append binary byte sequences into
126  // the output string returned by GetOutput. Note that s is not necessarily
127  // '\0' terminated (and can contain '\0' within it).
128  // This method will grow the output buffer if needed.
129  void AppendData(const char* s, int len);
130 
131  private:
132  const char* file_extension_; // standard extension for generated output
133  STRING title_; // title of document being renderered
134  int imagenum_; // index of last image added
135 
136  FILE* fout_; // output file pointer
137  TessResultRenderer* next_; // Can link multiple renderers together
138  bool happy_; // I get grumpy when the disk fills up, etc.
139 };
140 
145  public:
146  explicit TessTextRenderer(const char *outputbase);
147 
148  protected:
149  virtual bool AddImageHandler(TessBaseAPI* api);
150 };
151 
156  public:
157  explicit TessHOcrRenderer(const char *outputbase, bool font_info);
158  explicit TessHOcrRenderer(const char *outputbase);
159 
160  protected:
161  virtual bool BeginDocumentHandler();
162  virtual bool AddImageHandler(TessBaseAPI* api);
163  virtual bool EndDocumentHandler();
164 
165  private:
166  bool font_info_; // whether to print font information
167 };
168 
173  public:
174  explicit TessTsvRenderer(const char* outputbase, bool font_info);
175  explicit TessTsvRenderer(const char* outputbase);
176 
177  protected:
178  virtual bool BeginDocumentHandler();
179  virtual bool AddImageHandler(TessBaseAPI* api);
180  virtual bool EndDocumentHandler();
181 
182  private:
183  bool font_info_; // whether to print font information
184 };
185 
190  public:
191  // datadir is the location of the TESSDATA. We need it because
192  // we load a custom PDF font from this location.
193  TessPDFRenderer(const char* outputbase, const char* datadir, bool textonly = false);
194 
195  protected:
196  virtual bool BeginDocumentHandler();
197  virtual bool AddImageHandler(TessBaseAPI* api);
198  virtual bool EndDocumentHandler();
199 
200  private:
201  // We don't want to have every image in memory at once,
202  // so we store some metadata as we go along producing
203  // PDFs one page at a time. At the end, that metadata is
204  // used to make everything that isn't easily handled in a
205  // streaming fashion.
206  long int obj_; // counter for PDF objects
207  GenericVector<long int> offsets_; // offset of every PDF object in bytes
208  GenericVector<long int> pages_; // object number for every /Page object
209  std::string datadir_; // where to find the custom font
210  bool textonly_; // skip images if set
211  // Bookkeeping only. DIY = Do It Yourself.
212  void AppendPDFObjectDIY(size_t objectsize);
213  // Bookkeeping + emit data.
214  void AppendPDFObject(const char *data);
215  // Create the /Contents object for an entire page.
216  char* GetPDFTextObjects(TessBaseAPI* api, double width, double height);
217  // Turn an image into a PDF object. Only transcode if we have to.
218  static bool imageToPDFObj(Pix* pix, const char* filename, long int objnum,
219  char** pdf_object, long int* pdf_object_size, const int jpg_quality);
220 };
221 
222 
227  public:
228  explicit TessUnlvRenderer(const char *outputbase);
229 
230  protected:
231  virtual bool AddImageHandler(TessBaseAPI* api);
232 };
233 
238  public:
239  explicit TessBoxTextRenderer(const char *outputbase);
240 
241  protected:
242  virtual bool AddImageHandler(TessBaseAPI* api);
243 };
244 
245 #ifndef DISABLED_LEGACY_ENGINE
246 
251  public:
252  explicit TessOsdRenderer(const char* outputbase);
253 
254  protected:
255  virtual bool AddImageHandler(TessBaseAPI* api);
256 };
257 
258 #endif // ndef DISABLED_LEGACY_ENGINE
259 
260 } // namespace tesseract.
261 
262 #endif // TESSERACT_API_RENDERER_H_
TessResultRenderer * next()
Definition: renderer.h:55
struct TessUnlvRenderer TessUnlvRenderer
Definition: capi.h:87
#define TESS_API
Definition: platform.h:74
struct TessPDFRenderer TessPDFRenderer
Definition: capi.h:86
struct TessBaseAPI TessBaseAPI
Definition: capi.h:89
struct TessTextRenderer TessTextRenderer
Definition: capi.h:84
const char * file_extension() const
Definition: renderer.h:80
struct TessHOcrRenderer TessHOcrRenderer
Definition: capi.h:85
void insert(LIST list, void *node)
Definition: oldlist.cpp:201
const char * title() const
Definition: renderer.h:81
Definition: strngs.h:45
struct TessResultRenderer TessResultRenderer
Definition: capi.h:83
struct TessBoxTextRenderer TessBoxTextRenderer
Definition: capi.h:88