| 1 |
/* |
| 2 |
* SPDX-FileCopyrightText: 2020~2020 CSSlayer <[email protected]> |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 5 |
* |
| 6 |
*/ |
| 7 |
#include "font.h" |
| 8 |
#include <QMap> |
| 9 |
|
| 10 |
QFont fcitx::parseFont(const QString &string) { |
| 11 |
#if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0)) |
| 12 |
auto list = string.split(" ", QString::SkipEmptyParts); |
| 13 |
#else |
| 14 |
auto list = string.split(" ", Qt::SkipEmptyParts); |
| 15 |
#endif |
| 16 |
int size = 9; // Default size. |
| 17 |
if (!list.empty()) { |
| 18 |
bool ok = false; |
| 19 |
auto fontSize = list.back().toInt(&ok); |
| 20 |
if (ok) { |
| 21 |
if (fontSize > 0) { |
| 22 |
size = fontSize; |
| 23 |
} |
| 24 |
list.pop_back(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
QFont::Style style = QFont::StyleNormal; |
| 29 |
QFont::Weight weight = QFont::Normal; |
| 30 |
const QMap<QString, QFont::Weight> strToWeight = { |
| 31 |
{"Thin", QFont::Thin}, |
| 32 |
{"Ultra-Light", QFont::Thin}, |
| 33 |
{"Extra-Light", QFont::ExtraLight}, |
| 34 |
{"Light", QFont::Light}, |
| 35 |
{"Semi-Light", QFont::Light}, |
| 36 |
{"Demi-Light", QFont::Light}, |
| 37 |
{"Book", QFont::Light}, |
| 38 |
{"Regular", QFont::Normal}, |
| 39 |
{"Medium", QFont::Medium}, |
| 40 |
{"Semi-Bold", QFont::Medium}, |
| 41 |
{"Demi-Bold", QFont::DemiBold}, |
| 42 |
{"Bold", QFont::Bold}, |
| 43 |
{"Ultra-Bold", QFont::Bold}, |
| 44 |
{"Extra-Bold", QFont::ExtraBold}, |
| 45 |
{"Black", QFont::Black}, |
| 46 |
{"Ultra-Black", QFont::Black}, |
| 47 |
{"Extra-Black", QFont::Black}, |
| 48 |
}; |
| 49 |
const QMap<QString, QFont::Style> strToStyle = { |
| 50 |
{"Italic", QFont::StyleItalic}, {"Oblique", QFont::StyleOblique}}; |
| 51 |
while (!list.empty()) { |
| 52 |
if (strToWeight.contains(list.back())) { |
| 53 |
weight = strToWeight.value(list.back(), QFont::Normal); |
| 54 |
list.pop_back(); |
| 55 |
} else if (strToStyle.contains(list.back())) { |
| 56 |
style = strToStyle.value(list.back(), QFont::StyleNormal); |
| 57 |
list.pop_back(); |
| 58 |
} else { |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
QString family = list.join(" "); |
| 63 |
QFont font; |
| 64 |
font.setFamily(family); |
| 65 |
font.setWeight(weight); |
| 66 |
font.setStyle(style); |
| 67 |
font.setPointSize(size); |
| 68 |
return font; |
| 69 |
} |