| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2005, 2009, 2014 Albert Astals Cid <aacid@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
| 5 | */ |
| 6 | |
| 7 | #include "kfontutils.h" |
| 8 | |
| 9 | #include <QPainter> |
| 10 | #include <qmath.h> |
| 11 | |
| 12 | static bool checkFits(QPainter &painter, const QString &string, qreal width, qreal height, qreal size, KFontUtils::AdaptFontSizeOptions flags) |
| 13 | { |
| 14 | QFont f = painter.font(); |
| 15 | f.setPointSizeF(size); |
| 16 | painter.setFont(f); |
| 17 | int qtFlags = Qt::AlignCenter | Qt::TextWordWrap; |
| 18 | if (flags & KFontUtils::DoNotAllowWordWrap) { |
| 19 | qtFlags &= ~Qt::TextWordWrap; |
| 20 | } |
| 21 | const QRectF boundingRect = painter.boundingRect(rect: QRectF(0, 0, width, height), flags: qtFlags, text: string); |
| 22 | if (boundingRect.width() == 0.0 || boundingRect.height() == 0.0) { |
| 23 | return false; |
| 24 | } else if (boundingRect.width() > width || boundingRect.height() > height) { |
| 25 | return false; |
| 26 | } |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | qreal KFontUtils::adaptFontSize(QPainter &painter, |
| 31 | const QString &string, |
| 32 | qreal width, |
| 33 | qreal height, |
| 34 | qreal maxFontSize, |
| 35 | qreal minFontSize, |
| 36 | AdaptFontSizeOptions flags) |
| 37 | { |
| 38 | // A invalid range is an error (-1) |
| 39 | if (maxFontSize < minFontSize) { |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | // If the max font size already fits, return it |
| 44 | if (checkFits(painter, string, width, height, size: maxFontSize, flags)) { |
| 45 | return maxFontSize; |
| 46 | } |
| 47 | |
| 48 | qreal fontSizeDoesNotFit = maxFontSize; |
| 49 | |
| 50 | // If the min font size does not fit, try to see if a font size of 1 fits, |
| 51 | // if it does not return error (-1) |
| 52 | // if it does, we'll return a fontsize smaller than the minFontSize as documented |
| 53 | if (!checkFits(painter, string, width, height, size: minFontSize, flags)) { |
| 54 | fontSizeDoesNotFit = minFontSize; |
| 55 | |
| 56 | minFontSize = 1; |
| 57 | if (!checkFits(painter, string, width, height, size: minFontSize, flags)) { |
| 58 | return -1; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | qreal fontSizeFits = minFontSize; |
| 63 | qreal nextFontSizeToTry = (fontSizeDoesNotFit + fontSizeFits) / 2; |
| 64 | |
| 65 | while (qFloor(v: fontSizeFits) != qFloor(v: nextFontSizeToTry)) { |
| 66 | if (checkFits(painter, string, width, height, size: nextFontSizeToTry, flags)) { |
| 67 | fontSizeFits = nextFontSizeToTry; |
| 68 | nextFontSizeToTry = (fontSizeDoesNotFit + fontSizeFits) / 2; |
| 69 | } else { |
| 70 | fontSizeDoesNotFit = nextFontSizeToTry; |
| 71 | nextFontSizeToTry = (nextFontSizeToTry + fontSizeFits) / 2; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | QFont f = painter.font(); |
| 76 | f.setPointSizeF(fontSizeFits); |
| 77 | painter.setFont(f); |
| 78 | |
| 79 | return fontSizeFits; |
| 80 | } |
| 81 | |
| 82 | qreal KFontUtils::adaptFontSize(QPainter &painter, |
| 83 | const QString &text, |
| 84 | const QSizeF &availableSize, |
| 85 | qreal maxFontSize, |
| 86 | qreal minFontSize, |
| 87 | AdaptFontSizeOptions flags) |
| 88 | { |
| 89 | return adaptFontSize(painter, string: text, width: availableSize.width(), height: availableSize.height(), maxFontSize, minFontSize, flags); |
| 90 | } |
| 91 | |