tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
stringrenderer.h
1 /**********************************************************************
2  * File: stringrenderer.h
3  * Description: Class for rendering UTF-8 text to an image, and retrieving
4  * bounding boxes around each grapheme cluster.
5  *
6  * Instances are created using a font description string
7  * (eg. "Arial Italic 12"; see pango_font_info.h for the format)
8  * and the page dimensions. Other renderer properties such as
9  * spacing, ligaturization, as well a preprocessing behavior such
10  * as removal of unrenderable words and a special n-gram mode may
11  * be set using respective set_* methods.
12  *
13  * Author: Ranjith Unnikrishnan
14  * Created: Mon Nov 18 2013
15  *
16  * (C) Copyright 2013, Google Inc.
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  * http://www.apache.org/licenses/LICENSE-2.0
21  * Unless required by applicable law or agreed to in writing, software
22  * distributed under the License is distributed on an "AS IS" BASIS,
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24  * See the License for the specific language governing permissions and
25  * limitations under the License.
26  *
27  **********************************************************************/
28 
29 #ifndef TESSERACT_TRAINING_STRINGRENDERER_H_
30 #define TESSERACT_TRAINING_STRINGRENDERER_H_
31 
32 #include <string>
33 #include <unordered_map>
34 #include <vector>
35 
36 #include "host.h"
37 #include "pango_font_info.h"
38 #include "pango/pango-layout.h"
39 #include "pango/pangocairo.h"
40 
41 struct Boxa;
42 struct Pix;
43 
44 #ifdef _MSC_VER
45 # define strdup(s) _strdup(s)
46 #endif
47 
48 namespace tesseract {
49 
50 class BoxChar;
51 
53  public:
54  StringRenderer(const std::string& font_desc, int page_width, int page_height);
56 
57  // Renders the text with the chosen font and returns the byte offset up to
58  // which the text could be rendered so as to fit the specified page
59  // dimensions.
60  int RenderToImage(const char* text, int text_length, Pix** pix);
61  int RenderToGrayscaleImage(const char* text, int text_length, Pix** pix);
62  int RenderToBinaryImage(const char* text, int text_length, int threshold,
63  Pix** pix);
64  // Renders a line of text with all available fonts that were able to render
65  // at least min_coverage fraction of the input text. Use 1.0 to require that
66  // a font be able to render all the text.
67  int RenderAllFontsToImage(double min_coverage, const char* text,
68  int text_length, std::string* font_used, Pix** pix);
69 
70  bool set_font(const std::string& desc);
71  // Char spacing is in PIXELS!!!!.
72  void set_char_spacing(int char_spacing) { char_spacing_ = char_spacing; }
73  void set_leading(int leading) {
74  leading_ = leading;
75  }
76  void set_resolution(const int resolution);
77  void set_vertical_text(bool vertical_text) {
78  vertical_text_ = vertical_text;
79  }
80  void set_gravity_hint_strong(bool gravity_hint_strong) {
81  gravity_hint_strong_ = gravity_hint_strong;
82  }
83  void set_render_fullwidth_latin(bool render_fullwidth_latin) {
84  render_fullwidth_latin_ = render_fullwidth_latin;
85  }
86  // Sets the probability (value in [0, 1]) of starting to render a word with an
87  // underline. This implementation consider words to be space-delimited
88  // sequences of characters.
89  void set_underline_start_prob(const double frac);
90  // Set the probability (value in [0, 1]) of continuing a started underline to
91  // the next word.
92  void set_underline_continuation_prob(const double frac);
93  void set_underline_style(const PangoUnderline style) {
94  underline_style_ = style;
95  }
96  void set_features(const char* features) {
97  free(features_);
98  features_ = strdup(features);
99  }
100  void set_page(int page) {
101  page_ = page;
102  }
103  void set_box_padding(int val) {
104  box_padding_ = val;
105  }
106  void set_drop_uncovered_chars(bool val) {
107  drop_uncovered_chars_ = val;
108  }
111  }
112  void set_output_word_boxes(bool val) {
113  output_word_boxes_ = val;
114  }
115  // Before rendering the string, replace latin characters with their optional
116  // ligatured forms (such as "fi", "ffi" etc.) if the font_ covers those
117  // unicodes.
118  void set_add_ligatures(bool add_ligatures) {
119  add_ligatures_ = add_ligatures;
120  }
121  // Set the rgb value of the text ink. Values range in [0, 1.0]
122  void set_pen_color(double r, double g, double b) {
123  pen_color_[0] = r;
124  pen_color_[1] = g;
125  pen_color_[2] = b;
126  }
127  void set_h_margin(const int h_margin) {
129  }
130  void set_v_margin(const int v_margin) {
132  }
133  const PangoFontInfo& font() const {
134  return font_;
135  }
136  int h_margin() const { return h_margin_; }
137  int v_margin() const { return v_margin_; }
138 
139  // Get the boxchars of all clusters rendered thus far (or since the last call
140  // to ClearBoxes()).
141  const std::vector<BoxChar*>& GetBoxes() const;
142  // Get the rendered page bounding boxes of all pages created thus far (or
143  // since last call to ClearBoxes()).
144  Boxa* GetPageBoxes() const;
145 
146  // Rotate the boxes on the most recent page by the given rotation.
147  void RotatePageBoxes(float rotation);
148  // Delete all boxes.
149  void ClearBoxes();
150  // Returns the boxes in a boxfile string.
151  std::string GetBoxesStr();
152  // Writes the boxes to a boxfile.
153  void WriteAllBoxes(const std::string& filename);
154  // Removes space-delimited words from the string that are not renderable by
155  // the current font and returns the count of such words.
156  int StripUnrenderableWords(std::string* utf8_text) const;
157 
158  // Insert a Word Joiner symbol (U+2060) between adjacent characters, excluding
159  // spaces and combining types, in each word before rendering to ensure words
160  // are not broken across lines. The output boxchars will not contain the
161  // joiner.
162  static std::string InsertWordJoiners(const std::string& text);
163 
164  // Helper functions to convert fullwidth Latin and halfwidth Basic Latin.
165  static std::string ConvertBasicLatinToFullwidthLatin(const std::string& text);
166  static std::string ConvertFullwidthLatinToBasicLatin(const std::string& text);
167 
168  protected:
169  // Init and free local renderer objects.
170  void InitPangoCairo();
171  void FreePangoCairo();
172  // Set rendering properties.
173  void SetLayoutProperties();
174  void SetWordUnderlineAttributes(const std::string& page_text);
175  // Compute bounding boxes around grapheme clusters.
176  void ComputeClusterBoxes();
177  void CorrectBoxPositionsToLayout(std::vector<BoxChar*>* boxchars);
178  bool GetClusterStrings(std::vector<std::string>* cluster_text);
179  int FindFirstPageBreakOffset(const char* text, int text_length);
180 
182  // Page properties
184  // Text rendering properties
185  double pen_color_[3];
193  PangoUnderline underline_style_;
194  char* features_;
195  // Text filtering options
200  // Pango and cairo specific objects
201  cairo_surface_t* surface_;
202  cairo_t* cr_;
203  PangoLayout* layout_;
204  // Internal state of current page number, updated on successive calls to
205  // RenderToImage()
207  int page_;
208  // Boxes and associated text for all pages rendered with RenderToImage() since
209  // the last call to ClearBoxes().
210  std::vector<BoxChar*> boxchars_;
212  // Bounding boxes for pages since the last call to ClearBoxes().
213  Boxa* page_boxes_;
214 
215  // Objects cached for subsequent calls to RenderAllFontsToImage()
216  std::unordered_map<char32, int64_t> char_map_; // Time-saving char histogram.
217  int total_chars_; // Number in the string to be rendered.
218  unsigned int font_index_; // Index of next font to use in font list.
219  int last_offset_; // Offset returned from last successful rendering
220 
221  private:
223  void operator=(const StringRenderer&);
224 };
225 } // namespace tesseract
226 
227 #endif // THIRD_PARTY_TESSERACT_TRAINING_STRINGRENDERER_H_
~StringRenderer()
Definition: stringrenderer.cpp:144
void set_resolution(const int resolution)
Definition: stringrenderer.cpp:131
int total_chars_
Definition: stringrenderer.h:217
double pen_color_[3]
Definition: stringrenderer.h:185
bool drop_uncovered_chars_
Definition: stringrenderer.h:196
PangoUnderline underline_style_
Definition: stringrenderer.h:193
void set_underline_start_prob(const double frac)
Definition: stringrenderer.cpp:136
int v_margin_
Definition: stringrenderer.h:183
int page_width_
Definition: stringrenderer.h:183
void SetLayoutProperties()
Definition: stringrenderer.cpp:172
void set_output_word_boxes(bool val)
Definition: stringrenderer.h:112
cairo_t * cr_
Definition: stringrenderer.h:202
void set_v_margin(const int v_margin)
Definition: stringrenderer.h:130
void set_gravity_hint_strong(bool gravity_hint_strong)
Definition: stringrenderer.h:80
void set_add_ligatures(bool add_ligatures)
Definition: stringrenderer.h:118
Boxa * page_boxes_
Definition: stringrenderer.h:213
bool set_font(const std::string &desc)
Definition: stringrenderer.cpp:125
cairo_surface_t * surface_
Definition: stringrenderer.h:201
void RotatePageBoxes(float rotation)
Definition: stringrenderer.cpp:331
bool output_word_boxes_
Definition: stringrenderer.h:199
int RenderToImage(const char *text, int text_length, Pix **pix)
Definition: stringrenderer.cpp:733
int leading_
Definition: stringrenderer.h:187
int box_padding_
Definition: stringrenderer.h:211
int last_offset_
Definition: stringrenderer.h:219
int StripUnrenderableWords(std::string *utf8_text) const
Definition: stringrenderer.cpp:615
static std::string ConvertBasicLatinToFullwidthLatin(const std::string &text)
Definition: stringrenderer.cpp:694
void InitPangoCairo()
Definition: stringrenderer.cpp:150
bool strip_unrenderable_words_
Definition: stringrenderer.h:197
int FindFirstPageBreakOffset(const char *text, int text_length)
Definition: stringrenderer.cpp:279
Definition: baseapi.cpp:94
int page_
Definition: stringrenderer.h:207
int start_box_
Definition: stringrenderer.h:206
void ClearBoxes()
Definition: stringrenderer.cpp:337
void set_pen_color(double r, double g, double b)
Definition: stringrenderer.h:122
void set_page(int page)
Definition: stringrenderer.h:100
void set_drop_uncovered_chars(bool val)
Definition: stringrenderer.h:106
void set_box_padding(int val)
Definition: stringrenderer.h:103
void set_underline_continuation_prob(const double frac)
Definition: stringrenderer.cpp:140
void operator=(const StringRenderer &)
static std::string ConvertFullwidthLatinToBasicLatin(const std::string &text)
Definition: stringrenderer.cpp:714
unsigned int font_index_
Definition: stringrenderer.h:218
void set_render_fullwidth_latin(bool render_fullwidth_latin)
Definition: stringrenderer.h:83
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, std::string *font_used, Pix **pix)
Definition: stringrenderer.cpp:834
std::string GetBoxesStr()
Definition: stringrenderer.cpp:343
Definition: pango_font_info.h:44
const PangoFontInfo & font() const
Definition: stringrenderer.h:133
void WriteAllBoxes(const std::string &filename)
Definition: stringrenderer.cpp:348
int v_margin() const
Definition: stringrenderer.h:137
void set_features(const char *features)
Definition: stringrenderer.h:96
Boxa * GetPageBoxes() const
Definition: stringrenderer.cpp:327
bool GetClusterStrings(std::vector< std::string > *cluster_text)
Definition: stringrenderer.cpp:354
int h_margin() const
Definition: stringrenderer.h:136
const std::vector< BoxChar * > & GetBoxes() const
Definition: stringrenderer.cpp:323
bool vertical_text_
Definition: stringrenderer.h:188
StringRenderer(const std::string &font_desc, int page_width, int page_height)
Definition: stringrenderer.cpp:90
int page_height_
Definition: stringrenderer.h:183
Definition: stringrenderer.h:52
int resolution_
Definition: stringrenderer.h:187
bool gravity_hint_strong_
Definition: stringrenderer.h:189
void set_char_spacing(int char_spacing)
Definition: stringrenderer.h:72
static std::string InsertWordJoiners(const std::string &text)
Definition: stringrenderer.cpp:671
void FreePangoCairo()
Definition: stringrenderer.cpp:218
void set_vertical_text(bool vertical_text)
Definition: stringrenderer.h:77
double underline_continuation_prob_
Definition: stringrenderer.h:192
int char_spacing_
Definition: stringrenderer.h:186
void set_leading(int leading)
Definition: stringrenderer.h:73
int RenderToGrayscaleImage(const char *text, int text_length, Pix **pix)
Definition: stringrenderer.cpp:642
PangoFontInfo font_
Definition: stringrenderer.h:181
void set_strip_unrenderable_words(bool val)
Definition: stringrenderer.h:109
char * features_
Definition: stringrenderer.h:194
void set_underline_style(const PangoUnderline style)
Definition: stringrenderer.h:93
int RenderToBinaryImage(const char *text, int text_length, int threshold, Pix **pix)
Definition: stringrenderer.cpp:653
void CorrectBoxPositionsToLayout(std::vector< BoxChar *> *boxchars)
Definition: stringrenderer.cpp:602
bool add_ligatures_
Definition: stringrenderer.h:198
bool render_fullwidth_latin_
Definition: stringrenderer.h:190
void set_h_margin(const int h_margin)
Definition: stringrenderer.h:127
std::vector< BoxChar * > boxchars_
Definition: stringrenderer.h:210
void SetWordUnderlineAttributes(const std::string &page_text)
Definition: stringrenderer.cpp:233
void ComputeClusterBoxes()
Definition: stringrenderer.cpp:460
std::unordered_map< char32, int64_t > char_map_
Definition: stringrenderer.h:216
PangoLayout * layout_
Definition: stringrenderer.h:203
double underline_start_prob_
Definition: stringrenderer.h:191
int h_margin_
Definition: stringrenderer.h:183