40 #include "allheaders.h" 54 # define putenv(s) _putenv(s) 68 "Degrade rendered image with speckle noise, dilation/erosion " 72 BOOL_PARAM_FLAG(rotate_image,
true,
"Rotate the image in a random way.");
87 INT_PARAM_FLAG(max_pages, 0,
"Maximum number of pages to output (0=unlimited)");
101 "Fraction of words to underline (value in [0,1])");
105 "Fraction of words to underline (value in [0,1])");
112 "Specify one of the following writing" 114 "'horizontal' : Render regular horizontal text. (default)\n" 115 "'vertical' : Render vertical text. Glyph orientation is" 116 " selected by Pango.\n" 117 "'vertical-upright' : Render vertical text. Glyph " 118 " orientation is set to be upright.");
120 INT_PARAM_FLAG(box_padding, 0,
"Padding around produced bounding boxes");
123 "Remove unrenderable words from source text");
129 "Rebuild and render ligatures");
132 "Search for all fonts that can render the text");
134 "If find_fonts==true, render each font to its own image. " 135 "Image filenames are of the form output_name.font_name.tif");
137 "If find_fonts==true, the minimum coverage the font has of " 138 "the characters in the text file to include it, between " 141 BOOL_PARAM_FLAG(list_available_fonts,
false,
"List available fonts and quit.");
143 BOOL_PARAM_FLAG(render_ngrams,
false,
"Put each space-separated entity from the" 144 " input file into one bounding box. The ngrams in the input" 145 " file will be randomly permuted before rendering (so that" 146 " there is sufficient variety of characters on each line).");
149 "Output word bounding boxes instead of character boxes. " 150 "This is used for Cube training, and implied by " 154 "File with characters in the unicharset. If --render_ngrams" 155 " is true and --unicharset_file is specified, ngrams with" 156 " characters that are not in unicharset will be omitted");
159 "Rotate the generated characters both ways.");
162 "Assumes that the input file contains a list of ngrams. Renders" 163 " each ngram, extracts spacing properties and records them in" 164 " output_base/[font_name].fontinfo file.");
168 "If true also outputs individual character images");
170 "Each glyph is square with this side length in pixels");
172 "Final_size=glyph_resized_size+2*glyph_num_border_pixels_to_pad");
186 static bool IsWhitespaceBox(
const BoxChar* boxchar) {
187 return (boxchar->
box() ==
nullptr ||
191 static std::string StringReplace(
const std::string& in,
192 const std::string& oldsub,
const std::string& newsub) {
194 size_t start_pos = 0, pos;
195 while ((pos = in.find(oldsub, start_pos)) != std::string::npos) {
196 out.append(in.data() + start_pos, pos - start_pos);
197 out.append(newsub.data(), newsub.length());
198 start_pos = pos + oldsub.length();
200 out.append(in.data() + start_pos, in.length() - start_pos);
214 static void ExtractFontProperties(
const std::string &utf8_text,
216 const std::string &output_base) {
217 std::map<std::string, SpacingProperties> spacing_map;
218 std::map<std::string, SpacingProperties>::iterator spacing_map_it0;
219 std::map<std::string, SpacingProperties>::iterator spacing_map_it1;
220 int x_bearing, x_advance;
221 int len = utf8_text.length();
223 const char* text = utf8_text.c_str();
224 while (offset < len) {
226 render->
RenderToImage(text + offset, strlen(text + offset),
nullptr);
227 const std::vector<BoxChar*> &boxes = render->
GetBoxes();
231 if (boxes.size() > 2 && !IsWhitespaceBox(boxes[boxes.size() - 1]) &&
232 IsWhitespaceBox(boxes[boxes.size() - 2])) {
233 if (boxes.size() > 3) {
234 tprintf(
"WARNING: Adjusting to bad page break after '%s%s'\n",
235 boxes[boxes.size() - 4]->ch().c_str(),
236 boxes[boxes.size() - 3]->ch().c_str());
238 offset -= boxes[boxes.size() - 1]->ch().size();
241 for (
size_t b = 0; b < boxes.size(); b += 2) {
242 while (b < boxes.size() && IsWhitespaceBox(boxes[b])) ++b;
243 if (b + 1 >= boxes.size())
break;
244 const std::string &ch0 = boxes[b]->ch();
257 if (IsWhitespaceBox(boxes[b+1])) {
260 int xgap = (boxes[b+1]->box()->x -
261 (boxes[b]->box()->x + boxes[b]->box()->w));
262 spacing_map_it0 = spacing_map.find(ch0);
264 if (spacing_map_it0 == spacing_map.end() &&
266 spacing_map[ch0] = SpacingProperties(
267 x_bearing, x_advance - x_bearing - boxes[b]->box()->w);
268 spacing_map_it0 = spacing_map.find(ch0);
271 const std::string &ch1 = boxes[b+1]->ch();
272 tlog(3,
"%s%s\n", ch0.c_str(), ch1.c_str());
273 spacing_map_it1 = spacing_map.find(ch1);
274 if (spacing_map_it1 == spacing_map.end() &&
276 spacing_map[ch1] = SpacingProperties(
277 x_bearing, x_advance - x_bearing - boxes[b+1]->box()->w);
278 spacing_map_it1 = spacing_map.find(ch1);
281 if (ok_count == 2 && xgap != (spacing_map_it0->second.x_gap_after +
282 spacing_map_it1->second.x_gap_before)) {
283 spacing_map_it0->second.kerned_x_gaps[ch1] = xgap;
288 std::string output_string;
289 const int kBufSize = 1024;
291 snprintf(buf, kBufSize,
"%d\n", static_cast<int>(spacing_map.size()));
292 output_string.append(buf);
293 std::map<std::string, SpacingProperties>::const_iterator spacing_map_it;
294 for (spacing_map_it = spacing_map.begin();
295 spacing_map_it != spacing_map.end(); ++spacing_map_it) {
296 snprintf(buf, kBufSize,
297 "%s %d %d %d", spacing_map_it->first.c_str(),
298 spacing_map_it->second.x_gap_before,
299 spacing_map_it->second.x_gap_after,
300 static_cast<int>(spacing_map_it->second.kerned_x_gaps.size()));
301 output_string.append(buf);
302 std::map<std::string, int>::const_iterator kern_it;
303 for (kern_it = spacing_map_it->second.kerned_x_gaps.begin();
304 kern_it != spacing_map_it->second.kerned_x_gaps.end(); ++kern_it) {
305 snprintf(buf, kBufSize,
306 " %s %d", kern_it->first.c_str(), kern_it->second);
307 output_string.append(buf);
309 output_string.append(
"\n");
314 static bool MakeIndividualGlyphs(Pix* pix,
const std::vector<BoxChar*>& vbox,
315 const int input_tiff_page) {
318 tprintf(
"ERROR: MakeIndividualGlyphs(): Input Pix* is nullptr\n");
320 }
else if (FLAGS_glyph_resized_size <= 0) {
321 tprintf(
"ERROR: --glyph_resized_size must be positive\n");
323 }
else if (FLAGS_glyph_num_border_pixels_to_pad < 0) {
324 tprintf(
"ERROR: --glyph_num_border_pixels_to_pad must be 0 or positive\n");
328 const int n_boxes = vbox.size();
329 int n_boxes_saved = 0;
330 int current_tiff_page = 0;
332 static int glyph_count = 0;
333 for (
int i = 0; i < n_boxes; i++) {
335 Box* b = vbox[i]->mutable_box();
342 if (y < y_previous-pixGetHeight(pix)/10) {
343 tprintf(
"ERROR: Wrap-around encountered, at i=%d\n", i);
346 if (current_tiff_page < input_tiff_page)
continue;
347 else if (current_tiff_page > input_tiff_page)
break;
349 if (x < 0 || y < 0 ||
350 (x+w-1) >= pixGetWidth(pix) ||
351 (y+h-1) >= pixGetHeight(pix)) {
352 tprintf(
"ERROR: MakeIndividualGlyphs(): Index out of range, at i=%d" 353 " (x=%d, y=%d, w=%d, h=%d\n)", i, x, y, w, h);
355 }
else if (w < FLAGS_glyph_num_border_pixels_to_pad &&
356 h < FLAGS_glyph_num_border_pixels_to_pad) {
357 tprintf(
"ERROR: Input image too small to be a character, at i=%d\n", i);
361 Pix* pix_glyph = pixClipRectangle(pix, b,
nullptr);
363 tprintf(
"ERROR: MakeIndividualGlyphs(): Failed to clip, at i=%d\n", i);
367 Pix* pix_glyph_sq = pixScaleToSize(pix_glyph,
368 FLAGS_glyph_resized_size,
369 FLAGS_glyph_resized_size);
371 tprintf(
"ERROR: MakeIndividualGlyphs(): Failed to resize, at i=%d\n", i);
375 Pix* pix_glyph_sq_pad = pixAddBorder(pix_glyph_sq,
376 FLAGS_glyph_num_border_pixels_to_pad,
378 if (!pix_glyph_sq_pad) {
379 tprintf(
"ERROR: MakeIndividualGlyphs(): Failed to zero-pad, at i=%d\n",
384 Pix* pix_glyph_sq_pad_8 = pixConvertTo8(pix_glyph_sq_pad,
false);
386 snprintf(filename, 1024,
"%s_%d.jpg", FLAGS_outputbase.c_str(),
388 if (pixWriteJpeg(filename, pix_glyph_sq_pad_8, 100, 0)) {
389 tprintf(
"ERROR: MakeIndividualGlyphs(): Failed to write JPEG to %s," 390 " at i=%d\n", filename, i);
394 pixDestroy(&pix_glyph);
395 pixDestroy(&pix_glyph_sq);
396 pixDestroy(&pix_glyph_sq_pad);
397 pixDestroy(&pix_glyph_sq_pad_8);
401 if (n_boxes_saved == 0) {
404 tprintf(
"Total number of characters saved = %d\n", n_boxes_saved);
411 using tesseract::ExtractFontProperties;
419 if (FLAGS_list_available_fonts) {
420 const std::vector<std::string>& all_fonts = FontUtils::ListAvailableFonts();
421 for (
unsigned int i = 0; i < all_fonts.size(); ++i) {
425 std::string font_name(all_fonts[i].c_str());
426 if (font_name.back() ==
',')
427 font_name.pop_back();
428 printf(
"%3u: %s\n", i, font_name.c_str());
430 "Font %s is unrecognized.\n", all_fonts[i].c_str());
436 if (FLAGS_text.empty()) {
437 tprintf(
"'--text' option is missing!\n");
440 if (FLAGS_outputbase.empty()) {
441 tprintf(
"'--outputbase' option is missing!\n");
444 if (!FLAGS_unicharset_file.empty() && FLAGS_render_ngrams) {
445 tprintf(
"Use '--unicharset_file' only if '--render_ngrams' is set.\n");
449 std::string font_name = FLAGS_font.c_str();
450 if (!FLAGS_find_fonts && !FontUtils::IsAvailableFont(font_name.c_str())) {
452 std::string pango_name;
453 if (!FontUtils::IsAvailableFont(font_name.c_str(), &pango_name)) {
454 tprintf(
"Could not find font named '%s'.\n", FLAGS_font.c_str());
455 if (!pango_name.empty()) {
456 tprintf(
"Pango suggested font '%s'.\n", pango_name.c_str());
458 tprintf(
"Please correct --font arg.\n");
463 if (FLAGS_render_ngrams)
464 FLAGS_output_word_boxes =
true;
466 char font_desc_name[1024];
467 snprintf(font_desc_name, 1024,
"%s %d", font_name.c_str(),
468 static_cast<int>(FLAGS_ptsize));
484 if (FLAGS_writing_mode ==
"horizontal") {
489 }
else if (FLAGS_writing_mode ==
"vertical") {
494 }
else if (FLAGS_writing_mode ==
"vertical-upright") {
504 tprintf(
"Invalid writing mode: %s\n", FLAGS_writing_mode.c_str());
508 std::string src_utf8;
510 if (!File::ReadFileToString(FLAGS_text.c_str(), &src_utf8)) {
511 tprintf(
"Failed to read file: %s\n", FLAGS_text.c_str());
516 if (strncmp(src_utf8.c_str(),
"\xef\xbb\xbf", 3) == 0) {
517 src_utf8.erase(0, 3);
519 tlog(1,
"Render string of size %d\n", src_utf8.length());
521 if (FLAGS_render_ngrams || FLAGS_only_extract_font_properties) {
524 const std::string kSeparator = FLAGS_render_ngrams ?
" " :
" ";
528 const unsigned int kCharsPerLine = (FLAGS_ptsize > 20) ? 50 : 100;
529 std::string rand_utf8;
531 if (FLAGS_render_ngrams && !FLAGS_unicharset_file.empty() &&
533 tprintf(
"Failed to load unicharset from file %s\n",
534 FLAGS_unicharset_file.c_str());
541 const char *str8 = src_utf8.c_str();
542 int len = src_utf8.length();
544 std::vector<std::pair<int, int> > offsets;
546 while (offset < len) {
548 offsets.push_back(std::make_pair(offset, step));
552 if (FLAGS_render_ngrams)
553 std::random_shuffle(offsets.begin(), offsets.end());
555 for (
size_t i = 0, line = 1; i < offsets.size(); ++i) {
556 const char *curr_pos = str8 + offsets[i].first;
557 int ngram_len = offsets[i].second;
560 if (!FLAGS_unicharset_file.empty() &&
564 rand_utf8.append(curr_pos, ngram_len);
565 if (rand_utf8.length() > line * kCharsPerLine) {
566 rand_utf8.append(
" \n");
568 if (line & 0x1) rand_utf8.append(kSeparator);
570 rand_utf8.append(kSeparator);
573 tlog(1,
"Rendered ngram string of size %d\n", rand_utf8.length());
574 src_utf8.swap(rand_utf8);
576 if (FLAGS_only_extract_font_properties) {
577 tprintf(
"Extracting font properties only\n");
578 ExtractFontProperties(src_utf8, &render, FLAGS_outputbase.c_str());
584 std::vector<float> page_rotation;
585 const char* to_render_utf8 = src_utf8.c_str();
589 std::vector<std::string> font_names;
593 int num_pass = FLAGS_bidirectional_rotation ? 2 : 1;
594 for (
int pass = 0; pass < num_pass; ++pass) {
596 std::string font_used;
597 for (
size_t offset = 0;
598 offset < strlen(to_render_utf8) &&
599 (FLAGS_max_pages == 0 || page_num < FLAGS_max_pages);
601 tlog(1,
"Starting page %d\n", im);
603 if (FLAGS_find_fonts) {
605 to_render_utf8 + offset,
606 strlen(to_render_utf8 + offset),
610 strlen(to_render_utf8 + offset), &pix);
612 if (pix !=
nullptr) {
616 rotation = -1 * page_rotation[page_num];
618 if (FLAGS_degrade_image) {
620 FLAGS_rotate_image ? &rotation :
nullptr);
626 page_rotation.push_back(rotation);
629 Pix* gray_pix = pixConvertTo8(pix,
false);
631 Pix* binary = pixThresholdToBinary(gray_pix, 128);
632 pixDestroy(&gray_pix);
633 char tiff_name[1024];
634 if (FLAGS_find_fonts) {
635 if (FLAGS_render_per_font) {
636 std::string fontname_for_file = tesseract::StringReplace(
637 font_used,
" ",
"_");
638 snprintf(tiff_name, 1024,
"%s.%s.tif", FLAGS_outputbase.c_str(),
639 fontname_for_file.c_str());
640 pixWriteTiff(tiff_name, binary, IFF_TIFF_G4,
"w");
641 tprintf(
"Rendered page %d to file %s\n", im, tiff_name);
643 font_names.push_back(font_used);
646 snprintf(tiff_name, 1024,
"%s.tif", FLAGS_outputbase.c_str());
647 pixWriteTiff(tiff_name, binary, IFF_TIFF_G4, im == 0 ?
"w" :
"a");
648 tprintf(
"Rendered page %d to file %s\n", im, tiff_name);
651 if (FLAGS_output_individual_glyph_images) {
652 if (!MakeIndividualGlyphs(binary, render.
GetBoxes(), im)) {
653 tprintf(
"ERROR: Individual glyphs not saved\n");
658 if (FLAGS_find_fonts && offset != 0) {
665 if (!FLAGS_find_fonts) {
666 std::string box_name = FLAGS_outputbase.c_str();
669 }
else if (!FLAGS_render_per_font && !font_names.empty()) {
670 std::string filename = FLAGS_outputbase.c_str();
671 filename +=
".fontlist.txt";
672 FILE* fp = fopen(filename.c_str(),
"wb");
674 tprintf(
"Failed to create output font list %s\n", filename.c_str());
676 for (
size_t i = 0; i < font_names.size(); ++i) {
677 fprintf(fp,
"%s\n", font_names[i].c_str());
686 int main(
int argc,
char** argv) {
692 backend = getenv(
"PANGOCAIRO_BACKEND");
693 if (backend == NULL) {
694 putenv(
"PANGOCAIRO_BACKEND=fc");
696 printf(
"Using '%s' as pango cairo backend based on enviroment " 697 "variable.\n", backend);
699 tesseract::CheckSharedLibraryVersion();
701 if ((strcmp(argv[1],
"-v") == 0) ||
702 (strcmp(argv[1],
"--version") == 0)) {
703 FontUtils::PangoFontTypeInfo();
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, std::string *font_used, Pix **pix)
INT_PARAM_FLAG(exposure, 0, "Exposure level in photocopier")
void set_underline_continuation_prob(const double frac)
const std::string & ch() const
void set_vertical_text(bool vertical_text)
void set_gravity_hint_strong(bool gravity_hint_strong)
SpacingProperties(int b, int a)
const std::vector< BoxChar * > & GetBoxes() const
void RotatePageBoxes(float rotation)
std::map< std::string, int > kerned_x_gaps
void set_render_fullwidth_latin(bool render_fullwidth_latin)
void set_output_word_boxes(bool val)
unsigned int SpanUTF8Whitespace(const char *text)
void set_add_ligatures(bool add_ligatures)
void set_box_padding(int val)
void set_resolution(const int resolution)
Pix * DegradeImage(Pix *input, int exposure, TRand *randomizer, float *rotation)
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
bool GetSpacingProperties(const std::string &utf8_char, int *x_bearing, int *x_advance) const
bool encodable_string(const char *str, int *first_bad_position) const
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
DLLSYM void tprintf(const char *format,...)
void set_strip_unrenderable_words(bool val)
void WriteAllBoxes(const std::string &filename)
void set_v_margin(const int v_margin)
static std::string CleanupString(const char *utf8_str)
#define ASSERT_HOST_MSG(x,...)
void set_leading(int leading)
void set_underline_start_prob(const double frac)
unsigned int SpanUTF8NotWhitespace(const char *text)
DOUBLE_PARAM_FLAG(char_spacing, 0, "Inter-character space in ems")
STRING_PARAM_FLAG(text, "", "File name of text input to process")
int main(int argc, char **argv)
bool load_from_file(const char *const filename, bool skip_fragments)
void set_char_spacing(int char_spacing)
void set_h_margin(const int h_margin)
BOOL_PARAM_FLAG(degrade_image, true, "Degrade rendered image with speckle noise, dilation/erosion " "and rotation")
const PangoFontInfo & font() const
int RenderToImage(const char *text, int text_length, Pix **pix)
void set_seed(uint64_t seed)