KGuiAddons

kfontutils.cpp
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
12static 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;
19 qtFlags &= ~Qt::TextWordWrap;
20 }
21 const QRectF boundingRect = painter.boundingRect(QRectF(0, 0, width, height), qtFlags, 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
31 const QString &string,
32 qreal width,
33 qreal height,
34 qreal maxFontSize,
35 qreal minFontSize,
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, 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, minFontSize, flags)) {
54 fontSizeDoesNotFit = minFontSize;
55
56 minFontSize = 1;
57 if (!checkFits(painter, string, width, height, minFontSize, flags)) {
58 return -1;
59 }
60 }
61
62 qreal fontSizeFits = minFontSize;
63 qreal nextFontSizeToTry = (fontSizeDoesNotFit + fontSizeFits) / 2;
64
65 while (qFloor(fontSizeFits) != qFloor(nextFontSizeToTry)) {
66 if (checkFits(painter, string, width, height, 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
83 const QString &text,
84 const QSizeF &availableSize,
85 qreal maxFontSize,
86 qreal minFontSize,
88{
89 return adaptFontSize(painter, text, availableSize.width(), availableSize.height(), maxFontSize, minFontSize, flags);
90}
qreal KGUIADDONS_EXPORT adaptFontSize(QPainter &painter, const QString &text, qreal width, qreal height, qreal maxFontSize=28.0, qreal minFontSize=1.0, AdaptFontSizeOptions flags=NoFlags)
Helper function that calculates the biggest font size (in points) used drawing a centered text using ...
@ DoNotAllowWordWrap
Do not use word wrapping.
Definition kfontutils.h:30
void setPointSizeF(qreal pointSize)
QRect boundingRect(const QRect &rectangle, int flags, const QString &text)
const QFont & font() const const
void setFont(const QFont &font)
qreal height() const const
qreal width() const const
qreal height() const const
qreal width() const const
AlignCenter
TextWordWrap
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:51:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.