Cockatrice/cockatrice/src/interface/widgets/cards/art_crop_attribution.cpp
BruebachL 396196b762
[Fonts] Migrate CardText surfaces to theme font role (#7538)
* [Fonts] Migrate CardText surfaces to theme font role

* [Fonts] Keep CardText call sites on the app font and fix px sizes

- CardText's unset fallback is qApp->font() again: only the three counter
  painters (paintNumberEllipse, GeneralCounter, PlayerCounter) were Serif
  before the role existed, and they now ask for it through
  ThemeManager::cardTextFont(). Card names, player names, counters, the
  overlay, the Archidekt and EDHREC labels and the completer delegate keep the
  app font they used before.
- Use setPixelSize for the EDHREC card name (20px) and commander name (28px)
  and details (16px) sizes: the stylesheets said px, and setPointSize made
  them about a third larger.

---------

Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
2026-11-04 07:57:54 +01:00

74 lines
2.2 KiB
C++

#include "art_crop_attribution.h"
#include "../../theme_manager.h"
#include "libcockatrice/card/printing/printing_info.h"
#include <QColor>
#include <QFont>
#include <QFontMetrics>
#include <QObject>
#include <QPainter>
#include <QPointF>
#include <QRectF>
#include <QSizeF>
#include <QString>
#include <QtGlobal>
#include <libcockatrice/card/printing/exact_card.h>
QString buildArtAttribution(const ExactCard &card)
{
const QString artist = card.getPrinting().getArtist();
if (artist.isEmpty()) {
return QString();
}
return QObject::tr("Art: %1").arg(artist);
}
QRectF paintArtAttribution(QPainter &painter,
const QRectF &rect,
const QString &attribution,
Qt::Alignment anchor,
qreal scale)
{
if (attribution.isEmpty()) {
return QRectF();
}
painter.save();
// Family comes from the CardText theme role; the incoming painter size
// stays the basis for the scale factor exactly as before.
QFont font = painter.font();
font.setFamily(themeManager->appFont(AppFont::CardText).family());
font.setPointSizeF(qMax(6.0, font.pointSizeF() * scale));
painter.setFont(font);
const QFontMetrics fm(font);
const qreal maxTextWidth = rect.width() * 0.45;
const QString elided = fm.elidedText(attribution, Qt::ElideRight, qMax(qreal(80.0) * scale, maxTextWidth));
const qreal pad = 6.0 * scale;
QRectF captionRect(QPointF(0, 0), QSizeF(fm.horizontalAdvance(elided) + pad * 2.0, fm.height() + pad * 2.0));
const qreal margin = 4.0 * scale;
if (anchor.testFlag(Qt::AlignLeft)) {
captionRect.moveLeft(rect.left() + margin);
} else {
captionRect.moveRight(rect.right() - margin);
}
if (anchor.testFlag(Qt::AlignTop)) {
captionRect.moveTop(rect.top() + margin);
} else {
captionRect.moveBottom(rect.bottom() - margin);
}
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(0, 0, 0, 120));
painter.drawRoundedRect(captionRect, 4, 4);
painter.setPen(QColor(255, 255, 255, 220));
painter.drawText(captionRect, Qt::AlignCenter, elided);
painter.restore();
return captionRect;
}