21 #include "config_auto.h" 24 #if (defined __MINGW32__) || (defined __CYGWIN__) 26 #undef __STRICT_ANSI__ 33 #include <sys/param.h> 44 #include "pango/pango.h" 45 #include "pango/pangocairo.h" 46 #include "pango/pangofc-font.h" 49 "Overrides fontconfig default temporary dir");
51 #ifdef GOOGLE_TESSERACT 52 #include "ocr/trainingdata/typesetting/legacy_fonts.h" 54 "Overrides --fonts_dir and sets the known universe of fonts to" 55 "the list in legacy_fonts.h");
58 "Overrides system default font location");
62 "If empty it use system default. Otherwise it overrides" 63 " system default font location");
72 std::string PangoFontInfo::fonts_dir_;
73 std::string PangoFontInfo::cache_dir_;
76 : desc_(nullptr), resolution_(kDefaultResolution) {
81 : desc_(nullptr), resolution_(kDefaultResolution) {
83 tprintf(
"ERROR: Could not parse %s\n", desc.c_str());
88 void PangoFontInfo::Clear() {
93 pango_font_description_free(desc_);
101 if (!desc_)
return "";
102 char* desc_str = pango_font_description_to_string(desc_);
103 std::string desc_name(desc_str);
113 if (fonts_dir_.empty()) {
115 FLAGS_fontconfig_tmpdir.c_str());
123 const std::string& cache_dir) {
124 if (!cache_dir_.empty()) {
128 const int MAX_FONTCONF_FILESIZE = 1024;
129 char fonts_conf_template[MAX_FONTCONF_FILESIZE];
130 cache_dir_ = cache_dir;
131 fonts_dir_ = fonts_dir;
132 snprintf(fonts_conf_template, MAX_FONTCONF_FILESIZE,
133 "<?xml version=\"1.0\"?>\n" 134 "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n" 137 "<cachedir>%s</cachedir>\n" 138 "<config></config>\n" 140 fonts_dir.c_str(), cache_dir_.c_str());
141 std::string fonts_conf_file =
File::JoinPath(cache_dir_.c_str(),
"fonts.conf");
144 std::string env(
"FONTCONFIG_PATH=");
145 env.append(cache_dir_.c_str());
146 _putenv(env.c_str());
147 _putenv(
"LANG=en_US.utf8");
149 setenv(
"FONTCONFIG_PATH", cache_dir_.c_str(),
true);
151 setenv(
"LANG",
"en_US.utf8",
true);
154 if (FcInitReinitialize() != FcTrue) {
155 tprintf(
"FcInitiReinitialize failed!!\n");
159 pango_cairo_font_map_set_default(
nullptr);
162 static void ListFontFamilies(PangoFontFamily*** families,
165 PangoFontMap* font_map = pango_cairo_font_map_get_default();
167 pango_font_map_list_families(font_map, families, n_families);
170 bool PangoFontInfo::ParseFontDescription(
const PangoFontDescription *desc) {
172 const char* family = pango_font_description_get_family(desc);
174 char* desc_str = pango_font_description_to_string(desc);
175 tprintf(
"WARNING: Could not parse family name from description: '%s'\n",
180 family_name_ = std::string(family);
181 desc_ = pango_font_description_copy(desc);
184 font_size_ = pango_font_description_get_size(desc);
185 if (!pango_font_description_get_size_is_absolute(desc)) {
186 font_size_ /= PANGO_SCALE;
193 PangoFontDescription *desc = pango_font_description_from_string(name.c_str());
194 bool success = ParseFontDescription(desc);
195 pango_font_description_free(desc);
202 PangoFont* PangoFontInfo::ToPangoFont()
const {
204 PangoFontMap* font_map = pango_cairo_font_map_get_default();
205 PangoContext* context = pango_context_new();
206 pango_cairo_context_set_resolution(context, resolution_);
207 pango_context_set_font_map(context, font_map);
208 PangoFont* font =
nullptr;
211 font = pango_font_map_load_font(font_map, context, desc_);
213 g_object_unref(context);
218 PangoFont* font = ToPangoFont();
219 if (font ==
nullptr) {
223 PangoCoverage* coverage = pango_font_get_coverage(font,
nullptr);
229 if (pango_coverage_get(coverage, *it) != PANGO_COVERAGE_EXACT) {
231 int len = it.get_utf8(tmp);
233 tlog(2,
"'%s' (U+%x) not covered by font\n", tmp, *it);
234 pango_coverage_unref(coverage);
235 g_object_unref(font);
239 pango_coverage_unref(coverage);
240 g_object_unref(font);
246 static char* my_strnmove(
char*
dest,
const char* src,
size_t n) {
255 }
while (n && src[0]);
267 int num_dropped_chars = 0;
268 PangoFont* font = ToPangoFont();
269 if (font ==
nullptr) {
271 num_dropped_chars = utf8_text->length();
272 utf8_text->resize(0);
273 return num_dropped_chars;
275 PangoCoverage* coverage = pango_font_get_coverage(font,
nullptr);
279 char* out =
const_cast<char*
>(utf8_text->c_str());
286 if (!it.is_legal()) {
292 const char* utf8_char = it.utf8_data();
295 if (!
IsWhitespace(unicode) && !pango_is_zero_width(unicode) &&
296 pango_coverage_get(coverage, unicode) != PANGO_COVERAGE_EXACT) {
300 tlog(2,
"'%s' (U+%x) not covered by font\n", str, unicode);
306 my_strnmove(out, utf8_char, utf8_len);
309 pango_coverage_unref(coverage);
310 g_object_unref(font);
311 utf8_text->resize(out - utf8_text->c_str());
312 return num_dropped_chars;
316 int* x_bearing,
int* x_advance)
const {
318 PangoFont* font = ToPangoFont();
320 int total_advance = 0;
330 PangoGlyph glyph_index = pango_fc_font_get_glyph(
331 reinterpret_cast<PangoFcFont*>(font), *it);
334 g_object_unref(font);
338 PangoRectangle ink_rect, logical_rect;
339 pango_font_get_glyph_extents(font, glyph_index, &ink_rect, &logical_rect);
340 pango_extents_to_pixels(&ink_rect,
nullptr);
341 pango_extents_to_pixels(&logical_rect,
nullptr);
343 int bearing = total_advance + PANGO_LBEARING(ink_rect);
344 if (it == it_begin || bearing < min_bearing) {
345 min_bearing = bearing;
347 total_advance += PANGO_RBEARING(logical_rect);
349 *x_bearing = min_bearing;
350 *x_advance = total_advance;
351 g_object_unref(font);
356 std::vector<std::string> graphemes;
361 std::vector<std::string>* graphemes)
const {
362 if (graphemes) graphemes->clear();
371 const char32 kDottedCircleGlyph = 9676;
372 bool bad_glyph =
false;
373 PangoFontMap* font_map = pango_cairo_font_map_get_default();
374 PangoContext* context = pango_context_new();
375 pango_context_set_font_map(context, font_map);
380 layout = pango_layout_new(context);
383 pango_layout_set_font_description(layout, desc_);
385 PangoFontDescription *desc = pango_font_description_from_string(
387 pango_layout_set_font_description(layout, desc);
388 pango_font_description_free(desc);
390 pango_layout_set_text(layout, utf8_word, len);
391 PangoLayoutIter* run_iter =
nullptr;
394 run_iter = pango_layout_get_iter(layout);
397 PangoLayoutRun* run = pango_layout_iter_get_run_readonly(run_iter);
399 tlog(2,
"Found end of line nullptr run marker\n");
402 PangoGlyph dotted_circle_glyph;
403 PangoFont* font = run->item->analysis.font;
405 #ifdef _WIN32 // Fixme! Leaks memory and breaks unittests. 406 PangoGlyphString* glyphs = pango_glyph_string_new();
407 char s[] =
"\xc2\xa7";
408 pango_shape(s,
sizeof(s), &(run->item->analysis), glyphs);
409 dotted_circle_glyph = glyphs->glyphs[0].glyph;
411 dotted_circle_glyph = pango_fc_font_get_glyph(
412 reinterpret_cast<PangoFcFont*>(font), kDottedCircleGlyph);
416 PangoFontDescription* desc = pango_font_describe(font);
417 char* desc_str = pango_font_description_to_string(desc);
418 tlog(2,
"Desc of font in run: %s\n", desc_str);
420 pango_font_description_free(desc);
423 PangoGlyphItemIter cluster_iter;
424 gboolean have_cluster;
425 for (have_cluster = pango_glyph_item_iter_init_start(&cluster_iter,
427 have_cluster && !bad_glyph;
428 have_cluster = pango_glyph_item_iter_next_cluster(&cluster_iter)) {
429 const int start_byte_index = cluster_iter.start_index;
430 const int end_byte_index = cluster_iter.end_index;
431 int start_glyph_index = cluster_iter.start_glyph;
432 int end_glyph_index = cluster_iter.end_glyph;
433 std::string cluster_text = std::string(utf8_word + start_byte_index,
434 end_byte_index - start_byte_index);
435 if (graphemes) graphemes->push_back(cluster_text);
437 tlog(2,
"Skipping whitespace\n");
441 printf(
"start_byte=%d end_byte=%d start_glyph=%d end_glyph=%d ",
442 start_byte_index, end_byte_index,
443 start_glyph_index, end_glyph_index);
445 for (
int i = start_glyph_index,
446 step = (end_glyph_index > start_glyph_index) ? 1 : -1;
447 !bad_glyph && i != end_glyph_index; i+= step) {
448 const bool unknown_glyph =
449 (cluster_iter.glyph_item->glyphs->glyphs[i].glyph &
450 PANGO_GLYPH_UNKNOWN_FLAG);
451 const bool illegal_glyph =
452 (cluster_iter.glyph_item->glyphs->glyphs[i].glyph ==
453 dotted_circle_glyph);
454 bad_glyph = unknown_glyph || illegal_glyph;
456 printf(
"(%d=%d)", cluster_iter.glyph_item->glyphs->glyphs[i].glyph,
461 printf(
" '%s'\n", cluster_text.c_str());
464 tlog(1,
"Found illegal glyph!\n");
466 }
while (!bad_glyph && pango_layout_iter_next_run(run_iter));
468 pango_layout_iter_free(run_iter);
469 g_object_unref(context);
470 g_object_unref(layout);
471 if (bad_glyph && graphemes) graphemes->clear();
477 std::vector<std::string> FontUtils::available_fonts_;
490 std::string* best_match) {
491 std::string query_desc(input_query_desc);
492 PangoFontDescription *desc = pango_font_description_from_string(
494 PangoFont* selected_font =
nullptr;
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);
502 selected_font = pango_font_map_load_font(font_map, context, desc);
504 g_object_unref(context);
506 if (selected_font ==
nullptr) {
507 pango_font_description_free(desc);
510 PangoFontDescription* selected_desc = pango_font_describe(selected_font);
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));
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(),
520 if (!equal && best_match !=
nullptr) {
521 *best_match = selected_desc_str;
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);
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);
537 static bool ShouldIgnoreFontFamilyName(
const char* query) {
538 static const char* kIgnoredFamilyNames[] = {
"Sans",
"Serif",
"Monospace",
540 const char** list = kIgnoredFamilyNames;
541 for (; *list !=
nullptr; ++list) {
542 if (!strcmp(*list, query))
551 if (!available_fonts_.empty()) {
552 return available_fonts_;
554 #ifdef GOOGLE_TESSERACT 555 if (FLAGS_use_only_legacy_fonts) {
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]);
564 return available_fonts_;
568 PangoFontFamily** families =
nullptr;
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)) {
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);
587 pango_font_description_free(desc);
593 std::sort(available_fonts_.begin(), available_fonts_.end());
594 return available_fonts_;
598 static void CharCoverageMapToBitmap(PangoCoverage* coverage,
599 std::vector<bool>* unichar_bitmap) {
600 const int kMinUnicodeValue = 33;
601 const int kMaxUnicodeValue = 0x10FFFF;
602 unichar_bitmap->resize(kMaxUnicodeValue + 1,
false);
604 for (
int i = kMinUnicodeValue; i <= kMaxUnicodeValue; ++i) {
607 = (pango_coverage_get(coverage, i) == PANGO_COVERAGE_EXACT);
614 const std::vector<std::string>& all_fonts = ListAvailableFonts();
615 return GetAllRenderableCharacters(all_fonts, unichar_bitmap);
620 std::vector<bool>* unichar_bitmap) {
622 PangoFont* font = font_info.ToPangoFont();
623 if (font !=
nullptr) {
625 PangoCoverage* coverage = pango_font_get_coverage(font,
nullptr);
626 CharCoverageMapToBitmap(coverage, unichar_bitmap);
627 pango_coverage_unref(coverage);
628 g_object_unref(font);
634 std::vector<bool>* unichar_bitmap) {
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) {
640 PangoFont* font = font_info.ToPangoFont();
641 if (font !=
nullptr) {
643 PangoCoverage* coverage = pango_font_get_coverage(font,
nullptr);
645 pango_coverage_max(all_coverage, coverage);
646 pango_coverage_unref(coverage);
647 g_object_unref(font);
650 CharCoverageMapToBitmap(all_coverage, unichar_bitmap);
651 pango_coverage_unref(all_coverage);
659 const std::string& fontname,
int* raw_score,
660 std::vector<bool>* ch_flags) {
663 tprintf(
"ERROR: Could not parse %s\n", fontname.c_str());
665 PangoFont* font = font_info.ToPangoFont();
666 PangoCoverage* coverage =
nullptr;
667 if (font !=
nullptr) coverage = pango_font_get_coverage(font,
nullptr);
670 ch_flags->reserve(ch_map.size());
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));
681 ok_chars += it->second;
684 ch_flags->push_back(covered);
687 pango_coverage_unref(coverage);
688 g_object_unref(font);
695 const std::unordered_map<char32, int64_t>& ch_map,
696 std::vector<std::pair<
const char*, std::vector<bool> > >* fonts) {
697 const double kMinOKFraction = 0.99;
700 const double kMinWeightedFraction = 0.99995;
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;
709 for (
unsigned i = 0; i < font_names.size(); ++i) {
710 std::vector<bool> ch_flags;
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);
716 font_flags.push_back(ch_flags);
717 font_scores.push_back(ok_chars);
718 raw_scores.push_back(raw_score);
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);
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];
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);
758 std::string* font_name, std::vector<std::string>* graphemes) {
759 return SelectFont(utf8_word, utf8_len, ListAvailableFonts(), font_name,
765 const std::vector<std::string>& all_fonts,
766 std::string* font_name, std::vector<std::string>* graphemes) {
767 if (font_name) font_name->clear();
768 if (graphemes) graphemes->clear();
769 for (
unsigned i = 0; i < all_fonts.size(); ++i) {
771 std::vector<std::string> found_graphemes;
773 "Could not parse font desc name %s\n",
774 all_fonts[i].c_str());
776 if (graphemes) graphemes->swap(found_graphemes);
777 if (font_name) *font_name = all_fonts[i];
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");
static bool IsAvailableFont(const char *font_desc)
static void HardInitFontConfig(const std::string &fonts_dir, const std::string &cache_dir)
bool IsUTF8Whitespace(const char *text)
static bool SelectFont(const char *utf8_word, const int utf8_len, std::string *font_name, std::vector< std::string > *graphemes)
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
bool IsWhitespace(const char32 ch)
const int kDefaultResolution
STRING_PARAM_FLAG(fontconfig_tmpdir,"/tmp","Overrides fontconfig default temporary dir")
int DropUncoveredChars(std::string *utf8_text) const
static void GetAllRenderableCharacters(std::vector< bool > *unichar_bitmap)
bool CanRenderString(const char *utf8_word, int len, std::vector< std::string > *graphemes) const
static const_iterator end(const char *utf8_str, int byte_length)
bool CoversUTF8Text(const char *utf8_text, int byte_length) const
static void PangoFontTypeInfo()
#define BOOL_PARAM_FLAG(name, val, comment)
static void SoftInitFontConfig()
static const std::vector< std::string > & ListAvailableFonts()
#define DISABLE_HEAP_LEAK_CHECK
DLLSYM void tprintf(const char *format,...)
bool ParseFontDescriptionName(const std::string &name)
const std::string & family_name() const
std::string DescriptionName() const
bool GetSpacingProperties(const std::string &utf8_char, int *x_bearing, int *x_advance) const
static const_iterator begin(const char *utf8_str, int byte_length)
static bool DeleteMatchingFiles(const char *pattern)
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)
bool IsInterchangeValid(const char32 ch)
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
#define ASSERT_HOST_MSG(x,...)
#define TLOG_IS_ON(level)