📄 qfontdatabase.cpp
字号:
characters in a writing system with writingSystemSample(). Example: \quotefromfile snippets/qfontdatabase/main.cpp \skipto QFontDatabase database; \printuntil } \printuntil } This example gets the list of font families, the list of styles for each family, and the point sizes that are available for each combination of family and style, displaying this information in a tree view. \sa QFont, QFontInfo, QFontMetrics, QFontComboBox, {Character Map Example}*//*! Creates a font database object.*/QFontDatabase::QFontDatabase(){ createDatabase(); d = privateDb();}/*! \enum QFontDatabase::WritingSystem \value Any \value Latin \value Greek \value Cyrillic \value Armenian \value Hebrew \value Arabic \value Syriac \value Thaana \value Devanagari \value Bengali \value Gurmukhi \value Gujarati \value Oriya \value Tamil \value Telugu \value Kannada \value Malayalam \value Sinhala \value Thai \value Lao \value Tibetan \value Myanmar \value Georgian \value Khmer \value SimplifiedChinese \value TraditionalChinese \value Japanese \value Korean \value Vietnamese \value Symbol \value Other (the same as Symbol) \value Ogham \value Runic \omitvalue WritingSystemsCount*//*! Returns a sorted list of the available writing systems. This is list generated from information about all installed fonts on the system. \sa families()*/QList<QFontDatabase::WritingSystem> QFontDatabase::writingSystems() const{ ::load();#ifdef Q_WS_X11 ::checkSymbolFonts();#endif QList<WritingSystem> list; for (int i = 0; i < d->count; ++i) { QtFontFamily *family = d->families[i]; if (family->count == 0) continue; for (int x = Latin; x < WritingSystemsCount; ++x) { const WritingSystem writingSystem = WritingSystem(x); if (!(family->writingSystems[writingSystem] & QtFontFamily::Supported)) continue; if (!list.contains(writingSystem)) list.append(writingSystem); } } qSort(list); return list;}/*! Returns a sorted list of the writing systems supported by a given font \a family. \sa families()*/QList<QFontDatabase::WritingSystem> QFontDatabase::writingSystems(const QString &family) const{ QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load();#ifdef Q_WS_X11 ::checkSymbolFonts(familyName);#endif QList<WritingSystem> list; QtFontFamily *f = d->family(familyName); if (!f || f->count == 0) return list; for (int x = Latin; x < WritingSystemsCount; ++x) { const WritingSystem writingSystem = WritingSystem(x); if (f->writingSystems[writingSystem] & QtFontFamily::Supported) list.append(writingSystem); } return list;}/*! Returns a sorted list of the available font families which support the \a writingSystem. If a family exists in several foundries, the returned name for that font is in the form "family [foundry]". Examples: "Times [Adobe]", "Times [Cronyx]", "Palatino". \sa writingSystems()*/QStringList QFontDatabase::families(WritingSystem writingSystem) const{ ::load();#ifdef Q_WS_X11 if (writingSystem != Any) ::checkSymbolFonts();#endif QStringList flist; for (int i = 0; i < d->count; i++) { QtFontFamily *f = d->families[i]; if (f->count == 0) continue; if (writingSystem != Any && (f->writingSystems[writingSystem] != QtFontFamily::Supported)) continue; if (f->count == 1) { flist.append(f->name); } else { for (int j = 0; j < f->count; j++) { QString str = f->name; QString foundry = f->foundries[j]->name; if (!foundry.isEmpty()) { str += QLatin1String(" ["); str += foundry; str += QLatin1String("]"); } flist.append(str); } } } return flist;}/*! Returns a list of the styles available for the font family \a family. Some example styles: "Light", "Light Italic", "Bold", "Oblique", "Demi". The list may be empty. \sa families()*/QStringList QFontDatabase::styles(const QString &family) const{ QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QStringList l; QtFontFamily *f = d->family(familyName); if (!f) return l; QtFontFoundry allStyles(foundryName); for (int j = 0; j < f->count; j++) { QtFontFoundry *foundry = f->foundries[j]; if (foundryName.isEmpty() || foundry->name.compare(foundryName, Qt::CaseInsensitive) == 0) { for (int k = 0; k < foundry->count; k++) { QtFontStyle::Key ke(foundry->styles[k]->key); ke.stretch = 0; allStyles.style(ke, true); } } } for (int i = 0; i < allStyles.count; i++) l.append(::styleString(allStyles.styles[i]->key.weight, (QFont::Style)allStyles.styles[i]->key.style)); return l;}/*! Returns true if the font that has family \a family and style \a style is fixed pitch; otherwise returns false.*/bool QFontDatabase::isFixedPitch(const QString &family, const QString &style) const{ Q_UNUSED(style); QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QtFontFamily *f = d->family(familyName);#if !defined(QWS) && defined(Q_OS_MAC) qt_mac_get_fixed_pitch(f);#endif return (f && f->fixedPitch);}/*! Returns true if the font that has family \a family and style \a style is a scalable bitmap font; otherwise returns false. Scaling a bitmap font usually produces an unattractive hardly readable result, because the pixels of the font are scaled. If you need to scale a bitmap font it is better to scale it to one of the fixed sizes returned by smoothSizes(). \sa isScalable(), isSmoothlyScalable()*/bool QFontDatabase::isBitmapScalable(const QString &family, const QString &style) const{ bool bitmapScalable = false; QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QtFontStyle::Key styleKey(style); QtFontFamily *f = d->family(familyName); if (!f) return bitmapScalable; for (int j = 0; j < f->count; j++) { QtFontFoundry *foundry = f->foundries[j]; if (foundryName.isEmpty() || foundry->name.compare(foundryName, Qt::CaseInsensitive) == 0) { for (int k = 0; k < foundry->count; k++) if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && foundry->styles[k]->bitmapScalable && !foundry->styles[k]->smoothScalable) { bitmapScalable = true; goto end; } } } end: return bitmapScalable;}/*! Returns true if the font that has family \a family and style \a style is smoothly scalable; otherwise returns false. If this function returns true, it's safe to scale this font to any size, and the result will always look attractive. \sa isScalable(), isBitmapScalable()*/bool QFontDatabase::isSmoothlyScalable(const QString &family, const QString &style) const{ bool smoothScalable = false; QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QtFontStyle::Key styleKey(style); QtFontFamily *f = d->family(familyName); if (!f) return smoothScalable; for (int j = 0; j < f->count; j++) { QtFontFoundry *foundry = f->foundries[j]; if (foundryName.isEmpty() || foundry->name.compare(foundryName, Qt::CaseInsensitive) == 0) { for (int k = 0; k < foundry->count; k++) if ((style.isEmpty() || foundry->styles[k]->key == styleKey) && foundry->styles[k]->smoothScalable) { smoothScalable = true; goto end; } } } end: return smoothScalable;}/*! Returns true if the font that has family \a family and style \a style is scalable; otherwise returns false. \sa isBitmapScalable(), isSmoothlyScalable()*/bool QFontDatabase::isScalable(const QString &family, const QString &style) const{ if (isSmoothlyScalable(family, style)) return true; return isBitmapScalable(family, style);}/*! Returns a list of the point sizes available for the font that has family \a family and style \a style. The list may be empty. \sa smoothSizes(), standardSizes()*/QList<int> QFontDatabase::pointSizes(const QString &family, const QString &style){#if defined(Q_WS_WIN) // windows and macosx are always smoothly scalable Q_UNUSED(family); Q_UNUSED(style); return standardSizes();#else bool smoothScalable = false; QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QtFontStyle::Key styleKey(style); QList<int> sizes; QtFontFamily *fam = d->family(familyName); if (!fam) return sizes;#ifdef Q_WS_X11 int dpi = QX11Info::appDpiY();#else const int dpi = qt_defaultDpiY(); // embedded#endif for (int j = 0; j < fam->count; j++) { QtFontFoundry *foundry = fam->foundries[j]; if (foundryName.isEmpty() || foundry->name.compare(foundryName, Qt::CaseInsensitive) == 0) { QtFontStyle *style = foundry->style(styleKey); if (!style) continue; if (style->smoothScalable) { smoothScalable = true; goto end; } for (int l = 0; l < style->count; l++) { const QtFontSize *size = style->pixelSizes + l; if (size->pixelSize != 0 && size->pixelSize != USHRT_MAX) { const uint pointSize = qRound(size->pixelSize * 72.0 / dpi); if (! sizes.contains(pointSize)) sizes.append(pointSize); } } } } end: if (smoothScalable) return standardSizes(); qSort(sizes); return sizes;#endif}/*! Returns a QFont object that has family \a family, style \a style and point size \a pointSize. If no matching font could be created, a QFont object that uses the application's default font is returned.*/QFont QFontDatabase::font(const QString &family, const QString &style, int pointSize) const{ QString familyName, foundryName; parseFontName(family, foundryName, familyName); ::load(familyName); QtFontFoundry allStyles(foundryName); QtFontFamily *f = d->family(familyName); if (!f) return QApplication::font(); for (int j = 0; j < f->count; j++) { QtFontFoundry *foundry = f->foundries[j]; if (foundryName.isEmpty() || foundry->name.compare(foundryName, Qt::CaseInsensitive) == 0) { for (int k = 0; k < foundry->count; k++) allStyles.style(foundry->styles[k]->key, true); } } QtFontStyle::Key styleKey(style); QtFontStyle *s = bestStyle(&allStyles, styleKey); if (!s) // no styles found? return QApplication::font(); QFont fnt(family, pointSize, s->key.weight); fnt.setStyle((QFont::Style)s->key.style); return fnt;}/*! Returns the point sizes of a font that has family \a family and style \a style that will look attractive. The list may be empty. For non-scalable fonts and bitmap scalable fonts, this function is equivalent to pointSizes(). \sa pointSizes(), standardSizes()*/QList<int> QFontDatabase::smoothSizes(const QString &family, const QString &style){#ifdef Q_WS_WIN Q_UNUSED(family); Q_UNUSED(style); return QFontDatabase::standardSizes();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -