tesseract  4.0.0-1-g2a2b
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 std::string &name)
 
bool ParseFontDescriptionName (const std::string &name)
 
bool CoversUTF8Text (const char *utf8_text, int byte_length) const
 
int DropUncoveredChars (std::string *utf8_text) const
 
bool CanRenderString (const char *utf8_word, int len, std::vector< std::string > *graphemes) const
 
bool CanRenderString (const char *utf8_word, int len) const
 
bool GetSpacingProperties (const std::string &utf8_char, int *x_bearing, int *x_advance) const
 
std::string DescriptionName () const
 
const std::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 std::string &fonts_dir, const std::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.

77  : desc_(nullptr), resolution_(kDefaultResolution) {
78  Clear();
79 }
const int kDefaultResolution

◆ ~PangoFontInfo()

tesseract::PangoFontInfo::~PangoFontInfo ( )

Definition at line 99 of file pango_font_info.cpp.

99 { pango_font_description_free(desc_); }

◆ PangoFontInfo() [2/2]

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

Definition at line 81 of file pango_font_info.cpp.

82  : desc_(nullptr), resolution_(kDefaultResolution) {
83  if (!ParseFontDescriptionName(desc)) {
84  tprintf("ERROR: Could not parse %s\n", desc.c_str());
85  Clear();
86  }
87 }
const int kDefaultResolution
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
bool ParseFontDescriptionName(const std::string &name)

Member Function Documentation

◆ CanRenderString() [1/2]

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

Definition at line 349 of file pango_font_info.cpp.

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

◆ CanRenderString() [2/2]

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

Definition at line 344 of file pango_font_info.cpp.

344  {
345  std::vector<std::string> graphemes;
346  return CanRenderString(utf8_word, len, &graphemes);
347 }
bool CanRenderString(const char *utf8_word, int len, std::vector< std::string > *graphemes) const

◆ CoversUTF8Text()

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

Definition at line 218 of file pango_font_info.cpp.

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

◆ DescriptionName()

std::string tesseract::PangoFontInfo::DescriptionName ( ) const

Definition at line 101 of file pango_font_info.cpp.

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

◆ DropUncoveredChars()

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

Definition at line 261 of file pango_font_info.cpp.

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

◆ family_name()

const std::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 std::string &  utf8_char,
int *  x_bearing,
int *  x_advance 
) const

Definition at line 304 of file pango_font_info.cpp.

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

◆ HardInitFontConfig()

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

Definition at line 123 of file pango_font_info.cpp.

124  {
125  if (!cache_dir_.empty()) {
127  File::JoinPath(cache_dir_.c_str(), "*cache-?").c_str());
128  }
129  const int MAX_FONTCONF_FILESIZE = 1024;
130  char fonts_conf_template[MAX_FONTCONF_FILESIZE];
131  cache_dir_ = cache_dir;
132  fonts_dir_ = fonts_dir;
133  snprintf(fonts_conf_template, MAX_FONTCONF_FILESIZE,
134  "<?xml version=\"1.0\"?>\n"
135  "<!DOCTYPE fontconfig SYSTEM \"fonts.dtd\">\n"
136  "<fontconfig>\n"
137  "<dir>%s</dir>\n"
138  "<cachedir>%s</cachedir>\n"
139  "<config></config>\n"
140  "</fontconfig>",
141  fonts_dir.c_str(), cache_dir_.c_str());
142  std::string fonts_conf_file = File::JoinPath(cache_dir_.c_str(), "fonts.conf");
143  File::WriteStringToFileOrDie(fonts_conf_template, fonts_conf_file);
144 #ifdef _WIN32
145  std::string env("FONTCONFIG_PATH=");
146  env.append(cache_dir_.c_str());
147  _putenv(env.c_str());
148  _putenv("LANG=en_US.utf8");
149 #else
150  setenv("FONTCONFIG_PATH", cache_dir_.c_str(), true);
151  // Fix the locale so that the reported font names are consistent.
152  setenv("LANG", "en_US.utf8", true);
153 #endif // _WIN32
154 
155  if (FcInitReinitialize() != FcTrue) {
156  tprintf("FcInitiReinitialize failed!!\n");
157  }
159  // Clear Pango's font cache too.
160  pango_cairo_font_map_set_default(nullptr);
161 }
static bool DeleteMatchingFiles(const char *pattern)
Definition: fileio.cpp:112
static void WriteStringToFileOrDie(const std::string &str, const std::string &filename)
Definition: fileio.cpp:53
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:37
static std::string JoinPath(const std::string &prefix, const std::string &suffix)
Definition: fileio.cpp:82

◆ ParseFontDescriptionName()

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

Definition at line 193 of file pango_font_info.cpp.

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

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

113  {
114  if (fonts_dir_.empty()) {
115  HardInitFontConfig(FLAGS_fonts_dir.c_str(),
116  FLAGS_fontconfig_tmpdir.c_str());
117  }
118 }
static void HardInitFontConfig(const std::string &fonts_dir, const std::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: