qt: Add Montserrat (picked in Dash Style Guide) as application font

Includes the following methods
 - GUIUtil::loadFonts to load the fonts into the application's font
database
 - GUIUtil::setApplicationFont to set the application font depending on
the active theme
 - GUIUtil::setFont to set a specific font variation for a vector of widgets (Montserrat for dash themes, system default for traditional theme).
 - GUIUtil::getFont to get a specific montserrat variation as QFont (Montserrat for dash themes, system default for traditional theme).
 - GUIUtil::getFontWeightNormal to get the font weight to be used for normal text
 - GUIUtil::getFontWeightBold to get the font weight to be used for
bolder text
 - GUIUtil::getFontNormal to get the default normal weighted QFont
 - GUIUtil::getFontBold to get the default bold weighted QFont

There is a Qt bug on macOS (at least with macOS 10.15.4 and Qt 5.7.1)
which leads to not mapping the font weights correctly to the different
font types so that they can be selected by the appropriate methods to
set the font weight. As result there is now way to properly select the
different font styles from stylesheet or with QFont::setWeight() for the
font Montserrat. There are only two different weights selectable over
the full range of possible weight values. One very bold one and the
"ExtraLight" version. As workaround its possible to select the fonts by
using the font style string (QFont::setStyleName) as the Montserrat font becomes loaded as one
family with one "font style" for each font file. The font wrapper added
by this commit are taking care of this behaviour for all operating
systems so there should be no other usage of QFont outside of GUIUtil and no more font changes in stylesheets.
This commit is contained in:
xdustinface 2020-05-29 10:16:08 +02:00
parent b463e5780e
commit 787740c0b2
25 changed files with 415 additions and 2 deletions

View File

@ -355,6 +355,26 @@ RES_CSS = \
qt/res/css/scrollbars.css \
qt/res/css/trad.css
RES_FONTS = \
qt/res/fonts/Montserrat/Montserrat-Black.otf \
qt/res/fonts/Montserrat/Montserrat-BlackItalic.otf \
qt/res/fonts/Montserrat/Montserrat-Bold.otf \
qt/res/fonts/Montserrat/Montserrat-BoldItalic.otf \
qt/res/fonts/Montserrat/Montserrat-ExtraBold.otf \
qt/res/fonts/Montserrat/Montserrat-ExtraBoldItalic.otf \
qt/res/fonts/Montserrat/Montserrat-ExtraLight.otf \
qt/res/fonts/Montserrat/Montserrat-ExtraLightItalic.otf \
qt/res/fonts/Montserrat/Montserrat-Italic.otf \
qt/res/fonts/Montserrat/Montserrat-Light.otf \
qt/res/fonts/Montserrat/Montserrat-LightItalic.otf \
qt/res/fonts/Montserrat/Montserrat-Medium.otf \
qt/res/fonts/Montserrat/Montserrat-MediumItalic.otf \
qt/res/fonts/Montserrat/Montserrat-Regular.otf \
qt/res/fonts/Montserrat/Montserrat-SemiBold.otf \
qt/res/fonts/Montserrat/Montserrat-SemiBoldItalic.otf \
qt/res/fonts/Montserrat/Montserrat-Thin.otf \
qt/res/fonts/Montserrat/Montserrat-ThinItalic.otf
RES_MOVIES = $(wildcard $(srcdir)/qt/res/movies/spinner-*.png)
BITCOIN_RC = qt/res/dash-qt-res.rc
@ -367,7 +387,7 @@ qt_libdashqt_a_CXXFLAGS = $(AM_CXXFLAGS) $(QT_PIE_FLAGS)
qt_libdashqt_a_OBJCXXFLAGS = $(AM_OBJCXXFLAGS) $(QT_PIE_FLAGS)
qt_libdashqt_a_SOURCES = $(BITCOIN_QT_CPP) $(BITCOIN_QT_H) $(QT_FORMS_UI) \
$(QT_QRC) $(QT_QRC_LOCALE) $(QT_TS) $(PROTOBUF_PROTO) $(RES_ICONS) $(RES_IMAGES) $(RES_CSS) $(RES_MOVIES)
$(QT_QRC) $(QT_QRC_LOCALE) $(QT_TS) $(PROTOBUF_PROTO) $(RES_ICONS) $(RES_IMAGES) $(RES_CSS) $(RES_FONTS) $(RES_MOVIES)
nodist_qt_libdashqt_a_SOURCES = $(QT_MOC_CPP) $(QT_MOC) $(PROTOBUF_CC) \
$(PROTOBUF_H) $(QT_QRC_CPP) $(QT_QRC_LOCALE_CPP)
@ -430,7 +450,7 @@ $(QT_QRC_LOCALE_CPP): $(QT_QRC_LOCALE) $(QT_QM)
$(SED) -e '/^\*\*.*Created:/d' -e '/^\*\*.*by:/d' > $@
@rm $(@D)/temp_$(<F)
$(QT_QRC_CPP): $(QT_QRC) $(QT_FORMS_H) $(RES_ICONS) $(RES_IMAGES) $(RES_CSS) $(RES_MOVIES) $(PROTOBUF_H)
$(QT_QRC_CPP): $(QT_QRC) $(QT_FORMS_H) $(RES_ICONS) $(RES_IMAGES) $(RES_CSS) $(RES_FONTS) $(RES_MOVIES) $(PROTOBUF_H)
@test -f $(RCC)
$(AM_V_GEN) QT_SELECT=$(QT_SELECT) $(RCC) $(RCCFLAGS) -name dash $< | \
$(SED) -e '/^\*\*.*Created:/d' -e '/^\*\*.*by:/d' > $@

