tesseract  4.1.0
tesseract::FontUtils Class Reference

#include <pango_font_info.h>

Static Public Member Functions

static bool IsAvailableFont (const char *font_desc)
 
static bool IsAvailableFont (const char *font_desc, std::string *best_match)
 
static const std::vector< std::string > & ListAvailableFonts ()
 
static bool SelectFont (const char *utf8_word, const int utf8_len, std::string *font_name, std::vector< std::string > *graphemes)
 
static bool SelectFont (const char *utf8_word, const int utf8_len, const std::vector< std::string > &all_fonts, std::string *font_name, std::vector< std::string > *graphemes)
 
static void GetAllRenderableCharacters (std::vector< bool > *unichar_bitmap)
 
static void GetAllRenderableCharacters (const std::vector< std::string > &font_names, std::vector< bool > *unichar_bitmap)
 
static void GetAllRenderableCharacters (const std::string &font_name, std::vector< bool > *unichar_bitmap)
 
static std::string BestFonts (const std::unordered_map< char32, int64_t > &ch_map, std::vector< std::pair< const char *, std::vector< bool > > > *font_flag)
 
static int FontScore (const std::unordered_map< char32, int64_t > &ch_map, const std::string &fontname, int *raw_score, std::vector< bool > *ch_flags)
 
static void ReInit ()
 
static void PangoFontTypeInfo ()
 

Detailed Description

Definition at line 146 of file pango_font_info.h.

Member Function Documentation

std::string tesseract::FontUtils::BestFonts ( const std::unordered_map< char32, int64_t > &  ch_map,
std::vector< std::pair< const char *, std::vector< bool > > > *  font_flag 
)
static

Definition at line 694 of file pango_font_info.cpp.

696  {
697  const double kMinOKFraction = 0.99;
698  // Weighted fraction of characters that must be renderable in a font to make
699  // it OK even if the raw count is not good.
700  const double kMinWeightedFraction = 0.99995;
701 
702  fonts->clear();
703  std::vector<std::vector<bool> > font_flags;
704  std::vector<int> font_scores;
705  std::vector<int> raw_scores;
706  int most_ok_chars = 0;
707  int best_raw_score = 0;
708  const std::vector<std::string>& font_names = FontUtils::ListAvailableFonts();
709  for (unsigned i = 0; i < font_names.size(); ++i) {
710  std::vector<bool> ch_flags;
711  int raw_score = 0;
712  int ok_chars = FontScore(ch_map, font_names[i], &raw_score, &ch_flags);
713  most_ok_chars = std::max(ok_chars, most_ok_chars);
714  best_raw_score = std::max(raw_score, best_raw_score);
715 
716  font_flags.push_back(ch_flags);
717  font_scores.push_back(ok_chars);
718  raw_scores.push_back(raw_score);
719  }
720 
721  // Now select the fonts with a score above a threshold fraction
722  // of both the raw and weighted best scores. To prevent bogus fonts being
723  // selected for CJK, we require a high fraction (kMinOKFraction = 0.99) of
724  // BOTH weighted and raw scores.
725  // In low character-count scripts, the issue is more getting enough fonts,
726  // when only 1 or 2 might have all those rare dingbats etc in them, so we
727  // allow a font with a very high weighted (coverage) score
728  // (kMinWeightedFraction = 0.99995) to be used even if its raw score is poor.
729  int least_good_enough = static_cast<int>(most_ok_chars * kMinOKFraction);
730  int least_raw_enough = static_cast<int>(best_raw_score * kMinOKFraction);
731  int override_enough = static_cast<int>(most_ok_chars * kMinWeightedFraction);
732 
733  std::string font_list;
734  for (unsigned i = 0; i < font_names.size(); ++i) {
735  int score = font_scores[i];
736  int raw_score = raw_scores[i];
737  if ((score >= least_good_enough && raw_score >= least_raw_enough) ||
738  score >= override_enough) {
739  fonts->push_back(std::make_pair(font_names[i].c_str(), font_flags[i]));
740  tlog(1, "OK font %s = %.4f%%, raw = %d = %.2f%%\n",
741  font_names[i].c_str(),
742  100.0 * score / most_ok_chars,
743  raw_score, 100.0 * raw_score / best_raw_score);
744  font_list += font_names[i];
745  font_list += "\n";
746  } else if (score >= least_good_enough || raw_score >= least_raw_enough) {
747  tlog(1, "Runner-up font %s = %.4f%%, raw = %d = %.2f%%\n",
748  font_names[i].c_str(),
749  100.0 * score / most_ok_chars,
750  raw_score, 100.0 * raw_score / best_raw_score);
751  }
752  }
753  return font_list;
754 }
static const std::vector< std::string > & ListAvailableFonts()
#define tlog(level,...)
Definition: tlog.h:33
static int FontScore(const std::unordered_map< char32, int64_t > &ch_map, const std::string &fontname, int *raw_score, std::vector< bool > *ch_flags)
int tesseract::FontUtils::FontScore ( const std::unordered_map< char32, int64_t > &  ch_map,
const std::string &  fontname,
int *  raw_score,
std::vector< bool > *  ch_flags 
)
static

