mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-07 06:36:55 +02:00
Add afwul hack to DpiScaler to guess actual DPI from font metrics.
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "DpiScaler.h"
|
#include "DpiScaler.h"
|
||||||
|
#include "TomahawkUtilsGui.h"
|
||||||
|
|
||||||
|
|
||||||
namespace TomahawkUtils
|
namespace TomahawkUtils
|
||||||
@@ -26,34 +27,36 @@ namespace TomahawkUtils
|
|||||||
DpiScaler::DpiScaler( const QPaintDevice* that )
|
DpiScaler::DpiScaler( const QPaintDevice* that )
|
||||||
: that( that )
|
: that( that )
|
||||||
{
|
{
|
||||||
|
m_ratioX = ratioX( that );
|
||||||
|
m_ratioY = ratioY( that );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QSize
|
QSize
|
||||||
DpiScaler::scaled( int w, int h ) const
|
DpiScaler::scaled( int w, int h ) const
|
||||||
{
|
{
|
||||||
return scaled( that, w, h );
|
return QSize( scaledX( w ), scaledY( h ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QSize
|
QSize
|
||||||
DpiScaler::scaled( const QSize& size ) const
|
DpiScaler::scaled( const QSize& size ) const
|
||||||
{
|
{
|
||||||
return scaled( that, size );
|
return scaled( size.width(), size.height() );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
DpiScaler::scaledX( int x ) const
|
DpiScaler::scaledX( int x ) const
|
||||||
{
|
{
|
||||||
return scaledX( that, x );
|
return qRound( x * m_ratioX );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
DpiScaler::scaledY( int y ) const
|
DpiScaler::scaledY( int y ) const
|
||||||
{
|
{
|
||||||
return scaledY( that, y );
|
return qRound( y * m_ratioY );
|
||||||
}
|
}
|
||||||
|
|
||||||
// static methods start here
|
// static methods start here
|
||||||
@@ -75,16 +78,58 @@ DpiScaler::scaled( const QPaintDevice* pd, const QSize& size )
|
|||||||
int
|
int
|
||||||
DpiScaler::scaledX( const QPaintDevice* pd, int x )
|
DpiScaler::scaledX( const QPaintDevice* pd, int x )
|
||||||
{
|
{
|
||||||
float ratioX = pd->logicalDpiX() / 100.0;
|
return qRound( x * ratioX( pd ) );
|
||||||
return qRound( x * ratioX );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
DpiScaler::scaledY( const QPaintDevice* pd, int y )
|
DpiScaler::scaledY( const QPaintDevice* pd, int y )
|
||||||
{
|
{
|
||||||
float ratioY = pd->logicalDpiY() / 100.0;
|
return qRound( y * ratioY( pd ) );
|
||||||
return qRound( y * ratioY );
|
}
|
||||||
|
|
||||||
|
|
||||||
|
qreal
|
||||||
|
DpiScaler::ratioX( const QPaintDevice* pd )
|
||||||
|
{
|
||||||
|
qreal basePpp = s_baseDpi / 72.; //72*(1.333)=96 dpi
|
||||||
|
|
||||||
|
qreal ratioFromPpp = getPpp() / basePpp;
|
||||||
|
qreal ratioYFromDpi = pd->logicalDpiY() / s_baseDpi; //using Y because we compare with height
|
||||||
|
|
||||||
|
//if the error is less than 1%, we trust that the logical DPI setting has the best value
|
||||||
|
if ( qAbs( ratioFromPpp / ratioYFromDpi - 1 ) < 0.01 )
|
||||||
|
return pd->logicalDpiX() / s_baseDpi;
|
||||||
|
else
|
||||||
|
return ratioFromPpp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
qreal
|
||||||
|
DpiScaler::ratioY( const QPaintDevice* pd )
|
||||||
|
{
|
||||||
|
qreal basePpp = s_baseDpi / 72.; //72*(1.333)=96 dpi
|
||||||
|
|
||||||
|
qreal ratioFromPpp = getPpp() / basePpp;
|
||||||
|
qreal ratioYFromDpi = pd->logicalDpiY() / s_baseDpi; //using Y because we compare with height
|
||||||
|
|
||||||
|
//if the error is less than 1%, we trust that the logical DPI setting has the best value
|
||||||
|
if ( qAbs( ratioFromPpp / ratioYFromDpi - 1 ) < 0.01 )
|
||||||
|
return ratioYFromDpi;
|
||||||
|
else
|
||||||
|
return ratioFromPpp;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
qreal
|
||||||
|
DpiScaler::getPpp()
|
||||||
|
{
|
||||||
|
int fS = TomahawkUtils::defaultFontSize();
|
||||||
|
QFont f;
|
||||||
|
f.setPointSize( fS );
|
||||||
|
int fH = QFontMetrics( f ).ascent() + 1; //a font's em-height should be ascent + 1px (baseline)
|
||||||
|
qreal ppp = fH / (qreal)fS; //pixels per point
|
||||||
|
return ppp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -50,7 +50,20 @@ public:
|
|||||||
inline static int scaledY( const QPaintDevice* pd, int y );
|
inline static int scaledY( const QPaintDevice* pd, int y );
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
inline static qreal ratioX( const QPaintDevice* pd );
|
||||||
|
inline static qreal ratioY( const QPaintDevice* pd );
|
||||||
|
inline static qreal getPpp();
|
||||||
|
|
||||||
|
qreal m_ratioX;
|
||||||
|
qreal m_ratioY;
|
||||||
|
|
||||||
const QPaintDevice* that;
|
const QPaintDevice* that;
|
||||||
|
|
||||||
|
#ifdef Q_OS_MAC
|
||||||
|
static const qreal s_baseDpi = 72.;
|
||||||
|
#else
|
||||||
|
static const qreal s_baseDpi = 96.;
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user