View File

@ -272,6 +272,9 @@ bool BitcoinCore::baseInitialize()
{
return false;
}
if (!GUIUtil::loadFonts()) {
return false;
}
return true;
}

View File

@ -58,6 +58,26 @@
<file alias="general">res/css/general.css</file>
<file alias="scrollbars">res/css/scrollbars.css</file>
</qresource>
<qresource prefix="/fonts">
<file alias="Montserrat-Black">res/fonts/Montserrat/Montserrat-Black.otf</file>
<file alias="Montserrat-BlackItalic">res/fonts/Montserrat/Montserrat-BlackItalic.otf</file>
<file alias="Montserrat-Bold">res/fonts/Montserrat/Montserrat-Bold.otf</file>
<file alias="Montserrat-BoldItalic">res/fonts/Montserrat/Montserrat-BoldItalic.otf</file>
<file alias="Montserrat-ExtraBold">res/fonts/Montserrat/Montserrat-ExtraBold.otf</file>
<file alias="Montserrat-ExtraBoldItalic">res/fonts/Montserrat/Montserrat-ExtraBoldItalic.otf</file>
<file alias="Montserrat-ExtraLight">res/fonts/Montserrat/Montserrat-ExtraLight.otf</file>
<file alias="Montserrat-ExtraLightItalic">res/fonts/Montserrat/Montserrat-ExtraLightItalic.otf</file>
<file alias="Montserrat-Italic">res/fonts/Montserrat/Montserrat-Italic.otf</file>
<file alias="Montserrat-Light">res/fonts/Montserrat/Montserrat-Light.otf</file>
<file alias="Montserrat-LightItalic">res/fonts/Montserrat/Montserrat-LightItalic.otf</file>
<file alias="Montserrat-Medium">res/fonts/Montserrat/Montserrat-Medium.otf</file>
<file alias="Montserrat-MediumItalic">res/fonts/Montserrat/Montserrat-MediumItalic.otf</file>
<file alias="Montserrat-Regular">res/fonts/Montserrat/Montserrat-Regular.otf</file>
<file alias="Montserrat-SemiBold">res/fonts/Montserrat/Montserrat-SemiBold.otf</file>
<file alias="Montserrat-SemiBoldItalic">res/fonts/Montserrat/Montserrat-SemiBoldItalic.otf</file>
<file alias="Montserrat-Thin">res/fonts/Montserrat/Montserrat-Thin.otf</file>
<file alias="Montserrat-ThinItalic">res/fonts/Montserrat/Montserrat-ThinItalic.otf</file>
</qresource>
<qresource prefix="/themes">
<file alias="Dark">res/css/dark.css</file>
<file alias="Light">res/css/light.css</file>

View File

