tesseract  4.00.00dev
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
 
bool is_bold () const
 
bool is_italic () const
 
bool is_smallcaps () const
 
bool is_monospace () const
 
bool is_fraktur () 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 88 of file pango_font_info.cpp.

89  : desc_(nullptr), resolution_(kDefaultResolution) {
90  Clear();
91 }
const int kDefaultResolution

◆ ~PangoFontInfo()

tesseract::PangoFontInfo::~PangoFontInfo ( )

Definition at line 115 of file pango_font_info.cpp.

115 { pango_font_description_free(desc_); }

◆ PangoFontInfo() [2/2]

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

Definition at line 93 of file pango_font_info.cpp.

94  : desc_(nullptr), resolution_(kDefaultResolution) {
95  if (!ParseFontDescriptionName(desc)) {
96  tprintf("ERROR: Could not parse %s\n", desc.c_str());
97  Clear();
98  }
99 }
#define tprintf(...)
Definition: tprintf.h:31
bool ParseFontDescriptionName(const string &name)
const int kDefaultResolution

Member Function Documentation

◆ CanRenderString() [1/2]

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

Definition at line 394 of file pango_font_info.cpp.

395  {
396  if (graphemes) graphemes->clear();
397  // We check for font coverage of the text first, as otherwise Pango could
398  // (undesirably) fall back to another font that does have the required
399  // coverage.
400  if (!CoversUTF8Text(utf8_word, len)) {
401  return false;
402  }
403  // U+25CC dotted circle character that often (but not always) gets rendered
404  // when there is an illegal grapheme sequence.
405  const char32 kDottedCircleGlyph = 9676;
406  bool bad_glyph = false;
407  PangoFontMap* font_map = pango_cairo_font_map_get_default();
408  PangoContext* context = pango_context_new();
409  pango_context_set_font_map(context, font_map);
410  PangoLayout* layout;
411  {
412  // Pango is not relasing the cached layout.
414  layout = pango_layout_new(context);
415  }
416  if (desc_) {
417  pango_layout_set_font_description(layout, desc_);
418  } else {
419  PangoFontDescription *desc = pango_font_description_from_string(
420  DescriptionName().c_str());
421  pango_layout_set_font_description(layout, desc);
422  pango_font_description_free(desc);
423  }
424  pango_layout_set_text(layout, utf8_word, len);
425  PangoLayoutIter* run_iter = nullptr;
426  { // Fontconfig caches some information here that is not freed before exit.
428  run_iter = pango_layout_get_iter(layout);
429  }
430  do {
431  PangoLayoutRun* run = pango_layout_iter_get_run_readonly(run_iter);
432  if (!run) {
433  tlog(2, "Found end of line nullptr run marker\n");
434  continue;
435  }
436  PangoGlyph dotted_circle_glyph;
437  PangoFont* font = run->item->analysis.font;
438 
439 #ifdef _WIN32 // Fixme! Leaks memory and breaks unittests.
440  PangoGlyphString* glyphs = pango_glyph_string_new();
441  char s[] = "\xc2\xa7";
442  pango_shape(s, sizeof(s), &(run->item->analysis), glyphs);
443  dotted_circle_glyph = glyphs->glyphs[0].glyph;
444 #else
445  dotted_circle_glyph = pango_fc_font_get_glyph(
446  reinterpret_cast<PangoFcFont*>(font), kDottedCircleGlyph);
447 #endif
448 
449  if (TLOG_IS_ON(2)) {
450  PangoFontDescription* desc = pango_font_describe(font);
451  char* desc_str = pango_font_description_to_string(desc);
452  tlog(2, "Desc of font in run: %s\n", desc_str);
453  g_free(desc_str);
454  pango_font_description_free(desc);
455  }
456 
457  PangoGlyphItemIter cluster_iter;
458  gboolean have_cluster;
459  for (have_cluster = pango_glyph_item_iter_init_start(&cluster_iter,
460  run, utf8_word);
461  have_cluster && !bad_glyph;
462  have_cluster = pango_glyph_item_iter_next_cluster(&cluster_iter)) {
463  const int start_byte_index = cluster_iter.start_index;
464  const int end_byte_index = cluster_iter.end_index;
465  int start_glyph_index = cluster_iter.start_glyph;
466  int end_glyph_index = cluster_iter.end_glyph;
467  string cluster_text = string(utf8_word + start_byte_index,
468  end_byte_index - start_byte_index);
469  if (graphemes) graphemes->push_back(cluster_text);
470  if (IsUTF8Whitespace(cluster_text.c_str())) {
471  tlog(2, "Skipping whitespace\n");
472  continue;
473  }
474  if (TLOG_IS_ON(2)) {
475  printf("start_byte=%d end_byte=%d start_glyph=%d end_glyph=%d ",
476  start_byte_index, end_byte_index,
477  start_glyph_index, end_glyph_index);
478  }
479  for (int i = start_glyph_index,
480  step = (end_glyph_index > start_glyph_index) ? 1 : -1;
481  !bad_glyph && i != end_glyph_index; i+= step) {
482  const bool unknown_glyph =
483  (cluster_iter.glyph_item->glyphs->glyphs[i].glyph &
484  PANGO_GLYPH_UNKNOWN_FLAG);
485  const bool illegal_glyph =
486  (cluster_iter.glyph_item->glyphs->glyphs[i].glyph ==
487  dotted_circle_glyph);
488  bad_glyph = unknown_glyph || illegal_glyph;
489  if (TLOG_IS_ON(2)) {
490  printf("(%d=%d)", cluster_iter.glyph_item->glyphs->glyphs[i].glyph,
491  bad_glyph ? 1 : 0);
492  }
493  }
494  if (TLOG_IS_ON(2)) {
495  printf(" '%s'\n", cluster_text.c_str());
496  }
497  if (bad_glyph)
498  tlog(1, "Found illegal glyph!\n");
499  }
500  } while (!bad_glyph && pango_layout_iter_next_run(run_iter));
501 
502  pango_layout_iter_free(run_iter);
503  g_object_unref(context);
504  g_object_unref(layout);
505  if (bad_glyph && graphemes) graphemes->clear();
506  return !bad_glyph;
507 }
#define tlog(level,...)
Definition: tlog.h:33
signed int char32
Definition: normstrngs.h:27
#define TLOG_IS_ON(level)
Definition: tlog.h:39
#define DISABLE_HEAP_LEAK_CHECK
Definition: util.h:63
bool CoversUTF8Text(const char *utf8_text, int byte_length) const
bool IsUTF8Whitespace(const char *text)
Definition: normstrngs.cpp:184