Definition at line 658 of file pango_font_info.cpp.

660  {
661  PangoFontInfo font_info;
662  if (!font_info.ParseFontDescriptionName(fontname)) {
663  tprintf("ERROR: Could not parse %s\n", fontname.c_str());
664  }
665  PangoFont* font = font_info.ToPangoFont();
666  PangoCoverage* coverage = nullptr;
667  if (font != nullptr) coverage = pango_font_get_coverage(font, nullptr);
668  if (ch_flags) {
669  ch_flags->clear();
670  ch_flags->reserve(ch_map.size());
671  }
672  *raw_score = 0;
673  int ok_chars = 0;
674  for (std::unordered_map<char32, int64_t>::const_iterator it = ch_map.begin();
675  it != ch_map.end(); ++it) {
676  bool covered = (coverage != nullptr) && (IsWhitespace(it->first) ||
677  (pango_coverage_get(coverage, it->first)
678  == PANGO_COVERAGE_EXACT));
679  if (covered) {
680  ++(*raw_score);
681  ok_chars += it->second;
682  }
683  if (ch_flags) {
684  ch_flags->push_back(covered);
685  }
686  }
687  pango_coverage_unref(coverage);
688  g_object_unref(font);
689  return ok_chars;
690 }
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:223
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
void tesseract::FontUtils::GetAllRenderableCharacters ( std::vector< bool > *  unichar_bitmap)
static

Definition at line 613 of file pango_font_info.cpp.

613  {
614  const std::vector<std::string>& all_fonts = ListAvailableFonts();
615  return GetAllRenderableCharacters(all_fonts, unichar_bitmap);
616 }
static void GetAllRenderableCharacters(std::vector< bool > *unichar_bitmap)
static const std::vector< std::string > & ListAvailableFonts()
void tesseract::FontUtils::GetAllRenderableCharacters ( const std::vector< std::string > &  font_names,
std::vector< bool > *  unichar_bitmap 
)
static

Definition at line 633 of file pango_font_info.cpp.

634  {
635  // Form the union of coverage maps from the fonts
636  PangoCoverage* all_coverage = pango_coverage_new();
637  tlog(1, "Processing %u fonts\n", static_cast<unsigned>(fonts.size()));
638  for (unsigned i = 0; i < fonts.size(); ++i) {
639  PangoFontInfo font_info(fonts[i]);
640  PangoFont* font = font_info.ToPangoFont();
641  if (font != nullptr) {
642  // Font found.
643  PangoCoverage* coverage = pango_font_get_coverage(font, nullptr);
644  // Mark off characters that any font can render.
645  pango_coverage_max(all_coverage, coverage);
646  pango_coverage_unref(coverage);
647  g_object_unref(font);
648  }
649  }
650  CharCoverageMapToBitmap(all_coverage, unichar_bitmap);
651  pango_coverage_unref(all_coverage);
652 }
#define tlog(level,...)
Definition: tlog.h:33
void tesseract::FontUtils::GetAllRenderableCharacters ( const std::string &  font_name,
std::vector< bool > *  unichar_bitmap 
)
static

Definition at line 619 of file pango_font_info.cpp.

