tesseract  3.05.01
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 TessHashMap< char32, inT64 > &ch_map, std::vector< std::pair< const char *, std::vector< bool > > > *font_flag)
 
static int FontScore (const TessHashMap< 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 TessHashMap< char32, inT64 > &  ch_map,
std::vector< std::pair< const char *, std::vector< bool > > > *  font_flag 
)
static

Definition at line 725 of file pango_font_info.cpp.

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

◆ FontScore()

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

Definition at line 691 of file pango_font_info.cpp.

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

◆ GetAllRenderableCharacters() [1/3]

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

◆ GetAllRenderableCharacters() [2/3]

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

◆ GetAllRenderableCharacters() [3/3]

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

◆ 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, NULL);
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 522 of file pango_font_info.cpp.

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

◆ ListAvailableFonts()

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

Definition at line 593 of file pango_font_info.cpp.

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

816 { 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 787 of file pango_font_info.cpp.

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

◆ 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 794 of file pango_font_info.cpp.

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

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