[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    Re: Extra font scaling? (Re: koffice/kspread)
From:       Stefan Nikolaus <stefan.nikolaus () kdemail ! net>
Date:       2007-02-03 10:08:31
Message-ID: 200702031108.34584.stefan.nikolaus () kdemail ! net
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


On Tuesday 16 January 2007 21:01:47 Thomas Zander wrote:
> If I'm correct then the solution is to have all QFont and QFontMetrics
> constructors use a special PaintDevice.
> I'll move it from KoText to flake. It will probably be called
> KoPostscriptPaintDevice
>
> Hope that helps.

You are right. Just for the sake of completion: You have to construct the 
fonts with a paintdevice defining it's own metric and have to use QTextLayout 
to draw the text.
If you just reimplement the widget's metric method, it does not work - neither 
by QPainter::drawText nor by QTextLayout. Simon Hausmann told me, that it's a 
drawback when using the double buffered widget painting (default).
Attached is a small sample program, that shows what I've meant and how to 
achieve, that the font is only scaled once.

Regards,
Stefan

["ignoredpi.cpp" (text/x-c++src)]

#include <QApplication>
#include <QDebug>
#include <QMainWindow>
#include <QPaintEngine>
#include <QPainter>
#include <QTextLayout>
#include <QX11Info>

class MainWindow : public QMainWindow
{
public:
    void paintEvent( QPaintEvent* event )
    {
        Q_UNUSED( event );
        QPainter painter( this );
        // QX11Info provides us ppi (pixels per inch).
        // We pass points to QPainter's methods. Hence, we scale with pixels/point.
        painter.scale( QX11Info::appDpiX() / 72., QX11Info::appDpiY() / 72. );
        qDebug() << ( QX11Info::appDpiY() / 72. ) << "pixels/point";

        // Draw a line of 36 pt (!) height.
        // 36 pt * 1/72 "/pt * 25.4 mm/" = 0.5 inch * 25.4 mm/" = 12.7 mm
        // Measure the line height on your screen. It should be equal to 12.7 mm.
        // Or 0.5", if you dig it. ;-)
        const int height = 36;
        painter.drawLine( QLineF( 22., 10., 22., 10. + height ) );

        // Draw a text with a font size of also 36 pt.
        // Its height on screen should also be 12.7 mm, but it's not.
        const QFont font( QFont( "Times New Roman", height ), this );
        painter.setFont( font );
        qDebug() << painter.fontInfo().pointSize() << "pt";
        painter.drawText( QPointF( 10., 40. ), "lp" );

        // Try to use QTextLayout to control the point to pixel conversion.
        QString string( "lp" );
        QTextLayout textLayout( string, font );
        textLayout.beginLayout();
        QTextLine line = textLayout.createLine();
        if ( line.isValid() )
            line.setLineWidth( 20.0 );
        textLayout.endLayout();
        textLayout.draw( &painter, QPointF( 50., 0. ) );
        painter.drawLine( QLineF( 60., 5., 60., 5. + height ) );
    }

protected:
    int metric( PaintDeviceMetric metric ) const
    {
        switch( metric )
        {
            case QPaintDevice::PdmDpiX:
            case QPaintDevice::PdmDpiY:
            case QPaintDevice::PdmPhysicalDpiX:
            case QPaintDevice::PdmPhysicalDpiY:
                return 72;
            default:
                break;
        }
        return QMainWindow::metric( metric );
    }
};

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    MainWindow mainWin;
    mainWin.show();
    return app.exec();
}

[Attachment #8 (application/pgp-signature)]

_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic