Qt 4.8
Public Functions | Properties | List of all members
QCoreTextFontDatabase Class Reference

#include <qcoretextfontdatabase.h>

Inheritance diagram for QCoreTextFontDatabase:
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 QPlatformFontDatabase
virtual 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...
 
virtual QString fontDir () const
 Returns the path to the font directory. More...
 
virtual QFontEnginefontEngine (const QByteArray &fontData, qreal pixelSize, QFont::HintingPreference hintingPreference)
 
virtual void releaseHandle (void *handle)
 Releases the font handle and deletes any associated data loaded from a file. More...
 

Properties

QHash< QFont::StyleHint, QStringListfallbackLists
 

Additional Inherited Members

- 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 49 of file qcoretextfontdatabase.h.

Functions

◆ fallbacksForFamily()

QStringList QCoreTextFontDatabase::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 QPlatformFontDatabase.

Definition at line 52 of file qcoretextfontdatabase.mm.

56 {
57  Q_UNUSED(family);
58  Q_UNUSED(style);
60  if (fallbackLists.isEmpty())
61  const_cast<QCoreTextFontDatabase *>(this)->populateFontDatabase();
62 
63  return fallbackLists[styleHint];
64 }
bool isEmpty() const
Returns true if the hash contains no items; otherwise returns false.
Definition: qhash.h:297
const char * styleHint(const QFontDef &request)
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
QHash< QFont::StyleHint, QStringList > fallbackLists
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ fontEngine()

QFontEngine * QCoreTextFontDatabase::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 QPlatformFontDatabase.

Definition at line 208 of file qcoretextfontdatabase.mm.

209 {
211  Q_UNUSED(handle)
212  CTFontSymbolicTraits symbolicTraits = 0;
213  if (fontDef.weight >= QFont::Bold)
214  symbolicTraits |= kCTFontBoldTrait;
215  switch (fontDef.style) {
216  case QFont::StyleNormal:
217  break;
218  case QFont::StyleItalic:
219  case QFont::StyleOblique:
220  symbolicTraits |= kCTFontItalicTrait;
221  break;
222  }
223 
224  CGAffineTransform transform = CGAffineTransformIdentity;
225  if (fontDef.stretch != 100) {
226  transform = CGAffineTransformMakeScale(float(fontDef.stretch) / float(100), 1);
227  }
228 
229  QCFType<CTFontRef> baseFont = CTFontCreateWithName(QCFString(fontDef.family), fontDef.pixelSize, &transform);
230  QCFType<CTFontRef> ctFont = NULL;
231  // There is a side effect in Core Text: if we apply 0 as symbolic traits to a font in normal weight,
232  // we will get the light version of that font (while the way supposed to work doesn't:
233  // setting kCTFontWeightTrait to some value between -1.0 to 0.0 has no effect on font selection)
234  if (fontDef.weight != QFont::Normal || symbolicTraits)
235  ctFont = CTFontCreateCopyWithSymbolicTraits(baseFont, fontDef.pixelSize, &transform, symbolicTraits, symbolicTraits);
236 
237  // CTFontCreateCopyWithSymbolicTraits returns NULL if we ask for a trait that does
238  // not exist for the given font. (for example italic)
239  if (ctFont == 0) {
240  ctFont = baseFont;
241  }
242 
243  if (ctFont)
244  return new QCoreTextFontEngine(ctFont, fontDef);
245  return 0;
246 }
uint weight
Definition: qfont_p.h:95
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
qreal pixelSize
Definition: qfont_p.h:90
uint style
Definition: qfont_p.h:97
uint stretch
Definition: qfont_p.h:98
QString family
Definition: qfont_p.h:82
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ populateFontDatabase()

void QCoreTextFontDatabase::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 QPlatformFontDatabase.

Definition at line 112 of file qcoretextfontdatabase.mm.

