⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pagepainter.cpp.svn-base

📁 okular
💻 SVN-BASE
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************** *   Copyright (C) 2005 by Enrico Ros <eros.kde@email.it>                  * *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * ***************************************************************************/// qt / kde includes#include <qrect.h>#include <qpainter.h>#include <qpixmap.h>#include <qimage.h>#include <qapplication.h>#include <kglobal.h>#include <kimageeffect.h>#include <kiconloader.h>#include <kstaticdeleter.h>#include <kdebug.h>// system includes#include <math.h>// local includes#include "pagepainter.h"#include "core/area.h"#include "core/page.h"#include "core/annotations.h"#include "settings.h"static KStaticDeleter<QPixmap> sd;QPixmap * busyPixmap = 0;void PagePainter::paintPageOnPainter( QPainter * destPainter, const KPDFPage * page,    int pixID, int flags, int scaledWidth, int scaledHeight, const QRect & limits ){    /** 1 - RETRIEVE THE 'PAGE+ID' PIXMAP OR A SIMILAR 'PAGE' ONE **/    const QPixmap * pixmap = 0;    // if a pixmap is present for given id, use it    if ( page->m_pixmaps.contains( pixID ) )        pixmap = page->m_pixmaps[ pixID ];    // else find the closest match using pixmaps of other IDs (great optim!)    else if ( !page->m_pixmaps.isEmpty() )    {        int minDistance = -1;        QMap< int,QPixmap * >::const_iterator it = page->m_pixmaps.begin(), end = page->m_pixmaps.end();        for ( ; it != end; ++it )        {            int pixWidth = (*it)->width(),                distance = pixWidth > scaledWidth ? pixWidth - scaledWidth : scaledWidth - pixWidth;            if ( minDistance == -1 || distance < minDistance )            {                pixmap = *it;                minDistance = distance;            }        }    }    /** 1B - IF NO PIXMAP, DRAW EMPTY PAGE **/    double pixmapRescaleRatio = pixmap ? scaledWidth / (double)pixmap->width() : -1;    long pixmapPixels = pixmap ? (long)pixmap->width() * (long)pixmap->height() : 0;    if ( !pixmap || pixmapRescaleRatio > 20.0 || pixmapRescaleRatio < 0.25 ||         (scaledWidth != pixmap->width() && pixmapPixels > 6000000L) )    {        if ( KpdfSettings::changeColors() &&             KpdfSettings::renderMode() == KpdfSettings::EnumRenderMode::Paper )            destPainter->fillRect( limits, KpdfSettings::paperColor() );        else            destPainter->fillRect( limits, Qt::white );        if ( !busyPixmap )        {            sd.setObject(busyPixmap, new QPixmap());            *busyPixmap = KGlobal::iconLoader()->loadIcon("okular", K3Icon::NoGroup, 32, K3Icon::DefaultState, 0, true);        }        // draw something on the blank page: the okular icon or a cross (as a fallback)        if ( busyPixmap && !busyPixmap->isNull() )        {            destPainter->drawPixmap( QPoint( 10, 10 ), *busyPixmap );        }        else        {            destPainter->setPen( Qt::gray );            destPainter->drawLine( 0, 0, scaledWidth-1, scaledHeight-1 );            destPainter->drawLine( 0, scaledHeight-1, scaledWidth-1, 0 );        }        return;    }    /** 2 - FIND OUT WHAT TO PAINT (Flags + Configuration + Presence) **/    bool canDrawHighlights = (flags & Highlights) && !page->m_highlights.isEmpty();    bool canDrawAnnotations = (flags & Annotations) && !page->m_annotations.isEmpty();    bool enhanceLinks = (flags & EnhanceLinks) && KpdfSettings::highlightLinks();    bool enhanceImages = (flags & EnhanceImages) && KpdfSettings::highlightImages();    // vectors containing objects to draw    // make this a qcolor, rect map, since we dont need     // to know s_id here! we are only drawing this right?    QList< QPair<QColor, NormalizedRect *> > * bufferedHighlights = 0;    QList< Annotation * > * bufferedAnnotations = 0;    QList< Annotation * > * unbufferedAnnotations = 0;    // fill up lists with visible annotation/highlight objects    if ( canDrawHighlights || canDrawAnnotations )    {        // precalc normalized 'limits rect' for intersection        double nXMin = (double)limits.left() / (double)scaledWidth,               nXMax = (double)limits.right() / (double)scaledWidth,               nYMin = (double)limits.top() / (double)scaledHeight,               nYMax = (double)limits.bottom() / (double)scaledHeight;        // append all highlights inside limits to their list        if ( canDrawHighlights )        {            if ( !bufferedHighlights )                 bufferedHighlights = new QList< QPair<QColor, NormalizedRect *>  >();/*            else            {*/                                NormalizedRect* limitRect = new NormalizedRect(nXMin, nYMin, nXMax, nYMax );                QLinkedList< HighlightAreaRect * >::const_iterator h2It = page->m_highlights.begin(), hEnd = page->m_highlights.end();                QList< NormalizedRect * >::const_iterator hIt;                for ( ; h2It != hEnd; ++h2It )                    for (hIt=(*h2It)->begin(); hIt!=(*h2It)->end(); ++hIt)                    {                        if ((*hIt)->intersects(limitRect))                            bufferedHighlights->append( qMakePair((*h2It)->color,*hIt) );                    }                delete limitRect;            //}        }        // append annotations inside limits to the un/buffered list        if ( canDrawAnnotations )        {            QLinkedList< Annotation * >::const_iterator aIt = page->m_annotations.begin(), aEnd = page->m_annotations.end();            for ( ; aIt != aEnd; ++aIt )            {                Annotation * ann = *aIt;                if ( ann->boundary.intersects( nXMin, nYMin, nXMax, nYMax ) )                {                    Annotation::SubType type = ann->subType();                    if ( type == Annotation::ALine || type == Annotation::AHighlight ||                         type == Annotation::AInk  /*|| (type == Annotation::AGeom && ann->style.opacity < 0.99)*/ )                    {                        if ( !bufferedAnnotations )                            bufferedAnnotations = new QList< Annotation * >();                        bufferedAnnotations->append( ann );                    }                    else                    {                        if ( !unbufferedAnnotations )                            unbufferedAnnotations = new QList< Annotation * >();                        unbufferedAnnotations->append( ann );                    }                }            }        }        // end of intersections checking    }    /** 3 - ENABLE BACKBUFFERING IF DIRECT IMAGE MANIPULATION IS NEEDED **/    bool bufferAccessibility = (flags & Accessibility) && KpdfSettings::changeColors() && (KpdfSettings::renderMode() != KpdfSettings::EnumRenderMode::Paper);    bool useBackBuffer = bufferAccessibility || bufferedHighlights || bufferedAnnotations;    QPixmap * backPixmap = 0;    QPainter * mixedPainter = 0;    /** 4A -- REGULAR FLOW. PAINT PIXMAP NORMAL OR RESCALED USING GIVEN QPAINTER **/    if ( !useBackBuffer )    {        // 4A.1. if size is ok, draw the page pixmap using painter        if ( pixmap->width() == scaledWidth && pixmap->height() == scaledHeight )            destPainter->drawPixmap( limits.topLeft(), *pixmap, limits );        // else draw a scaled portion of the magnified pixmap        else        {            QImage destImage;            scalePixmapOnImage( destImage, pixmap, scaledWidth, scaledHeight, limits );            destPainter->drawImage( limits.left(), limits.top(), destImage, 0, 0,                                     limits.width(),limits.height() );        }        // 4A.2. active painter is the one passed to this method        mixedPainter = destPainter;    }    /** 4B -- BUFFERED FLOW. IMAGE PAINTING + OPERATIONS. QPAINTER OVER PIXMAP  **/    else    {        // the image over which we are going to draw        QImage backImage;        // 4B.1. draw the page pixmap: normal or scaled        if ( pixmap->width() == scaledWidth && pixmap->height() == scaledHeight )            cropPixmapOnImage( backImage, pixmap, limits );        else            scalePixmapOnImage( backImage, pixmap, scaledWidth, scaledHeight, limits );        // 4B.2. modify pixmap following accessibility settings        if ( bufferAccessibility )        {            switch ( KpdfSettings::renderMode() )            {                case KpdfSettings::EnumRenderMode::Inverted:                    // Invert image pixels using QImage internal function                    backImage.invertPixels(QImage::InvertRgb);                    break;                case KpdfSettings::EnumRenderMode::Recolor:                    // Recolor image using KImageEffect::flatten with dither:0                    KImageEffect::flatten( backImage, KpdfSettings::recolorForeground(), KpdfSettings::recolorBackground() );                    break;                case KpdfSettings::EnumRenderMode::BlackWhite:                    // Manual Gray and Contrast                    unsigned int * data = (unsigned int *)backImage.bits();                    int val, pixels = backImage.width() * backImage.height(),                        con = KpdfSettings::bWContrast(), thr = 255 - KpdfSettings::bWThreshold();                    for( int i = 0; i < pixels; ++i )                    {                        val = qGray( data[i] );                        if ( val > thr )                            val = 128 + (127 * (val - thr)) / (255 - thr);                        else if ( val < thr )                            val = (128 * val) / thr;                        if ( con > 2 )                        {                            val = con * ( val - thr ) / 2 + thr;                            if ( val > 255 )                                val = 255;                            else if ( val < 0 )                                val = 0;                        }                        data[i] = qRgba( val, val, val, 255 );                    }                    break;            }        }        // 4B.3. highlight rects in page        if ( bufferedHighlights )        {            // draw highlights that are inside the 'limits' paint region            QList< QPair<QColor, NormalizedRect *> >::const_iterator hIt = bufferedHighlights->begin(), hEnd = bufferedHighlights->end();            for ( ; hIt != hEnd; ++hIt )            {                NormalizedRect * r = (*hIt).second;                // find out the rect to highlight on pixmap                QRect highlightRect = r->geometry( scaledWidth, scaledHeight ).intersect( limits );                highlightRect.translate( -limits.left(), -limits.top() );                // highlight composition (product: highlight color * destcolor)                unsigned int * data = (unsigned int *)backImage.bits();                int val, newR, newG, newB,                    rh = (*hIt).first.red(),                    gh = (*hIt).first.green(),                    bh = (*hIt).first.blue(),                    offset = highlightRect.top() * backImage.width();                for( int y = highlightRect.top(); y <= highlightRect.bottom(); ++y )                {                    for( int x = highlightRect.left(); x <= highlightRect.right(); ++x )                    {                        val = data[ x + offset ];                        newR = (qRed(val) * rh) / 255;                        newG = (qGreen(val) * gh) / 255;                        newB = (qBlue(val) * bh) / 255;                        data[ x + offset ] = qRgba( newR, newG, newB, 255 );                    }                    offset += backImage.width();                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -