tesseract  4.00.00dev
text2image.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: text2image.cpp
3  * Description: Program to generate OCR training pages. Given a text file it
4  * outputs an image with a given font and degradation.
5  *
6  * Note that since the results depend on the fonts available on
7  * your system, running the code on a different machine, or
8  * different OS, or even at a different time on the same machine,
9  * may produce different fonts even if --font is given explicitly.
10  * To see names of available fonts, use --list_available_fonts with
11  * the appropriate --fonts_dir path.
12  * Specifying --use_only_legacy_fonts will restrict the available
13  * fonts to those listed in legacy_fonts.h
14  *
15  * Authors: Ranjith Unnikrishnan, Ray Smith
16  * Created: Tue Nov 19 2013
17  *
18  * (C) Copyright 2013, Google Inc.
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  * http://www.apache.org/licenses/LICENSE-2.0
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  *
29  **********************************************************************/
30 
31 #include <stdlib.h>
32 #include <string.h>
33 #include <algorithm>
34 #include <iostream>
35 #include <map>
36 #include <string>
37 #include <utility>
38 #include <vector>
39 
40 #include "allheaders.h" // from leptonica
41 #include "boxchar.h"
42 #include "commandlineflags.h"
43 #include "degradeimage.h"
44 #include "errcode.h"
45 #include "fileio.h"
46 #include "helpers.h"
47 #include "normstrngs.h"
48 #include "stringrenderer.h"
49 #include "tlog.h"
50 #include "unicharset.h"
51 #include "util.h"
52 
53 // A number with which to initialize the random number generator.
54 const int kRandomSeed = 0x18273645;
55 
56 // The text input file.
57 STRING_PARAM_FLAG(text, "", "File name of text input to process");
58 
59 // The text output file.
60 STRING_PARAM_FLAG(outputbase, "", "Basename for output image/box file");
61 
62 // Degrade the rendered image to mimic scanner quality.
63 BOOL_PARAM_FLAG(degrade_image, true,
64  "Degrade rendered image with speckle noise, dilation/erosion "
65  "and rotation");
66 
67 // Rotate the rendered image to have more realistic glyph borders
68 BOOL_PARAM_FLAG(rotate_image, true, "Rotate the image in a random way.");
69 
70 // Degradation to apply to the image.
71 INT_PARAM_FLAG(exposure, 0, "Exposure level in photocopier");
72 
73 // Output image resolution.
74 INT_PARAM_FLAG(resolution, 300, "Pixels per inch");
75 
76 // Width of output image (in pixels).
77 INT_PARAM_FLAG(xsize, 3600, "Width of output image");
78 
79 // Max height of output image (in pixels).
80 INT_PARAM_FLAG(ysize, 4800, "Height of output image");
81 
82 // Max number of pages to produce.
83 INT_PARAM_FLAG(max_pages, 0, "Maximum number of pages to output (0=unlimited)");
84 
85 // Margin around text (in pixels).
86 INT_PARAM_FLAG(margin, 100, "Margin round edges of image");
87 
88 // Size of text (in points).
89 INT_PARAM_FLAG(ptsize, 12, "Size of printed text");
90 
91 // Inter-character space (in ems).
92 DOUBLE_PARAM_FLAG(char_spacing, 0, "Inter-character space in ems");
93 
94 // Sets the probability (value in [0, 1]) of starting to render a word with an
95 // underline. Words are assumed to be space-delimited.
96 DOUBLE_PARAM_FLAG(underline_start_prob, 0,
97  "Fraction of words to underline (value in [0,1])");
98 // Set the probability (value in [0, 1]) of continuing a started underline to
99 // the next word.
100 DOUBLE_PARAM_FLAG(underline_continuation_prob, 0,
101  "Fraction of words to underline (value in [0,1])");
102 
103 // Inter-line space (in pixels).
104 INT_PARAM_FLAG(leading, 12, "Inter-line space (in pixels)");
105 
106 // Layout and glyph orientation on rendering.
107 STRING_PARAM_FLAG(writing_mode, "horizontal",
108  "Specify one of the following writing"
109  " modes.\n"
110  "'horizontal' : Render regular horizontal text. (default)\n"
111  "'vertical' : Render vertical text. Glyph orientation is"
112  " selected by Pango.\n"
113  "'vertical-upright' : Render vertical text. Glyph "
114  " orientation is set to be upright.");
115 
116 INT_PARAM_FLAG(box_padding, 0, "Padding around produced bounding boxes");
117 
118 BOOL_PARAM_FLAG(strip_unrenderable_words, true,
119  "Remove unrenderable words from source text");
120 
121 // Font name.
122 STRING_PARAM_FLAG(font, "Arial", "Font description name to use");
123 
124 BOOL_PARAM_FLAG(ligatures, false,
125  "Rebuild and render ligatures");
126 
127 BOOL_PARAM_FLAG(find_fonts, false,
128  "Search for all fonts that can render the text");
129 BOOL_PARAM_FLAG(render_per_font, true,
130  "If find_fonts==true, render each font to its own image. "
131  "Image filenames are of the form output_name.font_name.tif");
132 DOUBLE_PARAM_FLAG(min_coverage, 1.0,
133  "If find_fonts==true, the minimum coverage the font has of "
134  "the characters in the text file to include it, between "
135  "0 and 1.");
136 
137 BOOL_PARAM_FLAG(list_available_fonts, false, "List available fonts and quit.");
138 
139 BOOL_PARAM_FLAG(render_ngrams, false, "Put each space-separated entity from the"
140  " input file into one bounding box. The ngrams in the input"
141  " file will be randomly permuted before rendering (so that"
142  " there is sufficient variety of characters on each line).");
143 
144 BOOL_PARAM_FLAG(output_word_boxes, false,
145  "Output word bounding boxes instead of character boxes. "
146  "This is used for Cube training, and implied by "
147  "--render_ngrams.");
148 
149 STRING_PARAM_FLAG(unicharset_file, "",
150  "File with characters in the unicharset. If --render_ngrams"
151  " is true and --unicharset_file is specified, ngrams with"
152  " characters that are not in unicharset will be omitted");
153 
154 BOOL_PARAM_FLAG(bidirectional_rotation, false,
155  "Rotate the generated characters both ways.");
156 
157 BOOL_PARAM_FLAG(only_extract_font_properties, false,
158  "Assumes that the input file contains a list of ngrams. Renders"
159  " each ngram, extracts spacing properties and records them in"
160  " output_base/[font_name].fontinfo file.");
161 
162 // Use these flags to output zero-padded, square individual character images
163 BOOL_PARAM_FLAG(output_individual_glyph_images, false,
164  "If true also outputs individual character images");
165 INT_PARAM_FLAG(glyph_resized_size, 0,
166  "Each glyph is square with this side length in pixels");
167 INT_PARAM_FLAG(glyph_num_border_pixels_to_pad, 0,
168  "Final_size=glyph_resized_size+2*glyph_num_border_pixels_to_pad");
169 
170 namespace tesseract {
171 
174  SpacingProperties(int b, int a) : x_gap_before(b), x_gap_after(a) {}
175  // These values are obtained from FT_Glyph_Metrics struct
176  // used by the FreeType font engine.
177  int x_gap_before; // horizontal x bearing
178  int x_gap_after; // horizontal advance - x_gap_before - width
179  std::map<string, int> kerned_x_gaps;
180 };
181 
182 static bool IsWhitespaceBox(const BoxChar* boxchar) {
183  return (boxchar->box() == nullptr ||
184  SpanUTF8Whitespace(boxchar->ch().c_str()));
185 }
186 
187 static string StringReplace(const string& in,
188  const string& oldsub, const string& newsub) {
189  string out;
190  size_t start_pos = 0, pos;
191  while ((pos = in.find(oldsub, start_pos)) != string::npos) {
192  out.append(in.data() + start_pos, pos - start_pos);
193  out.append(newsub.data(), newsub.length());
194  start_pos = pos + oldsub.length();
195  }
196  out.append(in.data() + start_pos, in.length() - start_pos);
197  return out;
198 }
199 
200 // Assumes that each word (whitespace-separated entity) in text is a bigram.
201 // Renders the bigrams and calls FontInfo::GetSpacingProperties() to
202 // obtain spacing information. Produces the output .fontinfo file with a line
203 // per unichar of the form:
204 // unichar space_before space_after kerned1 kerned_space1 kerned2 ...
205 // Fox example, if unichar "A" has spacing of 0 pixels before and -1 pixels
206 // after, is kerned with "V" resulting in spacing of "AV" to be -7 and kerned
207 // with "T", such that "AT" has spacing of -5, the entry/line for unichar "A"
208 // in .fontinfo file will be:
209 // A 0 -1 T -5 V -7
210 void ExtractFontProperties(const string &utf8_text,
211  StringRenderer *render,
212  const string &output_base) {
213  std::map<string, SpacingProperties> spacing_map;
214  std::map<string, SpacingProperties>::iterator spacing_map_it0;
215  std::map<string, SpacingProperties>::iterator spacing_map_it1;
216  int x_bearing, x_advance;
217  int len = utf8_text.length();
218  int offset = 0;
219  const char* text = utf8_text.c_str();
220  while (offset < len) {
221  offset +=
222  render->RenderToImage(text + offset, strlen(text + offset), nullptr);
223  const std::vector<BoxChar*> &boxes = render->GetBoxes();
224 
225  // If the page break split a bigram, correct the offset so we try the bigram
226  // on the next iteration.
227  if (boxes.size() > 2 && !IsWhitespaceBox(boxes[boxes.size() - 1]) &&
228  IsWhitespaceBox(boxes[boxes.size() - 2])) {
229  if (boxes.size() > 3) {
230  tprintf("WARNING: Adjusting to bad page break after '%s%s'\n",
231  boxes[boxes.size() - 4]->ch().c_str(),
232  boxes[boxes.size() - 3]->ch().c_str());
233  }
234  offset -= boxes[boxes.size() - 1]->ch().size();
235  }
236 
237  for (size_t b = 0; b < boxes.size(); b += 2) {
238  while (b < boxes.size() && IsWhitespaceBox(boxes[b])) ++b;
239  if (b + 1 >= boxes.size()) break;
240  const string &ch0 = boxes[b]->ch();
241  // We encountered a ligature. This happens in at least two scenarios:
242  // One is when the rendered bigram forms a grapheme cluster (eg. the
243  // second character in the bigram is a combining vowel), in which case we
244  // correctly output only one bounding box.
245  // A second far less frequent case is when caused some fonts like 'DejaVu
246  // Sans Ultra-Light' force Pango to render a ligatured character even if
247  // the input consists of the separated characters. NOTE(ranjith): As per
248  // behdad@ this is not currently controllable at the level of the Pango
249  // API.
250  // The most frequent of all is a single character "word" made by the CJK
251  // segmenter.
252  // Safeguard against these cases here by just skipping the bigram.
253  if (IsWhitespaceBox(boxes[b+1])) {
254  continue;
255  }
256  int xgap = (boxes[b+1]->box()->x -
257  (boxes[b]->box()->x + boxes[b]->box()->w));
258  spacing_map_it0 = spacing_map.find(ch0);
259  int ok_count = 0;
260  if (spacing_map_it0 == spacing_map.end() &&
261  render->font().GetSpacingProperties(ch0, &x_bearing, &x_advance)) {
262  spacing_map[ch0] = SpacingProperties(
263  x_bearing, x_advance - x_bearing - boxes[b]->box()->w);
264  spacing_map_it0 = spacing_map.find(ch0);
265  ++ok_count;
266  }
267  const string &ch1 = boxes[b+1]->ch();
268  tlog(3, "%s%s\n", ch0.c_str(), ch1.c_str());
269  spacing_map_it1 = spacing_map.find(ch1);
270  if (spacing_map_it1 == spacing_map.end() &&
271  render->font().GetSpacingProperties(ch1, &x_bearing, &x_advance)) {
272  spacing_map[ch1] = SpacingProperties(
273  x_bearing, x_advance - x_bearing - boxes[b+1]->box()->w);
274  spacing_map_it1 = spacing_map.find(ch1);
275  ++ok_count;
276  }
277  if (ok_count == 2 && xgap != (spacing_map_it0->second.x_gap_after +
278  spacing_map_it1->second.x_gap_before)) {
279  spacing_map_it0->second.kerned_x_gaps[ch1] = xgap;
280  }
281  }
282  render->ClearBoxes();
283  }
284  string output_string;
285  const int kBufSize = 1024;
286  char buf[kBufSize];
287  snprintf(buf, kBufSize, "%d\n", static_cast<int>(spacing_map.size()));
288  output_string.append(buf);
289  std::map<string, SpacingProperties>::const_iterator spacing_map_it;
290  for (spacing_map_it = spacing_map.begin();
291  spacing_map_it != spacing_map.end(); ++spacing_map_it) {
292  snprintf(buf, kBufSize,
293  "%s %d %d %d", spacing_map_it->first.c_str(),
294  spacing_map_it->second.x_gap_before,
295  spacing_map_it->second.x_gap_after,
296  static_cast<int>(spacing_map_it->second.kerned_x_gaps.size()));
297  output_string.append(buf);
298  std::map<string, int>::const_iterator kern_it;
299  for (kern_it = spacing_map_it->second.kerned_x_gaps.begin();
300  kern_it != spacing_map_it->second.kerned_x_gaps.end(); ++kern_it) {
301  snprintf(buf, kBufSize,
302  " %s %d", kern_it->first.c_str(), kern_it->second);
303  output_string.append(buf);
304  }
305  output_string.append("\n");
306  }
307  File::WriteStringToFileOrDie(output_string, output_base + ".fontinfo");
308 }
309 
310 bool MakeIndividualGlyphs(Pix* pix,
311  const std::vector<BoxChar*>& vbox,
312  const int input_tiff_page) {
313  // If checks fail, return false without exiting text2image
314  if (!pix) {
315  tprintf("ERROR: MakeIndividualGlyphs(): Input Pix* is nullptr\n");
316  return false;
317  } else if (FLAGS_glyph_resized_size <= 0) {
318  tprintf("ERROR: --glyph_resized_size must be positive\n");
319  return false;
320  } else if (FLAGS_glyph_num_border_pixels_to_pad < 0) {
321  tprintf("ERROR: --glyph_num_border_pixels_to_pad must be 0 or positive\n");
322  return false;
323  }
324 
325  const int n_boxes = vbox.size();
326  int n_boxes_saved = 0;
327  int current_tiff_page = 0;
328  int y_previous = 0;
329  static int glyph_count = 0;
330  for (int i = 0; i < n_boxes; i++) {
331  // Get one bounding box
332  Box* b = vbox[i]->mutable_box();
333  if (!b) continue;
334  const int x = b->x;
335  const int y = b->y;
336  const int w = b->w;
337  const int h = b->h;
338  // Check present tiff page (for multipage tiff)
339  if (y < y_previous-pixGetHeight(pix)/10) {
340  tprintf("ERROR: Wrap-around encountered, at i=%d\n", i);
341  current_tiff_page++;
342  }
343  if (current_tiff_page < input_tiff_page) continue;
344  else if (current_tiff_page > input_tiff_page) break;
345  // Check box validity
346  if (x < 0 || y < 0 ||
347  (x+w-1) >= pixGetWidth(pix) ||
348  (y+h-1) >= pixGetHeight(pix)) {
349  tprintf("ERROR: MakeIndividualGlyphs(): Index out of range, at i=%d"
350  " (x=%d, y=%d, w=%d, h=%d\n)", i, x, y, w, h);
351  continue;
352  } else if (w < FLAGS_glyph_num_border_pixels_to_pad &&
353  h < FLAGS_glyph_num_border_pixels_to_pad) {
354  tprintf("ERROR: Input image too small to be a character, at i=%d\n", i);
355  continue;
356  }
357  // Crop the boxed character
358  Pix* pix_glyph = pixClipRectangle(pix, b, nullptr);
359  if (!pix_glyph) {
360  tprintf("ERROR: MakeIndividualGlyphs(): Failed to clip, at i=%d\n", i);
361  continue;
362  }
363  // Resize to square
364  Pix* pix_glyph_sq = pixScaleToSize(pix_glyph,
365  FLAGS_glyph_resized_size,
366  FLAGS_glyph_resized_size);
367  if (!pix_glyph_sq) {
368  tprintf("ERROR: MakeIndividualGlyphs(): Failed to resize, at i=%d\n", i);
369  continue;
370  }
371  // Zero-pad
372  Pix* pix_glyph_sq_pad = pixAddBorder(pix_glyph_sq,
373  FLAGS_glyph_num_border_pixels_to_pad,
374  0);
375  if (!pix_glyph_sq_pad) {
376  tprintf("ERROR: MakeIndividualGlyphs(): Failed to zero-pad, at i=%d\n",
377  i);
378  continue;
379  }
380  // Write out
381  Pix* pix_glyph_sq_pad_8 = pixConvertTo8(pix_glyph_sq_pad, false);
382  char filename[1024];
383  snprintf(filename, 1024, "%s_%d.jpg", FLAGS_outputbase.c_str(),
384  glyph_count++);
385  if (pixWriteJpeg(filename, pix_glyph_sq_pad_8, 100, 0)) {
386  tprintf("ERROR: MakeIndividualGlyphs(): Failed to write JPEG to %s,"
387  " at i=%d\n", filename, i);
388  continue;
389  }
390 
391  pixDestroy(&pix_glyph);
392  pixDestroy(&pix_glyph_sq);
393  pixDestroy(&pix_glyph_sq_pad);
394  pixDestroy(&pix_glyph_sq_pad_8);
395  n_boxes_saved++;
396  y_previous = y;
397  }
398  if (n_boxes_saved == 0) {
399  return false;
400  } else {
401  tprintf("Total number of characters saved = %d\n", n_boxes_saved);
402  return true;
403  }
404 }
405 } // namespace tesseract
406 
409 using tesseract::File;
414 
415 int Main() {
416  if (FLAGS_list_available_fonts) {
417  const std::vector<string>& all_fonts = FontUtils::ListAvailableFonts();
418  for (unsigned int i = 0; i < all_fonts.size(); ++i) {
419  printf("%3u: %s\n", i, all_fonts[i].c_str());
420  ASSERT_HOST_MSG(FontUtils::IsAvailableFont(all_fonts[i].c_str()),
421  "Font %s is unrecognized.\n", all_fonts[i].c_str());
422  }
423  return EXIT_SUCCESS;
424  }
425 
426  // Check validity of input flags.
427  if (FLAGS_text.empty()) {
428  tprintf("'--text' option is missing!\n");
429  exit(1);
430  }
431  if (FLAGS_outputbase.empty()) {
432  tprintf("'--outputbase' option is missing!\n");
433  exit(1);
434  }
435  if (!FLAGS_unicharset_file.empty() && FLAGS_render_ngrams) {
436  tprintf("Use '--unicharset_file' only if '--render_ngrams' is set.\n");
437  exit(1);
438  }
439 
440  if (!FLAGS_find_fonts && !FontUtils::IsAvailableFont(FLAGS_font.c_str())) {
441  string pango_name;
442  if (!FontUtils::IsAvailableFont(FLAGS_font.c_str(), &pango_name)) {
443  tprintf("Could not find font named %s.\n", FLAGS_font.c_str());
444  if (!pango_name.empty()) {
445  tprintf("Pango suggested font %s.\n", pango_name.c_str());
446  }
447  tprintf("Please correct --font arg.\n");
448  exit(1);
449  }
450  }
451 
452  if (FLAGS_render_ngrams)
453  FLAGS_output_word_boxes = true;
454 
455  char font_desc_name[1024];
456  snprintf(font_desc_name, 1024, "%s %d", FLAGS_font.c_str(),
457  static_cast<int>(FLAGS_ptsize));
458  StringRenderer render(font_desc_name, FLAGS_xsize, FLAGS_ysize);
459  render.set_add_ligatures(FLAGS_ligatures);
460  render.set_leading(FLAGS_leading);
461  render.set_resolution(FLAGS_resolution);
462  render.set_char_spacing(FLAGS_char_spacing * FLAGS_ptsize);
463  render.set_h_margin(FLAGS_margin);
464  render.set_v_margin(FLAGS_margin);
465  render.set_output_word_boxes(FLAGS_output_word_boxes);
466  render.set_box_padding(FLAGS_box_padding);
467  render.set_strip_unrenderable_words(FLAGS_strip_unrenderable_words);
468  render.set_underline_start_prob(FLAGS_underline_start_prob);
469  render.set_underline_continuation_prob(FLAGS_underline_continuation_prob);
470 
471  // Set text rendering orientation and their forms.
472  if (FLAGS_writing_mode == "horizontal") {
473  // Render regular horizontal text (default).
474  render.set_vertical_text(false);
475  render.set_gravity_hint_strong(false);
476  render.set_render_fullwidth_latin(false);
477  } else if (FLAGS_writing_mode == "vertical") {
478  // Render vertical text. Glyph orientation is selected by Pango.
479  render.set_vertical_text(true);
480  render.set_gravity_hint_strong(false);
481  render.set_render_fullwidth_latin(false);
482  } else if (FLAGS_writing_mode == "vertical-upright") {
483  // Render vertical text. Glyph orientation is set to be upright.
484  // Also Basic Latin characters are converted to their fullwidth forms
485  // on rendering, since fullwidth Latin characters are well designed to fit
486  // vertical text lines, while .box files store halfwidth Basic Latin
487  // unichars.
488  render.set_vertical_text(true);
489  render.set_gravity_hint_strong(true);
490  render.set_render_fullwidth_latin(true);
491  } else {
492  tprintf("Invalid writing mode: %s\n", FLAGS_writing_mode.c_str());
493  exit(1);
494  }
495 
496  string src_utf8;
497  // This c_str is NOT redundant!
498  if (!File::ReadFileToString(FLAGS_text.c_str(), &src_utf8)) {
499  tprintf("Failed to read file: %s\n", FLAGS_text.c_str());
500  exit(1);
501  }
502 
503  // Remove the unicode mark if present.
504  if (strncmp(src_utf8.c_str(), "\xef\xbb\xbf", 3) == 0) {
505  src_utf8.erase(0, 3);
506  }
507  tlog(1, "Render string of size %d\n", src_utf8.length());
508 
509  if (FLAGS_render_ngrams || FLAGS_only_extract_font_properties) {
510  // Try to preserve behavior of old text2image by expanding inter-word
511  // spaces by a factor of 4.
512  const string kSeparator = FLAGS_render_ngrams ? " " : " ";
513  // Also restrict the number of characters per line to try and avoid
514  // line-breaking in the middle of words like "-A", "R$" etc. which are
515  // otherwise allowed by the standard unicode line-breaking rules.
516  const unsigned int kCharsPerLine = (FLAGS_ptsize > 20) ? 50 : 100;
517  string rand_utf8;
518  UNICHARSET unicharset;
519  if (FLAGS_render_ngrams && !FLAGS_unicharset_file.empty() &&
520  !unicharset.load_from_file(FLAGS_unicharset_file.c_str())) {
521  tprintf("Failed to load unicharset from file %s\n",
522  FLAGS_unicharset_file.c_str());
523  exit(1);
524  }
525 
526  // If we are rendering ngrams that will be OCRed later, shuffle them so that
527  // tesseract does not have difficulties finding correct baseline, word
528  // spaces, etc.
529  const char *str8 = src_utf8.c_str();
530  int len = src_utf8.length();
531  int step;
532  std::vector<std::pair<int, int> > offsets;
533  int offset = SpanUTF8Whitespace(str8);
534  while (offset < len) {
535  step = SpanUTF8NotWhitespace(str8 + offset);
536  offsets.push_back(std::make_pair(offset, step));
537  offset += step;
538  offset += SpanUTF8Whitespace(str8 + offset);
539  }
540  if (FLAGS_render_ngrams)
541  std::random_shuffle(offsets.begin(), offsets.end());
542 
543  for (size_t i = 0, line = 1; i < offsets.size(); ++i) {
544  const char *curr_pos = str8 + offsets[i].first;
545  int ngram_len = offsets[i].second;
546  // Skip words that contain characters not in found in unicharset.
547  string cleaned = UNICHARSET::CleanupString(curr_pos, ngram_len);
548  if (!FLAGS_unicharset_file.empty() &&
549  !unicharset.encodable_string(cleaned.c_str(), nullptr)) {
550  continue;
551  }
552  rand_utf8.append(curr_pos, ngram_len);
553  if (rand_utf8.length() > line * kCharsPerLine) {
554  rand_utf8.append(" \n");
555  ++line;
556  if (line & 0x1) rand_utf8.append(kSeparator);
557  } else {
558  rand_utf8.append(kSeparator);
559  }
560  }
561  tlog(1, "Rendered ngram string of size %d\n", rand_utf8.length());
562  src_utf8.swap(rand_utf8);
563  }
564  if (FLAGS_only_extract_font_properties) {
565  tprintf("Extracting font properties only\n");
566  ExtractFontProperties(src_utf8, &render, FLAGS_outputbase.c_str());
567  tprintf("Done!\n");
568  return 0;
569  }
570 
571  int im = 0;
572  std::vector<float> page_rotation;
573  const char* to_render_utf8 = src_utf8.c_str();
574 
575  tesseract::TRand randomizer;
576  randomizer.set_seed(kRandomSeed);
577  std::vector<string> font_names;
578  // We use a two pass mechanism to rotate images in both direction.
579  // The first pass(0) will rotate the images in random directions and
580  // the second pass(1) will mirror those rotations.
581  int num_pass = FLAGS_bidirectional_rotation ? 2 : 1;
582  for (int pass = 0; pass < num_pass; ++pass) {
583  int page_num = 0;
584  string font_used;
585  for (size_t offset = 0;
586  offset < strlen(to_render_utf8) &&
587  (FLAGS_max_pages == 0 || page_num < FLAGS_max_pages);
588  ++im, ++page_num) {
589  tlog(1, "Starting page %d\n", im);
590  Pix* pix = nullptr;
591  if (FLAGS_find_fonts) {
592  offset += render.RenderAllFontsToImage(FLAGS_min_coverage,
593  to_render_utf8 + offset,
594  strlen(to_render_utf8 + offset),
595  &font_used, &pix);
596  } else {
597  offset += render.RenderToImage(to_render_utf8 + offset,
598  strlen(to_render_utf8 + offset), &pix);
599  }
600  if (pix != nullptr) {
601  float rotation = 0;
602  if (pass == 1) {
603  // Pass 2, do mirror rotation.
604  rotation = -1 * page_rotation[page_num];
605  }
606  if (FLAGS_degrade_image) {
607  pix = DegradeImage(pix, FLAGS_exposure, &randomizer,
608  FLAGS_rotate_image ? &rotation : nullptr);
609  }
610  render.RotatePageBoxes(rotation);
611 
612  if (pass == 0) {
613  // Pass 1, rotate randomly and store the rotation..
614  page_rotation.push_back(rotation);
615  }
616 
617  Pix* gray_pix = pixConvertTo8(pix, false);
618  pixDestroy(&pix);
619  Pix* binary = pixThresholdToBinary(gray_pix, 128);
620  pixDestroy(&gray_pix);
621  char tiff_name[1024];
622  if (FLAGS_find_fonts) {
623  if (FLAGS_render_per_font) {
624  string fontname_for_file = tesseract::StringReplace(
625  font_used, " ", "_");
626  snprintf(tiff_name, 1024, "%s.%s.tif", FLAGS_outputbase.c_str(),
627  fontname_for_file.c_str());
628  pixWriteTiff(tiff_name, binary, IFF_TIFF_G4, "w");
629  tprintf("Rendered page %d to file %s\n", im, tiff_name);
630  } else {
631  font_names.push_back(font_used);
632  }
633  } else {
634  snprintf(tiff_name, 1024, "%s.tif", FLAGS_outputbase.c_str());
635  pixWriteTiff(tiff_name, binary, IFF_TIFF_G4, im == 0 ? "w" : "a");
636  tprintf("Rendered page %d to file %s\n", im, tiff_name);
637  }
638  // Make individual glyphs
639  if (FLAGS_output_individual_glyph_images) {
640  if (!MakeIndividualGlyphs(binary, render.GetBoxes(), im)) {
641  tprintf("ERROR: Individual glyphs not saved\n");
642  }
643  }
644  pixDestroy(&binary);
645  }
646  if (FLAGS_find_fonts && offset != 0) {
647  // We just want a list of names, or some sample images so we don't need
648  // to render more than the first page of the text.
649  break;
650  }
651  }
652  }
653  if (!FLAGS_find_fonts) {
654  string box_name = FLAGS_outputbase.c_str();
655  box_name += ".box";
656  render.WriteAllBoxes(box_name);
657  } else if (!FLAGS_render_per_font && !font_names.empty()) {
658  string filename = FLAGS_outputbase.c_str();
659  filename += ".fontlist.txt";
660  FILE* fp = fopen(filename.c_str(), "wb");
661  if (fp == nullptr) {
662  tprintf("Failed to create output font list %s\n", filename.c_str());
663  } else {
664  for (size_t i = 0; i < font_names.size(); ++i) {
665  fprintf(fp, "%s\n", font_names[i].c_str());
666  }
667  fclose(fp);
668  }
669  }
670 
671  return 0;
672 }
673 
674 int main(int argc, char** argv) {
675  tesseract::ParseCommandLineFlags(argv[0], &argc, &argv, true);
676  Main();
677 }
bool GetSpacingProperties(const string &utf8_char, int *x_bearing, int *x_advance) const
static const std::vector< string > & ListAvailableFonts()
#define ASSERT_HOST_MSG(x,...)
Definition: errcode.h:90
void set_add_ligatures(bool add_ligatures)
unsigned int SpanUTF8NotWhitespace(const char *text)
Definition: normstrngs.cpp:243
unsigned int SpanUTF8Whitespace(const char *text)
Definition: normstrngs.cpp:233
void RotatePageBoxes(float rotation)
const string & ch() const
Definition: boxchar.h:43
STRING_PARAM_FLAG(text, "", "File name of text input to process")
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, string *font_used, Pix **pix)
void set_h_margin(const int h_margin)
void set_resolution(const int resolution)
void set_leading(int leading)
static string CleanupString(const char *utf8_str)
Definition: unicharset.h:241
void set_seed(uinT64 seed)
Definition: helpers.h:45
void set_underline_start_prob(const double frac)
void ExtractFontProperties(const string &utf8_text, StringRenderer *render, const string &output_base)
Definition: text2image.cpp:210
BOOL_PARAM_FLAG(degrade_image, true, "Degrade rendered image with speckle noise, dilation/erosion " "and rotation")
#define tprintf(...)
Definition: tprintf.h:31
int Main(int argc, char **argv)
void WriteAllBoxes(const string &filename)
void set_vertical_text(bool vertical_text)
INT_PARAM_FLAG(exposure, 0, "Exposure level in photocopier")
bool encodable_string(const char *str, int *first_bad_position) const
Definition: unicharset.cpp:241
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
DOUBLE_PARAM_FLAG(char_spacing, 0, "Inter-character space in ems")
void set_char_spacing(int char_spacing)
void set_output_word_boxes(bool val)
void set_box_padding(int val)
std::map< string, int > kerned_x_gaps
Definition: text2image.cpp:179
const int kRandomSeed
Definition: text2image.cpp:54
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:387
int main(int argc, char **argv)
Definition: text2image.cpp:674
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:72
#define tlog(level,...)
Definition: tlog.h:33
const std::vector< BoxChar * > & GetBoxes() const
void set_strip_unrenderable_words(bool val)
void set_gravity_hint_strong(bool gravity_hint_strong)
void set_v_margin(const int v_margin)
Pix * DegradeImage(Pix *input, int exposure, TRand *randomizer, float *rotation)
int RenderToImage(const char *text, int text_length, Pix **pix)
bool MakeIndividualGlyphs(Pix *pix, const std::vector< BoxChar *> &vbox, const int input_tiff_page)
Definition: text2image.cpp:310
const PangoFontInfo & font() const
void set_render_fullwidth_latin(bool render_fullwidth_latin)
void set_underline_continuation_prob(const double frac)
const Box * box() const
Definition: boxchar.h:44
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:52
static bool IsAvailableFont(const char *font_desc)