Qt 4.8
Public Functions | List of all members
QFontconfigDatabase Class Reference

#include <qfontconfigdatabase.h>

Inheritance diagram for QFontconfigDatabase:
QBasicUnixFontDatabase QPlatformFontDatabase

Public Functions

QStringList fallbacksForFamily (const QString family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script) const
 Returns a list of alternative fonts for the specified family and style and script using the styleHint given. More...
 
QFontEnginefontEngine (const QFontDef &fontDef, QUnicodeTables::Script script, void *handle)
 Returns the font engine that can be used to render the font described by the font definition, fontDef, in the specified script. More...
 
void populateFontDatabase ()
 This function is called once at startup by Qts internal fontdatabase. More...
 
- Public Functions inherited from QBasicUnixFontDatabase
QStringList addApplicationFont (const QByteArray &fontData, const QString &fileName)
 Adds an application font described by the font contained supplied fontData or using the font contained in the file referenced by fileName. More...
 
void releaseHandle (void *handle)
 Releases the font handle and deletes any associated data loaded from a file. More...
 
- Public Functions inherited from QPlatformFontDatabase
virtual QString fontDir () const
 Returns the path to the font directory. More...
 
virtual QFontEnginefontEngine (const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
 

Additional Inherited Members

- Static Public Functions inherited from QBasicUnixFontDatabase
static QStringList addTTFile (const QByteArray &fontData, const QByteArray &file)
 
- Static Public Functions inherited from QPlatformFontDatabase
static void registerFont (const QString &familyname, const QString &foundryname, QFont::Weight weight, QFont::Style style, QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize, const QSupportedWritingSystems &writingSystems, void *handle)
 Registers a font with the given set of attributes describing the font's foundry, family name, style and stretch information, pixel size, and supported writing systems. More...
 
static void registerQPF2Font (const QByteArray &dataArray, void *handle)
 Registers the pre-rendered QPF2 font contained in the given dataArray. More...
 

Detailed Description

Definition at line 48 of file qfontconfigdatabase.h.

Functions

◆ fallbacksForFamily()

QStringList QFontconfigDatabase::fallbacksForFamily ( const QString  family,
const QFont::Style style,
const QFont::StyleHint styleHint,
const QUnicodeTables::Script script 
) const
virtual

Returns a list of alternative fonts for the specified family and style and script using the styleHint given.

Reimplemented from QBasicUnixFontDatabase.

Definition at line 557 of file qfontconfigdatabase.cpp.

558 {
560  FcPattern *pattern = FcPatternCreate();
561  if (!pattern)
562  return fallbackFamilies;
563 
564  FcValue value;
565  value.type = FcTypeString;
566  QByteArray cs = family.toUtf8();
567  value.u.s = (const FcChar8 *)cs.data();
568  FcPatternAdd(pattern,FC_FAMILY,value,true);
569 
570  int slant_value = FC_SLANT_ROMAN;
571  if (style == QFont::StyleItalic)
572  slant_value = FC_SLANT_ITALIC;
573  else if (style == QFont::StyleOblique)
574  slant_value = FC_SLANT_OBLIQUE;
575  FcPatternAddInteger(pattern, FC_SLANT, slant_value);
576 
579  FcLangSet *ls = FcLangSetCreate();
580  FcLangSetAdd(ls, (const FcChar8*)specialLanguages[script]);
581  FcPatternAddLangSet(pattern, FC_LANG, ls);
582  FcLangSetDestroy(ls);
583  }
584 
585  const char *stylehint = getFcFamilyForStyleHint(styleHint);
586  if (stylehint) {
587  value.u.s = (const FcChar8 *)stylehint;
588  FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue);
589  }
590 
591  FcConfigSubstitute(0, pattern, FcMatchPattern);
592  FcDefaultSubstitute(pattern);
593 
594  FcResult result = FcResultMatch;
595  FcFontSet *fontSet = FcFontSort(0,pattern,FcFalse,0,&result);
596  FcPatternDestroy(pattern);
597 
598  if (fontSet)
599  {
600  for (int i = 0; i < fontSet->nfont; i++) {
601  FcChar8 *value = 0;
602  if (FcPatternGetString(fontSet->fonts[i], FC_FAMILY, 0, &value) != FcResultMatch)
603  continue;
604  // capitalize(value);
605  QString familyName = QString::fromUtf8((const char *)value);
606  if (!fallbackFamilies.contains(familyName,Qt::CaseInsensitive)) {
607  fallbackFamilies << familyName;
608  }
609  }
610  FcFontSetDestroy(fontSet);
611  }
612 // qDebug() << "fallbackFamilies for:" << family << fallbackFamilies;
613 
614  return fallbackFamilies;
615 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
static const char * getFcFamilyForStyleHint(const QFont::StyleHint style)
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
const char * styleHint(const QFontDef &request)
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
static const char * specialLanguages[]
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
static QStringList fallbackFamilies(const QString &family, const QFont::Style &style, const QFont::StyleHint &styleHint, const QUnicodeTables::Script &script)

