tesseract  3.05.02
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 150 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 674 of file pango_font_info.cpp.

675  {
676  const double kMinOKFraction = 0.99;
677  // Weighted fraction of characters that must be renderable in a font to make
678  // it OK even if the raw count is not good.
679  const double kMinWeightedFraction = 0.99995;
680 
681  fonts->clear();
682  vector<vector<bool> > font_flags;
683  vector<int> font_scores;
684  vector<int> raw_scores;
685  int most_ok_chars = 0;
686  int best_raw_score = 0;
687  const vector<string>& font_names = FontUtils::ListAvailableFonts();
688  for (int i = 0; i < font_names.size(); ++i) {
689  vector<bool> ch_flags;
690  int raw_score = 0;
691  int ok_chars = FontScore(ch_map, font_names[i], &raw_score, &ch_flags);
692  most_ok_chars = MAX(ok_chars, most_ok_chars);
693  best_raw_score = MAX(raw_score, best_raw_score);
694 
695  font_flags.push_back(ch_flags);
696  font_scores.push_back(ok_chars);
697  raw_scores.push_back(raw_score);
698  }
699 
700  // Now select the fonts with a score above a threshold fraction
701  // of both the raw and weighted best scores. To prevent bogus fonts being
702  // selected for CJK, we require a high fraction (kMinOKFraction = 0.99) of
703  // BOTH weighted and raw scores.
704  // In low character-count scripts, the issue is more getting enough fonts,
705  // when only 1 or 2 might have all those rare dingbats etc in them, so we
706  // allow a font with a very high weighted (coverage) score
707  // (kMinWeightedFraction = 0.99995) to be used even if its raw score is poor.
708  int least_good_enough = static_cast<int>(most_ok_chars * kMinOKFraction);
709  int least_raw_enough = static_cast<int>(best_raw_score * kMinOKFraction);
710  int override_enough = static_cast<int>(most_ok_chars * kMinWeightedFraction);
711 
712  string font_list;
713  for (int i = 0; i < font_names.size(); ++i) {
714  int score = font_scores[i];
715  int raw_score = raw_scores[i];
716  if ((score >= least_good_enough && raw_score >= least_raw_enough) ||
717  score >= override_enough) {
718  fonts->push_back(make_pair(font_names[i].c_str(), font_flags[i]));
719  tlog(1, "OK font %s = %.4f%%, raw = %d = %.2f%%\n",
720  font_names[i].c_str(),
721  100.0 * score / most_ok_chars,
722  raw_score, 100.0 * raw_score / best_raw_score);
723  font_list += font_names[i];
724  font_list += "\n";
725  } else if (score >= least_good_enough || raw_score >= least_raw_enough) {
726  tlog(1, "Runner-up font %s = %.4f%%, raw = %d = %.2f%%\n",
727  font_names[i].c_str(),
728  100.0 * score / most_ok_chars,
729  raw_score, 100.0 * raw_score / best_raw_score);
730  }
731  }
732  return font_list;
733 }
#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 640 of file pango_font_info.cpp.

642  {
643  PangoFontInfo font_info;
644  if (!font_info.ParseFontDescriptionName(fontname)) {
645  tprintf("ERROR: Could not parse %s\n", fontname.c_str());
646  }
647  PangoFont* font = font_info.ToPangoFont();
648  PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
649 
650  if (ch_flags) {
651  ch_flags->clear();
652  ch_flags->reserve(ch_map.size());
653  }
654  *raw_score = 0;
655  int ok_chars = 0;
656  for (TessHashMap<char32, inT64>::const_iterator it = ch_map.begin();
657  it != ch_map.end(); ++it) {
658  bool covered = (IsWhitespace(it->first) ||
659  (pango_coverage_get(coverage, it->first)
660  == PANGO_COVERAGE_EXACT));
661  if (covered) {
662  ++(*raw_score);
663  ok_chars += it->second;
664  }
665  if (ch_flags) {
666  ch_flags->push_back(covered);
667  }
668  }
669  return ok_chars;
670 }
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 154 of file pango_font_info.h.

154  {
155  return IsAvailableFont(font_desc, NULL);
156  }
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 471 of file pango_font_info.cpp.