@ -16,6 +16,7 @@
#include <protocol.h>
#include <script/script.h>
#include <script/standard.h>
#include <ui_interface.h>
#include <util.h>
#ifdef WIN32
@ -42,6 +43,7 @@
#include <QApplication>
#include <QClipboard>
#include <QDateTime>
#include <QDebug>
#include <QDesktopServices>
#include <QDesktopWidget>
#include <QDoubleValidator>
@ -85,6 +87,26 @@ static const QString defaultTheme = "Light";
// The prefix a theme name should have if we want to apply dark colors and styles to it
static const QString darkThemePrefix = "Dark";
/** Font related default values. */
static const QString defaultFontFamily = "Montserrat";
#ifdef Q_OS_MAC
static const QFont::Weight defaultFontWeightNormal = QFont::ExtraLight;
static const QFont::Weight defaultFontWeightBold = QFont::Medium;
static const int defaultFontSize = 12;
#else
static const QFont::Weight defaultFontWeightNormal = QFont::Light;
static const QFont::Weight defaultFontWeightBold = QFont::Medium;
static const int defaultFontSize = 12;
#endif
/** Font related variables. */
// Weight for normal text
QFont::Weight fontWeightNormal = defaultFontWeightNormal;
// Weight for bold text
QFont::Weight fontWeightBold = defaultFontWeightBold;
// Weight for bold text
int fontSize = defaultFontSize;
static const std::map<ThemedColor, QColor> themedColors = {
{ ThemedColor::DEFAULT, QColor(0, 0, 0) },
{ ThemedColor::UNCONFIRMED, QColor(128, 128, 128) },
@ -978,6 +1000,184 @@ QString loadStyleSheet()
return *stylesheet.get();
}
QFont::Weight getFontWeightNormal()
{
return fontWeightNormal;
}
QFont::Weight getFontWeightBold()
{
return fontWeightBold;
}
int getFontSize()
{
return fontSize;
}
bool loadFonts()
{
QString family = defaultFontFamily;
QString italic = "Italic";
std::map<QString, bool> mapStyles{
{"Thin", true},
{"ExtraLight", true},
{"Light", true},
{"Italic", false},
{"Regular", false},
{"Medium", true},
{"SemiBold", true},
{"Bold", true},
{"ExtraBold", true},
{"Black", true},
};
QFontDatabase database;
std::vector<int> vecFontIds;
for (const auto& it : mapStyles) {
QString font = ":fonts/" + family + "-" + it.first;
vecFontIds.push_back(QFontDatabase::addApplicationFont(font));
qDebug() << __func__ << ": " << font << " loaded with id " << vecFontIds.back();
if (it.second) {
vecFontIds.push_back(QFontDatabase::addApplicationFont(font + italic));
qDebug() << __func__ << ": " << font + italic << " loaded with id " << vecFontIds.back();
}
}
// Fail if an added id is -1 which means QFontDatabase::addApplicationFont failed.
if (std::find(vecFontIds.begin(), vecFontIds.end(), -1) != vecFontIds.end()) {
return InitError("Font loading failed.");
}
// Print debug logs for added fonts fetched by the added ids
for (const auto& i : vecFontIds) {
auto families = QFontDatabase::applicationFontFamilies(i);
for (const QString& f : families) {
qDebug() << __func__ << ": - Font id " << i << " is family: " << f;
const QStringList fontStyles = database.styles(f);
for (const QString& style : fontStyles) {
qDebug() << __func__ << ": Style for family " << f << " with id: " << i << ": " << style;
}
}
}
// Print debug logs for added fonts fetched by the family name
const QStringList fontFamilies = database.families();
for (const QString& f : fontFamilies) {
if (f.contains(family)) {
const QStringList fontStyles = database.styles(f);
for (const QString& style : fontStyles) {
qDebug() << __func__ << ": Family: " << f << ", Style: " << style;
}
}
}
setApplicationFont();
return true;
}
void setApplicationFont()
{
static QFont osDefaultFont = QApplication::font();
if (dashThemeActive()) {
QString family = defaultFontFamily;
QFont appFont = QFont(family);
#ifdef Q_OS_MAC
if (getFontWeightNormal() != defaultFontWeightNormal) {
appFont = getFontNormal();
} else {
appFont.setWeight(defaultFontWeightNormal);
}
appFont.setPointSize(getFontSize());
#else
appFont.setWeight(getFontWeightNormal());
appFont.setPointSize(getFontSize());
#endif
qApp->setFont(appFont);
} else {
qApp->setFont(osDefaultFont);
}
qDebug() << __func__ << ": " << qApp->font().toString() <<
" family: " << qApp->font().family() <<
", style: " << qApp->font().styleName() <<
" match: " << qApp->font().exactMatch();
}
void setFont(const std::vector<QWidget*> &vecWidgets, QFont::Weight weight, bool fItalic)
{
QFont font = getFont(weight, fItalic);
for (auto it : vecWidgets) {
it->setFont(font);
}
}
QFont getFont(QFont::Weight weight, bool fItalic)
{
QFont font;
if (dashThemeActive()) {
static std::map<QFont::Weight, QString> mapMontserratMapping{
{QFont::Thin, "Thin"},
{QFont::ExtraLight, "ExtraLight"},
{QFont::Light, "Light"},
{QFont::Medium, "Medium"},
{QFont::DemiBold, "SemiBold"},
{QFont::ExtraBold, "ExtraBold"},
{QFont::Black, "Black"},
#ifdef Q_OS_MAC
{QFont::Normal, "Regular"},
{QFont::Bold, "Bold"},
#else
{QFont::Normal, ""},
{QFont::Bold, ""},
#endif
};
assert(mapMontserratMapping.count(weight));
#ifdef Q_OS_MAC
QString styleName = mapMontserratMapping[weight];
if (fItalic) {
if (styleName == "Regular") {
styleName = "Italic";
} else {
styleName += " Italic";
}
}
font.setFamily(defaultFontFamily);
font.setStyleName(styleName);
#else
font.setFamily(defaultFontFamily + " " + mapMontserratMapping[weight]);
font.setWeight(weight);
font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal);
#endif
} else {
font.setFamily(QApplication::font().family());
font.setWeight(weight > QFont::Normal ? QFont::Bold : QFont::Normal);
font.setStyle(fItalic ? QFont::StyleItalic : QFont::StyleNormal);
}
qDebug() << "GUIUtil::getFont() - " << font.toString() << " family: " << font.family() << ", style: " << font.styleName() << " match: " << font.exactMatch();
return font;
}
QFont getFontNormal()
{
return getFont(getFontWeightNormal());
}
QFont getFontBold()
{
return getFont(getFontWeightBold());
}
bool dashThemeActive()
{
QSettings settings;

View File

@ -233,6 +233,40 @@ namespace GUIUtil
/** Load global CSS theme */
QString loadStyleSheet();
/** Application font weight for normal text. */
extern QFont::Weight fontWeightNormal;
/** Application font weight for bold text. */
extern QFont::Weight fontWeightBold;
/** Application base font size. */
extern int fontSize;
/** get normal font weight */
QFont::Weight getFontWeightNormal();
/** get bold font weight */
QFont::Weight getFontWeightBold();
/** get font size */
int getFontSize();
/** Load dash specific appliciation fonts */
bool loadFonts();
/** Set an application wide default font, depends on the selected theme */
void setApplicationFont();
/** Workaround to set correct font styles in all themes since there is a bug in macOS which leads to
issues loading variations of montserrat in css */
void setFont(const std::vector<QWidget*>& vecWidgets, QFont::Weight weight, bool fItalic = false);
/** Get a properly weighted QFont object with the font Montserrat
Use ExtraLight as default as this lines up with the default in css. */
QFont getFont(QFont::Weight weight, bool fItalic = false);
/** Get the default normal QFont */
QFont getFontNormal();
/** Get the default bold QFont */
QFont getFontBold();
/** Check if a dash specific theme is activated (light/dark) */
bool dashThemeActive();

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,93 @@
Copyright 2011 The Montserrat Project Authors (https://github.com/JulietaUla/Montserrat)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each cop
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.

View File

@ -0,0 +1,43 @@
Copyright 2011 The Montserrat Project Authors (https://github.com/JulietaUla/Montserrat)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide development of collaborative font projects, to support the font creation efforts of academic and linguistic communities, and to provide a free and open framework in which fonts may be shared and improved in partnership with others.
The OFL allows the licensed fonts to be used, studied, modified and redistributed freely as long as they are not sold by themselves. The fonts, including any derivative works, can be bundled, embedded, redistributed and/or sold with any software provided that any reserved names are not used by derivative works. The fonts and derivatives, however, cannot be released under any other type of license. The requirement for fonts to remain under this license does not apply to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright Holder(s) under this license and clearly marked as such. This may include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the copyright statement(s).
"Original Version" refers to the collection of Font Software components as distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting, or substituting -- in part or in whole -- any of the components of the Original Version, by changing formats or by porting the Font Software to a new environment.
"Author" refers to any designer, engineer, programmer, technical writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining a copy of the Font Software, to use, study, copy, merge, embed, modify, redistribute, and sell modified and unmodified copies of the Font Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components, in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled, redistributed and/or sold with any software, provided that each copy contains the above copyright notice and this license. These can be included either as stand-alone text files, human-readable headers or in the appropriate machine-readable metadata fields within text or binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font Name(s) unless explicit written permission is granted by the corresponding Copyright Holder. This restriction only applies to the primary font name as presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font Software shall not be used to promote, endorse or advertise any Modified Version, except to acknowledge the contribution(s) of the Copyright Holder(s) and the Author(s) or with their explicit written permission.
5) The Font Software, modified or unmodified, in part or in whole, must be distributed entirely under this license, and must not be distributed under any other license. The requirement for fonts to remain under this license does not apply to any document created using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.