add possiblity to use system font (#2)
This commit is contained in:
parent
ffca05522f
commit
3106a3474b
6 changed files with 90 additions and 50 deletions
|
|
@ -19,6 +19,9 @@
|
||||||
|
|
||||||
namespace style {
|
namespace style {
|
||||||
namespace internal {
|
namespace internal {
|
||||||
|
|
||||||
|
bool GetUseSystemFont();
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
constexpr auto kMinContrastAlpha = 64;
|
constexpr auto kMinContrastAlpha = 64;
|
||||||
|
|
@ -55,16 +58,18 @@ void ResolveMonospaceFont() {
|
||||||
if (!CustomMonospaceFont.isEmpty()) {
|
if (!CustomMonospaceFont.isEmpty()) {
|
||||||
tryFont(CustomMonospaceFont);
|
tryFont(CustomMonospaceFont);
|
||||||
}
|
}
|
||||||
|
if (!GetUseSystemFont()) {
|
||||||
tryFont("Consolas");
|
tryFont("Consolas");
|
||||||
tryFont("Liberation Mono");
|
tryFont("Liberation Mono");
|
||||||
tryFont("Menlo");
|
tryFont("Menlo");
|
||||||
tryFont("Courier");
|
tryFont("Courier");
|
||||||
|
}
|
||||||
if (family.isEmpty()) {
|
if (family.isEmpty()) {
|
||||||
const auto type = QFontDatabase::FixedFont;
|
const auto type = QFontDatabase::FixedFont;
|
||||||
family = QFontDatabase::systemFont(type).family();
|
family = QFontDatabase::systemFont(type).family();
|
||||||
}
|
}
|
||||||
const auto size = st::normalFont->f.pixelSize();
|
const auto size = st::normalFont->f.pixelSize();
|
||||||
ResolvedMonospaceFont = style::font(size, 0, family);
|
ResolvedMonospaceFont = style::font(size, 0, family)->monospace();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ QVector<QString> fontFamilies;
|
||||||
QMap<uint32, FontData*> fontsMap;
|
QMap<uint32, FontData*> fontsMap;
|
||||||
|
|
||||||
uint32 fontKey(int size, uint32 flags, int family) {
|
uint32 fontKey(int size, uint32 flags, int family) {
|
||||||
return (((uint32(family) << 10) | uint32(size)) << 4) | flags;
|
return (((uint32(family) << 12) | uint32(size)) << 6) | flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString RemoveSemiboldFromName(const QString &familyName) {
|
QString RemoveSemiboldFromName(const QString &familyName) {
|
||||||
|
|
@ -170,9 +170,14 @@ QString Overrides[FontTypesCount];
|
||||||
QString CustomMainFont;
|
QString CustomMainFont;
|
||||||
QString CustomSemiboldFont;
|
QString CustomSemiboldFont;
|
||||||
bool CustomSemiboldIsBold = false;
|
bool CustomSemiboldIsBold = false;
|
||||||
|
bool UseSystemFont = false;
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
bool GetUseSystemFont() {
|
||||||
|
return UseSystemFont;
|
||||||
|
}
|
||||||
|
|
||||||
void SetMainFont(const QString &familyName) {
|
void SetMainFont(const QString &familyName) {
|
||||||
CustomMainFont = familyName;
|
CustomMainFont = familyName;
|
||||||
}
|
}
|
||||||
|
|
@ -185,6 +190,10 @@ void SetSemiboldIsBold(bool isBold) {
|
||||||
CustomSemiboldIsBold = isBold;
|
CustomSemiboldIsBold = isBold;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetUseSystemFont(bool isSystemFont) {
|
||||||
|
UseSystemFont = isSystemFont;
|
||||||
|
}
|
||||||
|
|
||||||
void StartFonts() {
|
void StartFonts() {
|
||||||
if (Started) {
|
if (Started) {
|
||||||
return;
|
return;
|
||||||
|
|
@ -193,6 +202,7 @@ void StartFonts() {
|
||||||
|
|
||||||
style_InitFontsResource();
|
style_InitFontsResource();
|
||||||
|
|
||||||
|
if (!UseSystemFont) {
|
||||||
#ifndef DESKTOP_APP_USE_PACKAGED_FONTS
|
#ifndef DESKTOP_APP_USE_PACKAGED_FONTS
|
||||||
bool areGood[FontTypesCount] = { false };
|
bool areGood[FontTypesCount] = { false };
|
||||||
for (auto i = 0; i != FontTypesCount; ++i) {
|
for (auto i = 0; i != FontTypesCount; ++i) {
|
||||||
|
|
@ -228,6 +238,7 @@ void StartFonts() {
|
||||||
QFont::insertSubstitutions(name, list);
|
QFont::insertSubstitutions(name, list);
|
||||||
}
|
}
|
||||||
#endif // Q_OS_MAC
|
#endif // Q_OS_MAC
|
||||||
|
}
|
||||||
|
|
||||||
if (!CustomMainFont.isEmpty() && ValidateFont(CustomMainFont)) {
|
if (!CustomMainFont.isEmpty() && ValidateFont(CustomMainFont)) {
|
||||||
Overrides[FontTypeRegular] = CustomMainFont;
|
Overrides[FontTypeRegular] = CustomMainFont;
|
||||||
|
|
@ -286,11 +297,14 @@ int registerFontFamily(const QString &family) {
|
||||||
}
|
}
|
||||||
|
|
||||||
FontData::FontData(int size, uint32 flags, int family, Font *other)
|
FontData::FontData(int size, uint32 flags, int family, Font *other)
|
||||||
: f(ParseFamilyName(GetFontOverride(fontFamilies[family], flags)))
|
: f()
|
||||||
, m(f)
|
, m(f)
|
||||||
, _size(size)
|
, _size(size)
|
||||||
, _flags(flags)
|
, _flags(flags)
|
||||||
, _family(family) {
|
, _family(family) {
|
||||||
|
const auto fontOverride = GetFontOverride(fontFamilies[family], flags);
|
||||||
|
const auto possibleEmptyOverride = GetPossibleEmptyOverride(fontFamilies[family], flags);
|
||||||
|
|
||||||
if (other) {
|
if (other) {
|
||||||
memcpy(modified, other, sizeof(modified));
|
memcpy(modified, other, sizeof(modified));
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -298,11 +312,18 @@ FontData::FontData(int size, uint32 flags, int family, Font *other)
|
||||||
}
|
}
|
||||||
modified[_flags] = Font(this);
|
modified[_flags] = Font(this);
|
||||||
|
|
||||||
|
if (!UseSystemFont || !possibleEmptyOverride.isEmpty() || (_flags & FontMonospace)) {
|
||||||
|
f.setFamily(fontOverride);
|
||||||
|
}
|
||||||
|
|
||||||
f.setPixelSize(size);
|
f.setPixelSize(size);
|
||||||
if (_flags & FontBold) {
|
if (_flags & FontBold) {
|
||||||
f.setBold(true);
|
f.setBold(true);
|
||||||
} else if (fontFamilies[family] == "Open Sans Semibold" && CustomSemiboldIsBold) {
|
} else if (fontFamilies[family] == "Open Sans Semibold" && CustomSemiboldIsBold) {
|
||||||
f.setBold(true);
|
f.setBold(true);
|
||||||
|
} else if (fontFamilies[family] == "Open Sans Semibold" && UseSystemFont
|
||||||
|
&& possibleEmptyOverride.isEmpty()) {
|
||||||
|
f.setWeight(QFont::DemiBold);
|
||||||
#ifdef DESKTOP_APP_USE_PACKAGED_FONTS
|
#ifdef DESKTOP_APP_USE_PACKAGED_FONTS
|
||||||
} else if (fontFamilies[family] == "Open Sans Semibold") {
|
} else if (fontFamilies[family] == "Open Sans Semibold") {
|
||||||
f.setWeight(QFont::DemiBold);
|
f.setWeight(QFont::DemiBold);
|
||||||
|
|
@ -313,7 +334,7 @@ FontData::FontData(int size, uint32 flags, int family, Font *other)
|
||||||
f.setStrikeOut(_flags & FontStrikeOut);
|
f.setStrikeOut(_flags & FontStrikeOut);
|
||||||
f.setStyleStrategy(QFont::PreferQuality);
|
f.setStyleStrategy(QFont::PreferQuality);
|
||||||
|
|
||||||
if (IsRealSemibold(GetFontOverride(fontFamilies[family], flags))) {
|
if (IsRealSemibold(fontOverride)) {
|
||||||
f.setStyleName("Semibold");
|
f.setStyleName("Semibold");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -341,6 +362,10 @@ Font FontData::strikeout(bool set) const {
|
||||||
return otherFlagsFont(FontStrikeOut, set);
|
return otherFlagsFont(FontStrikeOut, set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Font FontData::monospace(bool set) const {
|
||||||
|
return otherFlagsFont(FontMonospace, set);
|
||||||
|
}
|
||||||
|
|
||||||
int FontData::size() const {
|
int FontData::size() const {
|
||||||
return _size;
|
return _size;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ namespace internal {
|
||||||
void SetMainFont(const QString &familyName);
|
void SetMainFont(const QString &familyName);
|
||||||
void SetSemiboldFont(const QString &familyName);
|
void SetSemiboldFont(const QString &familyName);
|
||||||
void SetSemiboldIsBold(bool isBold);
|
void SetSemiboldIsBold(bool isBold);
|
||||||
|
void SetUseSystemFont(bool isSystemFont);
|
||||||
|
|
||||||
void StartFonts();
|
void StartFonts();
|
||||||
[[nodiscard]] QString GetFontOverride(const QString &familyName, int32 flags = 0);
|
[[nodiscard]] QString GetFontOverride(const QString &familyName, int32 flags = 0);
|
||||||
|
|
@ -63,8 +64,9 @@ enum FontFlags {
|
||||||
FontItalic = 0x02,
|
FontItalic = 0x02,
|
||||||
FontUnderline = 0x04,
|
FontUnderline = 0x04,
|
||||||
FontStrikeOut = 0x08,
|
FontStrikeOut = 0x08,
|
||||||
|
FontMonospace = 0x10,
|
||||||
|
|
||||||
FontDifferentFlags = 0x10,
|
FontDifferentFlags = 0x20,
|
||||||
};
|
};
|
||||||
|
|
||||||
class FontData {
|
class FontData {
|
||||||
|
|
@ -87,6 +89,7 @@ public:
|
||||||
Font italic(bool set = true) const;
|
Font italic(bool set = true) const;
|
||||||
Font underline(bool set = true) const;
|
Font underline(bool set = true) const;
|
||||||
Font strikeout(bool set = true) const;
|
Font strikeout(bool set = true) const;
|
||||||
|
Font monospace(bool set = true) const;
|
||||||
|
|
||||||
int size() const;
|
int size() const;
|
||||||
uint32 flags() const;
|
uint32 flags() const;
|
||||||
|
|
|
||||||
|
|
@ -2015,8 +2015,8 @@ private:
|
||||||
auto result = f;
|
auto result = f;
|
||||||
if ((flags & TextBlockFPre) || (flags & TextBlockFCode)) {
|
if ((flags & TextBlockFPre) || (flags & TextBlockFCode)) {
|
||||||
result = style::MonospaceFont();
|
result = style::MonospaceFont();
|
||||||
if (result->size() != f->size() || result->flags() != f->flags()) {
|
if (result->size() != f->size()) {
|
||||||
result = style::font(f->size(), f->flags(), result->family());
|
result = style::font(f->size(), result->flags(), result->family());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (flags & TextBlockFBold) {
|
if (flags & TextBlockFBold) {
|
||||||
|
|
@ -2027,9 +2027,13 @@ private:
|
||||||
result = style::font(f->size(), f->flags(), result->family());
|
result = style::font(f->size(), f->flags(), result->family());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & TextBlockFItalic) result = result->italic();
|
if (flags & TextBlockFItalic) result = result->italic();
|
||||||
if (flags & TextBlockFUnderline) result = result->underline();
|
if (flags & TextBlockFUnderline) result = result->underline();
|
||||||
if (flags & TextBlockFStrikeOut) result = result->strikeout();
|
if (flags & TextBlockFStrikeOut) result = result->strikeout();
|
||||||
|
|
||||||
|
if (flags & TextBlockFSemibold) {
|
||||||
if (flags & TextBlockFTilde) { // tilde fix in OpenSans
|
if (flags & TextBlockFTilde) { // tilde fix in OpenSans
|
||||||
result = st::semiboldFont;
|
result = st::semiboldFont;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -323,8 +323,8 @@ TextBlock::TextBlock(const style::font &font, const QString &str, QFixed minResi
|
||||||
|
|
||||||
if ((flags & TextBlockFPre) || (flags & TextBlockFCode)) {
|
if ((flags & TextBlockFPre) || (flags & TextBlockFCode)) {
|
||||||
blockFont = style::MonospaceFont();
|
blockFont = style::MonospaceFont();
|
||||||
if (blockFont->size() != font->size() || blockFont->flags() != font->flags()) {
|
if (blockFont->size() != font->size()) {
|
||||||
blockFont = style::font(font->size(), font->flags(), blockFont->family());
|
blockFont = style::font(font->size(), blockFont->flags(), blockFont->family());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (flags & TextBlockFBold) {
|
if (flags & TextBlockFBold) {
|
||||||
|
|
@ -335,9 +335,13 @@ TextBlock::TextBlock(const style::font &font, const QString &str, QFixed minResi
|
||||||
blockFont = style::font(font->size(), font->flags(), blockFont->family());
|
blockFont = style::font(font->size(), font->flags(), blockFont->family());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (flags & TextBlockFItalic) blockFont = blockFont->italic();
|
if (flags & TextBlockFItalic) blockFont = blockFont->italic();
|
||||||
if (flags & TextBlockFUnderline) blockFont = blockFont->underline();
|
if (flags & TextBlockFUnderline) blockFont = blockFont->underline();
|
||||||
if (flags & TextBlockFStrikeOut) blockFont = blockFont->strikeout();
|
if (flags & TextBlockFStrikeOut) blockFont = blockFont->strikeout();
|
||||||
|
|
||||||
|
if (flags & TextBlockFSemibold) {
|
||||||
if (flags & TextBlockFTilde) { // tilde fix in OpenSans
|
if (flags & TextBlockFTilde) { // tilde fix in OpenSans
|
||||||
blockFont = st::semiboldFont;
|
blockFont = st::semiboldFont;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -657,9 +657,8 @@ void RemoveDocumentTags(
|
||||||
style::font AdjustFont(
|
style::font AdjustFont(
|
||||||
const style::font &font,
|
const style::font &font,
|
||||||
const style::font &original) {
|
const style::font &original) {
|
||||||
return (font->size() != original->size()
|
return (font->size() != original->size())
|
||||||
|| font->flags() != original->flags())
|
? style::font(original->size(), font->flags(), font->family())
|
||||||
? style::font(original->size(), original->flags(), font->family())
|
|
||||||
: font;
|
: font;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue