All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * 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 <vector>
34 
35 #include "hashfn.h"
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 namespace tesseract {
45 
46 class BoxChar;
47 
49  public:
50  StringRenderer(const string& font_desc, int page_width, int page_height);
52 
53  // Renders the text with the chosen font and returns the byte offset upto
54  // which the text could be rendered so as to fit the specified page
55  // dimensions.
56  int RenderToImage(const char* text, int text_length, Pix** pix);
57  int RenderToGrayscaleImage(const char* text, int text_length, Pix** pix);
58  int RenderToBinaryImage(const char* text, int text_length, int threshold,
59  Pix** pix);
60  // Renders a line of text with all available fonts that were able to render
61  // at least min_coverage fraction of the input text. Use 1.0 to require that
62  // a font be able to render all the text.
63  int RenderAllFontsToImage(double min_coverage, const char* text,
64  int text_length, string* font_used, Pix** pix);
65 
66  bool set_font(const string& desc);
67  void set_char_spacing(double char_spacing) {
68  char_spacing_ = char_spacing;
69  }
70  void set_leading(int leading) {
71  leading_ = leading;
72  }
73  void set_resolution(const int resolution);
74  void set_vertical_text(bool vertical_text) {
75  vertical_text_ = vertical_text;
76  }
77  void set_gravity_hint_strong(bool gravity_hint_strong) {
78  gravity_hint_strong_ = gravity_hint_strong;
79  }
80  void set_render_fullwidth_latin(bool render_fullwidth_latin) {
81  render_fullwidth_latin_ = render_fullwidth_latin;
82  }
83  // Sets the probability (value in [0, 1]) of starting to render a word with an
84  // underline. This implementation consider words to be space-delimited
85  // sequences of characters.
86  void set_underline_start_prob(const double frac);
87  // Set the probability (value in [0, 1]) of continuing a started underline to
88  // the next word.
89  void set_underline_continuation_prob(const double frac);
90  void set_underline_style(const PangoUnderline style) {
91  underline_style_ = style;
92  }
93  void set_page(int page) {
94  page_ = page;
95  }
96  void set_box_padding(int val) {
97  box_padding_ = val;
98  }
99  void set_drop_uncovered_chars(bool val) {
100  drop_uncovered_chars_ = val;
101  }
104  }
105  void set_output_word_boxes(bool val) {
106  output_word_boxes_ = val;
107  }
108  // Before rendering the string, replace latin characters with their optional
109  // ligatured forms (such as "fi", "ffi" etc.) if the font_ covers those
110  // unicodes.
111  void set_add_ligatures(bool add_ligatures) {
112  add_ligatures_ = add_ligatures;
113  }
114  // Set the rgb value of the text ink. Values range in [0, 1.0]
115  void set_pen_color(double r, double g, double b) {
116  pen_color_[0] = r;
117  pen_color_[1] = g;
118  pen_color_[2] = b;
119  }
120  void set_h_margin(const int h_margin) {
122  }
123  void set_v_margin(const int v_margin) {
125  }
126  const PangoFontInfo& font() const {
127  return font_;
128  }
129  const int h_margin() const {
130  return h_margin_;
131  }
132  const int v_margin() const {
133  return v_margin_;
134  }
135 
136  // Get the boxchars of all clusters rendered thus far (or since the last call
137  // to ClearBoxes()).
138  const vector<BoxChar*>& GetBoxes() const;
139  // Get the rendered page bounding boxes of all pages created thus far (or
140  // since last call to ClearBoxes()).
141  Boxa* GetPageBoxes() const;
142 
143  // Rotate the boxes on the most recent page by the given rotation.
144  void RotatePageBoxes(float rotation);
145  // Delete all boxes.
146  void ClearBoxes();
147  void WriteAllBoxes(const string& filename);
148  // Removes space-delimited words from the string that are not renderable by
149  // the current font and returns the count of such words.
150  int StripUnrenderableWords(string* utf8_text) const;
151 
152  // Insert a Word Joiner symbol (U+2060) between adjacent characters, excluding
153  // spaces and combining types, in each word before rendering to ensure words
154  // are not broken across lines. The output boxchars will not contain the
155  // joiner.
156  static string InsertWordJoiners(const string& text);
157 
158  // Helper functions to convert fullwidth Latin and halfwidth Basic Latin.
159  static string ConvertBasicLatinToFullwidthLatin(const string& text);
160  static string ConvertFullwidthLatinToBasicLatin(const string& text);
161 
162  protected:
163  // Init and free local renderer objects.
164  void InitPangoCairo();
165  void FreePangoCairo();
166  // Set rendering properties.
167  void SetLayoutProperties();
168  void SetWordUnderlineAttributes(const string& page_text);
169  // Compute bounding boxes around grapheme clusters.
170  void ComputeClusterBoxes();
171  void CorrectBoxPositionsToLayout(vector<BoxChar*>* boxchars);
172  bool GetClusterStrings(vector<string>* cluster_text);
173  int FindFirstPageBreakOffset(const char* text, int text_length);
174 
176  // Page properties
178  // Text rendering properties
179  int pen_color_[3];
187  PangoUnderline underline_style_;
188  // Text filtering options
193  // Pango and cairo specific objects
194  cairo_surface_t* surface_;
195  cairo_t* cr_;
196  PangoLayout* layout_;
197  // Internal state of current page number, updated on successive calls to
198  // RenderToImage()
200  int page_;
201  // Boxes and associated text for all pages rendered with RenderToImage() since
202  // the last call to ClearBoxes().
203  vector<BoxChar*> boxchars_;
205  // Bounding boxes for pages since the last call to ClearBoxes().
206  Boxa* page_boxes_;
207 
208  // Objects cached for subsequent calls to RenderAllFontsToImage()
209  hash_map<char32, inT64> char_map_; // Time-saving char histogram.
210  int total_chars_; // Number in the string to be rendered.
211  int font_index_; // Index of next font to use in font list.
212  int last_offset_; // Offset returned from last successful rendering
213 
214  private:
216  void operator=(const StringRenderer&);
217 };
218 } // namespace tesseract
219 
220 #endif // THIRD_PARTY_TESSERACT_TRAINING_STRINGRENDERER_H_
cairo_surface_t * surface_
void set_gravity_hint_strong(bool gravity_hint_strong)
const int v_margin() const
void set_render_fullwidth_latin(bool render_fullwidth_latin)
void WriteAllBoxes(const string &filename)
int RenderToGrayscaleImage(const char *text, int text_length, Pix **pix)
const PangoFontInfo & font() const
PangoUnderline underline_style_
void set_strip_unrenderable_words(bool val)
void set_resolution(const int resolution)
void set_leading(int leading)
const int h_margin() const
void set_underline_start_prob(const double frac)
void set_vertical_text(bool vertical_text)
void RotatePageBoxes(float rotation)
int RenderToBinaryImage(const char *text, int text_length, int threshold, Pix **pix)
vector< BoxChar * > boxchars_
void set_box_padding(int val)
int StripUnrenderableWords(string *utf8_text) const
void set_underline_continuation_prob(const double frac)
void set_char_spacing(double char_spacing)
void set_underline_style(const PangoUnderline style)
const vector< BoxChar * > & GetBoxes() const
void set_drop_uncovered_chars(bool val)
hash_map< char32, inT64 > char_map_
int FindFirstPageBreakOffset(const char *text, int text_length)
void SetWordUnderlineAttributes(const string &page_text)
void set_add_ligatures(bool add_ligatures)
bool set_font(const string &desc)
void set_h_margin(const int h_margin)
void set_v_margin(const int v_margin)
static string InsertWordJoiners(const string &text)
bool GetClusterStrings(vector< string > *cluster_text)
static string ConvertBasicLatinToFullwidthLatin(const string &text)
StringRenderer(const string &font_desc, int page_width, int page_height)
static string ConvertFullwidthLatinToBasicLatin(const string &text)
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, string *font_used, Pix **pix)
void set_output_word_boxes(bool val)
void CorrectBoxPositionsToLayout(vector< BoxChar * > *boxchars)
int RenderToImage(const char *text, int text_length, Pix **pix)
void set_pen_color(double r, double g, double b)