00001 /* This file is part of the KDE project 00002 Copyright (C) 2001-2005 David Faure <faure@kde.org> 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Library General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "KoTextZoomHandler.h" 00021 #include <kdebug.h> 00022 #include <qpaintdevice.h> 00023 #include <KoUnit.h> 00024 #include <KoGlobal.h> 00025 00026 // Layout text at 1440 DPI 00027 // Well, not really always 1440 DPI, but always 20 times the point size 00028 // This is constant, no need to litterally apply 1440 DPI at all resolutions. 00029 int KoTextZoomHandler::m_layoutUnitFactor = 20; 00030 00031 #if 0 00032 int KoTextZoomHandler::fontSizeToLayoutUnit( double ptSizeFloat, bool forPrint ) const 00033 { 00034 return ptToLayoutUnit( ptSizeFloat / ( m_zoomedResolutionY * 00035 ( forPrint ? 1.0 : (72.0 / KoGlobal::dpiY()) ) ) ); 00036 } 00037 #endif 00038 00039 double KoTextZoomHandler::layoutUnitToFontSize( int luSize, bool /*forPrint*/ ) const 00040 { 00041 // Qt will use QPaintDevice::x11AppDpiY() to go from pt to pixel for fonts 00042 return layoutUnitPtToPt( luSize ) * m_zoomedResolutionY 00043 #ifdef Q_WS_X11 00044 / POINT_TO_INCH(QPaintDevice::x11AppDpiY()) 00045 #endif 00046 ; 00047 } 00048 00049 int KoTextZoomHandler::layoutUnitToPixelX( int x, int w ) const 00050 { 00051 // We call layoutUnitToPixelX on the right value, i.e. x+w-1, 00052 // and then determine the height from the result (i.e. right-left+1). 00053 // Calling layoutUnitToPixelX(w) leads to rounding problems. 00054 return layoutUnitToPixelY( x + w - 1 ) - layoutUnitToPixelY( x ) + 1; 00055 } 00056 00057 int KoTextZoomHandler::layoutUnitToPixelY( int y, int h ) const 00058 { 00059 // We call layoutUnitToPixelY on the bottom value, i.e. y+h-1, 00060 // and then determine the height from the result (i.e. bottom-top+1). 00061 // Calling layoutUnitToPixelY(h) leads to rounding problems. 00062 return layoutUnitToPixelY( y + h - 1 ) - layoutUnitToPixelY( y ) + 1; 00063 } 00064 00065 int KoTextZoomHandler::layoutUnitToPixelX( int lupix ) const 00066 { 00067 return int( static_cast<double>( lupix * m_zoomedResolutionX ) 00068 / ( static_cast<double>( m_layoutUnitFactor ) * m_resolutionX ) ); 00069 } 00070 00071 int KoTextZoomHandler::layoutUnitToPixelY( int lupix ) const 00072 { 00073 // qRound replaced with a truncation, too many problems (e.g. bottom of parags) 00074 return int( static_cast<double>( lupix * m_zoomedResolutionY ) 00075 / ( static_cast<double>( m_layoutUnitFactor ) * m_resolutionY ) ); 00076 } 00077 00078 int KoTextZoomHandler::pixelToLayoutUnitX( int x ) const 00079 { 00080 return qRound( static_cast<double>( x * m_layoutUnitFactor * m_resolutionX ) 00081 / m_zoomedResolutionX ); 00082 } 00083 00084 int KoTextZoomHandler::pixelToLayoutUnitY( int y ) const 00085 { 00086 return qRound( static_cast<double>( y * m_layoutUnitFactor * m_resolutionY ) 00087 / m_zoomedResolutionY ); 00088 } 00089