◆ fontEngine()

QFontEngine * QFontconfigDatabase::fontEngine ( const QFontDef fontDef,
QUnicodeTables::Script  script,
void *  handle 
)
virtual

Returns the font engine that can be used to render the font described by the font definition, fontDef, in the specified script.

Reimplemented from QBasicUnixFontDatabase.

Definition at line 479 of file qfontconfigdatabase.cpp.

480 {
481  if (!usrPtr)
482  return 0;
483  QFontDef fontDef = f;
484 
485  QFontEngineFT *engine;
486  FontFile *fontfile = static_cast<FontFile *> (usrPtr);
488  fid.filename = fontfile->fileName.toLocal8Bit();
489  fid.index = fontfile->indexValue;
490 
491  //try and get the pattern
492  FcPattern *pattern = FcPatternCreate();
493 
494  bool antialias = !(fontDef.styleStrategy & QFont::NoAntialias);
496 
497  engine = new QFontEngineFT(fontDef);
498 
499  FcValue value;
500  value.type = FcTypeString;
501  QByteArray cs = fontDef.family.toUtf8();
502  value.u.s = (const FcChar8 *)cs.data();
503  FcPatternAdd(pattern,FC_FAMILY,value,true);
504 
505 
506  value.u.s = (const FcChar8 *)fid.filename.data();
507  FcPatternAdd(pattern,FC_FILE,value,true);
508 
509  value.type = FcTypeInteger;
510  value.u.i = fid.index;
511  FcPatternAdd(pattern,FC_INDEX,value,true);
512 
513  QFontEngineFT::HintStyle default_hint_style;
514 
515  if (FcConfigSubstitute(0,pattern,FcMatchPattern)) {
516 
517  //hinting
518  int hint_style = 0;
519  if (FcPatternGetInteger (pattern, FC_HINT_STYLE, 0, &hint_style) == FcResultNoMatch)
520  hint_style = QFontEngineFT::HintFull;
521  switch (hint_style) {
522  case FC_HINT_NONE:
523  default_hint_style = QFontEngineFT::HintNone;
524  break;
525  case FC_HINT_SLIGHT:
526  default_hint_style = QFontEngineFT::HintLight;
527  break;
528  case FC_HINT_MEDIUM:
529  default_hint_style = QFontEngineFT::HintMedium;
530  break;
531  default:
532  default_hint_style = QFontEngineFT::HintFull;
533  break;
534  }
535  }
536 
537  engine->setDefaultHintStyle(default_hint_style);
538  if (!engine->init(fid,antialias,format)) {
539  delete engine;
540  engine = 0;
541  return engine;
542  }
543  if (engine->invalid()) {
544  delete engine;
545  engine = 0;
546  } else if (scriptRequiresOpenType(script)) {
547  HB_Face hbFace = engine->harfbuzzFace();
548  if (!hbFace || !hbFace->supported_scripts[script]) {
549  delete engine;
550  engine = 0;
551  }
552  }
553 
554  return engine;
555 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool init(FaceId faceId, bool antiaalias, GlyphFormat defaultFormat=Format_None, const QByteArray &fontData=QByteArray())
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
static bool scriptRequiresOpenType(int script)
void setDefaultHintStyle(HintStyle style)
bool invalid() const
QString family
Definition: qfont_p.h:82
uint styleStrategy
Definition: qfont_p.h:92
HB_Face harfbuzzFace() const

◆ populateFontDatabase()

void QFontconfigDatabase::populateFontDatabase ( )
virtual

This function is called once at startup by Qts internal fontdatabase.

Reimplement this function in a subclass for a convenient place to initialise the internal fontdatabase.

The default implementation looks in the fontDir() location and registers all qpf2 fonts.

Reimplemented from QBasicUnixFontDatabase.

Definition at line 296 of file qfontconfigdatabase.cpp.

297 {
298  FcFontSet *fonts;
299 
300  QString familyName;
301  FcChar8 *value = 0;
302  int weight_value;
303  int slant_value;
304  int spacing_value;
305  FcChar8 *file_value;
306  int indexValue;
307  FcChar8 *foundry_value;
308  FcBool scalable;
309  FcBool antialias;
310 
311  {
312  FcObjectSet *os = FcObjectSetCreate();
313  FcPattern *pattern = FcPatternCreate();
314  const char *properties [] = {
315  FC_FAMILY, FC_WEIGHT, FC_SLANT,
316  FC_SPACING, FC_FILE, FC_INDEX,
317  FC_LANG, FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE,
318  FC_WIDTH,
319 #if FC_VERSION >= 20297
320  FC_CAPABILITY,
321 #endif
322  (const char *)0
323  };
324  const char **p = properties;
325  while (*p) {
326  FcObjectSetAdd(os, *p);
327  ++p;
328  }
329  fonts = FcFontList(0, pattern, os);
330  FcObjectSetDestroy(os);
331  FcPatternDestroy(pattern);
332  }
333 
334  for (int i = 0; i < fonts->nfont; i++) {
335  if (FcPatternGetString(fonts->fonts[i], FC_FAMILY, 0, &value) != FcResultMatch)
336  continue;
337  // capitalize(value);
338  familyName = QString::fromUtf8((const char *)value);
339  slant_value = FC_SLANT_ROMAN;
340  weight_value = FC_WEIGHT_MEDIUM;
341  spacing_value = FC_PROPORTIONAL;
342  file_value = 0;
343  indexValue = 0;
344  scalable = FcTrue;
345 
346 
347  if (FcPatternGetInteger (fonts->fonts[i], FC_SLANT, 0, &slant_value) != FcResultMatch)
348  slant_value = FC_SLANT_ROMAN;
349  if (FcPatternGetInteger (fonts->fonts[i], FC_WEIGHT, 0, &weight_value) != FcResultMatch)
350  weight_value = FC_WEIGHT_MEDIUM;
351  if (FcPatternGetInteger (fonts->fonts[i], FC_SPACING, 0, &spacing_value) != FcResultMatch)
352  spacing_value = FC_PROPORTIONAL;
353  if (FcPatternGetString (fonts->fonts[i], FC_FILE, 0, &file_value) != FcResultMatch)
354  file_value = 0;
355  if (FcPatternGetInteger (fonts->fonts[i], FC_INDEX, 0, &indexValue) != FcResultMatch)
356  indexValue = 0;
357  if (FcPatternGetBool(fonts->fonts[i], FC_SCALABLE, 0, &scalable) != FcResultMatch)
358  scalable = FcTrue;
359  if (FcPatternGetString(fonts->fonts[i], FC_FOUNDRY, 0, &foundry_value) != FcResultMatch)
360  foundry_value = 0;
361  if(FcPatternGetBool(fonts->fonts[i],FC_ANTIALIAS,0,&antialias) != FcResultMatch)
362  antialias = true;
363 
364  QSupportedWritingSystems writingSystems;
365  FcLangSet *langset = 0;
366  FcResult res = FcPatternGetLangSet(fonts->fonts[i], FC_LANG, 0, &langset);
367  if (res == FcResultMatch) {
368  for (int i = 1; i < LanguageCount; ++i) {
369  const FcChar8 *lang = (const FcChar8*) languageForWritingSystem[i];
370  if (lang) {
371  FcLangResult langRes = FcLangSetHasLang(langset, lang);
372  if (langRes != FcLangDifferentLang)
373  writingSystems.setSupported(QFontDatabase::WritingSystem(i));
374  }
375  }
376  } else {
377  // we set Other to supported for symbol fonts. It makes no
378  // sense to merge these with other ones, as they are
379  // special in a way.
380  writingSystems.setSupported(QFontDatabase::Other);
381  }
382 
383  FcCharSet *cs = 0;
384  res = FcPatternGetCharSet(fonts->fonts[i], FC_CHARSET, 0, &cs);
385  if (res == FcResultMatch) {
386  // some languages are not supported by FontConfig, we rather check the
387  // charset to detect these
388  for (int i = 1; i < SampleCharCount; ++i) {
390  continue;
391  if (FcCharSetHasChar(cs, sampleCharForWritingSystem[i]))
392  writingSystems.setSupported(QFontDatabase::WritingSystem(i));
393  }
394  }
395 
396 #if FC_VERSION >= 20297
397  for (int j = 1; j < LanguageCount; ++j) {
398  if (writingSystems.supported(QFontDatabase::WritingSystem(j))
399  && requiresOpenType(j) && openType[j]) {
400  FcChar8 *cap;
401  res = FcPatternGetString (fonts->fonts[i], FC_CAPABILITY, 0, &cap);
402  if (res != FcResultMatch || !strstr((const char *)cap, openType[j]))
403  writingSystems.setSupported(QFontDatabase::WritingSystem(j),false);
404  }
405  }
406 #endif
407 
408  FontFile *fontFile = new FontFile;
409  fontFile->fileName = QLatin1String((const char *)file_value);
410  fontFile->indexValue = indexValue;
411 
412  QFont::Style style = (slant_value == FC_SLANT_ITALIC)
414  : ((slant_value == FC_SLANT_OBLIQUE)
417  QFont::Weight weight = QFont::Weight(getFCWeight(weight_value));
418 
419  double pixel_size = 0;
420  if (!scalable)
421  FcPatternGetDouble (fonts->fonts[i], FC_PIXEL_SIZE, 0, &pixel_size);
422 
423  int width = FC_WIDTH_NORMAL;
424  FcPatternGetInteger(fonts->fonts[i], FC_WIDTH, 0, &width);
425 
426  QFont::Stretch stretch;
427  switch (width) {
428  case FC_WIDTH_ULTRACONDENSED: stretch = QFont::UltraCondensed; break;
429  case FC_WIDTH_EXTRACONDENSED: stretch = QFont::ExtraCondensed; break;
430  case FC_WIDTH_CONDENSED: stretch = QFont::Condensed; break;
431  case FC_WIDTH_SEMICONDENSED: stretch = QFont::SemiCondensed; break;
432  case FC_WIDTH_NORMAL: stretch = QFont::Unstretched; break;
433  case FC_WIDTH_SEMIEXPANDED: stretch = QFont::SemiExpanded; break;
434  case FC_WIDTH_EXPANDED: stretch = QFont::Expanded; break;
435  case FC_WIDTH_EXTRAEXPANDED: stretch = QFont::ExtraExpanded; break;
436  case FC_WIDTH_ULTRAEXPANDED: stretch = QFont::UltraExpanded; break;
437  default: stretch = QFont::Unstretched; break;
438  }
439 
440  QPlatformFontDatabase::registerFont(familyName,QLatin1String((const char *)foundry_value),weight,style,stretch,antialias,scalable,pixel_size,writingSystems,fontFile);
441 // qDebug() << familyName << (const char *)foundry_value << weight << style << &writingSystems << scalable << true << pixel_size;
442  }
443 
444  FcFontSetDestroy (fonts);
445 
446  struct FcDefaultFont {
447  const char *qtname;
448  const char *rawname;
449  bool fixed;
450  };
451  const FcDefaultFont defaults[] = {
452  { "Serif", "serif", false },
453  { "Sans Serif", "sans-serif", false },
454  { "Monospace", "monospace", true },
455  { 0, 0, false }
456  };
457  const FcDefaultFont *f = defaults;
458  // aliases only make sense for 'common', not for any of the specials
461 
462 
463  while (f->qtname) {
467  ++f;
468  }
469 
470  //Lighthouse has very lazy population of the font db. We want it to be initialized when
471  //QApplication is constructed, so that the population procedure can do something like this to
472  //set the default font
473 // const FcDefaultFont *s = defaults;
474 // QFont font("Sans Serif");
475 // font.setPointSize(9);
476 // QApplication::setFont(font);
477 }
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets the supported state of the writing system given by writingSystem to the value specified by suppo...
Q_CORE_EXPORT QTextStream & ws(QTextStream &s)
static const char * languageForWritingSystem[]
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
Weight
Qt uses a weighting scale from 0 to 99 similar to, but not the same as, the scales used in Windows or...
Definition: qfont.h:103
The QString class provides a Unicode character string.
Definition: qstring.h:83
Stretch
Predefined stretch values that follow the CSS naming convention.
Definition: qfont.h:117
bool supported(QFontDatabase::WritingSystem) const
Returns true if the writing system given by writingSystem is supported; otherwise returns false...
Q_CORE_EXPORT QTextStream & fixed(QTextStream &s)
static const char * openType[]
static QFontEngine::FaceId fontFile(const QByteArray &_xname, QFreetypeFace **freetype, int *synth)
static const ushort sampleCharForWritingSystem[]
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase...
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
static void registerFont(const QString &familyname, const QString &foundryname, QFont::Weight weight, QFont::Style style, QFont::Stretch stretch, bool antialiased, bool scalable, int pixelSize, const QSupportedWritingSystems &writingSystems, void *handle)
Registers a font with the given set of attributes describing the font&#39;s foundry, family name...
static bool requiresOpenType(int writingSystem)
#define FC_WIDTH
static const QCssKnownValue properties[NumProperties - 1]
Definition: qcssparser.cpp:67
static int getFCWeight(int fc_weight)

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