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