113 {
114  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
115 
116  QCFType<CTFontCollectionRef> collection = CTFontCollectionCreateFromAvailableFonts(0);
117  if (!collection)
118  return;
119  QCFType<CFArrayRef> fonts = CTFontCollectionCreateMatchingFontDescriptors(collection);
120  if (!fonts)
121  return;
122  QSupportedWritingSystems supportedWritingSystems;
123  for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i)
124  supportedWritingSystems.setSupported((QFontDatabase::WritingSystem)i, true);
125  QString foundry_name = "CoreText";
126  const int numFonts = CFArrayGetCount(fonts);
127  QHash<QString, QString> psNameToFamily;
128  for (int i = 0; i < numFonts; ++i) {
129  CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(fonts, i);
130 
131  QCFString family_name = (CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontFamilyNameAttribute);
132 // QCFString style_name = (CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontStyleNameAttribute);
133 
134  QFont::Weight fontWeight = QFont::Normal;
135  QFont::Style fontStyle = QFont::StyleNormal;
136  if (QCFType<CFDictionaryRef> styles = (CFDictionaryRef)CTFontDescriptorCopyAttribute(font, kCTFontTraitsAttribute)) {
137  if (CFNumberRef weight = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontWeightTrait)) {
138  Q_ASSERT(CFNumberIsFloatType(weight));
139  double d;
140  if (CFNumberGetValue(weight, kCFNumberDoubleType, &d)) {
141  if (d > 0.0)
142  fontWeight = QFont::Bold;
143  }
144  }
145  if (CFNumberRef italic = (CFNumberRef)CFDictionaryGetValue(styles, kCTFontSlantTrait)) {
146  Q_ASSERT(CFNumberIsFloatType(italic));
147  double d;
148  if (CFNumberGetValue(italic, kCFNumberDoubleType, &d)) {
149  if (d > 0.0)
150  fontStyle = QFont::StyleItalic;
151  }
152  }
153  }
154 
155  int pixelSize = 0;
156  if (QCFType<CFNumberRef> size = (CFNumberRef)CTFontDescriptorCopyAttribute(font, kCTFontSizeAttribute)) {
157  if (CFNumberIsFloatType(size)) {
158  double d;
159  CFNumberGetValue(size, kCFNumberDoubleType, &d);
160  pixelSize = d;
161  } else {
162  CFNumberGetValue(size, kCFNumberIntType, &pixelSize);
163  }
164  }
165  QString familyName = QCFString::toQString(family_name);
166  registerFont(familyName,
167  foundry_name,
168  fontWeight,
169  fontStyle,
171  true,
172  true,
173  pixelSize,
174  supportedWritingSystems,
175  0);
176 
177  CFStringRef psName = (CFStringRef) CTFontDescriptorCopyAttribute(font,
178  kCTFontNameAttribute);
179  psNameToFamily[QCFString::toQString(psName)] = familyName;
180  CFRelease(psName);
181  }
182 
183  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
184  NSArray *languages = [defaults stringArrayForKey: @"AppleLanguages"];
185 
186  NSDictionary *fallbackDict = [NSDictionary dictionaryWithContentsOfFile: @"/System/Library/Frameworks/CoreText.framework/DefaultFontFallbacks.plist"];
187  for (NSString *style in [fallbackDict allKeys]) {
188  NSArray *list = [fallbackDict valueForKey: style];
190  QStringList fallbackList;
191  for (id item in list) {
192  if ([item isKindOfClass: [NSArray class]]) {
193  NSArray *langs = [(NSArray *) item sortedArrayUsingFunction: languageMapSort
194  context: languages];
195  for (NSArray *map in langs)
196  fallbackList.append(familyNameFromPostScriptName(&psNameToFamily, [map objectAtIndex: 1]));
197  } else if ([item isKindOfClass: [NSString class]]) {
198  fallbackList.append(familyNameFromPostScriptName(&psNameToFamily, item));
199  }
200  }
201 
202  fallbackLists[styleHint] = fallbackList;
203  }
204 
205  [pool release];
206 }
static void allKeys(HKEY parentHandle, const QString &rSubKey, NameSet *result)
double d
Definition: qnumeric_p.h:62
void setSupported(QFontDatabase::WritingSystem, bool supported=true)
Sets the supported state of the writing system given by writingSystem to the value specified by suppo...
const struct __CFString * CFStringRef
static NSInteger languageMapSort(id obj1, id obj2, void *context)
static QString toQString(CFStringRef cfstr)
Definition: qcore_mac.cpp:47
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
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
static float pixelSize(const QFontDef &request, int dpi)
Definition: qfont_win.cpp:80
static bool init
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
The QSupportedWritingSystems class is used when registering fonts with the internal Qt fontdatabase...
const char * styleHint(const QFontDef &request)
Style
This enum describes the different styles of glyphs that are used to display text. ...
Definition: qfont.h:111
static QString familyNameFromPostScriptName(QHash< QString, QString > *psNameToFamily, NSString *psName)
StyleHint
Style hints are used by the font matching algorithm to find an appropriate default family if a select...
Definition: qfont.h:69
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...
QHash< QFont::StyleHint, QStringList > fallbackLists
static QFont::StyleHint styleHintFromNSString(NSString *style)

Properties

◆ fallbackLists

QHash<QFont::StyleHint, QStringList> QCoreTextFontDatabase::fallbackLists
private

Definition at line 61 of file qcoretextfontdatabase.h.

Referenced by fallbacksForFamily(), and populateFontDatabase().


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