tesseract  4.00.00dev
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 <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 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 up to
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_features(const char* features) {
94  free(features_);
95  features_ = strdup(features);
96  }
97  void set_page(int page) {
98  page_ = page;
99  }
100  void set_box_padding(int val) {
101  box_padding_ = val;
102  }
103  void set_drop_uncovered_chars(bool val) {
104  drop_uncovered_chars_ = val;
105  }
108  }
109  void set_output_word_boxes(bool val) {
110  output_word_boxes_ = val;
111  }
112  // Before rendering the string, replace latin characters with their optional
113  // ligatured forms (such as "fi", "ffi" etc.) if the font_ covers those
114  // unicodes.
115  void set_add_ligatures(bool add_ligatures) {
116  add_ligatures_ = add_ligatures;
117  }
118  // Set the rgb value of the text ink. Values range in [0, 1.0]
119  void set_pen_color(double r, double g, double b) {
120  pen_color_[0] = r;
121  pen_color_[1] = g;
122  pen_color_[2] = b;
123  }
124  void set_h_margin(const int h_margin) {
126  }
127  void set_v_margin(const int v_margin) {
129  }
130  const PangoFontInfo& font() const {
131  return font_;
132  }
133  int h_margin() const { return h_margin_; }
134  int v_margin() const { return v_margin_; }
135 
136  // Get the boxchars of all clusters rendered thus far (or since the last call
137  // to ClearBoxes()).
138  const std::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  // Returns the boxes in a boxfile string.
148  string GetBoxesStr();
149  // Writes the boxes to a boxfile.
150  void WriteAllBoxes(const string& filename);
151  // Removes space-delimited words from the string that are not renderable by
152  // the current font and returns the count of such words.
153  int StripUnrenderableWords(string* utf8_text) const;
154 
155  // Insert a Word Joiner symbol (U+2060) between adjacent characters, excluding
156  // spaces and combining types, in each word before rendering to ensure words
157  // are not broken across lines. The output boxchars will not contain the
158  // joiner.
159  static string InsertWordJoiners(const string& text);
160 
161  // Helper functions to convert fullwidth Latin and halfwidth Basic Latin.
162  static string ConvertBasicLatinToFullwidthLatin(const string& text);
163  static string ConvertFullwidthLatinToBasicLatin(const string& text);
164 
165  protected:
166  // Init and free local renderer objects.
167  void InitPangoCairo();
168  void FreePangoCairo();
169  // Set rendering properties.
170  void SetLayoutProperties();
171  void SetWordUnderlineAttributes(const string& page_text);
172  // Compute bounding boxes around grapheme clusters.
173  void ComputeClusterBoxes();
174  void CorrectBoxPositionsToLayout(std::vector<BoxChar*>* boxchars);
175  bool GetClusterStrings(std::vector<string>* cluster_text);
176  int FindFirstPageBreakOffset(const char* text, int text_length);
177 
179  // Page properties
181  // Text rendering properties
182  int pen_color_[3];
190  PangoUnderline underline_style_;
191  char* features_;
192  // Text filtering options
197  // Pango and cairo specific objects
198  cairo_surface_t* surface_;
199  cairo_t* cr_;
200  PangoLayout* layout_;
201  // Internal state of current page number, updated on successive calls to
202  // RenderToImage()
204  int page_;
205  // Boxes and associated text for all pages rendered with RenderToImage() since
206  // the last call to ClearBoxes().
207  std::vector<BoxChar*> boxchars_;
209  // Bounding boxes for pages since the last call to ClearBoxes().
210  Boxa* page_boxes_;
211 
212  // Objects cached for subsequent calls to RenderAllFontsToImage()
213  std::unordered_map<char32, inT64> char_map_; // Time-saving char histogram.
214  int total_chars_; // Number in the string to be rendered.
215  int font_index_; // Index of next font to use in font list.
216  int last_offset_; // Offset returned from last successful rendering
217 
218  private:
220  void operator=(const StringRenderer&);
221 };
222 } // namespace tesseract
223 
224 #endif // THIRD_PARTY_TESSERACT_TRAINING_STRINGRENDERER_H_
void WriteAllBoxes(const string &filename)
bool GetClusterStrings(std::vector< string > *cluster_text)
PangoUnderline underline_style_
std::vector< BoxChar * > boxchars_
bool set_font(const string &desc)
static string ConvertBasicLatinToFullwidthLatin(const string &text)
void set_add_ligatures(bool add_ligatures)
StringRenderer(const string &font_desc, int page_width, int page_height)
void set_strip_unrenderable_words(bool val)
int RenderToImage(const char *text, int text_length, Pix **pix)
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, string *font_used, Pix **pix)
void set_features(const char *features)
int StripUnrenderableWords(string *utf8_text) const
void set_gravity_hint_strong(bool gravity_hint_strong)
int FindFirstPageBreakOffset(const char *text, int text_length)
int RenderToBinaryImage(const char *text, int text_length, int threshold, Pix **pix)
void set_underline_continuation_prob(const double frac)
static string ConvertFullwidthLatinToBasicLatin(const string &text)
void set_underline_style(const PangoUnderline style)
void SetWordUnderlineAttributes(const string &page_text)
void set_underline_start_prob(const double frac)
std::unordered_map< char32, inT64 > char_map_
void set_drop_uncovered_chars(bool val)
void set_char_spacing(double char_spacing)
void set_pen_color(double r, double g, double b)
void set_resolution(const int resolution)
static string InsertWordJoiners(const string &text)
void set_leading(int leading)
const char * filename
Definition: ioapi.h:38
void set_render_fullwidth_latin(bool render_fullwidth_latin)
int RenderToGrayscaleImage(const char *text, int text_length, Pix **pix)
void set_vertical_text(bool vertical_text)
void set_v_margin(const int v_margin)
const std::vector< BoxChar * > & GetBoxes() const
void RotatePageBoxes(float rotation)
void CorrectBoxPositionsToLayout(std::vector< BoxChar *> *boxchars)
void set_output_word_boxes(bool val)
const char features[]
Definition: feature_tests.c:2
void set_h_margin(const int h_margin)
cairo_surface_t * surface_
const PangoFontInfo & font() const