tesseract  3.05.02
tesseract::PangoFontInfo Class Reference

#include <pango_font_info.h>

Public Types

enum  FontTypeEnum { UNKNOWN, SERIF, SANS_SERIF, DECORATIVE }
 

Public Member Functions

 PangoFontInfo ()
 
 ~PangoFontInfo ()
 
 PangoFontInfo (const string &name)
 
bool ParseFontDescriptionName (const string &name)
 
bool CoversUTF8Text (const char *utf8_text, int byte_length) const
 
int DropUncoveredChars (string *utf8_text) const
 
bool CanRenderString (const char *utf8_word, int len, std::vector< string > *graphemes) const
 
bool CanRenderString (const char *utf8_word, int len) const
 
bool GetSpacingProperties (const string &utf8_char, int *x_bearing, int *x_advance) const
 
string DescriptionName () const
 
const string & family_name () const
 
int font_size () const
 
FontTypeEnum font_type () const
 
int resolution () const
 
void set_resolution (const int resolution)
 

Static Public Member Functions

static void SoftInitFontConfig ()
 
static void HardInitFontConfig (const string &fonts_dir, const string &cache_dir)
 

Friends

class FontUtils
 

Detailed Description

Definition at line 44 of file pango_font_info.h.

Member Enumeration Documentation

◆ FontTypeEnum

Constructor & Destructor Documentation

◆ PangoFontInfo() [1/2]

tesseract::PangoFontInfo::PangoFontInfo ( )

Definition at line 76 of file pango_font_info.cpp.

76  : desc_(NULL), resolution_(kDefaultResolution) {
77  Clear();
78 }
const int kDefaultResolution

◆ ~PangoFontInfo()

tesseract::PangoFontInfo::~PangoFontInfo ( )

Definition at line 98 of file pango_font_info.cpp.

98 { pango_font_description_free(desc_); }

◆ PangoFontInfo() [2/2]

tesseract::PangoFontInfo::PangoFontInfo ( const string &  name)
explicit

Definition at line 80 of file pango_font_info.cpp.

81  : desc_(NULL), resolution_(kDefaultResolution) {
82  if (!ParseFontDescriptionName(desc)) {
83  tprintf("ERROR: Could not parse %s\n", desc.c_str());
84  Clear();
85  }
86 }
bool ParseFontDescriptionName(const string &name)
const int kDefaultResolution
#define tprintf(...)
Definition: tprintf.h:31

Member Function Documentation

◆ CanRenderString() [1/2]

bool tesseract::PangoFontInfo::CanRenderString ( const char *  utf8_word,
int  len,
std::vector< string > *  graphemes 
) const

Definition at line 342 of file pango_font_info.cpp.

