tesseract  4.00.00dev
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, string *best_match)
 
static const std::vector< string > & ListAvailableFonts ()
 
static bool SelectFont (const char *utf8_word, const int utf8_len, string *font_name, std::vector< string > *graphemes)
 
static bool SelectFont (const char *utf8_word, const int utf8_len, const std::vector< string > &all_fonts, string *font_name, std::vector< string > *graphemes)
 
static void GetAllRenderableCharacters (std::vector< bool > *unichar_bitmap)
 
static void GetAllRenderableCharacters (const std::vector< string > &font_names, std::vector< bool > *unichar_bitmap)
 
static void GetAllRenderableCharacters (const string &font_name, std::vector< bool > *unichar_bitmap)
 
static string BestFonts (const std::unordered_map< char32, inT64 > &ch_map, std::vector< std::pair< const char *, std::vector< bool > > > *font_flag)
 
static int FontScore (const std::unordered_map< char32, inT64 > &ch_map, const string &fontname, int *raw_score, std::vector< bool > *ch_flags)
 
static void ReInit ()
 

Detailed Description

Definition at line 160 of file pango_font_info.h.

Member Function Documentation

◆ BestFonts()

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

Definition at line 726 of file pango_font_info.cpp.

728  {
729  const double kMinOKFraction = 0.99;
730  // Weighted fraction of characters that must be renderable in a font to make
731  // it OK even if the raw count is not good.
732  const double kMinWeightedFraction = 0.99995;
733 
734  fonts->clear();
735  std::vector<std::vector<bool> > font_flags;
736  std::vector<int> font_scores;
737  std::vector<int> raw_scores;
738  int most_ok_chars = 0;
739  int best_raw_score = 0;
740  const std::vector<string>& font_names = FontUtils::ListAvailableFonts();
741  for (unsigned i = 0; i < font_names.size(); ++i) {
742  std::vector<bool> ch_flags;
743  int raw_score = 0;
744  int ok_chars = FontScore(ch_map, font_names[i], &raw_score, &ch_flags);
745  most_ok_chars = MAX(ok_chars, most_ok_chars);
746  best_raw_score = MAX(raw_score, best_raw_score);
747 
748  font_flags.push_back(ch_flags);
749  font_scores.push_back(ok_chars);
750  raw_scores.push_back(raw_score);
751  }
752 
753  // Now select the fonts with a score above a threshold fraction
754  // of both the raw and weighted best scores. To prevent bogus fonts being
755  // selected for CJK, we require a high fraction (kMinOKFraction = 0.99) of
756  // BOTH weighted and raw scores.
757  // In low character-count scripts, the issue is more getting enough fonts,
758  // when only 1 or 2 might have all those rare dingbats etc in them, so we
759  // allow a font with a very high weighted (coverage) score
760  // (kMinWeightedFraction = 0.99995) to be used even if its raw score is poor.
761  int least_good_enough = static_cast<int>(most_ok_chars * kMinOKFraction);
762  int least_raw_enough = static_cast<int>(best_raw_score * kMinOKFraction);
763  int override_enough = static_cast<int>(most_ok_chars * kMinWeightedFraction);
764 
765  string font_list;
766  for (unsigned i = 0; i < font_names.size(); ++i) {
767  int score = font_scores[i];
768  int raw_score = raw_scores[i];
769  if ((score >= least_good_enough && raw_score >= least_raw_enough) ||
770  score >= override_enough) {
771  fonts->push_back(std::make_pair(font_names[i].c_str(), font_flags[i]));
772  tlog(1, "OK font %s = %.4f%%, raw = %d = %.2f%%\n",
773  font_names[i].c_str(),
774  100.0 * score / most_ok_chars,
775  raw_score, 100.0 * raw_score / best_raw_score);
776  font_list += font_names[i];
777  font_list += "\n";
778  } else if (score >= least_good_enough || raw_score >= least_raw_enough) {
779  tlog(1, "Runner-up font %s = %.4f%%, raw = %d = %.2f%%\n",
780  font_names[i].c_str(),
781  100.0 * score / most_ok_chars,
782  raw_score, 100.0 * raw_score / best_raw_score);
783  }
784  }
785  return font_list;
786 }
#define tlog(level,...)
Definition: tlog.h:33
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)
#define MAX(x, y)
Definition: ndminx.h:24

◆ FontScore()

int tesseract::FontUtils::FontScore ( const std::unordered_map< char32, inT64 > &  ch_map,
const string &  fontname,
int raw_score,
std::vector< bool > *  ch_flags 
)
static

Definition at line 692 of file pango_font_info.cpp.

694  {
695  PangoFontInfo font_info;
696  if (!font_info.ParseFontDescriptionName(fontname)) {
697  tprintf("ERROR: Could not parse %s\n", fontname.c_str());
698  }
699  PangoFont* font = font_info.ToPangoFont();
700  PangoCoverage* coverage = pango_font_get_coverage(font, nullptr);
701 
702  if (ch_flags) {
703  ch_flags->clear();
704  ch_flags->reserve(ch_map.size());
705  }
706  *raw_score = 0;
707  int ok_chars = 0;
708  for (std::unordered_map<char32, inT64>::const_iterator it = ch_map.begin();
709  it != ch_map.end(); ++it) {
710  bool covered = (IsWhitespace(it->first) ||
711  (pango_coverage_get(coverage, it->first)
712  == PANGO_COVERAGE_EXACT));
713  if (covered) {
714  ++(*raw_score);
715  ok_chars += it->second;
716  }
717  if (ch_flags) {
718  ch_flags->push_back(covered);
719  }
720  }
721  return ok_chars;
722 }
#define tprintf(...)
Definition: tprintf.h:31
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:178

◆ GetAllRenderableCharacters() [1/3]

void tesseract::FontUtils::GetAllRenderableCharacters ( std::vector< bool > *  unichar_bitmap)
static

Definition at line 657 of file pango_font_info.cpp.

657  {
658  const std::vector<string>& all_fonts = ListAvailableFonts();
659  return GetAllRenderableCharacters(all_fonts, unichar_bitmap);
660 }
static const std::vector< string > & ListAvailableFonts()
static void GetAllRenderableCharacters(std::vector< bool > *unichar_bitmap)

◆ GetAllRenderableCharacters() [2/3]

void tesseract::FontUtils::GetAllRenderableCharacters ( const std::vector< string > &  font_names,
std::vector< bool > *  unichar_bitmap 
)
static

Definition at line 672 of file pango_font_info.cpp.

673  {
674  // Form the union of coverage maps from the fonts
675  PangoCoverage* all_coverage = pango_coverage_new();
676  tlog(1, "Processing %u fonts\n", static_cast<unsigned>(fonts.size()));
677  for (unsigned i = 0; i < fonts.size(); ++i) {
678  PangoFontInfo font_info(fonts[i]);
679  PangoCoverage* coverage =
680  pango_font_get_coverage(font_info.ToPangoFont(), nullptr);
681  // Mark off characters that any font can render.
682  pango_coverage_max(all_coverage, coverage);
683  }
684  CharCoverageMapToBitmap(all_coverage, unichar_bitmap);
685  pango_coverage_unref(all_coverage);
686 }
#define tlog(level,...)
Definition: tlog.h:33

◆ GetAllRenderableCharacters() [3/3]

void tesseract::FontUtils::GetAllRenderableCharacters ( const string &  font_name,
std::vector< bool > *  unichar_bitmap 
)
static

Definition at line 663 of file pango_font_info.cpp.

664  {
665  PangoFontInfo font_info(font_name);
666  PangoCoverage* coverage =
667  pango_font_get_coverage(font_info.ToPangoFont(), nullptr);
668  CharCoverageMapToBitmap(coverage, unichar_bitmap);
669 }

◆ IsAvailableFont() [1/2]

static bool tesseract::FontUtils::IsAvailableFont ( const char *  font_desc)
inlinestatic

Definition at line 164 of file pango_font_info.h.

164  {
165  return IsAvailableFont(font_desc, nullptr);
166  }
static bool IsAvailableFont(const char *font_desc)

◆ IsAvailableFont() [2/2]

bool tesseract::FontUtils::IsAvailableFont ( const char *  font_desc,
string *  best_match 
)
static

Definition at line 523 of file pango_font_info.cpp.

524  {
525  string query_desc(input_query_desc);
526 #if (PANGO_VERSION <= 12005)
527  // Strip commas and any ' Medium' substring in the name.
528  query_desc.erase(std::remove(query_desc.begin(), query_desc.end(), ','),
529  query_desc.end());
530  const string kMediumStr = " Medium";
531  std::size_t found = query_desc.find(kMediumStr);
532  if (found != std::string::npos) {
533  query_desc.erase(found, kMediumStr.length());
534  }
535 #endif
536  PangoFontDescription *desc = pango_font_description_from_string(
537  query_desc.c_str());
538  PangoFont* selected_font = nullptr;
539  {
541  PangoFontMap* font_map = pango_cairo_font_map_get_default();
542  PangoContext* context = pango_context_new();
543  pango_context_set_font_map(context, font_map);
544  {
546  selected_font = pango_font_map_load_font(font_map, context, desc);
547  }
548  g_object_unref(context);
549  }
550  if (selected_font == nullptr) {
551  pango_font_description_free(desc);
552  return false;
553  }
554  PangoFontDescription* selected_desc = pango_font_describe(selected_font);
555 
556  bool equal = pango_font_description_equal(desc, selected_desc);
557  tlog(3, "query weight = %d \t selected weight =%d\n",
558  pango_font_description_get_weight(desc),
559  pango_font_description_get_weight(selected_desc));
560 
561  char* selected_desc_str = pango_font_description_to_string(selected_desc);
562  tlog(2, "query_desc: '%s' Selected: '%s'\n", query_desc.c_str(),
563  selected_desc_str);
564  if (!equal && best_match != nullptr) {
565  *best_match = selected_desc_str;
566  // Clip the ending ' 0' if there is one. It seems that, if there is no
567  // point size on the end of the fontname, then Pango always appends ' 0'.
568  int len = best_match->size();
569  if (len > 2 && best_match->at(len - 1) == '0' &&
570  best_match->at(len - 2) == ' ') {
571  *best_match = best_match->substr(0, len - 2);
572  }
573  }
574  g_free(selected_desc_str);
575  pango_font_description_free(selected_desc);
576  g_object_unref(selected_font);
577  pango_font_description_free(desc);
578  return equal;
579 }
#define tlog(level,...)
Definition: tlog.h:33
#define DISABLE_HEAP_LEAK_CHECK
Definition: util.h:63