472  {
473  string query_desc(input_query_desc);
474 #if (PANGO_VERSION <= 12005)
475  // Strip commas and any ' Medium' substring in the name.
476  query_desc.erase(std::remove(query_desc.begin(), query_desc.end(), ','),
477  query_desc.end());
478  const string kMediumStr = " Medium";
479  std::size_t found = query_desc.find(kMediumStr);
480  if (found != std::string::npos) {
481  query_desc.erase(found, kMediumStr.length());
482  }
483 #endif
484  PangoFontDescription *desc = pango_font_description_from_string(
485  query_desc.c_str());
486  PangoFont* selected_font = NULL;
487  {
489  PangoFontMap* font_map = pango_cairo_font_map_get_default();
490  PangoContext* context = pango_context_new();
491  pango_context_set_font_map(context, font_map);
492  {
494  selected_font = pango_font_map_load_font(font_map, context, desc);
495  }
496  g_object_unref(context);
497  }
498  if (selected_font == NULL) {
499  pango_font_description_free(desc);
500  return false;
501  }
502  PangoFontDescription* selected_desc = pango_font_describe(selected_font);
503 
504  bool equal = pango_font_description_equal(desc, selected_desc);
505  tlog(3, "query weight = %d \t selected weight =%d\n",
506  pango_font_description_get_weight(desc),
507  pango_font_description_get_weight(selected_desc));
508 
509  char* selected_desc_str = pango_font_description_to_string(selected_desc);
510  tlog(2, "query_desc: '%s' Selected: '%s'\n", query_desc.c_str(),
511  selected_desc_str);
512  if (!equal && best_match != NULL) {
513  *best_match = selected_desc_str;
514  // Clip the ending ' 0' if there is one. It seems that, if there is no
515  // point size on the end of the fontname, then Pango always appends ' 0'.
516  int len = best_match->size();
517  if (len > 2 && best_match->at(len - 1) == '0' &&
518  best_match->at(len - 2) == ' ') {
519  *best_match = best_match->substr(0, len - 2);
520  }
521  }
522  g_free(selected_desc_str);
523  pango_font_description_free(selected_desc);
524  g_object_unref(selected_font);
525  pango_font_description_free(desc);
526  return equal;
527 }
#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 542 of file pango_font_info.cpp.

542  {
543  if (!available_fonts_.empty()) {
544  return available_fonts_;
545  }
546 #ifndef USE_STD_NAMESPACE
547  if (FLAGS_use_only_legacy_fonts) {
548  // Restrict view to list of fonts in legacy_fonts.h
549  tprintf("Using list of legacy fonts only\n");
550  const int kNumFontLists = 4;
551  for (int i = 0; i < kNumFontLists; ++i) {
552  for (int j = 0; kFontlists[i][j] != NULL; ++j) {
553  available_fonts_.push_back(kFontlists[i][j]);
554  }
555  }
556  return available_fonts_;
557  }
558 #endif
559 
560  PangoFontFamily** families = 0;
561  int n_families = 0;
562  ListFontFamilies(&families, &n_families);
563  for (int i = 0; i < n_families; ++i) {
564  const char* family_name = pango_font_family_get_name(families[i]);
565  tlog(2, "Listing family %s\n", family_name);
566  if (ShouldIgnoreFontFamilyName(family_name)) {
567  continue;
568  }
569 
570  int n_faces;
571  PangoFontFace** faces = NULL;
572  pango_font_family_list_faces(families[i], &faces, &n_faces);
573  for (int j = 0; j < n_faces; ++j) {
574  PangoFontDescription* desc = pango_font_face_describe(faces[j]);
575  char* desc_str = pango_font_description_to_string(desc);
576  if (IsAvailableFont(desc_str)) {
577  available_fonts_.push_back(desc_str);
578  }
579  pango_font_description_free(desc);
580  g_free(desc_str);
581  }
582  g_free(faces);
583  }
584  g_free(families);
585  sort(available_fonts_.begin(), available_fonts_.end());
586  return available_fonts_;
587 }
#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 765 of file pango_font_info.cpp.

765 { 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 736 of file pango_font_info.cpp.

737  {
738  return SelectFont(utf8_word, utf8_len, ListAvailableFonts(), font_name,
739  graphemes);
740 }
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 743 of file pango_font_info.cpp.

745  {
746  if (font_name) font_name->clear();
747  if (graphemes) graphemes->clear();
748  for (int i = 0; i < all_fonts.size(); ++i) {
749  PangoFontInfo font;
750  vector<string> found_graphemes;
751  ASSERT_HOST_MSG(font.ParseFontDescriptionName(all_fonts[i]),
752  "Could not parse font desc name %s\n",
753  all_fonts[i].c_str());
754  if (font.CanRenderString(utf8_word, utf8_len, &found_graphemes)) {
755  if (graphemes) graphemes->swap(found_graphemes);
756  if (font_name) *font_name = all_fonts[i];
757  return true;
758  }
759  }
760  return false;
761 }
#define ASSERT_HOST_MSG(x,...)
Definition: errcode.h:90

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