343  {
344  if (graphemes) graphemes->clear();
345  // We check for font coverage of the text first, as otherwise Pango could
346  // (undesirably) fall back to another font that does have the required
347  // coverage.
348  if (!CoversUTF8Text(utf8_word, len)) {
349  return false;
350  }
351  // U+25CC dotted circle character that often (but not always) gets rendered
352  // when there is an illegal grapheme sequence.
353  const char32 kDottedCircleGlyph = 9676;
354  bool bad_glyph = false;
355  PangoFontMap* font_map = pango_cairo_font_map_get_default();
356  PangoContext* context = pango_context_new();
357  pango_context_set_font_map(context, font_map);
358  PangoLayout* layout;
359  {
360  // Pango is not relasing the cached layout.
362  layout = pango_layout_new(context);
363  }
364  if (desc_) {
365  pango_layout_set_font_description(layout, desc_);
366  } else {
367  PangoFontDescription *desc = pango_font_description_from_string(
368  DescriptionName().c_str());
369  pango_layout_set_font_description(layout, desc);
370  pango_font_description_free(desc);
371  }
372  pango_layout_set_text(layout, utf8_word, len);
373  PangoLayoutIter* run_iter = NULL;
374  { // Fontconfig caches some information here that is not freed before exit.
376  run_iter = pango_layout_get_iter(layout);
377  }
378  do {
379  PangoLayoutRun* run = pango_layout_iter_get_run_readonly(run_iter);
380  if (!run) {
381  tlog(2, "Found end of line NULL run marker\n");
382  continue;
383  }
384  PangoGlyph dotted_circle_glyph;
385  PangoFont* font = run->item->analysis.font;
386 
387 #ifdef _WIN32 // Fixme! Leaks memory and breaks unittests.
388  PangoGlyphString* glyphs = pango_glyph_string_new();
389  char s[] = "\xc2\xa7";
390  pango_shape(s, sizeof(s), &(run->item->analysis), glyphs);
391  dotted_circle_glyph = glyphs->glyphs[0].glyph;
392 #else
393  dotted_circle_glyph = pango_fc_font_get_glyph(
394  reinterpret_cast<PangoFcFont*>(font), kDottedCircleGlyph);
395 #endif
396 
397  if (TLOG_IS_ON(2)) {
398  PangoFontDescription* desc = pango_font_describe(font);
399  char* desc_str = pango_font_description_to_string(desc);
400  tlog(2, "Desc of font in run: %s\n", desc_str);
401  g_free(desc_str);
402  pango_font_description_free(desc);
403  }
404 
405  PangoGlyphItemIter cluster_iter;
406  gboolean have_cluster;
407  for (have_cluster = pango_glyph_item_iter_init_start(&cluster_iter,
408  run, utf8_word);
409  have_cluster && !bad_glyph;
410  have_cluster = pango_glyph_item_iter_next_cluster(&cluster_iter)) {
411  const int start_byte_index = cluster_iter.start_index;
412  const int end_byte_index = cluster_iter.end_index;
413  int start_glyph_index = cluster_iter.start_glyph;
414  int end_glyph_index = cluster_iter.end_glyph;
415  string cluster_text = string(utf8_word + start_byte_index,
416  end_byte_index - start_byte_index);
417  if (graphemes) graphemes->push_back(cluster_text);
418  if (IsUTF8Whitespace(cluster_text.c_str())) {
419  tlog(2, "Skipping whitespace\n");
420  continue;
421  }
422  if (TLOG_IS_ON(2)) {
423  printf("start_byte=%d end_byte=%d start_glyph=%d end_glyph=%d ",
424  start_byte_index, end_byte_index,
425  start_glyph_index, end_glyph_index);
426  }
427  for (int i = start_glyph_index,
428  step = (end_glyph_index > start_glyph_index) ? 1 : -1;
429  !bad_glyph && i != end_glyph_index; i+= step) {
430  const bool unknown_glyph =
431  (cluster_iter.glyph_item->glyphs->glyphs[i].glyph &
432  PANGO_GLYPH_UNKNOWN_FLAG);
433  const bool illegal_glyph =
434  (cluster_iter.glyph_item->glyphs->glyphs[i].glyph ==
435  dotted_circle_glyph);
436  bad_glyph = unknown_glyph || illegal_glyph;
437  if (TLOG_IS_ON(2)) {
438  printf("(%d=%d)", cluster_iter.glyph_item->glyphs->glyphs[i].glyph,
439  bad_glyph ? 1 : 0);
440  }
441  }
442  if (TLOG_IS_ON(2)) {
443  printf(" '%s'\n", cluster_text.c_str());
444  }
445  if (bad_glyph)
446  tlog(1, "Found illegal glyph!\n");
447  }
448  } while (!bad_glyph && pango_layout_iter_next_run(run_iter));
449 
450  pango_layout_iter_free(run_iter);
451  g_object_unref(context);
452  g_object_unref(layout);
453  if (bad_glyph && graphemes) graphemes->clear();
454  return !bad_glyph;
455 }
#define tlog(level,...)
Definition: tlog.h:33
bool CoversUTF8Text(const char *utf8_text, int byte_length) const
bool IsUTF8Whitespace(const char *text)
Definition: normstrngs.cpp:182
#define TLOG_IS_ON(level)
Definition: tlog.h:39
signed int char32
Definition: normstrngs.h:27
#define DISABLE_HEAP_LEAK_CHECK
Definition: util.h:63

◆ CanRenderString() [2/2]

bool tesseract::PangoFontInfo::CanRenderString ( const char *  utf8_word,
int  len 
) const

Definition at line 337 of file pango_font_info.cpp.

337  {
338  vector<string> graphemes;
339  return CanRenderString(utf8_word, len, &graphemes);
340 }
bool CanRenderString(const char *utf8_word, int len, std::vector< string > *graphemes) const

◆ CoversUTF8Text()

bool tesseract::PangoFontInfo::CoversUTF8Text ( const char *  utf8_text,
int  byte_length 
) const

Definition at line 217 of file pango_font_info.cpp.

