1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <qplatformdefs.h>
45 #include <qpaintdevice.h>
46 #include <qelapsedtimer.h>
48 #include <private/qt_x11_p.h>
49 #include "qx11info_x11.h"
52 #include <qtemporaryfile.h>
53 #include <qabstractfileengine.h>
59 #include <sys/types.h>
64 #include <private/qfontengine_x11_p.h>
66 #ifndef QT_NO_FONTCONFIG
68 #include FT_FREETYPE_H
70 #if FC_VERSION >= 20402
71 #include <fontconfig/fcfreetype.h>
78 extern double qt_pointSize(double pixelSize, int dpi);
79 extern double qt_pixelSize(double pointSize, int dpi);
81 // from qapplication.cpp
82 extern bool qt_is_gui_used;
84 static inline void capitalize (char *s)
97 To regenerate the writingSystems_for_xlfd_encoding table, run
98 'util/unicode/x11/makeencodings' and paste the generated
101 // ----- begin of generated code -----
103 #define make_tag( c1, c2, c3, c4 ) \
104 ((((unsigned int)c1)<<24) | (((unsigned int)c2)<<16) | \
105 (((unsigned int)c3)<<8) | ((unsigned int)c4))
107 struct XlfdEncoding {
115 static const XlfdEncoding xlfd_encoding[] = {
116 { "iso8859-1", 0, 4, make_tag('i','s','o','8'), make_tag('5','9','-','1') },
117 { "iso8859-2", 1, 5, make_tag('i','s','o','8'), make_tag('5','9','-','2') },
118 { "iso8859-3", 2, 6, make_tag('i','s','o','8'), make_tag('5','9','-','3') },
119 { "iso8859-4", 3, 7, make_tag('i','s','o','8'), make_tag('5','9','-','4') },
120 { "iso8859-9", 4, 12, make_tag('i','s','o','8'), make_tag('5','9','-','9') },
121 { "iso8859-10", 5, 13, make_tag('i','s','o','8'), make_tag('9','-','1','0') },
122 { "iso8859-13", 6, 109, make_tag('i','s','o','8'), make_tag('9','-','1','3') },
123 { "iso8859-14", 7, 110, make_tag('i','s','o','8'), make_tag('9','-','1','4') },
124 { "iso8859-15", 8, 111, make_tag('i','s','o','8'), make_tag('9','-','1','5') },
125 { "hp-roman8", 9, 2004, make_tag('h','p','-','r'), make_tag('m','a','n','8') },
126 { "iso8859-5", 10, 8, make_tag('i','s','o','8'), make_tag('5','9','-','5') },
127 { "*-cp1251", 11, 2251, 0, make_tag('1','2','5','1') },
128 { "koi8-ru", 12, 2084, make_tag('k','o','i','8'), make_tag('8','-','r','u') },
129 { "koi8-u", 13, 2088, make_tag('k','o','i','8'), make_tag('i','8','-','u') },
130 { "koi8-r", 14, 2084, make_tag('k','o','i','8'), make_tag('i','8','-','r') },
131 { "iso8859-7", 15, 10, make_tag('i','s','o','8'), make_tag('5','9','-','7') },
132 { "iso8859-8", 16, 85, make_tag('i','s','o','8'), make_tag('5','9','-','8') },
133 { "gb18030-0", 17, -114, make_tag('g','b','1','8'), make_tag('3','0','-','0') },
134 { "gb18030.2000-0", 18, -113, make_tag('g','b','1','8'), make_tag('0','0','-','0') },
135 { "gbk-0", 19, -113, make_tag('g','b','k','-'), make_tag('b','k','-','0') },
136 { "gb2312.*-0", 20, 57, make_tag('g','b','2','3'), 0 },
137 { "jisx0201*-0", 21, 15, make_tag('j','i','s','x'), 0 },
138 { "jisx0208*-0", 22, 63, make_tag('j','i','s','x'), 0 },
139 { "ksc5601*-*", 23, 36, make_tag('k','s','c','5'), 0 },
140 { "big5hkscs-0", 24, -2101, make_tag('b','i','g','5'), make_tag('c','s','-','0') },
141 { "hkscs-1", 25, -2101, make_tag('h','k','s','c'), make_tag('c','s','-','1') },
142 { "big5*-*", 26, -2026, make_tag('b','i','g','5'), 0 },
143 { "tscii-*", 27, 2028, make_tag('t','s','c','i'), 0 },
144 { "tis620*-*", 28, 2259, make_tag('t','i','s','6'), 0 },
145 { "iso8859-11", 29, 2259, make_tag('i','s','o','8'), make_tag('9','-','1','1') },
146 { "mulelao-1", 30, -4242, make_tag('m','u','l','e'), make_tag('a','o','-','1') },
147 { "ethiopic-unicode", 31, 0, make_tag('e','t','h','i'), make_tag('c','o','d','e') },
148 { "iso10646-1", 32, 0, make_tag('i','s','o','1'), make_tag('4','6','-','1') },
149 { "unicode-*", 33, 0, make_tag('u','n','i','c'), 0 },
150 { "*-symbol", 34, 0, 0, make_tag('m','b','o','l') },
151 { "*-fontspecific", 35, 0, 0, make_tag('i','f','i','c') },
152 { "fontspecific-*", 36, 0, make_tag('f','o','n','t'), 0 },
156 static const char writingSystems_for_xlfd_encoding[sizeof(xlfd_encoding)][QFontDatabase::WritingSystemsCount] = {
158 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
159 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
163 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
164 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
165 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
168 { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
169 0, 0, 0, 0, 0,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, 1, 0, 0, 0,
328 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
329 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
330 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
333 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
334 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
335 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
338 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
339 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
340 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
345 // ----- end of generated code -----
348 const int numEncodings = sizeof(xlfd_encoding) / sizeof(XlfdEncoding) - 1;
350 int qt_xlfd_encoding_id(const char *encoding)
352 // qDebug("looking for encoding id for '%s'", encoding);
353 int len = strlen(encoding);
356 unsigned int hash1 = make_tag(encoding[0], encoding[1], encoding[2], encoding[3]);
357 const char *ch = encoding + len - 4;
358 unsigned int hash2 = make_tag(ch[0], ch[1], ch[2], ch[3]);
360 const XlfdEncoding *enc = xlfd_encoding;
361 for (; enc->name; ++enc) {
362 if ((enc->hash1 && enc->hash1 != hash1) ||
363 (enc->hash2 && enc->hash2 != hash2))
365 // hashes match, do a compare if strings match
366 // the enc->name can contain '*'s we have to interpret correctly
367 const char *n = enc->name;
368 const char *e = encoding;
370 // qDebug("bol: *e='%c', *n='%c'", *e, *n);
374 // qDebug("found encoding id %d", enc->id);
385 // qDebug("skip: *e='%c', *n='%c'", *e, *n);
386 while (*e && *e != *n)
390 // qDebug("couldn't find encoding %s", encoding);
394 int qt_mib_for_xlfd_encoding(const char *encoding)
396 int id = qt_xlfd_encoding_id(encoding);
397 if (id != -1) return xlfd_encoding[id].mib;
401 int qt_encoding_id_for_mib(int mib)
403 const XlfdEncoding *enc = xlfd_encoding;
404 for (; enc->name; ++enc) {
411 static const char * xlfd_for_id(int id)
413 // special case: -1 returns the "*-*" encoding, allowing us to do full
414 // database population in a single X server round trip.
415 if (id < 0 || id > numEncodings)
417 return xlfd_encoding[id].name;
420 enum XLFDFieldNames {
438 // Splits an X font name into fields separated by '-'
439 static bool parseXFontName(char *fontName, char **tokens)
441 if (! fontName || fontName[0] == '0' || fontName[0] != '-') {
448 for (i = 0; i < NFontFields && fontName && fontName[0]; ++i) {
449 tokens[i] = fontName;
450 for (;; ++fontName) {
451 if (*fontName == '-')
459 if (fontName) *fontName++ = '\0';
462 if (i < NFontFields) {
463 for (int j = i ; j < NFontFields; ++j)
471 static inline bool isZero(char *x)
473 return (x[0] == '0' && x[1] == 0);
476 static inline bool isScalable(char **tokens)
478 return (isZero(tokens[PixelSize]) &&
479 isZero(tokens[PointSize]) &&
480 isZero(tokens[AverageWidth]));
483 static inline bool isSmoothlyScalable(char **tokens)
485 return (isZero(tokens[ResolutionX]) &&
486 isZero(tokens[ResolutionY]));
489 static inline bool isFixedPitch(char **tokens)
491 return (tokens[Spacing][0] == 'm' ||
492 tokens[Spacing][0] == 'c' ||
493 tokens[Spacing][0] == 'M' ||
494 tokens[Spacing][0] == 'C');
498 Fills in a font definition (QFontDef) from an XLFD (X Logical Font
501 Returns true if the given xlfd is valid.
503 bool qt_fillFontDef(const QByteArray &xlfd, QFontDef *fd, int dpi, QtFontDesc *desc)
505 char *tokens[NFontFields];
506 QByteArray buffer = xlfd;
507 if (! parseXFontName(buffer.data(), tokens))
510 capitalize(tokens[Family]);
511 capitalize(tokens[Foundry]);
513 fd->styleStrategy |= QFont::NoAntialias;
514 fd->family = QString::fromLatin1(tokens[Family]);
515 QString foundry = QString::fromLatin1(tokens[Foundry]);
516 if (! foundry.isEmpty() && foundry != QLatin1String("*") && (!desc || desc->family->count > 1))
518 QLatin1String(" [") + foundry + QLatin1Char(']');
520 if (qstrlen(tokens[AddStyle]) > 0)
521 fd->addStyle = QString::fromLatin1(tokens[AddStyle]);
523 fd->addStyle.clear();
525 fd->pointSize = atoi(tokens[PointSize])/10.;
526 fd->styleHint = QFont::AnyStyle; // ### any until we match families
528 char slant = tolower((uchar) tokens[Slant][0]);
529 fd->style = (slant == 'o' ? QFont::StyleOblique : (slant == 'i' ? QFont::StyleItalic : QFont::StyleNormal));
530 char fixed = tolower((uchar) tokens[Spacing][0]);
531 fd->fixedPitch = (fixed == 'm' || fixed == 'c');
532 fd->weight = getFontWeight(QLatin1String(tokens[Weight]));
534 int r = atoi(tokens[ResolutionY]);
535 fd->pixelSize = atoi(tokens[PixelSize]);
536 // not "0" or "*", or required DPI
537 if (r && fd->pixelSize && r != dpi) {
538 // calculate actual pointsize for display DPI
539 fd->pointSize = qt_pointSize(fd->pixelSize, dpi);
540 } else if (fd->pixelSize == 0 && fd->pointSize) {
541 // calculate pixel size from pointsize/dpi
542 fd->pixelSize = qRound(qt_pixelSize(fd->pointSize, dpi));
549 Fills in a font definition (QFontDef) from the font properties in an
552 Returns true if the QFontDef could be filled with properties from
555 static bool qt_fillFontDef(XFontStruct *fs, QFontDef *fd, int dpi, QtFontDesc *desc)
558 if (!fs || !XGetFontProperty(fs, XA_FONT, &value))
561 char *n = XGetAtomName(QX11Info::display(), value);
565 return qt_fillFontDef(xlfd.toLower(), fd, dpi, desc);
569 static QtFontStyle::Key getStyle(char ** tokens)
571 QtFontStyle::Key key;
573 char slant0 = tolower((uchar) tokens[Slant][0]);
576 if (tokens[Slant][1]) {
577 char slant1 = tolower((uchar) tokens[Slant][1]);
580 key.style = QFont::StyleOblique;
581 else if (slant1 == 'i')
582 key.style = QFont::StyleItalic;
584 } else if (slant0 == 'o')
585 key.style = QFont::StyleOblique;
586 else if (slant0 == 'i')
587 key.style = QFont::StyleItalic;
589 key.weight = getFontWeight(QLatin1String(tokens[Weight]));
591 if (qstrcmp(tokens[Width], "normal") == 0) {
593 } else if (qstrcmp(tokens[Width], "semi condensed") == 0 ||
594 qstrcmp(tokens[Width], "semicondensed") == 0) {
596 } else if (qstrcmp(tokens[Width], "condensed") == 0) {
598 } else if (qstrcmp(tokens[Width], "narrow") == 0) {
606 static bool xlfdsFullyLoaded = false;
607 static unsigned char encodingLoaded[numEncodings];
609 static void loadXlfds(const char *reqFamily, int encoding_id)
611 QFontDatabasePrivate *db = privateDb();
612 QtFontFamily *fontFamily = reqFamily ? db->family(QLatin1String(reqFamily)) : 0;
614 // make sure we don't load twice
615 if ((encoding_id == -1 && xlfdsFullyLoaded)
616 || (encoding_id != -1 && encodingLoaded[encoding_id]))
618 if (fontFamily && fontFamily->xlfdLoaded)
622 // force the X server to give us XLFDs
623 QByteArray xlfd_pattern("-*-");
624 xlfd_pattern += (reqFamily && reqFamily[0] != '\0') ? reqFamily : "*";
625 xlfd_pattern += "-*-*-*-*-*-*-*-*-*-*-";
626 xlfd_pattern += xlfd_for_id(encoding_id);
628 char **fontList = XListFonts(QX11Info::display(),
631 // qDebug("requesting xlfd='%s', got %d fonts", xlfd_pattern.data(), fontCount);
634 char *tokens[NFontFields];
636 for(int i = 0 ; i < fontCount ; i++) {
637 if (! parseXFontName(fontList[i], tokens))
640 // get the encoding_id for this xlfd. we need to do this
641 // here, since we can pass -1 to this function to do full
642 // database population
643 *(tokens[CharsetEncoding] - 1) = '-';
644 int encoding_id = qt_xlfd_encoding_id(tokens[CharsetRegistry]);
645 if (encoding_id == -1)
648 char *familyName = tokens[Family];
649 capitalize(familyName);
650 char *foundryName = tokens[Foundry];
651 capitalize(foundryName);
652 QtFontStyle::Key styleKey = getStyle(tokens);
654 bool smooth_scalable = false;
655 bool bitmap_scalable = false;
656 if (isScalable(tokens)) {
657 if (isSmoothlyScalable(tokens))
658 smooth_scalable = true;
660 bitmap_scalable = true;
662 uint pixelSize = atoi(tokens[PixelSize]);
663 uint xpointSize = atoi(tokens[PointSize]);
664 uint xres = atoi(tokens[ResolutionX]);
665 uint yres = atoi(tokens[ResolutionY]);
666 uint avgwidth = atoi(tokens[AverageWidth]);
667 bool fixedPitch = isFixedPitch(tokens);
669 if (avgwidth == 0 && pixelSize != 0) {
671 Ignore bitmap scalable fonts that are automatically
672 generated by some X servers. We know they are bitmap
673 scalable because even though they have a specified pixel
674 size, the average width is zero.
679 QtFontFamily *family = fontFamily ? fontFamily : db->family(QLatin1String(familyName), true);
680 family->fontFileIndex = -1;
681 family->symbol_checked = true;
682 QtFontFoundry *foundry = family->foundry(QLatin1String(foundryName), true);
683 QtFontStyle *style = foundry->style(styleKey, true);
685 delete [] style->weightName;
686 style->weightName = qstrdup(tokens[Weight]);
687 delete [] style->setwidthName;
688 style->setwidthName = qstrdup(tokens[Width]);
690 if (smooth_scalable) {
691 style->smoothScalable = true;
692 style->bitmapScalable = false;
693 pixelSize = SMOOTH_SCALABLE;
695 if (!style->smoothScalable && bitmap_scalable)
696 style->bitmapScalable = true;
698 family->fixedPitch = false;
700 QtFontSize *size = style->pixelSize(pixelSize, true);
701 QtFontEncoding *enc =
702 size->encodingID(encoding_id, xpointSize, xres, yres, avgwidth, true);
703 enc->pitch = *tokens[Spacing];
704 if (!enc->pitch) enc->pitch = '*';
706 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
707 if (writingSystems_for_xlfd_encoding[encoding_id][i])
708 family->writingSystems[i] = QtFontFamily::Supported;
712 // mark encoding as loaded
713 if (encoding_id == -1)
714 xlfdsFullyLoaded = true;
716 encodingLoaded[encoding_id] = true;
719 XFreeFontNames(fontList);
723 #ifndef QT_NO_FONTCONFIG
726 #define FC_WIDTH "width"
729 static int getFCWeight(int fc_weight)
731 int qtweight = QFont::Black;
732 if (fc_weight <= (FC_WEIGHT_LIGHT + FC_WEIGHT_MEDIUM) / 2)
733 qtweight = QFont::Light;
734 else if (fc_weight <= (FC_WEIGHT_MEDIUM + FC_WEIGHT_DEMIBOLD) / 2)
735 qtweight = QFont::Normal;
736 else if (fc_weight <= (FC_WEIGHT_DEMIBOLD + FC_WEIGHT_BOLD) / 2)
737 qtweight = QFont::DemiBold;
738 else if (fc_weight <= (FC_WEIGHT_BOLD + FC_WEIGHT_BLACK) / 2)
739 qtweight = QFont::Bold;
744 QFontDef qt_FcPatternToQFontDef(FcPattern *pattern, const QFontDef &request)
747 fontDef.styleStrategy = request.styleStrategy;
749 fontDef.hintingPreference = request.hintingPreference;
751 if (FcPatternGetString(pattern, FC_FAMILY, 0, &value) == FcResultMatch) {
752 fontDef.family = QString::fromUtf8(reinterpret_cast<const char *>(value));
756 if (FcPatternGetDouble(pattern, FC_DPI, 0, &dpi) != FcResultMatch) {
758 dpi = QX11Info::appDpiY();
760 dpi = qt_defaultDpiY();
764 if (FcPatternGetDouble(pattern, FC_PIXEL_SIZE, 0, &size) == FcResultMatch)
765 fontDef.pixelSize = size;
767 fontDef.pixelSize = 12;
769 fontDef.pointSize = qt_pointSize(fontDef.pixelSize, qRound(dpi));
776 if (FcPatternGetInteger(pattern, FC_WEIGHT, 0, &weight) != FcResultMatch)
777 weight = FC_WEIGHT_MEDIUM;
778 fontDef.weight = getFCWeight(weight);
781 if (FcPatternGetInteger(pattern, FC_SLANT, 0, &slant) != FcResultMatch)
782 slant = FC_SLANT_ROMAN;
783 fontDef.style = (slant == FC_SLANT_ITALIC)
785 : ((slant == FC_SLANT_OBLIQUE)
786 ? QFont::StyleOblique
787 : QFont::StyleNormal);
791 if (FcPatternGetBool(pattern, FC_SCALABLE, 0, &scalable) != FcResultMatch)
794 fontDef.stretch = request.stretch;
795 fontDef.style = request.style;
798 if (FcPatternGetInteger(pattern, FC_WIDTH, 0, &width) == FcResultMatch)
799 fontDef.stretch = width;
801 fontDef.stretch = 100;
805 if (FcPatternGetInteger(pattern, FC_SPACING, 0, &spacing) == FcResultMatch) {
806 fontDef.fixedPitch = (spacing >= FC_MONO);
807 fontDef.ignorePitch = false;
809 fontDef.ignorePitch = true;
815 static const char *specialLanguages[] = {
845 enum { SpecialLanguageCount = sizeof(specialLanguages) / sizeof(const char *) };
847 static const ushort specialChars[] = {
877 enum { SpecialCharCount = sizeof(specialChars) / sizeof(ushort) };
879 // this could become a list of all languages used for each writing
880 // system, instead of using the single most common language.
881 static const char *languageForWritingSystem[] = {
907 "zh-cn", // SimplifiedChinese
908 "zh-tw", // TraditionalChinese
917 enum { LanguageCount = sizeof(languageForWritingSystem) / sizeof(const char *) };
919 // Unfortunately FontConfig doesn't know about some languages. We have to test these through the
920 // charset. The lists below contain the systems where we need to do this.
921 static const ushort sampleCharForWritingSystem[] = {
947 0, // SimplifiedChinese
948 0, // TraditionalChinese
957 enum { SampleCharCount = sizeof(sampleCharForWritingSystem) / sizeof(ushort) };
959 // Newer FontConfig let's us sort out fonts that contain certain glyphs, but no
960 // open type tables for is directly. Do this so we don't pick some strange
961 // pseudo unicode font
962 static const char *openType[] = {
972 "deva", // Devanagari
988 0, // SimplifiedChinese
989 0, // TraditionalChinese
998 enum { OpenTypeCount = sizeof(openType) / sizeof(const char *) };
1001 static void loadFontConfig()
1003 Q_ASSERT_X(X11, "QFontDatabase",
1004 "A QApplication object needs to be constructed before FontConfig is used.");
1005 if (!X11->has_fontconfig)
1008 Q_ASSERT_X(int(QUnicodeTables::ScriptCount) == SpecialLanguageCount,
1009 "QFontDatabase", "New scripts have been added.");
1010 Q_ASSERT_X(int(QUnicodeTables::ScriptCount) == SpecialCharCount,
1011 "QFontDatabase", "New scripts have been added.");
1012 Q_ASSERT_X(int(QFontDatabase::WritingSystemsCount) == LanguageCount,
1013 "QFontDatabase", "New writing systems have been added.");
1014 Q_ASSERT_X(int(QFontDatabase::WritingSystemsCount) == SampleCharCount,
1015 "QFontDatabase", "New writing systems have been added.");
1016 Q_ASSERT_X(int(QFontDatabase::WritingSystemsCount) == OpenTypeCount,
1017 "QFontDatabase", "New writing systems have been added.");
1019 QFontDatabasePrivate *db = privateDb();
1022 FcPattern *pattern = FcPatternCreate();
1023 FcDefaultSubstitute(pattern);
1025 if (FcPatternGetString(pattern, FC_LANG, 0, &lang) == FcResultMatch)
1026 db->systemLang = QString::fromUtf8((const char *) lang);
1027 FcPatternDestroy(pattern);
1034 FcChar8 *file_value;
1036 FcChar8 *foundry_value;
1040 FcObjectSet *os = FcObjectSetCreate();
1041 FcPattern *pattern = FcPatternCreate();
1042 const char *properties [] = {
1043 FC_FAMILY, FC_WEIGHT, FC_SLANT,
1044 FC_SPACING, FC_FILE, FC_INDEX,
1045 FC_LANG, FC_CHARSET, FC_FOUNDRY, FC_SCALABLE, FC_PIXEL_SIZE, FC_WEIGHT,
1047 #if FC_VERSION >= 20297
1052 const char **p = properties;
1054 FcObjectSetAdd(os, *p);
1057 fonts = FcFontList(0, pattern, os);
1058 FcObjectSetDestroy(os);
1059 FcPatternDestroy(pattern);
1062 for (int i = 0; i < fonts->nfont; i++) {
1063 if (FcPatternGetString(fonts->fonts[i], FC_FAMILY, 0, &value) != FcResultMatch)
1065 // capitalize(value);
1066 familyName = QString::fromUtf8((const char *)value);
1067 slant_value = FC_SLANT_ROMAN;
1068 weight_value = FC_WEIGHT_MEDIUM;
1069 spacing_value = FC_PROPORTIONAL;
1074 if (FcPatternGetInteger (fonts->fonts[i], FC_SLANT, 0, &slant_value) != FcResultMatch)
1075 slant_value = FC_SLANT_ROMAN;
1076 if (FcPatternGetInteger (fonts->fonts[i], FC_WEIGHT, 0, &weight_value) != FcResultMatch)
1077 weight_value = FC_WEIGHT_MEDIUM;
1078 if (FcPatternGetInteger (fonts->fonts[i], FC_SPACING, 0, &spacing_value) != FcResultMatch)
1079 spacing_value = FC_PROPORTIONAL;
1080 if (FcPatternGetString (fonts->fonts[i], FC_FILE, 0, &file_value) != FcResultMatch)
1082 if (FcPatternGetInteger (fonts->fonts[i], FC_INDEX, 0, &index_value) != FcResultMatch)
1084 if (FcPatternGetBool(fonts->fonts[i], FC_SCALABLE, 0, &scalable) != FcResultMatch)
1086 if (FcPatternGetString(fonts->fonts[i], FC_FOUNDRY, 0, &foundry_value) != FcResultMatch)
1088 QtFontFamily *family = db->family(familyName, true);
1090 FcLangSet *langset = 0;
1091 FcResult res = FcPatternGetLangSet(fonts->fonts[i], FC_LANG, 0, &langset);
1092 if (res == FcResultMatch) {
1093 for (int i = 1; i < LanguageCount; ++i) {
1094 const FcChar8 *lang = (const FcChar8*) languageForWritingSystem[i];
1096 family->writingSystems[i] |= QtFontFamily::UnsupportedFT;
1098 FcLangResult langRes = FcLangSetHasLang(langset, lang);
1099 if (langRes != FcLangDifferentLang)
1100 family->writingSystems[i] = QtFontFamily::Supported;
1102 family->writingSystems[i] |= QtFontFamily::UnsupportedFT;
1105 family->writingSystems[QFontDatabase::Other] = QtFontFamily::UnsupportedFT;
1106 family->ftWritingSystemCheck = true;
1108 // we set Other to supported for symbol fonts. It makes no
1109 // sense to merge these with other ones, as they are
1110 // special in a way.
1111 for (int i = 1; i < LanguageCount; ++i)
1112 family->writingSystems[i] |= QtFontFamily::UnsupportedFT;
1113 family->writingSystems[QFontDatabase::Other] = QtFontFamily::Supported;
1117 res = FcPatternGetCharSet(fonts->fonts[i], FC_CHARSET, 0, &cs);
1118 if (res == FcResultMatch) {
1119 // some languages are not supported by FontConfig, we rather check the
1120 // charset to detect these
1121 for (int i = 1; i < SampleCharCount; ++i) {
1122 if (!sampleCharForWritingSystem[i])
1124 if (FcCharSetHasChar(cs, sampleCharForWritingSystem[i]))
1125 family->writingSystems[i] = QtFontFamily::Supported;
1129 #if FC_VERSION >= 20297
1130 for (int j = 1; j < LanguageCount; ++j) {
1131 if (family->writingSystems[j] == QtFontFamily::Supported && requiresOpenType(j) && openType[j]) {
1133 res = FcPatternGetString (fonts->fonts[i], FC_CAPABILITY, 0, &cap);
1134 if (res != FcResultMatch || !strstr((const char *)cap, openType[j]))
1135 family->writingSystems[j] = QtFontFamily::UnsupportedFT;
1140 QByteArray file((const char *)file_value);
1141 family->fontFilename = file;
1142 family->fontFileIndex = index_value;
1144 QtFontStyle::Key styleKey;
1145 styleKey.style = (slant_value == FC_SLANT_ITALIC)
1146 ? QFont::StyleItalic
1147 : ((slant_value == FC_SLANT_OBLIQUE)
1148 ? QFont::StyleOblique
1149 : QFont::StyleNormal);
1150 styleKey.weight = getFCWeight(weight_value);
1153 FcPatternGetInteger (fonts->fonts[i], FC_WIDTH, 0, &width);
1154 styleKey.stretch = width;
1157 QtFontFoundry *foundry
1158 = family->foundry(foundry_value ? QString::fromUtf8((const char *)foundry_value) : QString(), true);
1159 QtFontStyle *style = foundry->style(styleKey, true);
1161 if (spacing_value < FC_MONO)
1162 family->fixedPitch = false;
1166 style->smoothScalable = true;
1167 size = style->pixelSize(SMOOTH_SCALABLE, true);
1169 double pixel_size = 0;
1170 FcPatternGetDouble (fonts->fonts[i], FC_PIXEL_SIZE, 0, &pixel_size);
1171 size = style->pixelSize((int)pixel_size, true);
1173 QtFontEncoding *enc = size->encodingID(-1, 0, 0, 0, 0, true);
1174 enc->pitch = (spacing_value >= FC_CHARCELL ? 'c' :
1175 (spacing_value >= FC_MONO ? 'm' : 'p'));
1178 FcFontSetDestroy (fonts);
1180 struct FcDefaultFont {
1182 const char *rawname;
1185 const FcDefaultFont defaults[] = {
1186 { "Serif", "serif", false },
1187 { "Sans Serif", "sans-serif", false },
1188 { "Monospace", "monospace", true },
1191 const FcDefaultFont *f = defaults;
1193 QtFontFamily *family = db->family(QLatin1String(f->qtname), true);
1194 family->fixedPitch = f->fixed;
1195 family->synthetic = true;
1196 QtFontFoundry *foundry = family->foundry(QString(), true);
1198 // aliases only make sense for 'common', not for any of the specials
1199 for (int i = 1; i < LanguageCount; ++i) {
1200 if (requiresOpenType(i))
1201 family->writingSystems[i] = QtFontFamily::UnsupportedFT;
1203 family->writingSystems[i] = QtFontFamily::Supported;
1205 family->writingSystems[QFontDatabase::Other] = QtFontFamily::UnsupportedFT;
1207 QtFontStyle::Key styleKey;
1208 for (int i = 0; i < 4; ++i) {
1209 styleKey.style = (i%2) ? QFont::StyleNormal : QFont::StyleItalic;
1210 styleKey.weight = (i > 1) ? QFont::Bold : QFont::Normal;
1211 QtFontStyle *style = foundry->style(styleKey, true);
1212 style->smoothScalable = true;
1213 QtFontSize *size = style->pixelSize(SMOOTH_SCALABLE, true);
1214 QtFontEncoding *enc = size->encodingID(-1, 0, 0, 0, 0, true);
1215 enc->pitch = (f->fixed ? 'm' : 'p');
1220 #endif // QT_NO_FONTCONFIG
1222 static void initializeDb();
1224 static void load(const QString &family = QString(), int script = -1, bool forceXLFD = false)
1226 if (X11->has_fontconfig && !forceXLFD) {
1231 #ifdef QFONTDATABASE_DEBUG
1236 if (family.isNull() && script == -1) {
1239 if (family.isNull()) {
1240 // load all families in all writing systems that match \a script
1241 for (int ws = 1; ws < QFontDatabase::WritingSystemsCount; ++ws) {
1242 if (scriptForWritingSystem[ws] != script)
1244 for (int i = 0; i < numEncodings; ++i) {
1245 if (writingSystems_for_xlfd_encoding[i][ws])
1250 QtFontFamily *f = privateDb()->family(family);
1251 // could reduce this further with some more magic:
1252 // would need to remember the encodings loaded for the family.
1253 if (!f || !f->xlfdLoaded)
1254 loadXlfds(family.toLatin1(), -1);
1258 #ifdef QFONTDATABASE_DEBUG
1259 FD_DEBUG("QFontDatabase: load(%s, %d) took %d ms",
1260 family.toLatin1().constData(), script, t.elapsed());
1264 static void checkSymbolFont(QtFontFamily *family)
1266 if (!family || family->symbol_checked || family->fontFilename.isEmpty())
1268 // qDebug() << "checking " << family->rawName;
1269 family->symbol_checked = true;
1271 QFontEngine::FaceId id;
1272 id.filename = family->fontFilename;
1273 id.index = family->fontFileIndex;
1274 QFreetypeFace *f = QFreetypeFace::getFace(id);
1276 qWarning("checkSymbolFonts: Couldn't open face %s (%s/%d)",
1277 qPrintable(family->name), family->fontFilename.data(), family->fontFileIndex);
1280 for (int i = 0; i < f->face->num_charmaps; ++i) {
1281 FT_CharMap cm = f->face->charmaps[i];
1282 if (cm->encoding == FT_ENCODING_ADOBE_CUSTOM
1283 || cm->encoding == FT_ENCODING_MS_SYMBOL) {
1284 for (int x = QFontDatabase::Latin; x < QFontDatabase::Other; ++x)
1285 family->writingSystems[x] = QtFontFamily::Unsupported;
1286 family->writingSystems[QFontDatabase::Other] = QtFontFamily::Supported;
1293 static void checkSymbolFonts(const QString &family = QString())
1295 #ifndef QT_NO_FONTCONFIG
1296 QFontDatabasePrivate *d = privateDb();
1298 if (family.isEmpty()) {
1299 for (int i = 0; i < d->count; ++i)
1300 checkSymbolFont(d->families[i]);
1302 checkSymbolFont(d->family(family));
1307 static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt);
1309 static void initializeDb()
1311 QFontDatabasePrivate *db = privateDb();
1312 if (!db || db->count)
1318 #ifndef QT_NO_FONTCONFIG
1319 if (db->reregisterAppFonts) {
1320 db->reregisterAppFonts = false;
1321 for (int i = 0; i < db->applicationFonts.count(); ++i)
1322 if (!db->applicationFonts.at(i).families.isEmpty()) {
1323 registerFont(&db->applicationFonts[i]);
1328 FD_DEBUG("QFontDatabase: loaded FontConfig: %d ms", int(t.elapsed()));
1333 #ifndef QT_NO_FONTCONFIG
1334 for (int i = 0; i < db->count; i++) {
1335 for (int j = 0; j < db->families[i]->count; ++j) { // each foundry
1336 QtFontFoundry *foundry = db->families[i]->foundries[j];
1337 for (int k = 0; k < foundry->count; ++k) {
1338 QtFontStyle *style = foundry->styles[k];
1339 if (style->key.style != QFont::StyleNormal) continue;
1341 QtFontSize *size = style->pixelSize(SMOOTH_SCALABLE);
1342 if (! size) continue; // should not happen
1343 QtFontEncoding *enc = size->encodingID(-1, 0, 0, 0, 0, true);
1344 if (! enc) continue; // should not happen either
1346 QtFontStyle::Key key = style->key;
1348 // does this style have an italic equivalent?
1349 key.style = QFont::StyleItalic;
1350 QtFontStyle *equiv = foundry->style(key);
1351 if (equiv) continue;
1353 // does this style have an oblique equivalent?
1354 key.style = QFont::StyleOblique;
1355 equiv = foundry->style(key);
1356 if (equiv) continue;
1358 // let's fake one...
1359 equiv = foundry->style(key, true);
1360 equiv->smoothScalable = true;
1362 QtFontSize *equiv_size = equiv->pixelSize(SMOOTH_SCALABLE, true);
1363 QtFontEncoding *equiv_enc = equiv_size->encodingID(-1, 0, 0, 0, 0, true);
1365 // keep the same pitch
1366 equiv_enc->pitch = enc->pitch;
1373 #ifdef QFONTDATABASE_DEBUG
1374 #ifndef QT_NO_FONTCONFIG
1375 if (!X11->has_fontconfig)
1377 // load everything at startup in debug mode.
1380 // print the database
1381 for (int f = 0; f < db->count; f++) {
1382 QtFontFamily *family = db->families[f];
1383 FD_DEBUG("'%s' %s fixed=%s", family->name.latin1(), (family->fixedPitch ? "fixed" : ""),
1384 (family->fixedPitch ? "yes" : "no"));
1385 for (int i = 0; i < QFontDatabase::WritingSystemsCount; ++i) {
1386 QFontDatabase::WritingSystem ws = QFontDatabase::WritingSystem(i);
1387 FD_DEBUG("\t%s: %s", QFontDatabase::writingSystemName(ws).toLatin1().constData(),
1388 ((family->writingSystems[i] & QtFontFamily::Supported) ? "Supported" :
1389 (family->writingSystems[i] & QtFontFamily::Unsupported) == QtFontFamily::Unsupported ?
1390 "Unsupported" : "Unknown"));
1393 for (int fd = 0; fd < family->count; fd++) {
1394 QtFontFoundry *foundry = family->foundries[fd];
1395 FD_DEBUG("\t\t'%s'", foundry->name.latin1());
1396 for (int s = 0; s < foundry->count; s++) {
1397 QtFontStyle *style = foundry->styles[s];
1398 FD_DEBUG("\t\t\tstyle: style=%d weight=%d (%s)\n"
1399 "\t\t\tstretch=%d (%s)",
1400 style->key.style, style->key.weight,
1401 style->weightName, style->key.stretch,
1402 style->setwidthName ? style->setwidthName : "nil");
1403 if (style->smoothScalable)
1404 FD_DEBUG("\t\t\t\tsmooth scalable");
1405 else if (style->bitmapScalable)
1406 FD_DEBUG("\t\t\t\tbitmap scalable");
1407 if (style->pixelSizes) {
1408 qDebug("\t\t\t\t%d pixel sizes", style->count);
1409 for (int z = 0; z < style->count; ++z) {
1410 QtFontSize *size = style->pixelSizes + z;
1411 for (int e = 0; e < size->count; ++e) {
1412 FD_DEBUG("\t\t\t\t size %5d pitch %c encoding %s",
1414 size->encodings[e].pitch,
1415 xlfd_for_id(size->encodings[e].encoding));
1422 #endif // QFONTDATABASE_DEBUG
1426 // --------------------------------------------------------------------------------------
1428 // --------------------------------------------------------------------------------------
1430 static const char *styleHint(const QFontDef &request)
1432 const char *stylehint = 0;
1433 switch (request.styleHint) {
1434 case QFont::SansSerif:
1435 stylehint = "sans-serif";
1438 stylehint = "serif";
1440 case QFont::TypeWriter:
1441 stylehint = "monospace";
1444 if (request.fixedPitch)
1445 stylehint = "monospace";
1451 #ifndef QT_NO_FONTCONFIG
1453 void qt_addPatternProps(FcPattern *pattern, int screen, int script, const QFontDef &request)
1455 int weight_value = FC_WEIGHT_BLACK;
1456 if (request.weight == 0)
1457 weight_value = FC_WEIGHT_MEDIUM;
1458 else if (request.weight < (QFont::Light + QFont::Normal) / 2)
1459 weight_value = FC_WEIGHT_LIGHT;
1460 else if (request.weight < (QFont::Normal + QFont::DemiBold) / 2)
1461 weight_value = FC_WEIGHT_MEDIUM;
1462 else if (request.weight < (QFont::DemiBold + QFont::Bold) / 2)
1463 weight_value = FC_WEIGHT_DEMIBOLD;
1464 else if (request.weight < (QFont::Bold + QFont::Black) / 2)
1465 weight_value = FC_WEIGHT_BOLD;
1466 FcPatternDel(pattern, FC_WEIGHT);
1467 FcPatternAddInteger(pattern, FC_WEIGHT, weight_value);
1469 int slant_value = FC_SLANT_ROMAN;
1470 if (request.style == QFont::StyleItalic)
1471 slant_value = FC_SLANT_ITALIC;
1472 else if (request.style == QFont::StyleOblique)
1473 slant_value = FC_SLANT_OBLIQUE;
1474 FcPatternDel(pattern, FC_SLANT);
1475 FcPatternAddInteger(pattern, FC_SLANT, slant_value);
1477 double size_value = qMax(qreal(1.), request.pixelSize);
1478 FcPatternDel(pattern, FC_PIXEL_SIZE);
1479 FcPatternAddDouble(pattern, FC_PIXEL_SIZE, size_value);
1481 int stretch = request.stretch;
1484 FcPatternDel(pattern, FC_WIDTH);
1485 FcPatternAddInteger(pattern, FC_WIDTH, stretch);
1487 if (X11->display && QX11Info::appDepth(screen) <= 8) {
1488 FcPatternDel(pattern, FC_ANTIALIAS);
1489 // can't do antialiasing on 8bpp
1490 FcPatternAddBool(pattern, FC_ANTIALIAS, false);
1491 } else if (request.styleStrategy & (QFont::PreferAntialias|QFont::NoAntialias)) {
1492 FcPatternDel(pattern, FC_ANTIALIAS);
1493 FcPatternAddBool(pattern, FC_ANTIALIAS,
1494 !(request.styleStrategy & QFont::NoAntialias));
1497 if (script != QUnicodeTables::Common && *specialLanguages[script] != '\0') {
1498 Q_ASSERT(script < QUnicodeTables::ScriptCount);
1499 FcLangSet *ls = FcLangSetCreate();
1500 FcLangSetAdd(ls, (const FcChar8*)specialLanguages[script]);
1501 FcPatternDel(pattern, FC_LANG);
1502 FcPatternAddLangSet(pattern, FC_LANG, ls);
1503 FcLangSetDestroy(ls);
1507 static bool preferScalable(const QFontDef &request)
1509 return request.styleStrategy & (QFont::PreferOutline|QFont::ForceOutline|QFont::PreferQuality|QFont::PreferAntialias);
1513 static FcPattern *getFcPattern(const QFontPrivate *fp, int script, const QFontDef &request)
1515 if (!X11->has_fontconfig)
1518 FcPattern *pattern = FcPatternCreate();
1523 value.type = FcTypeString;
1526 QStringList families_and_foundries = familyList(request);
1527 for (int i = 0; i < families_and_foundries.size(); ++i) {
1528 QString family, foundry;
1529 parseFontName(families_and_foundries.at(i), foundry, family);
1530 if (!family.isEmpty()) {
1531 QByteArray cs = family.toUtf8();
1532 value.u.s = (const FcChar8 *)cs.data();
1533 FcPatternAdd(pattern, FC_FAMILY, value, FcTrue);
1536 QT_PREPEND_NAMESPACE(match)(script, request, family, foundry, -1, &desc);
1537 if (!foundry.isEmpty()) {
1538 QByteArray cs = foundry.toUtf8();
1539 value.u.s = (const FcChar8 *)cs.data();
1540 FcPatternAddWeak(pattern, FC_FOUNDRY, value, FcTrue);
1545 const char *stylehint = styleHint(request);
1547 value.u.s = (const FcChar8 *)stylehint;
1548 FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue);
1551 if (!request.ignorePitch) {
1552 char pitch_value = FC_PROPORTIONAL;
1553 if (request.fixedPitch || (desc.family && desc.family->fixedPitch))
1554 pitch_value = FC_MONO;
1555 FcPatternAddInteger(pattern, FC_SPACING, pitch_value);
1557 FcPatternAddBool(pattern, FC_OUTLINE, !(request.styleStrategy & QFont::PreferBitmap));
1558 if (preferScalable(request) || (desc.style && desc.style->smoothScalable))
1559 FcPatternAddBool(pattern, FC_SCALABLE, true);
1561 qt_addPatternProps(pattern, fp->screen, script, request);
1563 FcDefaultSubstitute(pattern);
1564 FcConfigSubstitute(0, pattern, FcMatchPattern);
1565 FcConfigSubstitute(0, pattern, FcMatchFont);
1567 // these should only get added to the pattern _after_ substitution
1568 // append the default fallback font for the specified script
1569 extern QString qt_fallback_font_family(int);
1570 QString fallback = qt_fallback_font_family(script);
1571 if (!fallback.isEmpty()) {
1572 QByteArray cs = fallback.toUtf8();
1573 value.u.s = (const FcChar8 *)cs.data();
1574 FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue);
1577 // add the default family
1578 QString defaultFamily = QApplication::font().family();
1579 QByteArray cs = defaultFamily.toUtf8();
1580 value.u.s = (const FcChar8 *)cs.data();
1581 FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue);
1583 // add QFont::defaultFamily() to the list, for compatibility with
1584 // previous versions
1585 defaultFamily = QApplication::font().defaultFamily();
1586 cs = defaultFamily.toUtf8();
1587 value.u.s = (const FcChar8 *)cs.data();
1588 FcPatternAddWeak(pattern, FC_FAMILY, value, FcTrue);
1594 static void FcFontSetRemove(FcFontSet *fs, int at)
1596 Q_ASSERT(at < fs->nfont);
1597 FcPatternDestroy(fs->fonts[at]);
1598 int len = (--fs->nfont - at) * sizeof(FcPattern *);;
1600 memmove(fs->fonts + at, fs->fonts + at + 1, len);
1603 static QFontEngine *tryPatternLoad(FcPattern *p, int screen,
1604 const QFontDef &request, int script, FcPattern **matchedPattern = 0)
1606 #ifdef FONT_MATCH_DEBUG
1608 FcPatternGetString(p, FC_FAMILY, 0, &fam);
1609 FM_DEBUG("==== trying %s\n", fam);
1611 FM_DEBUG("passes charset test\n");
1612 FcPattern *pattern = FcPatternDuplicate(p);
1613 // add properties back in as the font selected from the
1614 // list doesn't contain them.
1615 qt_addPatternProps(pattern, screen, script, request);
1617 FcConfigSubstitute(0, pattern, FcMatchPattern);
1618 FcDefaultSubstitute(pattern);
1620 FcPattern *match = FcFontMatch(0, pattern, &res);
1623 *matchedPattern = 0;
1625 QFontEngineX11FT *engine = 0;
1626 if (!match) // probably no fonts available.
1630 *matchedPattern = FcPatternDuplicate(match);
1632 if (script != QUnicodeTables::Common) {
1633 // skip font if it doesn't support the language we want
1634 if (specialChars[script]) {
1635 // need to check the charset, as the langset doesn't work for these scripts
1637 if (FcPatternGetCharSet(match, FC_CHARSET, 0, &cs) != FcResultMatch)
1639 if (!FcCharSetHasChar(cs, specialChars[script]))
1641 } else if (*specialLanguages[script] != '\0'){
1642 FcLangSet *langSet = 0;
1643 if (FcPatternGetLangSet(match, FC_LANG, 0, &langSet) != FcResultMatch)
1645 if (FcLangSetHasLang(langSet, (const FcChar8*)specialLanguages[script]) != FcLangEqual)
1650 // enforce non-antialiasing if requested. the ft font engine looks at this property.
1651 if (request.styleStrategy & QFont::NoAntialias) {
1652 FcPatternDel(match, FC_ANTIALIAS);
1653 FcPatternAddBool(match, FC_ANTIALIAS, false);
1656 engine = new QFontEngineX11FT(match, qt_FcPatternToQFontDef(match, request), screen);
1657 if (engine->invalid()) {
1658 FM_DEBUG(" --> invalid!\n");
1661 } else if (scriptRequiresOpenType(script)) {
1662 HB_Face hbFace = engine->harfbuzzFace();
1663 if (!hbFace || !hbFace->supported_scripts[script]) {
1664 FM_DEBUG(" OpenType support missing for script\n");
1670 FcPatternDestroy(pattern);
1671 if (!engine && matchedPattern && *matchedPattern) {
1672 FcPatternDestroy(*matchedPattern);
1673 *matchedPattern = 0;
1678 FcFontSet *qt_fontSetForPattern(FcPattern *pattern, const QFontDef &request)
1681 FcFontSet *fs = FcFontSort(0, pattern, FcTrue, 0, &result);
1682 #ifdef FONT_MATCH_DEBUG
1683 FM_DEBUG("first font in fontset:\n");
1684 FcPatternPrint(fs->fonts[0]);
1687 FcBool forceScalable = request.styleStrategy & QFont::ForceOutline;
1689 // remove fonts if they are not scalable (and should be)
1690 if (forceScalable && fs) {
1691 for (int i = 0; i < fs->nfont; ++i) {
1692 FcPattern *font = fs->fonts[i];
1695 res = FcPatternGetBool(font, FC_SCALABLE, 0, &scalable);
1696 if (res != FcResultMatch || !scalable) {
1697 FcFontSetRemove(fs, i);
1698 #ifdef FONT_MATCH_DEBUG
1699 FM_DEBUG("removing pattern:");
1700 FcPatternPrint(font);
1707 FM_DEBUG("final pattern contains %d fonts\n", fs->nfont);
1712 static QFontEngine *loadFc(const QFontPrivate *fp, int script, const QFontDef &request)
1714 FM_DEBUG("===================== loadFc: script=%d family='%s'\n", script, request.family.toLatin1().data());
1715 FcPattern *pattern = getFcPattern(fp, script, request);
1717 #ifdef FONT_MATCH_DEBUG
1718 FM_DEBUG("\n\nfinal FcPattern contains:\n");
1719 FcPatternPrint(pattern);
1722 QFontEngine *fe = 0;
1723 FcPattern *matchedPattern = 0;
1724 fe = tryPatternLoad(pattern, fp->screen, request, script, &matchedPattern);
1726 FcFontSet *fs = qt_fontSetForPattern(pattern, request);
1729 for (int i = 0; !fe && i < fs->nfont; ++i)
1730 fe = tryPatternLoad(fs->fonts[i], fp->screen, request, script, &matchedPattern);
1731 FcFontSetDestroy(fs);
1733 FM_DEBUG("engine for script %d is %s\n", script, fe ? fe->fontDef.family.toLatin1().data(): "(null)");
1736 && script == QUnicodeTables::Common
1737 && !(request.styleStrategy & QFont::NoFontMerging) && !fe->symbol) {
1738 fe = new QFontEngineMultiFT(fe, matchedPattern, pattern, fp->screen, request);
1740 FcPatternDestroy(pattern);
1742 FcPatternDestroy(matchedPattern);
1747 static FcPattern *queryFont(const FcChar8 *file, const QByteArray &data, int id, FcBlanks *blanks, int *count)
1749 #if FC_VERSION < 20402
1751 return FcFreeTypeQuery(file, id, blanks, count);
1754 return FcFreeTypeQuery(file, id, blanks, count);
1756 extern FT_Library qt_getFreetype();
1757 FT_Library lib = qt_getFreetype();
1759 FcPattern *pattern = 0;
1762 if (!FT_New_Memory_Face(lib, (const FT_Byte *)data.constData(), data.size(), id, &face)) {
1763 *count = face->num_faces;
1765 pattern = FcFreeTypeQueryFace(face, file, id, blanks);
1773 #endif // QT_NO_FONTCONFIG
1775 static QFontEngine *loadRaw(const QFontPrivate *fp, const QFontDef &request)
1777 Q_ASSERT(fp && fp->rawMode);
1779 QByteArray xlfd = request.family.toLatin1();
1780 FM_DEBUG("Loading XLFD (rawmode) '%s'", xlfd.data());
1784 if (!(xfs = XLoadQueryFont(QX11Info::display(), xlfd.data())))
1785 if (!(xfs = XLoadQueryFont(QX11Info::display(), "fixed")))
1788 fe = new QFontEngineXLFD(xfs, xlfd, 0);
1789 if (! qt_fillFontDef(xfs, &fe->fontDef, fp->dpi, 0) &&
1790 ! qt_fillFontDef(xlfd, &fe->fontDef, fp->dpi, 0))
1791 fe->fontDef = QFontDef();
1795 QFontEngine *QFontDatabase::loadXlfd(int screen, int script, const QFontDef &request, int force_encoding_id)
1797 QMutexLocker locker(fontDatabaseMutex());
1800 FM_DEBUG() << "---> loadXlfd: request is" << request.family;
1801 QStringList families_and_foundries = familyList(request);
1802 const char *stylehint = styleHint(request);
1804 families_and_foundries << QString::fromLatin1(stylehint);
1805 families_and_foundries << QString();
1806 FM_DEBUG() << "loadXlfd: list is" << families_and_foundries;
1807 for (int i = 0; i < families_and_foundries.size(); ++i) {
1808 QString family, foundry;
1809 QT_PREPEND_NAMESPACE(parseFontName)(families_and_foundries.at(i), foundry, family);
1810 FM_DEBUG("loadXlfd: >>>>>>>>>>>>>>trying to match '%s' encoding=%d", family.toLatin1().data(), force_encoding_id);
1811 QT_PREPEND_NAMESPACE(match)(script, request, family, foundry, force_encoding_id, &desc, QList<int>(), true);
1816 QFontEngine *fe = 0;
1817 if (force_encoding_id != -1
1818 || (request.styleStrategy & QFont::NoFontMerging)
1819 || (desc.family && desc.family->writingSystems[QFontDatabase::Symbol] & QtFontFamily::Supported)) {
1821 int px = desc.size->pixelSize;
1822 if (desc.style->smoothScalable && px == SMOOTH_SCALABLE)
1823 px = request.pixelSize;
1824 else if (desc.style->bitmapScalable && px == 0)
1825 px = request.pixelSize;
1827 QByteArray xlfd("-");
1828 xlfd += desc.foundry->name.isEmpty() ? QByteArray("*") : desc.foundry->name.toLatin1();
1830 xlfd += desc.family->name.isEmpty() ? QByteArray("*") : desc.family->name.toLatin1();
1832 xlfd += desc.style->weightName ? desc.style->weightName : "*";
1834 xlfd += (desc.style->key.style == QFont::StyleItalic
1836 : (desc.style->key.style == QFont::StyleOblique ? 'o' : 'r'));
1838 xlfd += desc.style->setwidthName ? desc.style->setwidthName : "*";
1839 // ### handle add-style
1841 xlfd += QByteArray::number(px);
1843 xlfd += QByteArray::number(desc.encoding->xpoint);
1845 xlfd += QByteArray::number(desc.encoding->xres);
1847 xlfd += QByteArray::number(desc.encoding->yres);
1849 xlfd += desc.encoding->pitch;
1851 xlfd += QByteArray::number(desc.encoding->avgwidth);
1853 xlfd += xlfd_for_id(desc.encoding->encoding);
1855 FM_DEBUG(" using XLFD: %s\n", xlfd.data());
1857 const int mib = xlfd_encoding[desc.encoding->encoding].mib;
1859 if ((xfs = XLoadQueryFont(QX11Info::display(), xlfd))) {
1860 fe = new QFontEngineXLFD(xfs, xlfd, mib);
1861 const int dpi = QX11Info::appDpiY();
1862 if (!qt_fillFontDef(xfs, &fe->fontDef, dpi, &desc)
1863 && !qt_fillFontDef(xlfd, &fe->fontDef, dpi, &desc)) {
1864 initFontDef(desc, request, &fe->fontDef);
1869 fe = new QFontEngineBox(request.pixelSize);
1870 fe->fontDef = QFontDef();
1873 QList<int> encodings;
1874 if (desc.encoding) {
1875 if (desc.encoding->encoding >= 0)
1876 encodings.append(int(desc.encoding->encoding));
1880 // append all other encodings for the matched font
1881 for (int i = 0; i < desc.size->count; ++i) {
1882 QtFontEncoding *e = desc.size->encodings + i;
1883 if (e == desc.encoding || e->encoding < 0)
1885 encodings.append(int(e->encoding));
1888 // fill in the missing encodings
1889 const XlfdEncoding *enc = xlfd_encoding;
1890 for (; enc->name; ++enc) {
1891 if (!encodings.contains(enc->id) && enc->id >= 0) {
1892 encodings.append(enc->id);
1896 #if defined(FONT_MATCH_DEBUG)
1897 FM_DEBUG(" using MultiXLFD, encodings:");
1898 for (int i = 0; i < encodings.size(); ++i) {
1899 const int id = encodings.at(i);
1900 FM_DEBUG(" %2d: %s", xlfd_encoding[id].id, xlfd_encoding[id].name);
1904 fe = new QFontEngineMultiXLFD(request, encodings, screen);
1909 #if (defined(QT_ARCH_ARM) || defined(QT_ARCH_ARMV6)) && defined(Q_CC_GNU) && (__GNUC__ == 4) && (__GNUC_MINOR__ == 3)
1910 #define NEEDS_GCC_BUG_WORKAROUND
1913 #ifdef NEEDS_GCC_BUG_WORKAROUND
1914 static inline void gccBugWorkaround(const QFontDef &req)
1917 snprintf(buffer, 8, "%f", req.pixelSize);
1922 Loads a QFontEngine for the specified \a script that matches the
1923 QFontDef \e request member variable.
1925 void QFontDatabase::load(const QFontPrivate *d, int script)
1927 Q_ASSERT(script >= 0 && script < QUnicodeTables::ScriptCount);
1929 // normalize the request to get better caching
1930 QFontDef req = d->request;
1931 if (req.pixelSize <= 0)
1932 req.pixelSize = qFloor(qt_pixelSize(req.pointSize, d->dpi) * 100.0 + 0.5) * 0.01;
1933 if (req.pixelSize < 1)
1936 #ifdef NEEDS_GCC_BUG_WORKAROUND
1937 // req.pixelSize ends up with a bogus value unless this workaround is called
1938 gccBugWorkaround(req);
1941 if (req.weight == 0)
1942 req.weight = QFont::Normal;
1943 if (req.stretch == 0)
1946 QFontCache::Key key(req, d->rawMode ? QUnicodeTables::Common : script, d->screen);
1948 getEngineData(d, key);
1950 // the cached engineData could have already loaded the engine we want
1951 if (d->engineData->engines[script])
1954 // set it to the actual pointsize, so QFontInfo will do the right thing
1955 if (req.pointSize < 0)
1956 req.pointSize = qt_pointSize(req.pixelSize, d->dpi);
1959 QFontEngine *fe = QFontCache::instance()->findEngine(key);
1962 QMutexLocker locker(fontDatabaseMutex());
1963 if (!privateDb()->count)
1966 const bool mainThread = (qApp->thread() == QThread::currentThread());
1967 if (qt_enable_test_font && req.family == QLatin1String("__Qt__Box__Engine__")) {
1968 fe = new QTestFontEngine(req.pixelSize);
1970 } else if (d->rawMode) {
1972 fe = loadRaw(d, req);
1973 #ifndef QT_NO_FONTCONFIG
1974 } else if (X11->has_fontconfig) {
1975 fe = loadFc(d, script, req);
1977 } else if (mainThread && qt_is_gui_used) {
1978 fe = loadXlfd(d->screen, script, req);
1981 fe = new QFontEngineBox(req.pixelSize);
1982 fe->fontDef = QFontDef();
1985 if (fe->symbol || (d->request.styleStrategy & QFont::NoFontMerging)) {
1986 for (int i = 0; i < QUnicodeTables::ScriptCount; ++i) {
1987 if (!d->engineData->engines[i]) {
1988 d->engineData->engines[i] = fe;
1993 d->engineData->engines[script] = fe;
1996 QFontCache::instance()->insertEngine(key, fe);
1999 static void registerFont(QFontDatabasePrivate::ApplicationFont *fnt)
2001 #if defined(QT_NO_FONTCONFIG)
2004 if (!X11->has_fontconfig)
2007 FcConfig *config = FcConfigGetCurrent();
2011 FcFontSet *set = FcConfigGetFonts(config, FcSetApplication);
2013 FcConfigAppFontAddFile(config, (const FcChar8 *)":/non-existent");
2014 set = FcConfigGetFonts(config, FcSetApplication); // try again
2019 QString fileNameForQuery = fnt->fileName;
2020 #if FC_VERSION < 20402
2023 if (!fnt->data.isEmpty()) {
2026 tmp.write(fnt->data);
2028 fileNameForQuery = tmp.fileName();
2033 FcBlanks *blanks = FcConfigGetBlanks(0);
2036 QStringList families;
2037 QFontDatabasePrivate *db = privateDb();
2039 FcPattern *pattern = 0;
2041 pattern = queryFont((const FcChar8 *)QFile::encodeName(fileNameForQuery).constData(),
2042 fnt->data, id, blanks, &count);
2046 FcPatternDel(pattern, FC_FILE);
2047 FcPatternAddString(pattern, FC_FILE, (const FcChar8 *)fnt->fileName.toUtf8().constData());
2049 FcChar8 *fam = 0, *familylang = 0;
2051 for (i = 0; ; i++) {
2052 if (FcPatternGetString(pattern, FC_FAMILYLANG, i, &familylang) != FcResultMatch)
2054 QString familyLang = QString::fromUtf8((const char *) familylang);
2055 if (familyLang.compare(db->systemLang, Qt::CaseInsensitive) == 0) {
2061 if (FcPatternGetString(pattern, FC_FAMILY, n, &fam) == FcResultMatch) {
2062 QString family = QString::fromUtf8(reinterpret_cast<const char *>(fam));
2066 if (!FcFontSetAdd(set, pattern))
2070 } while (pattern && id < count);
2072 fnt->families = families;
2076 bool QFontDatabase::removeApplicationFont(int handle)
2078 #if defined(QT_NO_FONTCONFIG)
2081 QMutexLocker locker(fontDatabaseMutex());
2083 QFontDatabasePrivate *db = privateDb();
2084 if (handle < 0 || handle >= db->applicationFonts.count())
2087 FcConfigAppFontClear(0);
2089 db->applicationFonts[handle] = QFontDatabasePrivate::ApplicationFont();
2091 db->reregisterAppFonts = true;
2097 bool QFontDatabase::removeAllApplicationFonts()
2099 #if defined(QT_NO_FONTCONFIG)
2102 QMutexLocker locker(fontDatabaseMutex());
2104 QFontDatabasePrivate *db = privateDb();
2105 if (db->applicationFonts.isEmpty())
2108 FcConfigAppFontClear(0);
2109 db->applicationFonts.clear();
2115 bool QFontDatabase::supportsThreadedFontRendering()
2117 #if defined(QT_NO_FONTCONFIG)
2120 return X11->has_fontconfig;
2124 QString QFontDatabase::resolveFontFamilyAlias(const QString &family)
2126 #if defined(QT_NO_FONTCONFIG)
2129 FcPattern *pattern = FcPatternCreate();
2133 FcPatternAddString(pattern, FC_FAMILY, (const FcChar8 *) family.toUtf8().data());
2134 FcConfigSubstitute(0, pattern, FcMatchPattern);
2135 FcDefaultSubstitute(pattern);
2137 FcChar8 *familyAfterSubstitution;
2138 FcPatternGetString(pattern, FC_FAMILY, 0, &familyAfterSubstitution);
2139 QString resolved = QString::fromUtf8((const char *) familyAfterSubstitution);
2140 FcPatternDestroy(pattern);