File indexing completed on 2024-04-21 14:56:36

0001 /*
0002     SPDX-FileCopyrightText: 2005, 2009 Albert Astals Cid <aacid@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KFONTUTILS_H
0008 #define KFONTUTILS_H
0009 
0010 #include <kguiaddons_export.h>
0011 
0012 #include <qglobal.h>
0013 
0014 class QPainter;
0015 class QSizeF;
0016 class QString;
0017 
0018 /**
0019  * @namespace KFontUtils
0020  * Provides utility functions for font data.
0021  */
0022 namespace KFontUtils
0023 {
0024 /**
0025  * Modifiers for the adaptFontSize function
0026  * @see AdaptFontSizeOptions
0027  */
0028 enum AdaptFontSizeOption {
0029     NoFlags = 0x01, ///< No modifier
0030     DoNotAllowWordWrap = 0x02, ///< Do not use word wrapping
0031 };
0032 /**
0033  * Stores a combination of #AdaptFontSizeOption values.
0034  */
0035 Q_DECLARE_FLAGS(AdaptFontSizeOptions, AdaptFontSizeOption)
0036 Q_DECLARE_OPERATORS_FOR_FLAGS(AdaptFontSizeOptions)
0037 
0038 /** Helper function that calculates the biggest font size (in points) used
0039     drawing a centered text using word wrapping.
0040     @param painter The painter where the text will be painted. The font set
0041                    in the painter is used for the calculation. Note the
0042                    painter font size is modified by this call
0043     @param text The text you want to draw
0044     @param width The available width for drawing
0045     @param height The available height for drawing
0046     @param maxFontSize The maximum font size (in points) to consider
0047     @param minFontSize The minimum font size (in points) to consider
0048     @param flags The modifiers for how the text is painted
0049     @return The calculated biggest font size (in points) that draws the text
0050             in the given dimensions. Can return smaller than minFontSize,
0051             that means the text doesn't fit in the given rectangle. Can
0052             return -1 on error
0053     @since 4.7
0054 */
0055 qreal KGUIADDONS_EXPORT adaptFontSize(QPainter &painter,
0056                                       const QString &text,
0057                                       qreal width,
0058                                       qreal height,
0059                                       qreal maxFontSize = 28.0,
0060                                       qreal minFontSize = 1.0,
0061                                       AdaptFontSizeOptions flags = NoFlags);
0062 
0063 /** Convenience function for adaptFontSize that accepts a QSizeF instead two qreals
0064     @since 4.7
0065 */
0066 qreal KGUIADDONS_EXPORT adaptFontSize(QPainter &painter,
0067                                       const QString &text,
0068                                       const QSizeF &availableSize,
0069                                       qreal maxFontSize = 28.0,
0070                                       qreal minFontSize = 1.0,
0071                                       AdaptFontSizeOptions flags = NoFlags);
0072 }
0073 
0074 #endif