217  {
218  PangoFont* font = ToPangoFont();
219  PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
220  for (UNICHAR::const_iterator it = UNICHAR::begin(utf8_text, byte_length);
221  it != UNICHAR::end(utf8_text, byte_length);
222  ++it) {
223  if (IsWhitespace(*it) || pango_is_zero_width(*it))
224  continue;
225  if (pango_coverage_get(coverage, *it) != PANGO_COVERAGE_EXACT) {
226  char tmp[5];
227  int len = it.get_utf8(tmp);
228  tmp[len] = '\0';
229  tlog(2, "'%s' (U+%x) not covered by font\n", tmp, *it);
230  return false;
231  }
232  }
233  return true;
234 }
#define tlog(level,...)
Definition: tlog.h:33
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:176
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200

◆ DescriptionName()

string tesseract::PangoFontInfo::DescriptionName ( ) const

Definition at line 100 of file pango_font_info.cpp.

100  {
101  if (!desc_) return "";
102  char* desc_str = pango_font_description_to_string(desc_);
103  string desc_name(desc_str);
104  g_free(desc_str);
105  return desc_name;
106 }

◆ DropUncoveredChars()

int tesseract::PangoFontInfo::DropUncoveredChars ( string *  utf8_text) const

Definition at line 258 of file pango_font_info.cpp.

258  {
259  PangoFont* font = ToPangoFont();
260  PangoCoverage* coverage = pango_font_get_coverage(font, NULL);
261  int num_dropped_chars = 0;
262  // Maintain two iterators that point into the string. For space efficiency, we
263  // will repeatedly copy one covered UTF8 character from one to the other, and
264  // at the end resize the string to the right length.
265  char* out = const_cast<char*>(utf8_text->c_str());
266  const UNICHAR::const_iterator it_begin =
267  UNICHAR::begin(utf8_text->c_str(), utf8_text->length());
268  const UNICHAR::const_iterator it_end =
269  UNICHAR::end(utf8_text->c_str(), utf8_text->length());
270  for (UNICHAR::const_iterator it = it_begin; it != it_end;) {
271  // Skip bad utf-8.
272  if (!it.is_legal()) {
273  ++it; // One suitable error message will still be issued.
274  continue;
275  }
276  int unicode = *it;
277  int utf8_len = it.utf8_len();
278  const char* utf8_char = it.utf8_data();
279  // Move it forward before the data gets modified.
280  ++it;
281  if (!IsWhitespace(unicode) && !pango_is_zero_width(unicode) &&
282  pango_coverage_get(coverage, unicode) != PANGO_COVERAGE_EXACT) {
283  if (TLOG_IS_ON(2)) {
284  UNICHAR unichar(unicode);
285  char* str = unichar.utf8_str();
286  tlog(2, "'%s' (U+%x) not covered by font\n", str, unicode);
287  delete[] str;
288  }
289  ++num_dropped_chars;
290  continue;
291  }
292  my_strnmove(out, utf8_char, utf8_len);
293  out += utf8_len;
294  }
295  utf8_text->resize(out - utf8_text->c_str());
296  return num_dropped_chars;
297 }
#define tlog(level,...)
Definition: tlog.h:33
#define TLOG_IS_ON(level)
Definition: tlog.h:39
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:176
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200
int utf8_len() const
Definition: unichar.cpp:186

◆ family_name()

const string& tesseract::PangoFontInfo::family_name ( ) const
inline

Definition at line 105 of file pango_font_info.h.

105 { return family_name_; }

◆ font_size()

int tesseract::PangoFontInfo::font_size ( ) const
inline

Definition at line 107 of file pango_font_info.h.

107 { return font_size_; }

◆ font_type()

FontTypeEnum tesseract::PangoFontInfo::font_type ( ) const
inline

Definition at line 108 of file pango_font_info.h.

108 { return font_type_; }

◆ GetSpacingProperties()

bool tesseract::PangoFontInfo::GetSpacingProperties ( const string &  utf8_char,
int *  x_bearing,
int *  x_advance 
) const

Definition at line 299 of file pango_font_info.cpp.