620  {
621  PangoFontInfo font_info(font_name);
622  PangoFont* font = font_info.ToPangoFont();
623  if (font != nullptr) {
624  // Font found.
625  PangoCoverage* coverage = pango_font_get_coverage(font, nullptr);
626  CharCoverageMapToBitmap(coverage, unichar_bitmap);
627  pango_coverage_unref(coverage);
628  g_object_unref(font);
629  }
630 }
static bool tesseract::FontUtils::IsAvailableFont ( const char *  font_desc)
inlinestatic

Definition at line 150 of file pango_font_info.h.

150  {
151  return IsAvailableFont(font_desc, nullptr);
152  }
static bool IsAvailableFont(const char *font_desc)
bool tesseract::FontUtils::IsAvailableFont ( const char *  font_desc,
std::string *  best_match 
)
static

Definition at line 489 of file pango_font_info.cpp.

490  {
491  std::string query_desc(input_query_desc);
492  PangoFontDescription *desc = pango_font_description_from_string(
493  query_desc.c_str());
494  PangoFont* selected_font = nullptr;
495  {
497  PangoFontMap* font_map = pango_cairo_font_map_get_default();
498  PangoContext* context = pango_context_new();
499  pango_context_set_font_map(context, font_map);
500  {
502  selected_font = pango_font_map_load_font(font_map, context, desc);
503  }
504  g_object_unref(context);
505  }
506  if (selected_font == nullptr) {
507  pango_font_description_free(desc);
508  return false;
509  }
510  PangoFontDescription* selected_desc = pango_font_describe(selected_font);
511 
512  bool equal = pango_font_description_equal(desc, selected_desc);
513  tlog(3, "query weight = %d \t selected weight =%d\n",
514  pango_font_description_get_weight(desc),
515  pango_font_description_get_weight(selected_desc));
516 
517  char* selected_desc_str = pango_font_description_to_string(selected_desc);
518  tlog(2, "query_desc: '%s' Selected: '%s'\n", query_desc.c_str(),
519  selected_desc_str);
520  if (!equal && best_match != nullptr) {
521  *best_match = selected_desc_str;
522  // Clip the ending ' 0' if there is one. It seems that, if there is no
523  // point size on the end of the fontname, then Pango always appends ' 0'.
524  int len = best_match->size();
525  if (len > 2 && best_match->at(len - 1) == '0' &&
526  best_match->at(len - 2) == ' ') {
527  *best_match = best_match->substr(0, len - 2);
528  }
529  }
530  g_free(selected_desc_str);
531  pango_font_description_free(selected_desc);
532  g_object_unref(selected_font);
533  pango_font_description_free(desc);
534  return equal;
535 }
#define DISABLE_HEAP_LEAK_CHECK
Definition: util.h:61
#define tlog(level,...)
Definition: tlog.h:33
const std::vector< std::string > & tesseract::FontUtils::ListAvailableFonts ( )
static

Definition at line 550 of file pango_font_info.cpp.

550  {
551  if (!available_fonts_.empty()) {
552  return available_fonts_;
553  }
554 #ifdef GOOGLE_TESSERACT
555  if (FLAGS_use_only_legacy_fonts) {
556  // Restrict view to list of fonts in legacy_fonts.h
557  tprintf("Using list of legacy fonts only\n");
558  const int kNumFontLists = 4;
559  for (int i = 0; i < kNumFontLists; ++i) {
560  for (int j = 0; kFontlists[i][j] != nullptr; ++j) {
561  available_fonts_.push_back(kFontlists[i][j]);
562  }
563  }
564  return available_fonts_;
565  }
566 #endif
567 
568  PangoFontFamily** families = nullptr;
569  int n_families = 0;
570  ListFontFamilies(&families, &n_families);
571  for (int i = 0; i < n_families; ++i) {
572  const char* family_name = pango_font_family_get_name(families[i]);
573  tlog(2, "Listing family %s\n", family_name);
574  if (ShouldIgnoreFontFamilyName(family_name)) {
575  continue;
576  }
577 
578  int n_faces;
579  PangoFontFace** faces = nullptr;
580  pango_font_family_list_faces(families[i], &faces, &n_faces);
581  for (int j = 0; j < n_faces; ++j) {
582  PangoFontDescription* desc = pango_font_face_describe(faces[j]);
583  char* desc_str = pango_font_description_to_string(desc);
584  if (IsAvailableFont(desc_str)) {
585  available_fonts_.push_back(desc_str);
586  }
587  pango_font_description_free(desc);
588  g_free(desc_str);
589  }
590  g_free(faces);
591  }
592  g_free(families);
593  std::sort(available_fonts_.begin(), available_fonts_.end());
594  return available_fonts_;
595 }
static bool IsAvailableFont(const char *font_desc)
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
#define tlog(level,...)
Definition: tlog.h:33
void tesseract::FontUtils::PangoFontTypeInfo ( )
static

