mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-19 00:42:14 -07:00
Adjust font size dynamically.
This commit is contained in:
parent
bc0b4eff88
commit
94a6e846a2
1 changed files with 41 additions and 16 deletions
|
|
@ -96,16 +96,12 @@ void CardInfoPictureWithTextOverlayWidget::paintEvent(QPaintEvent *event)
|
||||||
// Call the base class's paintEvent to draw the card image
|
// Call the base class's paintEvent to draw the card image
|
||||||
CardInfoPictureWidget::paintEvent(event);
|
CardInfoPictureWidget::paintEvent(event);
|
||||||
|
|
||||||
// Now add the custom text overlay on top of the image
|
// If no overlay text, skip drawing the text
|
||||||
if (overlayText.isEmpty()) {
|
if (overlayText.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
QStylePainter painter(this);
|
|
||||||
|
|
||||||
// Set text properties
|
QStylePainter painter(this);
|
||||||
QFont font = painter.font();
|
|
||||||
font.setPointSize(fontSize);
|
|
||||||
painter.setFont(font);
|
|
||||||
|
|
||||||
// Get the pixmap from the base class using the getter
|
// Get the pixmap from the base class using the getter
|
||||||
const QPixmap &pixmap = getResizedPixmap();
|
const QPixmap &pixmap = getResizedPixmap();
|
||||||
|
|
@ -118,13 +114,34 @@ void CardInfoPictureWithTextOverlayWidget::paintEvent(QPaintEvent *event)
|
||||||
const QPoint topLeft{(width() - scaledSize.width()) / 2, (height() - scaledSize.height()) / 2};
|
const QPoint topLeft{(width() - scaledSize.width()) / 2, (height() - scaledSize.height()) / 2};
|
||||||
const QRect pixmapRect(topLeft, scaledSize);
|
const QRect pixmapRect(topLeft, scaledSize);
|
||||||
|
|
||||||
// Prepare text wrapping
|
// Calculate the optimal font size
|
||||||
const QFontMetrics fontMetrics(font);
|
QFont font = painter.font();
|
||||||
const int lineHeight = fontMetrics.height();
|
int optimalFontSize = fontSize; // Start with the user-defined font size
|
||||||
const int textWidth = pixmapRect.width();
|
const QFontMetrics baseMetrics(font);
|
||||||
QString wrappedText;
|
int textWidth = pixmapRect.width();
|
||||||
|
|
||||||
// Break the text into multiple lines to fit within the pixmap width
|
// Reduce the font size until the text fits within the pixmap's width
|
||||||
|
do {
|
||||||
|
font.setPointSize(optimalFontSize);
|
||||||
|
QFontMetrics fm(font);
|
||||||
|
int currentWidth = 0;
|
||||||
|
for (const QString &word : overlayText.split(' ')) {
|
||||||
|
currentWidth = std::max(currentWidth, fm.horizontalAdvance(word));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (currentWidth <= textWidth) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
--optimalFontSize;
|
||||||
|
} while (optimalFontSize > 1);
|
||||||
|
|
||||||
|
// Apply the calculated font size
|
||||||
|
painter.setFont(font);
|
||||||
|
|
||||||
|
// Wrap the text to fit within the pixmap width
|
||||||
|
const QFontMetrics fontMetrics(font);
|
||||||
|
QString wrappedText;
|
||||||
QString currentLine;
|
QString currentLine;
|
||||||
QStringList words = overlayText.split(' ');
|
QStringList words = overlayText.split(' ');
|
||||||
for (const QString &word : words) {
|
for (const QString &word : words) {
|
||||||
|
|
@ -141,13 +158,22 @@ void CardInfoPictureWithTextOverlayWidget::paintEvent(QPaintEvent *event)
|
||||||
wrappedText += currentLine;
|
wrappedText += currentLine;
|
||||||
|
|
||||||
// Calculate total text block height
|
// Calculate total text block height
|
||||||
const int totalTextHeight = static_cast<int>(wrappedText.count('\n')) * lineHeight + lineHeight;
|
int totalTextHeight = wrappedText.count('\n') * fontMetrics.height() + fontMetrics.height();
|
||||||
|
|
||||||
|
// Adjust font size if the total text height exceeds the pixmap height
|
||||||
|
while (totalTextHeight > pixmapRect.height() && optimalFontSize > 1) {
|
||||||
|
--optimalFontSize;
|
||||||
|
font.setPointSize(optimalFontSize);
|
||||||
|
painter.setFont(font);
|
||||||
|
const QFontMetrics newMetrics(font);
|
||||||
|
totalTextHeight = wrappedText.count('\n') * newMetrics.height() + newMetrics.height();
|
||||||
|
}
|
||||||
|
|
||||||
// Set up the text layout options
|
// Set up the text layout options
|
||||||
QTextOption textOption;
|
QTextOption textOption;
|
||||||
textOption.setAlignment(textAlignment);
|
textOption.setAlignment(textAlignment);
|
||||||
|
|
||||||
// Create a text rectangle centered within the pixmap rect
|
// Create a text rectangle centered vertically within the pixmap rect
|
||||||
auto textRect = QRect(pixmapRect.left(), pixmapRect.top(), pixmapRect.width(), totalTextHeight);
|
auto textRect = QRect(pixmapRect.left(), pixmapRect.top(), pixmapRect.width(), totalTextHeight);
|
||||||
textRect.moveTop((pixmapRect.height() - totalTextHeight) / 2 + pixmapRect.top());
|
textRect.moveTop((pixmapRect.height() - totalTextHeight) / 2 + pixmapRect.top());
|
||||||
|
|
||||||
|
|
@ -169,7 +195,6 @@ void CardInfoPictureWithTextOverlayWidget::drawOutlinedText(QPainter &painter,
|
||||||
const QString &text,
|
const QString &text,
|
||||||
const QTextOption &textOption) const
|
const QTextOption &textOption) const
|
||||||
{
|
{
|
||||||
// Draw the black outline (outlineColor)
|
|
||||||
painter.setPen(outlineColor);
|
painter.setPen(outlineColor);
|
||||||
for (int dx = -1; dx <= 1; ++dx) {
|
for (int dx = -1; dx <= 1; ++dx) {
|
||||||
for (int dy = -1; dy <= 1; ++dy) {
|
for (int dy = -1; dy <= 1; ++dy) {
|
||||||
|
|
@ -180,7 +205,7 @@ void CardInfoPictureWithTextOverlayWidget::drawOutlinedText(QPainter &painter,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw the main text (textColor)
|
// Draw the main text
|
||||||
painter.setPen(textColor);
|
painter.setPen(textColor);
|
||||||
painter.drawText(textRect, text, textOption);
|
painter.drawText(textRect, text, textOption);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue