25 #if (defined __MINGW32__) || (defined __CYGWIN__) 27 #undef __STRICT_ANSI__ 29 #if (defined __MINGW32__) 30 #include "strcasestr.h" 31 #elif !defined(_GNU_SOURCE) 36 #elif defined(_MSC_VER) 37 #include "strcasestr.h" 38 #define strncasecmp _strnicmp 39 #define strcasecmp _stricmp 46 #include <sys/param.h> 57 #include "pango/pango.h" 58 #include "pango/pangocairo.h" 59 #include "pango/pangofc-font.h" 62 "Overrides fontconfig default temporary dir");
64 #ifndef USE_STD_NAMESPACE 65 #include "ocr/trainingdata/typesetting/legacy_fonts.h" 67 "Overrides --fonts_dir and sets the known universe of fonts to" 68 "the list in legacy_fonts.h");
71 "Overrides system default font location");
75 "If empty it use system default. Otherwise it overrides" 76 " system default font location");
85 string PangoFontInfo::fonts_dir_;
86 string PangoFontInfo::cache_dir_;
95 tprintf(
"ERROR: Could not parse %s\n", desc.c_str());
100 void PangoFontInfo::Clear() {
104 is_smallcaps_ =
false;
105 is_monospace_ =
false;
106 family_name_.clear();
109 pango_font_description_free(desc_);
117 if (!desc_)
return "";
118 char* desc_str = pango_font_description_to_string(desc_);
119 string desc_name(desc_str);
129 if (fonts_dir_.empty()) {
131 FLAGS_fontconfig_tmpdir.c_str());
139 const string& cache_dir) {
140 if (!cache_dir_.empty()) {
144 const int MAX_FONTCONF_FILESIZE = 1024;
145 char fonts_conf_template[MAX_FONTCONF_FILESIZE];
146 cache_dir_ = cache_dir;
147 fonts_dir_ = fonts_dir;
148 snprintf(fonts_conf_template, MAX_FONTCONF_FILESIZE,
149 "<?xml version=\"1.0\"?>\n" 150 "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n" 153 "<cachedir>%s</cachedir>\n" 154 "<config></config>\n" 156 fonts_dir.c_str(), cache_dir_.c_str());
157 string fonts_conf_file =
File::JoinPath(cache_dir_.c_str(),
"fonts.conf");
160 std::string env(
"FONTCONFIG_PATH=");
161 env.append(cache_dir_.c_str());
163 putenv(
"LANG=en_US.utf8");
165 setenv(
"FONTCONFIG_PATH", cache_dir_.c_str(),
true);
167 setenv(
"LANG",
"en_US.utf8",
true);
170 if (FcInitReinitialize() != FcTrue) {
171 tprintf(
"FcInitiReinitialize failed!!\n");
175 pango_cairo_font_map_set_default(NULL);
178 static void ListFontFamilies(PangoFontFamily*** families,
181 PangoFontMap* font_map = pango_cairo_font_map_get_default();
183 pango_font_map_list_families(font_map, families, n_families);
188 static bool IsMonospaceFontFamily(
const char* family_name) {
189 PangoFontFamily** families = 0;
191 bool is_monospace =
false;
192 ListFontFamilies(&families, &n_families);
195 for (
int i = 0; i < n_families; ++i) {
196 if (!strcasecmp(family_name, pango_font_family_get_name(families[i]))) {
197 is_monospace = pango_font_family_is_monospace(families[i]);
203 tlog(1,
"Could not find monospace property of family %s\n", family_name);
209 bool PangoFontInfo::ParseFontDescription(
const PangoFontDescription *desc) {
211 const char* family = pango_font_description_get_family(desc);
213 char* desc_str = pango_font_description_to_string(desc);
214 tprintf(
"WARNING: Could not parse family name from description: '%s'\n",
219 family_name_ = string(family);
220 desc_ = pango_font_description_copy(desc);
221 is_monospace_ = IsMonospaceFontFamily(family);
224 font_size_ = pango_font_description_get_size(desc);
225 if (!pango_font_description_get_size_is_absolute(desc)) {
226 font_size_ /= PANGO_SCALE;
229 PangoStyle style = pango_font_description_get_style(desc);
230 is_italic_ = (PANGO_STYLE_ITALIC == style ||
231 PANGO_STYLE_OBLIQUE == style);
232 is_smallcaps_ = (pango_font_description_get_variant(desc)
233 == PANGO_VARIANT_SMALL_CAPS);
235 is_bold_ = (pango_font_description_get_weight(desc) >= PANGO_WEIGHT_BOLD);
239 is_fraktur_ = (strcasestr(family,
"Fraktur") != NULL);
244 PangoFontDescription *desc = pango_font_description_from_string(name.c_str());
245 bool success = ParseFontDescription(desc);
246 pango_font_description_free(desc);
253 PangoFont* PangoFontInfo::ToPangoFont()
const {
255 PangoFontMap* font_map = pango_cairo_font_map_get_default();
256 PangoContext* context = pango_context_new();
257 pango_cairo_context_set_resolution(context, resolution_);
258 pango_context_set_font_map(context, font_map);
259 PangoFont* font = NULL;
262 font = pango_font_map_load_font(font_map, context, desc_);
264 g_object_unref(context);
269 PangoFont* font = ToPangoFont();
270 PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
276 if (pango_coverage_get(coverage, *it) != PANGO_COVERAGE_EXACT) {
278 int len = it.get_utf8(tmp);
280 tlog(2,
"'%s' (U+%x) not covered by font\n", tmp, *it);
289 static char* my_strnmove(
char* dest,
const char* src,
size_t n) {
298 }
while (n && src[0]);
310 PangoFont* font = ToPangoFont();
311 PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
312 int num_dropped_chars = 0;
316 char* out =
const_cast<char*
>(utf8_text->c_str());
323 if (!it.is_legal()) {
329 const char* utf8_char = it.utf8_data();
332 if (!
IsWhitespace(unicode) && !pango_is_zero_width(unicode) &&
333 pango_coverage_get(coverage, unicode) != PANGO_COVERAGE_EXACT) {
337 tlog(2,
"'%s' (U+%x) not covered by font\n", str, unicode);
343 my_strnmove(out, utf8_char, utf8_len);
346 utf8_text->resize(out - utf8_text->c_str());
347 return num_dropped_chars;
351 int* x_bearing,
int* x_advance)
const {
353 PangoFont* font = ToPangoFont();
355 int total_advance = 0;
365 PangoGlyph glyph_index = pango_fc_font_get_glyph(
366 reinterpret_cast<PangoFcFont*>(font), *it);
372 PangoRectangle ink_rect, logical_rect;
373 pango_font_get_glyph_extents(font, glyph_index, &ink_rect, &logical_rect);
374 pango_extents_to_pixels(&ink_rect, NULL);
375 pango_extents_to_pixels(&logical_rect, NULL);
377 int bearing = total_advance + PANGO_LBEARING(ink_rect);
378 if (it == it_begin || bearing < min_bearing) {
379 min_bearing = bearing;
381 total_advance += PANGO_RBEARING(logical_rect);
383 *x_bearing = min_bearing;
384 *x_advance = total_advance;
389 vector<string> graphemes;
394 vector<string>* graphemes)
const {
395 if (graphemes) graphemes->clear();
404 const char32 kDottedCircleGlyph = 9676;
405 bool bad_glyph =
false;
406 PangoFontMap* font_map = pango_cairo_font_map_get_default();
407 PangoContext* context = pango_context_new();
408 pango_context_set_font_map(context, font_map);
413 layout = pango_layout_new(context);
416 pango_layout_set_font_description(layout, desc_);
418 PangoFontDescription *desc = pango_font_description_from_string(
420 pango_layout_set_font_description(layout, desc);
421 pango_font_description_free(desc);
423 pango_layout_set_text(layout, utf8_word, len);
424 PangoLayoutIter* run_iter = NULL;
427 run_iter = pango_layout_get_iter(layout);
430 PangoLayoutRun* run = pango_layout_iter_get_run_readonly(run_iter);
432 tlog(2,
"Found end of line NULL run marker\n");
435 PangoGlyph dotted_circle_glyph;
436 PangoFont* font = run->item->analysis.font;
438 #ifdef _WIN32 // Fixme! Leaks memory and breaks unittests. 439 PangoGlyphString* glyphs = pango_glyph_string_new();
440 char s[] =
"\xc2\xa7";
441 pango_shape(s,
sizeof(s), &(run->item->analysis), glyphs);
442 dotted_circle_glyph = glyphs->glyphs[0].glyph;
444 dotted_circle_glyph = pango_fc_font_get_glyph(
445 reinterpret_cast<PangoFcFont*>(font), kDottedCircleGlyph);
449 PangoFontDescription* desc = pango_font_describe(font);
450 char* desc_str = pango_font_description_to_string(desc);
451 tlog(2,
"Desc of font in run: %s\n", desc_str);
453 pango_font_description_free(desc);
456 PangoGlyphItemIter cluster_iter;
457 gboolean have_cluster;
458 for (have_cluster = pango_glyph_item_iter_init_start(&cluster_iter,
460 have_cluster && !bad_glyph;
461 have_cluster = pango_glyph_item_iter_next_cluster(&cluster_iter)) {
462 const int start_byte_index = cluster_iter.start_index;
463 const int end_byte_index = cluster_iter.end_index;
464 int start_glyph_index = cluster_iter.start_glyph;
465 int end_glyph_index = cluster_iter.end_glyph;
466 string cluster_text = string(utf8_word + start_byte_index,
467 end_byte_index - start_byte_index);
468 if (graphemes) graphemes->push_back(cluster_text);
470 tlog(2,
"Skipping whitespace\n");
474 printf(
"start_byte=%d end_byte=%d start_glyph=%d end_glyph=%d ",
475 start_byte_index, end_byte_index,
476 start_glyph_index, end_glyph_index);
478 for (
int i = start_glyph_index,
479 step = (end_glyph_index > start_glyph_index) ? 1 : -1;
480 !bad_glyph && i != end_glyph_index; i+= step) {
481 const bool unknown_glyph =
482 (cluster_iter.glyph_item->glyphs->glyphs[i].glyph &
483 PANGO_GLYPH_UNKNOWN_FLAG);
484 const bool illegal_glyph =
485 (cluster_iter.glyph_item->glyphs->glyphs[i].glyph ==
486 dotted_circle_glyph);
487 bad_glyph = unknown_glyph || illegal_glyph;
489 printf(
"(%d=%d)", cluster_iter.glyph_item->glyphs->glyphs[i].glyph,
494 printf(
" '%s'\n", cluster_text.c_str());
497 tlog(1,
"Found illegal glyph!\n");
499 }
while (!bad_glyph && pango_layout_iter_next_run(run_iter));
501 pango_layout_iter_free(run_iter);
502 g_object_unref(context);
503 g_object_unref(layout);
504 if (bad_glyph && graphemes) graphemes->clear();
510 vector<string> FontUtils::available_fonts_;
523 string* best_match) {
524 string query_desc(input_query_desc);
525 #if (PANGO_VERSION <= 12005) 527 query_desc.erase(std::remove(query_desc.begin(), 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());
535 PangoFontDescription *desc = pango_font_description_from_string(
537 PangoFont* selected_font = NULL;
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);
545 selected_font = pango_font_map_load_font(font_map, context, desc);
547 g_object_unref(context);
549 if (selected_font == NULL) {
550 pango_font_description_free(desc);
553 PangoFontDescription* selected_desc = pango_font_describe(selected_font);
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));
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(),
563 if (!equal && best_match != NULL) {
564 *best_match = selected_desc_str;
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);
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);
580 static bool ShouldIgnoreFontFamilyName(
const char* query) {
581 static const char* kIgnoredFamilyNames[]
582 = {
"Sans",
"Serif",
"Monospace", NULL };
583 const char** list = kIgnoredFamilyNames;
584 for (; *list != NULL; ++list) {
585 if (!strcmp(*list, query))
594 if (!available_fonts_.empty()) {
595 return available_fonts_;
597 #ifndef USE_STD_NAMESPACE 598 if (FLAGS_use_only_legacy_fonts) {
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]);
607 return available_fonts_;
611 PangoFontFamily** 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)) {
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);
628 available_fonts_.push_back(desc_str);
630 pango_font_description_free(desc);
636 sort(available_fonts_.begin(), available_fonts_.end());
637 return available_fonts_;
641 static void CharCoverageMapToBitmap(PangoCoverage* coverage,
642 vector<bool>* unichar_bitmap) {
643 const int kMinUnicodeValue = 33;
644 const int kMaxUnicodeValue = 0x10FFFF;
645 unichar_bitmap->resize(kMaxUnicodeValue + 1,
false);
647 for (
int i = kMinUnicodeValue; i <= kMaxUnicodeValue; ++i) {
650 = (pango_coverage_get(coverage, i) == PANGO_COVERAGE_EXACT);
663 vector<bool>* unichar_bitmap) {
664 PangoFontInfo font_info(font_name);
665 PangoCoverage* coverage = pango_font_get_coverage(
666 font_info.ToPangoFont(), NULL);
667 CharCoverageMapToBitmap(coverage, unichar_bitmap);
672 vector<bool>* unichar_bitmap) {
674 PangoCoverage* all_coverage = pango_coverage_new();
675 tlog(1,
"Processing %d fonts\n", fonts.size());
676 for (
int i = 0; i < fonts.size(); ++i) {
677 PangoFontInfo font_info(fonts[i]);
678 PangoCoverage* coverage = pango_font_get_coverage(
679 font_info.ToPangoFont(), NULL);
681 pango_coverage_max(all_coverage, coverage);
683 CharCoverageMapToBitmap(all_coverage, unichar_bitmap);
684 pango_coverage_unref(all_coverage);
692 const string& fontname,
int* raw_score,
693 vector<bool>* ch_flags) {
696 tprintf(
"ERROR: Could not parse %s\n", fontname.c_str());
698 PangoFont* font = font_info.ToPangoFont();
699 PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
703 ch_flags->reserve(ch_map.size());
707 for (TessHashMap<char32, inT64>::const_iterator it = ch_map.begin();
708 it != ch_map.end(); ++it) {
710 (pango_coverage_get(coverage, it->first)
711 == PANGO_COVERAGE_EXACT));
714 ok_chars += it->second;
717 ch_flags->push_back(covered);
726 vector<pair<
const char*, vector<bool> > >* fonts) {
727 const double kMinOKFraction = 0.99;
730 const double kMinWeightedFraction = 0.99995;
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;
739 for (
int i = 0; i < font_names.size(); ++i) {
740 vector<bool> ch_flags;
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);
746 font_flags.push_back(ch_flags);
747 font_scores.push_back(ok_chars);
748 raw_scores.push_back(raw_score);
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);
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];
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);
788 string* font_name, vector<string>* graphemes) {
795 const vector<string>& all_fonts,
796 string* font_name, vector<string>* graphemes) {
797 if (font_name) font_name->clear();
798 if (graphemes) graphemes->clear();
799 for (
int i = 0; i < all_fonts.size(); ++i) {
801 vector<string> found_graphemes;
803 "Could not parse font desc name %s\n",
804 all_fonts[i].c_str());
806 if (graphemes) graphemes->swap(found_graphemes);
807 if (font_name) *font_name = all_fonts[i];
static bool DeleteMatchingFiles(const char *pattern)
bool GetSpacingProperties(const string &utf8_char, int *x_bearing, int *x_advance) const
bool CanRenderString(const char *utf8_word, int len, std::vector< string > *graphemes) const
bool ParseFontDescriptionName(const string &name)
static string BestFonts(const TessHashMap< char32, inT64 > &ch_map, std::vector< std::pair< const char *, std::vector< bool > > > *font_flag)
bool CoversUTF8Text(const char *utf8_text, int byte_length) const
bool IsUTF8Whitespace(const char *text)
#define TLOG_IS_ON(level)
static int FontScore(const TessHashMap< char32, inT64 > &ch_map, const string &fontname, int *raw_score, std::vector< bool > *ch_flags)
bool IsWhitespace(const char32 ch)
static void SoftInitFontConfig()
static const_iterator end(const char *utf8_str, const int byte_length)
BOOL_PARAM_FLAG(use_only_legacy_fonts, false, "Overrides --fonts_dir and sets the known universe of fonts to" "the list in legacy_fonts.h")
int DropUncoveredChars(string *utf8_text) const
static void GetAllRenderableCharacters(std::vector< bool > *unichar_bitmap)
#define ASSERT_HOST_MSG(x,...)
static const_iterator begin(const char *utf8_str, const int byte_length)
const int kDefaultResolution
static bool SelectFont(const char *utf8_word, const int utf8_len, string *font_name, std::vector< string > *graphemes)
bool IsInterchangeValid(const char32 ch)
static bool IsAvailableFont(const char *font_desc)
string DescriptionName() const
static string JoinPath(const string &prefix, const string &suffix)
static void HardInitFontConfig(const string &fonts_dir, const string &cache_dir)
static void WriteStringToFileOrDie(const string &str, const string &filename)
static const std::vector< string > & ListAvailableFonts()
#define DISABLE_HEAP_LEAK_CHECK
STRING_PARAM_FLAG(fontconfig_tmpdir, "/tmp", "Overrides fontconfig default temporary dir")