Definition at line 790 of file pango_font_info.cpp.

790  {
791  PangoFontMap* font_map = pango_cairo_font_map_get_default();
792  if (pango_cairo_font_map_get_font_type(reinterpret_cast<PangoCairoFontMap*>(
793  font_map)) == CAIRO_FONT_TYPE_TOY) {
794  printf("Using CAIRO_FONT_TYPE_TOY.\n");
795  } else if (pango_cairo_font_map_get_font_type(
796  reinterpret_cast<PangoCairoFontMap*>(font_map)) ==
797  CAIRO_FONT_TYPE_FT) {
798  printf("Using CAIRO_FONT_TYPE_FT.\n");
799  } else if (pango_cairo_font_map_get_font_type(
800  reinterpret_cast<PangoCairoFontMap*>(font_map)) ==
801  CAIRO_FONT_TYPE_WIN32) {
802  printf("Using CAIRO_FONT_TYPE_WIN32.\n");
803  } else if (pango_cairo_font_map_get_font_type(
804  reinterpret_cast<PangoCairoFontMap*>(font_map)) ==
805  CAIRO_FONT_TYPE_QUARTZ) {
806  printf("Using CAIRO_FONT_TYPE_QUARTZ.\n");
807  } else if (pango_cairo_font_map_get_font_type(
808  reinterpret_cast<PangoCairoFontMap*>(font_map)) ==
809  CAIRO_FONT_TYPE_USER) {
810  printf("Using CAIRO_FONT_TYPE_USER.\n");
811  } else if (!font_map) {
812  printf("Can not create pango cairo font map!\n");
813  }
814 }
void tesseract::FontUtils::ReInit ( )
static

Definition at line 786 of file pango_font_info.cpp.

786 { available_fonts_.clear(); }
bool tesseract::FontUtils::SelectFont ( const char *  utf8_word,
const int  utf8_len,
std::string *  font_name,
std::vector< std::string > *  graphemes 
)
static

Definition at line 757 of file pango_font_info.cpp.

758  {
759  return SelectFont(utf8_word, utf8_len, ListAvailableFonts(), font_name,
760  graphemes);
761 }
static bool SelectFont(const char *utf8_word, const int utf8_len, std::string *font_name, std::vector< std::string > *graphemes)
static const std::vector< std::string > & ListAvailableFonts()
bool tesseract::FontUtils::SelectFont ( const char *  utf8_word,
const int  utf8_len,
const std::vector< std::string > &  all_fonts,
std::string *  font_name,
std::vector< std::string > *  graphemes 
)
static

Definition at line 764 of file pango_font_info.cpp.

766  {
767  if (font_name) font_name->clear();
768  if (graphemes) graphemes->clear();
769  for (unsigned i = 0; i < all_fonts.size(); ++i) {
770  PangoFontInfo font;
771  std::vector<std::string> found_graphemes;
772  ASSERT_HOST_MSG(font.ParseFontDescriptionName(all_fonts[i]),
773  "Could not parse font desc name %s\n",
774  all_fonts[i].c_str());
775  if (font.CanRenderString(utf8_word, utf8_len, &found_graphemes)) {
776  if (graphemes) graphemes->swap(found_graphemes);
777  if (font_name) *font_name = all_fonts[i];
778  return true;
779  }
780  }
781  return false;
782 }
#define ASSERT_HOST_MSG(x,...)
Definition: errcode.h:92

The documentation for this class was generated from the following files: