31 #include "allheaders.h" 35 #include "pango/pango-font.h" 36 #include "pango/pango-glyph-item.h" 39 #include "unicode/uchar.h" 42 #ifdef USE_STD_NAMESPACE 51 static const int kDefaultOutputResolution = 300;
56 static const char* kWordJoinerUTF8 =
"\xE2\x81\xA0";
57 static const char32 kWordJoiner = 0x2060;
59 static bool IsCombiner(
int ch) {
60 const int char_type = u_charType(ch);
61 return ((char_type == U_NON_SPACING_MARK) ||
62 (char_type == U_ENCLOSING_MARK) ||
63 (char_type == U_COMBINING_SPACING_MARK));
66 static string EncodeAsUTF8(
const char32 ch32) {
68 return string(uni_ch.utf8(), uni_ch.utf8_len());
72 static bool RandBool(
const double prob, TRand* rand) {
73 if (prob == 1.0)
return true;
74 if (prob == 0.0)
return false;
75 return rand->UnsignedRand(1.0) < prob;
80 if (cairo_image_surface_get_format(surface) != CAIRO_FORMAT_ARGB32) {
81 printf(
"Unexpected surface format %d\n",
82 cairo_image_surface_get_format(surface));
85 const int width = cairo_image_surface_get_width(surface);
86 const int height = cairo_image_surface_get_height(surface);
87 Pix* pix = pixCreate(width, height, 32);
88 int byte_stride = cairo_image_surface_get_stride(surface);
90 for (
int i = 0; i < height; ++i) {
91 memcpy(reinterpret_cast<unsigned char*>(pix->data + i * pix->wpl) + 1,
92 cairo_image_surface_get_data(surface) + i * byte_stride,
93 byte_stride - ((i == height - 1) ? 1 : 0));
100 : page_width_(page_width),
101 page_height_(page_height),
106 vertical_text_(false),
107 gravity_hint_strong_(false),
108 render_fullwidth_latin_(false),
109 underline_start_prob_(0),
110 underline_continuation_prob_(0),
111 underline_style_(PANGO_UNDERLINE_SINGLE),
113 drop_uncovered_chars_(true),
114 strip_unrenderable_words_(false),
115 add_ligatures_(false),
116 output_word_boxes_(false),
170 PangoContext* context = pango_layout_get_context(
layout_);
171 pango_context_set_base_gravity(context, PANGO_GRAVITY_EAST);
173 pango_context_set_gravity_hint(context, PANGO_GRAVITY_HINT_STRONG);
175 pango_layout_context_changed(
layout_);
184 PangoFontDescription *desc =
185 pango_font_description_from_string(font_desc.c_str());
187 pango_layout_set_font_description(
layout_, desc);
188 pango_font_description_free(desc);
189 pango_cairo_context_set_resolution(pango_layout_get_context(
layout_),
194 tlog(3,
"max_width = %d, max_height = %d\n", max_width, max_height);
196 swap(max_width, max_height);
198 pango_layout_set_width(
layout_, max_width * PANGO_SCALE);
199 pango_layout_set_wrap(
layout_, PANGO_WRAP_WORD);
202 PangoAttrList* attr_list = pango_attr_list_new();
204 PangoAttribute* spacing_attr = pango_attr_letter_spacing_new(
206 spacing_attr->start_index = 0;
207 spacing_attr->end_index =
static_cast<guint
>(-1);
208 pango_attr_list_change(attr_list, spacing_attr);
210 #if (PANGO_VERSION_MAJOR == 1 && PANGO_VERSION_MINOR >= 38) 213 PangoAttribute* feature_attr = pango_attr_font_features_new(
features_);
214 pango_attr_list_change(attr_list, feature_attr);
217 pango_layout_set_attributes(
layout_, attr_list);
218 pango_attr_list_unref(attr_list);
242 PangoAttrList* attr_list = pango_layout_get_attributes(
layout_);
244 const char* text = page_text.c_str();
247 bool started_underline =
false;
248 PangoAttribute* und_attr =
nullptr;
250 while (offset < page_text.length()) {
252 if (offset == page_text.length())
break;
257 if (started_underline) {
261 und_attr->end_index = word_start + word_len;
265 pango_attr_list_insert(attr_list, und_attr);
266 started_underline =
false;
273 und_attr->start_index = word_start;
274 und_attr->end_index = word_start + word_len;
275 started_underline =
true;
279 if (started_underline) {
280 und_attr->end_index = page_text.length();
281 pango_attr_list_insert(attr_list, und_attr);
288 if (!text_length)
return 0;
291 const int max_layout_height =
vertical_text_ ? max_width : max_height;
295 const int kMaxUnicodeBufLength = 15000;
296 for (
int i = 0; i < kMaxUnicodeBufLength && it != it_end; ++it, ++i);
298 tlog(1,
"len = %d buf_len = %d\n", text_length, buf_length);
299 pango_layout_set_text(
layout_, text, buf_length);
301 PangoLayoutIter* line_iter =
nullptr;
304 line_iter = pango_layout_get_iter(
layout_);
306 bool first_page =
true;
311 PangoRectangle line_ink_rect;
312 pango_layout_iter_get_line_extents(line_iter, &line_ink_rect,
nullptr);
313 pango_extents_to_pixels(&line_ink_rect,
nullptr);
314 PangoLayoutLine* line = pango_layout_iter_get_line_readonly(line_iter);
316 page_top = line_ink_rect.y;
319 int line_bottom = line_ink_rect.y + line_ink_rect.height;
320 if (line_bottom - page_top > max_layout_height) {
321 offset = line->start_index;
322 tlog(1,
"Found offset = %d\n", offset);
325 }
while (pango_layout_iter_next_line(line_iter));
326 pango_layout_iter_free(line_iter);
345 for (
size_t i = 0; i <
boxchars_.size(); ++i)
363 std::map<int, string> start_byte_to_text;
364 PangoLayoutIter* run_iter = pango_layout_get_iter(
layout_);
365 const char* full_text = pango_layout_get_text(
layout_);
367 PangoLayoutRun* run = pango_layout_iter_get_run_readonly(run_iter);
370 tlog(2,
"Found end of line marker\n");
373 PangoGlyphItemIter cluster_iter;
374 gboolean have_cluster;
375 for (have_cluster = pango_glyph_item_iter_init_start(&cluster_iter,
378 have_cluster = pango_glyph_item_iter_next_cluster(&cluster_iter)) {
379 const int start_byte_index = cluster_iter.start_index;
380 const int end_byte_index = cluster_iter.end_index;
381 string text = string(full_text + start_byte_index,
382 end_byte_index - start_byte_index);
384 tlog(2,
"Found whitespace\n");
387 tlog(2,
"start_byte=%d end_byte=%d : '%s'\n", start_byte_index,
388 end_byte_index, text.c_str());
394 start_byte_to_text[start_byte_index] = text;
396 }
while (pango_layout_iter_next_run(run_iter));
397 pango_layout_iter_free(run_iter);
399 cluster_text->clear();
400 for (std::map<int, string>::const_iterator it = start_byte_to_text.begin();
401 it != start_byte_to_text.end(); ++it) {
402 cluster_text->push_back(it->second);
404 return !cluster_text->empty();
417 static void MergeBoxCharsToWords(std::vector<BoxChar*>* boxchars) {
418 std::vector<BoxChar*> result;
419 bool started_word =
false;
420 for (
size_t i = 0; i < boxchars->size(); ++i) {
421 if (boxchars->at(i)->ch() ==
" " || boxchars->at(i)->box() ==
nullptr) {
422 result.push_back(boxchars->at(i));
423 boxchars->at(i) =
nullptr;
424 started_word =
false;
431 result.push_back(boxchars->at(i));
432 boxchars->at(i) =
nullptr;
434 BoxChar* last_boxchar = result.back();
436 const Box* box = boxchars->at(i)->box();
438 int left = min(last_box->x, box->x);
439 int right =
max(last_box->x + last_box->w, box->x + box->w);
440 int top = min(last_box->y, box->y);
441 int bottom =
max(last_box->y + last_box->h, box->y + box->h);
445 if (right - left > last_box->w + 5 * box->w) {
446 tlog(1,
"Found line break after '%s'", last_boxchar->
ch().c_str());
449 result.push_back(
new BoxChar(
" ", 1));
450 result.push_back(boxchars->at(i));
451 boxchars->at(i) =
nullptr;
455 last_boxchar->
mutable_ch()->append(boxchars->at(i)->ch());
457 last_box->w = right - left;
459 last_box->h = bottom - top;
460 delete boxchars->at(i);
461 boxchars->at(i) =
nullptr;
464 boxchars->swap(result);
469 const char* text = pango_layout_get_text(
layout_);
470 PangoLayoutIter* cluster_iter = pango_layout_get_iter(
layout_);
473 std::vector<int> cluster_start_indices;
475 cluster_start_indices.push_back(pango_layout_iter_get_index(cluster_iter));
476 tlog(3,
"Added %d\n", cluster_start_indices.back());
477 }
while (pango_layout_iter_next_cluster(cluster_iter));
478 pango_layout_iter_free(cluster_iter);
479 cluster_start_indices.push_back(strlen(text));
480 tlog(3,
"Added last index %d\n", cluster_start_indices.back());
482 std::sort(cluster_start_indices.begin(), cluster_start_indices.end());
483 std::map<int, int> cluster_start_to_end_index;
484 for (
size_t i = 0; i + 1 < cluster_start_indices.size(); ++i) {
485 cluster_start_to_end_index[cluster_start_indices[i]]
486 = cluster_start_indices[i + 1];
491 cluster_iter = pango_layout_get_iter(
layout_);
493 std::map<int, BoxChar*> start_byte_to_box;
495 PangoRectangle cluster_rect;
496 pango_layout_iter_get_cluster_extents(cluster_iter, &cluster_rect,
nullptr);
497 pango_extents_to_pixels(&cluster_rect,
nullptr);
498 const int start_byte_index = pango_layout_iter_get_index(cluster_iter);
499 const int end_byte_index = cluster_start_to_end_index[start_byte_index];
500 string cluster_text = string(text + start_byte_index,
501 end_byte_index - start_byte_index);
502 if (!cluster_text.empty() && cluster_text[0] ==
'\n') {
503 tlog(2,
"Skipping newlines at start of text.\n");
506 if (!cluster_rect.width || !cluster_rect.height ||
508 tlog(2,
"Skipping whitespace with boxdim (%d,%d) '%s'\n",
509 cluster_rect.width, cluster_rect.height, cluster_text.c_str());
511 boxchar->set_page(
page_);
512 start_byte_to_box[start_byte_index] = boxchar;
516 tlog(2,
"[%d %d], %d, %d : start_byte=%d end_byte=%d : '%s'\n",
517 cluster_rect.x, cluster_rect.y,
518 cluster_rect.width, cluster_rect.height,
519 start_byte_index, end_byte_index,
520 cluster_text.c_str());
522 "cluster_text:%s start_byte_index:%d\n",
523 cluster_text.c_str(), start_byte_index);
525 "cluster_text:%s start_byte_index:%d\n",
526 cluster_text.c_str(), start_byte_index);
538 BoxChar* boxchar =
new BoxChar(cluster_text.c_str(), cluster_text.size());
540 boxchar->
AddBox(cluster_rect.x, cluster_rect.y,
541 cluster_rect.width, cluster_rect.height);
542 start_byte_to_box[start_byte_index] = boxchar;
543 }
while (pango_layout_iter_next_cluster(cluster_iter));
544 pango_layout_iter_free(cluster_iter);
552 std::vector<string> cluster_text;
554 ASSERT_HOST(cluster_text.size() == start_byte_to_box.size());
556 for (std::map<int, BoxChar*>::iterator it = start_byte_to_box.begin();
557 it != start_byte_to_box.end(); ++it, ++ind) {
558 it->second->mutable_ch()->swap(cluster_text[ind]);
563 std::vector<BoxChar*> page_boxchars;
564 page_boxchars.reserve(start_byte_to_box.size());
566 for (std::map<int, BoxChar*>::const_iterator it = start_byte_to_box.begin();
567 it != start_byte_to_box.end(); ++it) {
568 if (it->second->ch() == kWordJoinerUTF8) {
572 page_boxchars.push_back(it->second);
578 for (std::map<int, BoxChar*>::iterator it = start_byte_to_box.begin();
579 it != start_byte_to_box.end(); ++it) {
582 it->second->mutable_ch()->swap(half);
588 MergeBoxCharsToWords(&page_boxchars);
594 Box* page_box =
nullptr;
595 Boxa* all_boxes =
nullptr;
596 for (
size_t i = 0; i < page_boxchars.size(); ++i) {
597 if (page_boxchars[i]->box() ==
nullptr)
continue;
598 if (all_boxes ==
nullptr) all_boxes = boxaCreate(0);
599 boxaAddBox(all_boxes, page_boxchars[i]->mutable_box(), L_CLONE);
601 if (all_boxes !=
nullptr) {
602 boxaGetExtent(all_boxes,
nullptr,
nullptr, &page_box);
603 boxaDestroy(&all_boxes);
611 std::vector<BoxChar*>* boxchars) {
613 const double rotation = - pango_gravity_to_rotation(
614 pango_context_get_base_gravity(pango_layout_get_context(
layout_)));
617 0, boxchars->size(), boxchars);
625 const char* text = utf8_text->c_str();
630 output_text.append(text + offset, space_len);
632 if (offset == utf8_text->length())
break;
636 output_text.append(text + offset, word_len);
642 utf8_text->swap(output_text);
644 if (num_dropped > 0) {
645 tprintf(
"Stripped %d unrenderable words\n", num_dropped);
652 Pix* orig_pix =
nullptr;
655 *pix = pixConvertTo8(orig_pix,
false);
656 pixDestroy(&orig_pix);
662 int threshold, Pix** pix) {
663 Pix* orig_pix =
nullptr;
666 Pix* gray_pix = pixConvertTo8(orig_pix,
false);
667 pixDestroy(&orig_pix);
668 *pix = pixThresholdToBinary(gray_pix, threshold);
669 pixDestroy(&gray_pix);
684 it != it_end; ++it) {
686 out_str.append(it.utf8_data(), it.utf8_len());
690 bool next_char_is_boundary = (next_it == it_end || *next_it ==
' ');
691 bool next_char_is_combiner = (next_it == it_end) ?
692 false : IsCombiner(*next_it);
693 if (*it !=
' ' && *it !=
'\n' && !next_char_is_boundary &&
694 !next_char_is_combiner) {
695 out_str += kWordJoinerUTF8;
707 it != it_end; ++it) {
712 char32 full_char = *it + 0xFEE0;
713 full_str.append(EncodeAsUTF8(full_char));
715 full_str.append(it.utf8_data(), it.utf8_len());
726 it != it_end; ++it) {
731 isprint(half_char) && !isspace(half_char)) {
732 half_str.append(EncodeAsUTF8(half_char));
734 half_str.append(it.utf8_data(), it.utf8_len());
743 if (pix && *pix) pixDestroy(pix);
765 double rotation = - pango_gravity_to_rotation(
766 pango_context_get_base_gravity(pango_layout_get_context(
layout_)));
767 tlog(2,
"Rotating by %f radians\n", rotation);
768 cairo_rotate(
cr_, rotation);
771 string page_text(text, page_offset);
783 tprintf(
"WARNING: Dropped %d uncovered characters\n", num_dropped);
794 pango_layout_set_text(
layout_, page_text.c_str(), page_text.length());
798 cairo_set_source_rgb(
cr_, 1.0, 1.0, 1.0);
843 const char* text,
int text_length,
844 string* font_used, Pix** image) {
847 const char kTitleTemplate[] =
"%s : %d hits = %.2f%%, raw = %d = %.2f%%";
850 &title_font,
nullptr)) {
851 tprintf(
"WARNING: Could not find a font to render image title with!\n");
852 title_font =
"Arial";
855 tlog(1,
"Selected title font: %s\n", title_font.c_str());
856 if (font_used) font_used->clear();
871 for (
unsigned int i = static_cast<unsigned int>(
font_index_); i < all_fonts.size(); ++i) {
876 if (ok_chars > 0 && ok_chars >=
total_chars_ * min_coverage) {
880 const int kMaxTitleLength = 1024;
881 char title[kMaxTitleLength];
882 snprintf(title, kMaxTitleLength, kTitleTemplate,
883 all_fonts[i].c_str(), ok_chars,
891 if (font_used) *font_used = all_fonts[i];
896 Pix* title_image =
nullptr;
898 pixOr(*image, *image, title_image);
899 pixDestroy(&title_image);
906 tprintf(
"Font %s failed with %d hits = %.2f%%\n",
907 all_fonts[i].c_str(), ok_chars, 100.0 * ok_chars /
total_chars_);
static const_iterator begin(const char *utf8_str, const int byte_length)
int SpanUTF8NotWhitespace(const char *text)
void WriteAllBoxes(const string &filename)
static void TranslateBoxes(int xshift, int yshift, std::vector< BoxChar *> *boxes)
bool GetClusterStrings(std::vector< string > *cluster_text)
PangoUnderline underline_style_
std::vector< BoxChar * > boxchars_
bool IsInterchangeValid7BitAscii(const char32 ch)
#define ASSERT_HOST_MSG(x,...)
int DropUncoveredChars(string *utf8_text) const
void ComputeClusterBoxes()
bool set_font(const string &desc)
static const std::vector< string > & ListAvailableFonts()
static int FontScore(const std::unordered_map< char32, inT64 > &ch_map, const string &fontname, int *raw_score, std::vector< bool > *ch_flags)
Pix * CairoARGB32ToPixFormat(cairo_surface_t *surface)
static const_iterator end(const char *utf8_str, const int byte_length)
static string ConvertBasicLatinToFullwidthLatin(const string &text)
bool CanRenderString(const char *utf8_word, int len, std::vector< string > *graphemes) const
double underline_continuation_prob_
char32 FullwidthToHalfwidth(const char32 ch)
const string & ch() const
StringRenderer(const string &font_desc, int page_width, int page_height)
int RenderToImage(const char *text, int text_length, Pix **pix)
double underline_start_prob_
int RenderAllFontsToImage(double min_coverage, const char *text, int text_length, string *font_used, Pix **pix)
void set_features(const char *features)
#define DISABLE_HEAP_LEAK_CHECK
static void WriteTesseractBoxFile(const string &name, int height, const std::vector< BoxChar *> &boxes)
const char * utf8_data() const
string AddLigatures(const string &str, const PangoFontInfo *font) const
static bool SelectFont(const char *utf8_word, const int utf8_len, string *font_name, std::vector< string > *graphemes)
bool CoversUTF8Text(const char *utf8_text, int byte_length) const
int StripUnrenderableWords(string *utf8_text) const
int FindFirstPageBreakOffset(const char *text, int text_length)
static LigatureTable * Get()
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)
string DescriptionName() const
void SetLayoutProperties()
bool drop_uncovered_chars_
void SetWordUnderlineAttributes(const string &page_text)
void set_underline_start_prob(const double frac)
std::unordered_map< char32, inT64 > char_map_
Boxa * GetPageBoxes() const
bool strip_unrenderable_words_
void set_resolution(const int resolution)
void set_resolution(const int resolution)
static string InsertWordJoiners(const string &text)
static string GetTesseractBoxStr(int height, const std::vector< BoxChar *> &boxes)
static void RotateBoxes(float rotation, int xcenter, int ycenter, int start_box, int end_box, std::vector< BoxChar *> *boxes)
static void PrepareToWrite(std::vector< BoxChar *> *boxes)
int RenderToGrayscaleImage(const char *text, int text_length, Pix **pix)
bool IsUTF8Whitespace(const char *text)
bool render_fullwidth_latin_
const std::vector< BoxChar * > & GetBoxes() const
void AddBox(int x, int y, int width, int height)
bool ParseFontDescriptionName(const string &name)
void RotatePageBoxes(float rotation)
int SpanUTF8Whitespace(const char *text)
void CorrectBoxPositionsToLayout(std::vector< BoxChar *> *boxchars)
cairo_surface_t * surface_
bool gravity_hint_strong_