KDEUI
kfontutils.cpp
Go to the documentation of this file.
00001 /********************************************************************************* 00002 * * 00003 * Copyright (C) 2005, 2009 by Albert Astals Cid <aacid@kde.org> * 00004 * * 00005 * This library is free software; you can redistribute it and/or * 00006 * modify it under the terms of the GNU Lesser General Public * 00007 * License as published by the Free Software Foundation; either * 00008 * version 2.1 of the License, or (at your option) version 3, or any * 00009 * later version accepted by the membership of KDE e.V. (or its * 00010 * successor approved by the membership of KDE e.V.), which shall * 00011 * act as a proxy defined in Section 6 of version 3 of the license. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library. If not, see <http://www.gnu.org/licenses/>. * 00020 * * 00021 *********************************************************************************/ 00022 00023 #include "kfontutils.h" 00024 00025 #include <qpainter.h> 00026 00027 qreal KFontUtils::adaptFontSize(QPainter &painter, const QString &string, qreal width, qreal height, qreal maxFontSize, qreal minFontSize, AdaptFontSizeOptions flags) 00028 { 00029 qreal size = maxFontSize; 00030 QRectF boundingRect; 00031 bool done = false; 00032 00033 while (!done && size > minFontSize) { 00034 QFont f = painter.font(); 00035 f.setPointSizeF(size); 00036 painter.setFont(f); 00037 int qtFlags = Qt::AlignCenter | Qt::TextWordWrap; 00038 if (flags & DoNotAllowWordWrap) { 00039 qtFlags = qtFlags & ~Qt::TextWordWrap; 00040 } 00041 boundingRect = painter.boundingRect(QRectF(0, 0, width, height), qtFlags, string); 00042 if (boundingRect.width() == 0 || boundingRect.height() == 0) { 00043 return -1; 00044 } else if (boundingRect.width() > width || boundingRect.height() > height) { 00045 size = qMin(width * size / boundingRect.width(), height * size / boundingRect.height()); 00046 } else { 00047 done = true; 00048 } 00049 } 00050 00051 return size; 00052 } 00053 00054 qreal KFontUtils::adaptFontSize(QPainter &painter, const QString &text, const QSizeF &availableSize, qreal maxFontSize, qreal minFontSize, AdaptFontSizeOptions flags) 00055 { 00056 return adaptFontSize(painter, text, availableSize.width(), availableSize.height(), maxFontSize, minFontSize, flags); 00057 } 00058
KDE 4.7 API Reference