◆ CanRenderString() [2/2]

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

Definition at line 389 of file pango_font_info.cpp.

389  {
390  std::vector<string> graphemes;
391  return CanRenderString(utf8_word, len, &graphemes);
392 }
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 269 of file pango_font_info.cpp.

269  {
270  PangoFont* font = ToPangoFont();
271  PangoCoverage* coverage = pango_font_get_coverage(font, nullptr);
272  for (UNICHAR::const_iterator it = UNICHAR::begin(utf8_text, byte_length);
273  it != UNICHAR::end(utf8_text, byte_length);
274  ++it) {
275  if (IsWhitespace(*it) || pango_is_zero_width(*it))
276  continue;
277  if (pango_coverage_get(coverage, *it) != PANGO_COVERAGE_EXACT) {
278  char tmp[5];
279  int len = it.get_utf8(tmp);
280  tmp[len] = '\0';
281  tlog(2, "'%s' (U+%x) not covered by font\n", tmp, *it);
282  return false;
283  }
284  }
285  return true;
286 }
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200
#define tlog(level,...)
Definition: tlog.h:33
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:178

◆ DescriptionName()

string tesseract::PangoFontInfo::DescriptionName ( ) const

Definition at line 117 of file pango_font_info.cpp.

117  {
118  if (!desc_) return "";
119  char* desc_str = pango_font_description_to_string(desc_);
120  string desc_name(desc_str);
121  g_free(desc_str);
122  return desc_name;
123 }

◆ DropUncoveredChars()

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

Definition at line 310 of file pango_font_info.cpp.

310  {
311  PangoFont* font = ToPangoFont();
312  PangoCoverage* coverage = pango_font_get_coverage(font, nullptr);
313  int num_dropped_chars = 0;
314  // Maintain two iterators that point into the string. For space efficiency, we
315  // will repeatedly copy one covered UTF8 character from one to the other, and
316  // at the end resize the string to the right length.
317  char* out = const_cast<char*>(utf8_text->c_str());
318  const UNICHAR::const_iterator it_begin =
319  UNICHAR::begin(utf8_text->c_str(), utf8_text->length());
320  const UNICHAR::const_iterator it_end =
321  UNICHAR::end(utf8_text->c_str(), utf8_text->length());
322  for (UNICHAR::const_iterator it = it_begin; it != it_end;) {
323  // Skip bad utf-8.
324  if (!it.is_legal()) {
325  ++it; // One suitable error message will still be issued.
326  continue;
327  }
328  int unicode = *it;
329  int utf8_len = it.utf8_len();
330  const char* utf8_char = it.utf8_data();
331  // Move it forward before the data gets modified.
332  ++it;
333  if (!IsWhitespace(unicode) && !pango_is_zero_width(unicode) &&
334  pango_coverage_get(coverage, unicode) != PANGO_COVERAGE_EXACT) {
335  if (TLOG_IS_ON(2)) {
336  UNICHAR unichar(unicode);
337  char* str = unichar.utf8_str();
338  tlog(2, "'%s' (U+%x) not covered by font\n", str, unicode);
339  delete[] str;
340  }
341  ++num_dropped_chars;
342  continue;
343  }
344  my_strnmove(out, utf8_char, utf8_len);
345  out += utf8_len;
346  }
347  utf8_text->resize(out - utf8_text->c_str());
348  return num_dropped_chars;
349 }
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200
#define tlog(level,...)
Definition: tlog.h:33
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
#define TLOG_IS_ON(level)
Definition: tlog.h:39
int utf8_len() const
Definition: unichar.cpp:186
bool IsWhitespace(const char32 ch)
Definition: normstrngs.cpp:178

◆ 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 113 of file pango_font_info.h.

113 { return font_type_; }

◆ GetSpacingProperties()

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

Definition at line 351 of file pango_font_info.cpp.

352  {
353  // Convert to equivalent PangoFont structure
354  PangoFont* font = ToPangoFont();
355  // Find the glyph index in the font for the supplied utf8 character.
356  int total_advance = 0;
357  int min_bearing = 0;
358  // Handle multi-unicode strings by reporting the left-most position of the
359  // x-bearing, and right-most position of the x-advance if the string were to
360  // be rendered.
361  const UNICHAR::const_iterator it_begin = UNICHAR::begin(utf8_char.c_str(),
362  utf8_char.length());
363  const UNICHAR::const_iterator it_end = UNICHAR::end(utf8_char.c_str(),
364  utf8_char.length());
365  for (UNICHAR::const_iterator it = it_begin; it != it_end; ++it) {
366  PangoGlyph glyph_index = pango_fc_font_get_glyph(
367  reinterpret_cast<PangoFcFont*>(font), *it);
368  if (!glyph_index) {
369  // Glyph for given unicode character doesn't exist in font.
370  return false;
371  }
372  // Find the ink glyph extents for the glyph
373  PangoRectangle ink_rect, logical_rect;
374  pango_font_get_glyph_extents(font, glyph_index, &ink_rect, &logical_rect);
375  pango_extents_to_pixels(&ink_rect, nullptr);
376  pango_extents_to_pixels(&logical_rect, nullptr);
377 
378  int bearing = total_advance + PANGO_LBEARING(ink_rect);
379  if (it == it_begin || bearing < min_bearing) {
380  min_bearing = bearing;
381  }
382  total_advance += PANGO_RBEARING(logical_rect);
383  }
384  *x_bearing = min_bearing;
385  *x_advance = total_advance;
386  return true;
387 }
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204

