60
60
61
61
STRING_PARAM_FLAG (fontconfig_tmpdir, " /tmp" ,
62
62
" Overrides fontconfig default temporary dir" );
63
- BOOL_PARAM_FLAG (fontconfig_refresh_cache, false ,
64
- " Does a one-time deletion of cache files from the "
65
- " fontconfig_tmpdir before initializing fontconfig." );
66
- BOOL_PARAM_FLAG (fontconfig_refresh_config_file, true ,
67
- " Does a one-time reset of the fontconfig config file to point"
68
- " to fonts_dir before initializing fontconfig. Set to true"
69
- " if fontconfig_refresh_cache is true. Set it to false to use"
70
- " multiple instances in separate processes without having to"
71
- " rescan the fonts_dir, using a previously setup font cache" );
72
63
73
64
#ifndef USE_STD_NAMESPACE
74
65
#include " ocr/trainingdata/typesetting/legacy_fonts.h"
@@ -91,7 +82,8 @@ namespace tesseract {
91
82
// in pixels.
92
83
const int kDefaultResolution = 300 ;
93
84
94
- bool PangoFontInfo::fontconfig_initialized_ = false ;
85
+ string PangoFontInfo::fonts_dir_;
86
+ string PangoFontInfo::cache_dir_;
95
87
96
88
PangoFontInfo::PangoFontInfo () : desc_(NULL ), resolution_(kDefaultResolution ) {
97
89
Clear ();
@@ -119,6 +111,8 @@ void PangoFontInfo::Clear() {
119
111
}
120
112
}
121
113
114
+ PangoFontInfo::~PangoFontInfo () { pango_font_description_free (desc_); }
115
+
122
116
string PangoFontInfo::DescriptionName () const {
123
117
if (!desc_) return " " ;
124
118
char * desc_str = pango_font_description_to_string (desc_);
@@ -127,59 +121,62 @@ string PangoFontInfo::DescriptionName() const {
127
121
return desc_name;
128
122
}
129
123
130
- // Initializes Fontconfig for use by writing a fake fonts.conf file into the
131
- // FLAGS_fontconfigs_tmpdir directory, that points to the supplied
132
- // fonts_dir, and then overrides the FONTCONFIG_PATH environment variable
133
- // to point to this fonts.conf file. If force_clear, the cache is refreshed
134
- // even if it has already been initialized.
135
- void PangoFontInfo::InitFontConfig (bool force_clear, const string& fonts_dir) {
136
- if ((fontconfig_initialized_ && !force_clear) || fonts_dir.empty ()) {
137
- fontconfig_initialized_ = true ;
138
- return ;
139
- }
140
- if (FLAGS_fontconfig_refresh_cache || force_clear) {
141
- File::DeleteMatchingFiles (File::JoinPath (
142
- FLAGS_fontconfig_tmpdir.c_str (), " *cache-?" ).c_str ());
143
- }
144
- if (FLAGS_fontconfig_refresh_config_file || FLAGS_fontconfig_refresh_cache ||
145
- force_clear) {
146
- const int MAX_FONTCONF_FILESIZE = 1024 ;
147
- char fonts_conf_template[MAX_FONTCONF_FILESIZE];
148
- snprintf (fonts_conf_template, MAX_FONTCONF_FILESIZE,
149
- " <?xml version=\" 1.0\" ?>\n "
150
- " <!DOCTYPE fontconfig SYSTEM \" fonts.dtd\" >\n "
151
- " <fontconfig>\n "
152
- " <dir>%s</dir>\n "
153
- " <cachedir>%s</cachedir>\n "
154
- " <config></config>\n "
155
- " </fontconfig>" , fonts_dir.c_str (),
156
- FLAGS_fontconfig_tmpdir.c_str ());
157
- string fonts_conf_file = File::JoinPath (FLAGS_fontconfig_tmpdir.c_str (),
158
- " fonts.conf" );
159
- File::WriteStringToFileOrDie (fonts_conf_template, fonts_conf_file);
124
+ // If not already initialized, initializes FontConfig by setting its
125
+ // environment variable and creating a fonts.conf file that points to the
126
+ // FLAGS_fonts_dir and the cache to FLAGS_fontconfig_tmpdir.
127
+ /* static */
128
+ void PangoFontInfo::SoftInitFontConfig () {
129
+ if (fonts_dir_.empty ()) {
130
+ HardInitFontConfig (FLAGS_fonts_dir, FLAGS_fontconfig_tmpdir);
160
131
}
132
+ }
133
+
134
+ // Re-initializes font config, whether or not already initialized.
135
+ // If already initialized, any existing cache is deleted, just to be sure.
136
+ /* static */
137
+ void PangoFontInfo::HardInitFontConfig (const string& fonts_dir,
138
+ const string& cache_dir) {
139
+ if (!cache_dir_.empty ()) {
140
+ File::DeleteMatchingFiles (
141
+ File::JoinPath (cache_dir_.c_str (), " *cache-?" ).c_str ());
142
+ }
143
+ const int MAX_FONTCONF_FILESIZE = 1024 ;
144
+ char fonts_conf_template[MAX_FONTCONF_FILESIZE];
145
+ cache_dir_ = cache_dir;
146
+ fonts_dir_ = fonts_dir;
147
+ snprintf (fonts_conf_template, MAX_FONTCONF_FILESIZE,
148
+ " <?xml version=\" 1.0\" ?>\n "
149
+ " <!DOCTYPE fontconfig SYSTEM \" fonts.dtd\" >\n "
150
+ " <fontconfig>\n "
151
+ " <dir>%s</dir>\n "
152
+ " <cachedir>%s</cachedir>\n "
153
+ " <config></config>\n "
154
+ " </fontconfig>" ,
155
+ fonts_dir.c_str (), cache_dir_.c_str ());
156
+ string fonts_conf_file = File::JoinPath (cache_dir_.c_str (), " fonts.conf" );
157
+ File::WriteStringToFileOrDie (fonts_conf_template, fonts_conf_file);
161
158
#ifdef _WIN32
162
159
std::string env (" FONTCONFIG_PATH=" );
163
- env.append (FLAGS_fontconfig_tmpdir .c_str ());
160
+ env.append (cache_dir_ .c_str ());
164
161
putenv (env.c_str ());
165
162
putenv (" LANG=en_US.utf8" );
166
163
#else
167
- setenv (" FONTCONFIG_PATH" , FLAGS_fontconfig_tmpdir .c_str (), true );
164
+ setenv (" FONTCONFIG_PATH" , cache_dir_ .c_str (), true );
168
165
// Fix the locale so that the reported font names are consistent.
169
166
setenv (" LANG" , " en_US.utf8" , true );
170
167
#endif // _WIN32
171
- if (!fontconfig_initialized_ || force_clear) {
172
- if (FcInitReinitialize () != FcTrue) {
173
- tprintf (" FcInitiReinitialize failed!!\n " );
174
- }
168
+
169
+ if (FcInitReinitialize () != FcTrue) {
170
+ tprintf (" FcInitiReinitialize failed!!\n " );
175
171
}
176
- fontconfig_initialized_ = true ;
177
172
FontUtils::ReInit ();
173
+ // Clear Pango's font cache too.
174
+ pango_cairo_font_map_set_default (NULL );
178
175
}
179
176
180
177
static void ListFontFamilies (PangoFontFamily*** families,
181
178
int * n_families) {
182
- PangoFontInfo::InitFontConfig ( false , FLAGS_fonts_dir. c_str () );
179
+ PangoFontInfo::SoftInitFontConfig ( );
183
180
PangoFontMap* font_map = pango_cairo_font_map_get_default ();
184
181
DISABLE_HEAP_LEAK_CHECK;
185
182
pango_font_map_list_families (font_map, families, n_families);
@@ -253,7 +250,7 @@ bool PangoFontInfo::ParseFontDescriptionName(const string& name) {
253
250
// in the font map. Note that if the font is wholly missing, this could
254
251
// correspond to a completely different font family and face.
255
252
PangoFont* PangoFontInfo::ToPangoFont () const {
256
- InitFontConfig ( false , FLAGS_fonts_dir. c_str () );
253
+ SoftInitFontConfig ( );
257
254
PangoFontMap* font_map = pango_cairo_font_map_get_default ();
258
255
PangoContext* context = pango_context_new ();
259
256
pango_cairo_context_set_resolution (context, resolution_);
@@ -538,7 +535,7 @@ bool FontUtils::IsAvailableFont(const char* input_query_desc,
538
535
query_desc.c_str ());
539
536
PangoFont* selected_font = NULL ;
540
537
{
541
- PangoFontInfo::InitFontConfig ( false , FLAGS_fonts_dir. c_str () );
538
+ PangoFontInfo::SoftInitFontConfig ( );
542
539
PangoFontMap* font_map = pango_cairo_font_map_get_default ();
543
540
PangoContext* context = pango_context_new ();
544
541
pango_context_set_font_map (context, font_map);
@@ -690,9 +687,8 @@ void FontUtils::GetAllRenderableCharacters(const vector<string>& fonts,
690
687
// Utilities written to be backward compatible with StringRender
691
688
692
689
/* static */
693
- int FontUtils::FontScore (const unordered_map<char32, inT64>& ch_map,
694
- const string& fontname,
695
- int * raw_score,
690
+ int FontUtils::FontScore (const TessHashMap<char32, inT64>& ch_map,
691
+ const string& fontname, int * raw_score,
696
692
vector<bool >* ch_flags) {
697
693
PangoFontInfo font_info;
698
694
if (!font_info.ParseFontDescriptionName (fontname)) {
@@ -707,7 +703,7 @@ int FontUtils::FontScore(const unordered_map<char32, inT64>& ch_map,
707
703
}
708
704
*raw_score = 0 ;
709
705
int ok_chars = 0 ;
710
- for (unordered_map <char32, inT64>::const_iterator it = ch_map.begin ();
706
+ for (TessHashMap <char32, inT64>::const_iterator it = ch_map.begin ();
711
707
it != ch_map.end (); ++it) {
712
708
bool covered = (IsWhitespace (it->first ) ||
713
709
(pango_coverage_get (coverage, it->first )
@@ -725,7 +721,7 @@ int FontUtils::FontScore(const unordered_map<char32, inT64>& ch_map,
725
721
726
722
727
723
/* static */
728
- string FontUtils::BestFonts (const unordered_map <char32, inT64>& ch_map,
724
+ string FontUtils::BestFonts (const TessHashMap <char32, inT64>& ch_map,
729
725
vector<pair<const char *, vector<bool > > >* fonts) {
730
726
const double kMinOKFraction = 0.99 ;
731
727
// Weighted fraction of characters that must be renderable in a font to make
0 commit comments