◆ ListAvailableFonts()

const std::vector< string > & tesseract::FontUtils::ListAvailableFonts ( )
static

Definition at line 594 of file pango_font_info.cpp.

594  {
595  if (!available_fonts_.empty()) {
596  return available_fonts_;
597  }
598 #ifndef USE_STD_NAMESPACE
599  if (FLAGS_use_only_legacy_fonts) {
600  // Restrict view to list of fonts in legacy_fonts.h
601  tprintf("Using list of legacy fonts only\n");
602  const int kNumFontLists = 4;
603  for (int i = 0; i < kNumFontLists; ++i) {
604  for (int j = 0; kFontlists[i][j] != nullptr; ++j) {
605  available_fonts_.push_back(kFontlists[i][j]);
606  }
607  }
608  return available_fonts_;
609  }
610 #endif
611 
612  PangoFontFamily** families = 0;
613  int n_families = 0;
614  ListFontFamilies(&families, &n_families);
615  for (int i = 0; i < n_families; ++i) {
616  const char* family_name = pango_font_family_get_name(families[i]);
617  tlog(2, "Listing family %s\n", family_name);
618  if (ShouldIgnoreFontFamilyName(family_name)) {
619  continue;
620  }
621 
622  int n_faces;
623  PangoFontFace** faces = nullptr;
624  pango_font_family_list_faces(families[i], &faces, &n_faces);
625  for (int j = 0; j < n_faces; ++j) {
626  PangoFontDescription* desc = pango_font_face_describe(faces[j]);
627  char* desc_str = pango_font_description_to_string(desc);
628  if (IsAvailableFont(desc_str)) {
629  available_fonts_.push_back(desc_str);
630  }
631  pango_font_description_free(desc);
632  g_free(desc_str);
633  }
634  g_free(faces);
635  }
636  g_free(families);
637  std::sort(available_fonts_.begin(), available_fonts_.end());
638  return available_fonts_;
639 }
#define tlog(level,...)
Definition: tlog.h:33
#define tprintf(...)
Definition: tprintf.h:31
static bool IsAvailableFont(const char *font_desc)

◆ ReInit()

void tesseract::FontUtils::ReInit ( )
static

Definition at line 818 of file pango_font_info.cpp.

818 { available_fonts_.clear(); }

◆ SelectFont() [1/2]

bool tesseract::FontUtils::SelectFont ( const char *  utf8_word,
const int  utf8_len,
string *  font_name,
std::vector< string > *  graphemes 
)
static

Definition at line 789 of file pango_font_info.cpp.

790  {
791  return SelectFont(utf8_word, utf8_len, ListAvailableFonts(), font_name,
792  graphemes);
793 }
static const std::vector< string > & ListAvailableFonts()
static bool SelectFont(const char *utf8_word, const int utf8_len, string *font_name, std::vector< string > *graphemes)

◆ SelectFont() [2/2]

bool tesseract::FontUtils::SelectFont ( const char *  utf8_word,
const int  utf8_len,
const std::vector< string > &  all_fonts,
string *  font_name,
std::vector< string > *  graphemes 
)
static

Definition at line 796 of file pango_font_info.cpp.

798  {
799  if (font_name) font_name->clear();
800  if (graphemes) graphemes->clear();
801  for (unsigned i = 0; i < all_fonts.size(); ++i) {
802  PangoFontInfo font;
803  std::vector<string> found_graphemes;
804  ASSERT_HOST_MSG(font.ParseFontDescriptionName(all_fonts[i]),
805  "Could not parse font desc name %s\n",
806  all_fonts[i].c_str());
807  if (font.CanRenderString(utf8_word, utf8_len, &found_graphemes)) {
808  if (graphemes) graphemes->swap(found_graphemes);
809  if (font_name) *font_name = all_fonts[i];
810  return true;
811  }
812  }
813  return false;
814 }
#define ASSERT_HOST_MSG(x,...)
Definition: errcode.h:90

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