◆ HardInitFontConfig()

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

Definition at line 139 of file pango_font_info.cpp.

140  {
141  if (!cache_dir_.empty()) {
143  File::JoinPath(cache_dir_.c_str(), "*cache-?").c_str());
144  }
145  const int MAX_FONTCONF_FILESIZE = 1024;
146  char fonts_conf_template[MAX_FONTCONF_FILESIZE];
147  cache_dir_ = cache_dir;
148  fonts_dir_ = fonts_dir;
149  snprintf(fonts_conf_template, MAX_FONTCONF_FILESIZE,
150  "<?xml version=\"1.0\"?>\n"
151  "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
152  "<fontconfig>\n"
153  "<dir>%s</dir>\n"
154  "<cachedir>%s</cachedir>\n"
155  "<config></config>\n"
156  "</fontconfig>",
157  fonts_dir.c_str(), cache_dir_.c_str());
158  string fonts_conf_file = File::JoinPath(cache_dir_.c_str(), "fonts.conf");
159  File::WriteStringToFileOrDie(fonts_conf_template, fonts_conf_file);
160 #ifdef _WIN32
161  std::string env("FONTCONFIG_PATH=");
162  env.append(cache_dir_.c_str());
163  putenv(env.c_str());
164  putenv("LANG=en_US.utf8");
165 #else
166  setenv("FONTCONFIG_PATH", cache_dir_.c_str(), true);
167  // Fix the locale so that the reported font names are consistent.
168  setenv("LANG", "en_US.utf8", true);
169 #endif // _WIN32
170 
171  if (FcInitReinitialize() != FcTrue) {
172  tprintf("FcInitiReinitialize failed!!\n");
173  }
175  // Clear Pango's font cache too.
176  pango_cairo_font_map_set_default(nullptr);
177 }
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:52
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:111
#define tprintf(...)
Definition: tprintf.h:31
static string JoinPath(const string &prefix, const string &suffix)
Definition: fileio.cpp:81

◆ is_bold()

bool tesseract::PangoFontInfo::is_bold ( ) const
inline

Definition at line 108 of file pango_font_info.h.

108 { return is_bold_; }

◆ is_fraktur()

bool tesseract::PangoFontInfo::is_fraktur ( ) const
inline

Definition at line 112 of file pango_font_info.h.

112 { return is_fraktur_; }

◆ is_italic()

bool tesseract::PangoFontInfo::is_italic ( ) const
inline

Definition at line 109 of file pango_font_info.h.

109 { return is_italic_; }

◆ is_monospace()

bool tesseract::PangoFontInfo::is_monospace ( ) const
inline

Definition at line 111 of file pango_font_info.h.

111 { return is_monospace_; }

◆ is_smallcaps()

bool tesseract::PangoFontInfo::is_smallcaps ( ) const
inline

Definition at line 110 of file pango_font_info.h.

110 { return is_smallcaps_; }

◆ ParseFontDescriptionName()

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

Definition at line 244 of file pango_font_info.cpp.

244  {
245  PangoFontDescription *desc = pango_font_description_from_string(name.c_str());
246  bool success = ParseFontDescription(desc);
247  pango_font_description_free(desc);
248  return success;
249 }

◆ resolution()

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

Definition at line 115 of file pango_font_info.h.

115 { return resolution_; }

◆ set_resolution()

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

Definition at line 116 of file pango_font_info.h.

116  {
117  resolution_ = resolution;
118  }

◆ SoftInitFontConfig()

void tesseract::PangoFontInfo::SoftInitFontConfig ( )
static

Definition at line 129 of file pango_font_info.cpp.

129  {
130  if (fonts_dir_.empty()) {
131  HardInitFontConfig(FLAGS_fonts_dir.c_str(),
132  FLAGS_fontconfig_tmpdir.c_str());
133  }
134 }
static void HardInitFontConfig(const string &fonts_dir, const string &cache_dir)

Friends And Related Function Documentation

◆ FontUtils

friend class FontUtils
friend

Definition at line 121 of file pango_font_info.h.


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