From 97f73e957756753b09a778daf2ee8f0ddb97f746 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Sat, 15 Sep 2018 00:12:42 +0200 Subject: Handle fonts that have commas/quotes in the family name Since the comma character was originally used as a separator, we need to extend QFont to have setFamilies() so that we can avoid joining the family strings together. This enables us to see the family name as a single string and for multiple family names, we have families(). Subsequently, this has added functions to QTextCharFormat to account for multiple font families too. So it is now possible to set a single one directly with setFontFamily() and multiple ones with setFontFamilies(). This also bumps up the datastream version to 19 as QFont now streams the families list as well. [ChangeLog][QtGui][QFont] Add setFamilies()/families() to aid using of font families with commas and quotes in their name. [ChangeLog][Important Behavior Changes] QDataStream version bumped up to 19 to account for changes in the serialization of QFont. Fixes: QTBUG-46322 Change-Id: Iee9f715e47544a7a705c7f36401aba216a7d42b0 Reviewed-by: Lars Knoll Reviewed-by: Allan Sandfeld Jensen --- src/gui/text/qfontdatabase.cpp | 54 ++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'src/gui/text/qfontdatabase.cpp') diff --git a/src/gui/text/qfontdatabase.cpp b/src/gui/text/qfontdatabase.cpp index 196eebb353..42e7871214 100644 --- a/src/gui/text/qfontdatabase.cpp +++ b/src/gui/text/qfontdatabase.cpp @@ -696,20 +696,20 @@ static QStringList familyList(const QFontDef &req) { // list of families to try QStringList family_list; - if (req.family.isEmpty()) - return family_list; - const auto list = req.family.splitRef(QLatin1Char(',')); - const int numFamilies = list.size(); - family_list.reserve(numFamilies); - for (int i = 0; i < numFamilies; ++i) { - QStringRef str = list.at(i).trimmed(); - if ((str.startsWith(QLatin1Char('"')) && str.endsWith(QLatin1Char('"'))) - || (str.startsWith(QLatin1Char('\'')) && str.endsWith(QLatin1Char('\'')))) - str = str.mid(1, str.length() - 2); - family_list << str.toString(); + family_list << req.families; + if (!req.family.isEmpty()) { + const auto list = req.family.splitRef(QLatin1Char(',')); + const int numFamilies = list.size(); + family_list.reserve(numFamilies); + for (int i = 0; i < numFamilies; ++i) { + QStringRef str = list.at(i).trimmed(); + if ((str.startsWith(QLatin1Char('"')) && str.endsWith(QLatin1Char('"'))) + || (str.startsWith(QLatin1Char('\'')) && str.endsWith(QLatin1Char('\'')))) + str = str.mid(1, str.length() - 2); + family_list << str.toString(); + } } - // append the substitute list for each family in family_list for (int i = 0, size = family_list.size(); i < size; ++i) family_list += QFont::substitutes(family_list.at(i)); @@ -2688,9 +2688,8 @@ QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) } QString family_name, foundry_name; - - parseFontName(request.family, foundry_name, family_name); - + const QString requestFamily = request.families.size() > 0 ? request.families.at(0) : request.family; + parseFontName(requestFamily, foundry_name, family_name); QtFontDesc desc; QList blackListed; int index = match(multi ? QChar::Script_Common : script, request, family_name, foundry_name, &desc, blackListed); @@ -2703,8 +2702,8 @@ QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) // Don't pass empty family names to the platform font database, since it will then invoke its own matching // and we will be out of sync with the matched font. - if (fontDef.family.isEmpty()) - fontDef.family = desc.family->name; + if (fontDef.families.isEmpty() && fontDef.family.isEmpty()) + fontDef.families = QStringList(desc.family->name); engine = loadEngine(script, fontDef, desc.family, desc.foundry, desc.style, desc.size); @@ -2717,13 +2716,13 @@ QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) } if (!engine) { - if (!request.family.isEmpty()) { + if (!requestFamily.isEmpty()) { QFont::StyleHint styleHint = QFont::StyleHint(request.styleHint); if (styleHint == QFont::AnyStyle && request.fixedPitch) styleHint = QFont::TypeWriter; QStringList fallbacks = request.fallBackFamilies - + fallbacksForFamily(request.family, + + fallbacksForFamily(requestFamily, QFont::Style(request.style), styleHint, QChar::Script(script)); @@ -2741,7 +2740,7 @@ QFontEngine *QFontDatabase::findFont(const QFontDef &request, int script) index = match(multi ? QChar::Script_Common : script, def, def.family, QLatin1String(""), &desc, blackListed); if (index >= 0) { QFontDef loadDef = def; - if (loadDef.family.isEmpty()) + if (loadDef.families.isEmpty() && loadDef.family.isEmpty()) loadDef.family = desc.family->name; engine = loadEngine(script, loadDef, desc.family, desc.foundry, desc.style, desc.size); if (engine) @@ -2782,7 +2781,10 @@ void QFontDatabase::load(const QFontPrivate *d, int script) // look for the requested font in the engine data cache // note: fallBackFamilies are not respected in the EngineData cache key; // join them with the primary selection family to avoid cache misses - req.family = fallBackFamilies.join(QLatin1Char(',')); + if (!d->request.family.isEmpty()) + req.family = fallBackFamilies.join(QLatin1Char(',')); + if (!d->request.families.isEmpty()) + req.families = fallBackFamilies; d->engineData = fontCache->findEngineData(req); if (!d->engineData) { @@ -2803,14 +2805,14 @@ void QFontDatabase::load(const QFontPrivate *d, int script) req.fallBackFamilies = fallBackFamilies; if (!req.fallBackFamilies.isEmpty()) - req.family = req.fallBackFamilies.takeFirst(); + req.families = QStringList(req.fallBackFamilies.takeFirst()); // list of families to try QStringList family_list; - if (!req.family.isEmpty()) { + if (!req.families.isEmpty()) { // Add primary selection - family_list << req.family; + family_list << req.families.at(0); // add the default family QString defaultFamily = QGuiApplication::font().family(); @@ -2824,11 +2826,11 @@ void QFontDatabase::load(const QFontPrivate *d, int script) QStringList::ConstIterator it = family_list.constBegin(), end = family_list.constEnd(); for (; !fe && it != end; ++it) { - req.family = *it; + req.families = QStringList(*it); fe = QFontDatabase::findFont(req, script); if (fe) { - if (fe->type() == QFontEngine::Box && !req.family.isEmpty()) { + if (fe->type() == QFontEngine::Box && !req.families.isEmpty()) { if (fe->ref.load() == 0) delete fe; fe = 0; -- cgit v1.2.3