300  {
301  // Convert to equivalent PangoFont structure
302  PangoFont* font = ToPangoFont();
303  // Find the glyph index in the font for the supplied utf8 character.
304  int total_advance = 0;
305  int min_bearing = 0;
306  // Handle multi-unicode strings by reporting the left-most position of the
307  // x-bearing, and right-most position of the x-advance if the string were to
308  // be rendered.
309  const UNICHAR::const_iterator it_begin = UNICHAR::begin(utf8_char.c_str(),
310  utf8_char.length());
311  const UNICHAR::const_iterator it_end = UNICHAR::end(utf8_char.c_str(),
312  utf8_char.length());
313  for (UNICHAR::const_iterator it = it_begin; it != it_end; ++it) {
314  PangoGlyph glyph_index = pango_fc_font_get_glyph(
315  reinterpret_cast<PangoFcFont*>(font), *it);
316  if (!glyph_index) {
317  // Glyph for given unicode character doesn't exist in font.
318  return false;
319  }
320  // Find the ink glyph extents for the glyph
321  PangoRectangle ink_rect, logical_rect;
322  pango_font_get_glyph_extents(font, glyph_index, &ink_rect, &logical_rect);
323  pango_extents_to_pixels(&ink_rect, NULL);
324  pango_extents_to_pixels(&logical_rect, NULL);
325 
326  int bearing = total_advance + PANGO_LBEARING(ink_rect);
327  if (it == it_begin || bearing < min_bearing) {
328  min_bearing = bearing;
329  }
330  total_advance += PANGO_RBEARING(logical_rect);
331  }
332  *x_bearing = min_bearing;
333  *x_advance = total_advance;
334  return true;
335 }
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200

◆ HardInitFontConfig()

void tesseract::PangoFontInfo::HardInitFontConfig ( const string &  fonts_dir,
const string &  cache_dir 
)
static

Definition at line 122 of file pango_font_info.cpp.

123  {
124  if (!cache_dir_.empty()) {
126  File::JoinPath(cache_dir_.c_str(), "*cache-?").c_str());
127  }
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"
135  "<fontconfig>\n"
136  "<dir>%s</dir>\n"
137  "<cachedir>%s</cachedir>\n"
138  "<config></config>\n"
139  "</fontconfig>",
140  fonts_dir.c_str(), cache_dir_.c_str());
141  string fonts_conf_file = File::JoinPath(cache_dir_.c_str(), "fonts.conf");
142  File::WriteStringToFileOrDie(fonts_conf_template, fonts_conf_file);
143 #ifdef _WIN32
144  std::string env("FONTCONFIG_PATH=");
145  env.append(cache_dir_.c_str());
146  putenv(env.c_str());
147  putenv("LANG=en_US.utf8");
148 #else
149  setenv("FONTCONFIG_PATH", cache_dir_.c_str(), true);
150  // Fix the locale so that the reported font names are consistent.
151  setenv("LANG", "en_US.utf8", true);
152 #endif // _WIN32
153 
154  if (FcInitReinitialize() != FcTrue) {
155  tprintf("FcInitiReinitialize failed!!\n");
156  }
158  // Clear Pango's font cache too.
159  pango_cairo_font_map_set_default(NULL);
160 }
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:113
#define tprintf(...)
Definition: tprintf.h:31
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:83
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:53

◆ ParseFontDescriptionName()

bool tesseract::PangoFontInfo::ParseFontDescriptionName ( const string &  name)

Definition at line 192 of file pango_font_info.cpp.

192  {
193  PangoFontDescription *desc = pango_font_description_from_string(name.c_str());
194  bool success = ParseFontDescription(desc);
195  pango_font_description_free(desc);
196  return success;
197 }

◆ resolution()

int tesseract::PangoFontInfo::resolution ( ) const
inline

Definition at line 110 of file pango_font_info.h.

110 { return resolution_; }

◆ set_resolution()

void tesseract::PangoFontInfo::set_resolution ( const int  resolution)
inline

Definition at line 111 of file pango_font_info.h.

111  {
112  resolution_ = resolution;
113  }

◆ SoftInitFontConfig()

void tesseract::PangoFontInfo::SoftInitFontConfig ( )
static

Definition at line 112 of file pango_font_info.cpp.

112  {
113  if (fonts_dir_.empty()) {
114  HardInitFontConfig(FLAGS_fonts_dir.c_str(),
115  FLAGS_fontconfig_tmpdir.c_str());
116  }
117 }
static void HardInitFontConfig(const string &fonts_dir, const string &cache_dir)

Friends And Related Function Documentation

◆ FontUtils

friend class FontUtils
friend

Definition at line 116 of file pango_font_info.h.


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