1 | /* |
2 | SPDX-FileCopyrightText: 2005, 2009 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 | #ifndef KFONTUTILS_H |
8 | #define KFONTUTILS_H |
9 | |
10 | #include <kguiaddons_export.h> |
11 | |
12 | #include <qglobal.h> |
13 | |
14 | class QPainter; |
15 | class QSizeF; |
16 | class QString; |
17 | |
18 | /** |
19 | * @namespace KFontUtils |
20 | * Provides utility functions for font data. |
21 | */ |
22 | namespace KFontUtils |
23 | { |
24 | /** |
25 | * Modifiers for the adaptFontSize function |
26 | * @see AdaptFontSizeOptions |
27 | */ |
28 | enum AdaptFontSizeOption { |
29 | NoFlags = 0x01, ///< No modifier |
30 | DoNotAllowWordWrap = 0x02, ///< Do not use word wrapping |
31 | }; |
32 | /** |
33 | * Stores a combination of #AdaptFontSizeOption values. |
34 | */ |
35 | Q_DECLARE_FLAGS(AdaptFontSizeOptions, AdaptFontSizeOption) |
36 | Q_DECLARE_OPERATORS_FOR_FLAGS(AdaptFontSizeOptions) |
37 | |
38 | /** Helper function that calculates the biggest font size (in points) used |
39 | drawing a centered text using word wrapping. |
40 | @param painter The painter where the text will be painted. The font set |
41 | in the painter is used for the calculation. Note the |
42 | painter font size is modified by this call |
43 | @param text The text you want to draw |
44 | @param width The available width for drawing |
45 | @param height The available height for drawing |
46 | @param maxFontSize The maximum font size (in points) to consider |
47 | @param minFontSize The minimum font size (in points) to consider |
48 | @param flags The modifiers for how the text is painted |
49 | @return The calculated biggest font size (in points) that draws the text |
50 | in the given dimensions. Can return smaller than minFontSize, |
51 | that means the text doesn't fit in the given rectangle. Can |
52 | return -1 on error |
53 | @since 4.7 |
54 | */ |
55 | qreal KGUIADDONS_EXPORT adaptFontSize(QPainter &painter, |
56 | const QString &text, |
57 | qreal width, |
58 | qreal height, |
59 | qreal maxFontSize = 28.0, |
60 | qreal minFontSize = 1.0, |
61 | AdaptFontSizeOptions flags = NoFlags); |
62 | |
63 | /** Convenience function for adaptFontSize that accepts a QSizeF instead two qreals |
64 | @since 4.7 |
65 | */ |
66 | qreal KGUIADDONS_EXPORT adaptFontSize(QPainter &painter, |
67 | const QString &text, |
68 | const QSizeF &availableSize, |
69 | qreal maxFontSize = 28.0, |
70 | qreal minFontSize = 1.0, |
71 | AdaptFontSizeOptions flags = NoFlags); |
72 | } |
73 | |
74 | #endif |
75 | |