tesseract  5.0.0-alpha-619-ge9db
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 
50 class StringRenderer {
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  }
107  void set_strip_unrenderable_words(bool val) {
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.
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.
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
197  bool output_word_boxes_;
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_;
209  int box_padding_;
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_
string
std::string string
Definition: equationdetect_test.cc:21
tesseract::StringRenderer::render_fullwidth_latin_
bool render_fullwidth_latin_
Definition: stringrenderer.h:187
tesseract::StringRenderer::RenderToImage
int RenderToImage(const char *text, int text_length, Pix **pix)
Definition: stringrenderer.cpp:750
tesseract::StringRenderer::underline_start_prob_
double underline_start_prob_
Definition: stringrenderer.h:188
tesseract::StringRenderer::char_spacing_
int char_spacing_
Definition: stringrenderer.h:183
tesseract::StringRenderer::underline_style_
PangoUnderline underline_style_
Definition: stringrenderer.h:190
tesseract::StringRenderer::ClearBoxes
void ClearBoxes()
Definition: stringrenderer.cpp:354
tesseract::StringRenderer::page_
int page_
Definition: stringrenderer.h:204
tesseract::StringRenderer::set_output_word_boxes
void set_output_word_boxes(bool val)
Definition: stringrenderer.h:109
tesseract::StringRenderer::StripUnrenderableWords
int StripUnrenderableWords(std::string *utf8_text) const
Definition: stringrenderer.cpp:632
tesseract::StringRenderer::page_width_
int page_width_
Definition: stringrenderer.h:180
tesseract::StringRenderer::GetClusterStrings
bool GetClusterStrings(std::vector< std::string > *cluster_text)
Definition: stringrenderer.cpp:371
tesseract::StringRenderer::font
const PangoFontInfo & font() const
Definition: stringrenderer.h:130
tesseract::StringRenderer::InsertWordJoiners
static std::string InsertWordJoiners(const std::string &text)
Definition: stringrenderer.cpp:688
tesseract::StringRenderer::h_margin
int h_margin() const
Definition: stringrenderer.h:133
tesseract::StringRenderer::cr_
cairo_t * cr_
Definition: stringrenderer.h:199
tesseract::StringRenderer::ComputeClusterBoxes
void ComputeClusterBoxes()
Definition: stringrenderer.cpp:477
tesseract::StringRenderer::resolution_
int resolution_
Definition: stringrenderer.h:184
tesseract::StringRenderer::font_index_
unsigned int font_index_
Definition: stringrenderer.h:215
tesseract::StringRenderer::font_
PangoFontInfo font_
Definition: stringrenderer.h:178
tesseract::StringRenderer::boxchars_
std::vector< BoxChar * > boxchars_
Definition: stringrenderer.h:207
tesseract::StringRenderer::RenderAllFontsToImage
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, std::string *font_used, Pix **pix)
Definition: stringrenderer.cpp:851
tesseract::StringRenderer::gravity_hint_strong_
bool gravity_hint_strong_
Definition: stringrenderer.h:186
tesseract::StringRenderer::drop_uncovered_chars_
bool drop_uncovered_chars_
Definition: stringrenderer.h:193
tesseract::StringRenderer::output_word_boxes_
bool output_word_boxes_
Definition: stringrenderer.h:196
tesseract::StringRenderer::set_char_spacing
void set_char_spacing(int char_spacing)
Definition: stringrenderer.h:69
tesseract::StringRenderer::set_add_ligatures
void set_add_ligatures(bool add_ligatures)
Definition: stringrenderer.h:115
tesseract::StringRenderer::set_drop_uncovered_chars
void set_drop_uncovered_chars(bool val)
Definition: stringrenderer.h:103
tesseract::StringRenderer::page_boxes_
Boxa * page_boxes_
Definition: stringrenderer.h:210
tesseract::StringRenderer::strip_unrenderable_words_
bool strip_unrenderable_words_
Definition: stringrenderer.h:194
tesseract::PangoFontInfo
Definition: pango_font_info.h:39
tesseract::StringRenderer::ConvertFullwidthLatinToBasicLatin
static std::string ConvertFullwidthLatinToBasicLatin(const std::string &text)
Definition: stringrenderer.cpp:731
tesseract::StringRenderer::set_pen_color
void set_pen_color(double r, double g, double b)
Definition: stringrenderer.h:119
tesseract::StringRenderer::set_features
void set_features(const char *features)
Definition: stringrenderer.h:93
tesseract::StringRenderer::page_height_
int page_height_
Definition: stringrenderer.h:180
tesseract::StringRenderer::box_padding_
int box_padding_
Definition: stringrenderer.h:208
tesseract::StringRenderer::set_strip_unrenderable_words
void set_strip_unrenderable_words(bool val)
Definition: stringrenderer.h:106
tesseract::StringRenderer::vertical_text_
bool vertical_text_
Definition: stringrenderer.h:185
tesseract::StringRenderer::set_render_fullwidth_latin
void set_render_fullwidth_latin(bool render_fullwidth_latin)
Definition: stringrenderer.h:80
tesseract::StringRenderer::features_
char * features_
Definition: stringrenderer.h:191
tesseract::StringRenderer::underline_continuation_prob_
double underline_continuation_prob_
Definition: stringrenderer.h:189
tesseract::StringRenderer::leading_
int leading_
Definition: stringrenderer.h:184
tesseract::StringRenderer::CorrectBoxPositionsToLayout
void CorrectBoxPositionsToLayout(std::vector< BoxChar * > *boxchars)
Definition: stringrenderer.cpp:619
pango_font_info.h
tesseract::StringRenderer::set_underline_continuation_prob
void set_underline_continuation_prob(const double frac)
Definition: stringrenderer.cpp:157
tesseract::StringRenderer::start_box_
int start_box_
Definition: stringrenderer.h:203
tesseract::StringRenderer::FindFirstPageBreakOffset
int FindFirstPageBreakOffset(const char *text, int text_length)
Definition: stringrenderer.cpp:296
tesseract
Definition: baseapi.h:65
tesseract::StringRenderer::h_margin_
int h_margin_
Definition: stringrenderer.h:180
tesseract::StringRenderer::set_underline_style
void set_underline_style(const PangoUnderline style)
Definition: stringrenderer.h:90
tesseract::StringRenderer::v_margin_
int v_margin_
Definition: stringrenderer.h:180
tesseract::StringRenderer::set_gravity_hint_strong
void set_gravity_hint_strong(bool gravity_hint_strong)
Definition: stringrenderer.h:77
tesseract::StringRenderer::SetWordUnderlineAttributes
void SetWordUnderlineAttributes(const std::string &page_text)
Definition: stringrenderer.cpp:250
tesseract::StringRenderer::GetPageBoxes
Boxa * GetPageBoxes() const
Definition: stringrenderer.cpp:344
tesseract::StringRenderer::RenderToBinaryImage
int RenderToBinaryImage(const char *text, int text_length, int threshold, Pix **pix)
Definition: stringrenderer.cpp:670
tesseract::StringRenderer
Definition: stringrenderer.h:49
tesseract::StringRenderer::set_resolution
void set_resolution(const int resolution)
Definition: stringrenderer.cpp:148
tesseract::StringRenderer::RotatePageBoxes
void RotatePageBoxes(float rotation)
Definition: stringrenderer.cpp:348
tesseract::StringRenderer::ConvertBasicLatinToFullwidthLatin
static std::string ConvertBasicLatinToFullwidthLatin(const std::string &text)
Definition: stringrenderer.cpp:711
tesseract::StringRenderer::GetBoxesStr
std::string GetBoxesStr()
Definition: stringrenderer.cpp:360
tesseract::StringRenderer::set_v_margin
void set_v_margin(const int v_margin)
Definition: stringrenderer.h:127
tesseract::StringRenderer::FreePangoCairo
void FreePangoCairo()
Definition: stringrenderer.cpp:235
tesseract::StringRenderer::char_map_
std::unordered_map< char32, int64_t > char_map_
Definition: stringrenderer.h:213
tesseract::StringRenderer::RenderToGrayscaleImage
int RenderToGrayscaleImage(const char *text, int text_length, Pix **pix)
Definition: stringrenderer.cpp:659
tesseract::StringRenderer::set_leading
void set_leading(int leading)
Definition: stringrenderer.h:70
tesseract::StringRenderer::pen_color_
double pen_color_[3]
Definition: stringrenderer.h:182
tesseract::StringRenderer::add_ligatures_
bool add_ligatures_
Definition: stringrenderer.h:195
tesseract::StringRenderer::SetLayoutProperties
void SetLayoutProperties()
Definition: stringrenderer.cpp:189
tesseract::StringRenderer::WriteAllBoxes
void WriteAllBoxes(const std::string &filename)
Definition: stringrenderer.cpp:365
tesseract::StringRenderer::GetBoxes
const std::vector< BoxChar * > & GetBoxes() const
Definition: stringrenderer.cpp:340
tesseract::StringRenderer::set_underline_start_prob
void set_underline_start_prob(const double frac)
Definition: stringrenderer.cpp:153
tesseract::StringRenderer::total_chars_
int total_chars_
Definition: stringrenderer.h:214
tesseract::StringRenderer::set_h_margin
void set_h_margin(const int h_margin)
Definition: stringrenderer.h:124
tesseract::StringRenderer::~StringRenderer
~StringRenderer()
Definition: stringrenderer.cpp:161
tesseract::StringRenderer::v_margin
int v_margin() const
Definition: stringrenderer.h:134
tesseract::StringRenderer::set_vertical_text
void set_vertical_text(bool vertical_text)
Definition: stringrenderer.h:74
tesseract::StringRenderer::set_font
bool set_font(const std::string &desc)
Definition: stringrenderer.cpp:142
tesseract::StringRenderer::set_box_padding
void set_box_padding(int val)
Definition: stringrenderer.h:100
tesseract::StringRenderer::layout_
PangoLayout * layout_
Definition: stringrenderer.h:200
tesseract::StringRenderer::set_page
void set_page(int page)
Definition: stringrenderer.h:97
tesseract::StringRenderer::InitPangoCairo
void InitPangoCairo()
Definition: stringrenderer.cpp:167
tesseract::StringRenderer::StringRenderer
StringRenderer(const std::string &font_desc, int page_width, int page_height)
Definition: stringrenderer.cpp:107
tesseract::StringRenderer::surface_
cairo_surface_t * surface_
Definition: stringrenderer.h:198
tesseract::StringRenderer::last_offset_
int last_offset_
Definition: stringrenderer.h:216