#include #include #include #include "font.h" static QList > weights() { QList > list; list.append(QPair("Thin", QFont::Thin)); list.append(QPair("Extra Light", QFont::ExtraLight)); list.append(QPair("Light", QFont::Light)); list.append(QPair("Regular", QFont::Normal)); list.append(QPair("Medium", QFont::Medium)); list.append(QPair("Demi Bold", QFont::DemiBold)); list.append(QPair("Bold", QFont::Bold)); list.append(QPair("Extra Bold", QFont::ExtraBold)); list.append(QPair("Black", QFont::Black)); return list; } static QList > stretches() { QList > list; list.append(QPair("Ultra Condensed", QFont::UltraCondensed)); list.append(QPair("Extra Condensed", QFont::ExtraCondensed)); list.append(QPair("Condensed", QFont::Condensed)); list.append(QPair("Semi Expanded", QFont::SemiExpanded)); list.append(QPair("Expanded", QFont::Expanded)); list.append(QPair("Extra Expanded", QFont::ExtraExpanded)); list.append(QPair("Ultra Expanded", QFont::UltraExpanded)); return list; } static QList > styles() { QList > list; list.append(QPair("Italic", QFont::StyleItalic)); list.append(QPair("Oblique", QFont::StyleOblique)); return list; } static QList > weightList = weights(); static QList > stretchList = stretches(); static QList > styleList = styles(); static const QStringList &fonts() { static QStringList l(QFontDatabase().families()); return l; } static void parse(const QString &str, QString &family, QFont::Weight &weight, QFont::Stretch &stretch, QFont::Style &style) { int weightIndex, stretchIndex, styleIndex, index; weightIndex = stretchIndex = styleIndex = str.length(); for (int i = 0; i < weightList.size(); i++) { if ((index = str.indexOf(weightList.at(i).first)) >= 0) { weightIndex = index - 1; weight = weightList.at(i).second; break; } } for (int i = 0; i < stretchList.size(); i++) { if ((index = str.indexOf(stretchList.at(i).first)) >= 0) { stretchIndex = index - 1; stretch = stretchList.at(i).second; break; } } for (int i = 0; i < styleList.size(); i++) { if ((index = str.indexOf(styleList.at(i).first)) >= 0) { styleIndex = index - 1; style = styleList.at(i).second; break; } } family = str.mid(0, qMin(qMin(weightIndex, stretchIndex), styleIndex)); } static bool matchFamily(const QString &family) { for (int j = 0; j < fonts().size(); j++) if (fonts().at(j).startsWith(family)) return true; return false; } QFont Font::fromJsonArray(const QJsonArray &json) { if (json.isEmpty()) return QFont("Open Sans"); // Try exact match in order of the font list QString family; QFont::Weight weight = QFont::Normal; QFont::Stretch stretch = QFont::Unstretched; QFont::Style style = QFont::StyleNormal; for (int i = 0; i < json.size(); i++) { if (!json.at(i).isString()) continue; parse(json.at(i).toString(), family, weight, stretch, style); if (matchFamily(family)) { QFont font(family); font.setWeight(weight); font.setStretch(stretch); font.setStyle(style); } } // Use Qt's font matching logic on the first font in the list parse(json.first().toString(), family, weight, stretch, style); QFont font(family); font.setWeight(weight); font.setStretch(stretch); font.setStyle(style); return font; }