Skip to content

Commit

Permalink
Make all splash screen text scalable
Browse files Browse the repository at this point in the history
  • Loading branch information
10110111 committed Jan 25, 2025
1 parent 023e726 commit 76fed07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
Binary file modified data/splash-gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified data/splash.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 59 additions & 6 deletions src/StelSplashScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace
constexpr int BASE_FONT_SIZE = 11;
constexpr int BASE_PIXMAP_HEIGHT = 365;

const QString titleText = "Stellarium";
const QString subtitleText = "stellarium.org";

QPixmap makePixmap(const double sizeRatio)
{
QPixmap pixmap(StelFileMgr::findFile("data/splash.png"));
Expand All @@ -39,6 +42,24 @@ QPixmap makePixmap(const double sizeRatio)
return pixmap;
}

void findBestFontStretch(QFont& font, const QString& text, const int desiredWidth)
{
int min = 1, max = 200;
while(max - min > 1)
{
const int curr = (min + max) / 2;
font.setStretch(curr);
const auto br = QFontMetrics(font).tightBoundingRect(text);
if(br.width() > desiredWidth)
max = curr;
else if(br.width() < desiredWidth)
min = curr;
else // if exactly equal
return;
}
font.setStretch(max);
}

}

void SplashScreen::present(const double sizeRatio)
Expand Down Expand Up @@ -72,6 +93,7 @@ void SplashScreen::clearMessage()

SplashScreen::SplashScreenWidget::SplashScreenWidget(QPixmap const& pixmap, const double sizeRatio)
: QSplashScreen(pixmap)
, titleFont("Sans")
, sizeRatio(sizeRatio)
{
splashFont.setPixelSize(std::lround(BASE_FONT_SIZE * sizeRatio));
Expand All @@ -85,30 +107,61 @@ SplashScreen::SplashScreenWidget::SplashScreenWidget(QPixmap const& pixmap, cons
splashFont.setStyleHint(QFont::AnyStyle, QFont::PreferAntialias);

setFont(splashFont);

titleFont.setPixelSize(55 * sizeRatio);
titleFont.setWeight(QFont::Bold);
findBestFontStretch(titleFont, titleText, sizeRatio * 290);

subtitleFont = titleFont;
subtitleFont.setWeight(QFont::Normal);
subtitleFont.setPixelSize(18 * sizeRatio);

versionFont = splashFont;
versionFont.setPixelSize(23 * sizeRatio);
versionFont.setBold(true);
const auto lastTitleLetterBR = QFontMetrics(titleFont).tightBoundingRect(titleText.back());
findBestFontStretch(versionFont, StelUtils::getApplicationPublicVersion(),
lastTitleLetterBR.width());
}

void SplashScreen::SplashScreenWidget::paintEvent(QPaintEvent* event)
{
QSplashScreen::paintEvent(event);

// Add version text
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing);
p.setPen(Qt::white);

// Add branding text: "Stellarium" in large bold font centered horizontally,
// and "stellarium.org" under it in a smaller font, both aligned to each
// other on the right side.
QFontMetrics titleFM(titleFont);
auto titleBR = titleFM.tightBoundingRect(titleText);
const QPoint titlePos((width() - titleBR.width()) / 2, std::lround(height() * 0.89));
titleBR.translate(titlePos);
p.setFont(titleFont);
p.drawText(titlePos, titleText);

const QFontMetrics subtitleFM(subtitleFont);
const auto subtitleBR = subtitleFM.tightBoundingRect(subtitleText);
p.setFont(subtitleFont);
p.drawText(QPoint(titleBR.right() - subtitleBR.width() - 1,
titleBR.bottom() + subtitleBR.height()),
subtitleText);

// Add version text
#if defined(GIT_REVISION)
QFontMetrics metrics(splashFont);
p.setFont(font());
const auto verStr = QString("%1+ (v%2)").arg(StelUtils::getApplicationPublicVersion(),
StelUtils::getApplicationVersion());
p.drawText(QPointF(metrics.averageCharWidth(), 1.3*metrics.height()), verStr);
#else
QFont versionFont(splashFont);
QString version = StelUtils::getApplicationPublicVersion();
versionFont.setPixelSize(23 * sizeRatio);
versionFont.setBold(true);
QFontMetrics versionMetrics(versionFont);
p.setFont(versionFont);
const int height = BASE_PIXMAP_HEIGHT * sizeRatio;
p.drawText(height - versionMetrics.horizontalAdvance(version), 0.803 * height, version);
const int verWidth = versionMetrics.tightBoundingRect(version).width();
p.drawText(titleBR.right() - verWidth - 1, 0.8 * height(), version);
#endif

painted=true;
Expand Down
3 changes: 3 additions & 0 deletions src/StelSplashScreen.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class SplashScreen
class SplashScreenWidget : public QSplashScreen
{
QFont splashFont;
QFont titleFont;
QFont subtitleFont;
QFont versionFont;
bool painted=false;
public:
SplashScreenWidget(QPixmap const& pixmap, double sizeRatio);
Expand Down

0 comments on commit 76fed07

Please sign in to comment.