# khtml Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# render_image.cpp

    00001
    00025 //#define DEBUG_LAYOUT
    00026
    00027 #include "render_image.h"
    00028 #include "render_root.h"
    00029
    00030 #include <qdrawutil.h>
    00031 #include <qpainter.h>
    00032
    00033 #include <kapplication.h>
    00034 #include <kdebug.h>
    00035
    00036 #include "css/csshelper.h"
    00037 #include "misc/helper.h"
    00038 #include "misc/htmlattrs.h"
    00039 #include "misc/htmltags.h"
    00040 #include "html/html_formimpl.h"
    00041 #include "html/html_imageimpl.h"
    00042 #include "html/dtd.h"
    00043 #include "xml/dom2_eventsimpl.h"
    00044 #include "html/html_documentimpl.h"
    00045 #include "html/html_objectimpl.h"
    00046 #include <math.h>
    00047
    00048 using namespace DOM;
    00049 using namespace khtml;
    00050
    00051 // -------------------------------------------------------------------------
    00052
    00053 RenderImage::RenderImage(HTMLElementImpl *_element)
    00054     : RenderReplaced(_element)
    00055 {
    00056     image = 0;
    00057     berrorPic = false;
    00058     loadEventSent = false;
    00059
    00060     setIntrinsicWidth( 0 );
    00061     setIntrinsicHeight( 0 );
    00062 }
    00063
    00064 RenderImage::~RenderImage()
    00065 {
    00066     if(image) image->deref(this);
    00067 }
    00068
    00069 QPixmap RenderImage::pixmap() const
    00070 {
    00071     return image ? image->pixmap() : QPixmap();
    00072 }
    00073
    00074 void RenderImage::setStyle(RenderStyle* _style)
    00075 {
    00076     RenderReplaced::setStyle(_style);
    00077     // init RenderObject attributes
    00078     setInline( style()->display()==INLINE );
    00079     //setOverhangingContents(style()->height().isPercent());
    00080     setSpecialObjects(true);
    00081
    00082     CachedObject* co = style()->contentObject();
    00083     if (co && image != co ) {
    00084         if (image) image->deref(this);
    00085         image = static_cast<CachedImage*>(style()->contentObject());
    00086         if (image) image->ref(this);
    00087     }
    00088 }
    00089
    00090 void RenderImage::setPixmap( const QPixmap &p, const QRect& r, CachedImage *o)
    00091 {
    00092     if(o != image) {
    00093         RenderReplaced::setPixmap(p, r, o);
    00094         return;
    00095     }
    00096
    00097     bool iwchanged = false;
    00098
    00099     if(o->isErrorImage()) {
    00100         int iw = p.width() + 8;
    00101         int ih = p.height() + 8;
    00102
    00103         // we have an alt and the user meant it (its not a text we invented)
    00104         if ( element() && !alt.isEmpty() && !element()->getAttribute( ATTR_ALT ).isNull()) {
    00105             const QFontMetrics &fm = style()->fontMetrics();
    00106             QRect br = fm.boundingRect (  0, 0, 1024, 256, Qt::AlignAuto|Qt::WordBreak, alt.string() );
    00107             if ( br.width() > iw )
    00108                 iw = br.width();
    00109             if ( br.height() > ih )
    00110                 ih = br.height();
    00111         }
    00112
    00113         if ( iw != intrinsicWidth() ) {
    00114             setIntrinsicWidth( iw );
    00115             iwchanged = true;
    00116         }
    00117         if ( ih != intrinsicHeight() ) {
    00118             setIntrinsicHeight( ih );
    00119             iwchanged = true;
    00120         }
    00121         if ( element() && element()->id() == ID_OBJECT ) {
    00122             static_cast<HTMLObjectElementImpl*>(  element() )->renderAlternative();
    00123             return;
    00124         }
    00125     }
    00126     berrorPic = o->isErrorImage();
    00127
    00128     bool needlayout = false;
    00129
    00130     // Image dimensions have been changed, see what needs to be done
    00131     if( o->pixmap_size().width() != intrinsicWidth() ||
    00132        o->pixmap_size().height() != intrinsicHeight() || iwchanged )
    00133     {
    00134 //          qDebug("image dimensions have been changed, old: %d/%d  new: %d/%d",
    00135 //                 intrinsicWidth(), intrinsicHeight(),
    00136 //               o->pixmap_size().width(), o->pixmap_size().height());
    00137
    00138         if(!o->isErrorImage()) {
    00139             setIntrinsicWidth( o->pixmap_size().width() );
    00140             setIntrinsicHeight( o->pixmap_size().height() );
    00141         }
    00142
    00143         // lets see if we need to relayout at all..
    00144         int oldwidth = m_width;
    00145         int oldheight = m_height;
    00146         calcWidth();
    00147         calcHeight();
    00148
    00149         if(iwchanged || m_width != oldwidth || m_height != oldheight)
    00150             needlayout = true;
    00151
    00152         m_width = oldwidth;
    00153         m_height = oldheight;
    00154     }
    00155
    00156
    00157     if(needlayout)
    00158     {
    00159         setLayouted(false);
    00160         setMinMaxKnown(false);
    00161
    00162 //         kdDebug( 6040 ) << "m_width: : " << m_width << " height: " << m_height << endl;
    00163 //         kdDebug( 6040 ) << "Image: size " << m_width << "/" << m_height << endl;
    00164     }
    00165     else
    00166     {
    00167         bool completeRepaint = !resizeCache.isNull();
    00168         int cHeight = contentHeight();
    00169         int scaledHeight = intrinsicHeight() ? ((o->valid_rect().height()*cHeight)/intrinsicHeight()) : 0;
    00170
    00171         // don't bog down X server doing xforms
    00172         if(completeRepaint && cHeight >= 5 &&  o->valid_rect().height() < intrinsicHeight() &&
    00173            (scaledHeight / (cHeight/5) == resizeCache.height() / (cHeight/5)))
    00174             return;
    00175
    00176         resizeCache = QPixmap(); // for resized animations
    00177
    00178         if(completeRepaint)
    00179             repaintRectangle(borderLeft()+paddingLeft(), borderTop()+paddingTop(), contentWidth(), contentHeight());
    00180         else
    00181         {
    00182             repaintRectangle(r.x() + borderLeft() + paddingLeft(), r.y() + borderTop() + paddingTop(),
    00183                              r.width(), r.height());
    00184         }
    00185     }
    00186 }
    00187
    00188 void RenderImage::paintObject(QPainter *p, int /*_x*/, int /*_y*/, int /*_w*/, int /*_h*/, int _tx, int _ty)
    00189 {
    00190     // add offset for relative positioning
    00191     if(isRelPositioned())
    00192         relativePositionOffset(_tx, _ty);
    00193
    00194     int cWidth = contentWidth();
    00195     int cHeight = contentHeight();
    00196     int leftBorder = borderLeft();
    00197     int topBorder = borderTop();
    00198     int leftPad = paddingLeft();
    00199     int topPad = paddingTop();
    00200
    00201     if (khtml::printpainter && !root()->paintImages())
    00202         return;
    00203
    00204     //kdDebug( 6040 ) << "    contents (" << contentWidth << "/" << contentHeight << ") border=" << borderLeft() << " padding=" << paddingLeft() << endl;
    00205     if ( !image || berrorPic)
    00206     {
    00207         if(cWidth > 2 && cHeight > 2)
    00208         {
    00209             if ( !berrorPic ) {
    00210                 //qDebug("qDrawShadePanel %d/%d/%d/%d", _tx + leftBorder, _ty + topBorder, cWidth, cHeight);
    00211                 qDrawShadePanel( p, _tx + leftBorder + leftPad, _ty + topBorder + topPad, cWidth, cHeight,
    00212                                  KApplication::palette().inactive(), true, 1 );
    00213             }
    00214             QPixmap const* pix = image ? &image->pixmap() : 0;
    00215             if(berrorPic && pix && (cWidth >= pix->width()+4) && (cHeight >= pix->height()+4) )
    00216             {
    00217                 QRect r(pix->rect());
    00218                 r = r.intersect(QRect(0, 0, cWidth-4, cHeight-4));
    00219                 p->drawPixmap( QPoint( _tx + leftBorder + leftPad+2, _ty + topBorder + topPad+2), *pix, r );
    00220             }
    00221             if(!alt.isEmpty()) {
    00222                 QString text = alt.string();
    00223                 p->setFont(style()->font());
    00224                 p->setPen( style()->color() );
    00225                 int ax = _tx + leftBorder + leftPad + 2;
    00226                 int ay = _ty + topBorder + topPad + 2;
    00227                 const QFontMetrics &fm = style()->fontMetrics();
    00228                 if (cWidth>5 && cHeight>=fm.height())
    00229                     p->drawText(ax, ay+1, cWidth - 4, cHeight - 4, Qt::WordBreak, text );
    00230             }
    00231         }
    00232     }
    00233     else if (image && !image->isTransparent())
    00234     {
    00235         const QPixmap& pix = image->pixmap();
    00236         if ( (cWidth != intrinsicWidth() ||  cHeight != intrinsicHeight()) &&
    00237              pix.width() > 0 && pix.height() > 0 && image->valid_rect().isValid())
    00238         {
    00239             if (resizeCache.isNull() && cWidth && cHeight)
    00240             {
    00241                 QRect scaledrect(image->valid_rect());
    00242 //                 kdDebug(6040) << "time elapsed: " << dt->elapsed() << endl;
    00243 //                  kdDebug( 6040 ) << "have to scale: " << endl;
    00244 //                  qDebug("cw=%d ch=%d  pw=%d ph=%d  rcw=%d, rch=%d",
    00245 //                          cWidth, cHeight, intrinsicWidth(), intrinsicHeight(), resizeCache.width(), resizeCache.height());
    00246                 QWMatrix matrix;
    00247                 matrix.scale( (float)(cWidth)/intrinsicWidth(),
    00248                               (float)(cHeight)/intrinsicHeight() );
    00249                 resizeCache = pix.xForm( matrix );
    00250                 scaledrect.setWidth( ( cWidth*scaledrect.width() ) / intrinsicWidth() );
    00251                 scaledrect.setHeight( ( cHeight*scaledrect.height() ) / intrinsicHeight() );
    00252 //                   qDebug("resizeCache size: %d/%d", resizeCache.width(), resizeCache.height());
    00253 //                   qDebug("valid: %d/%d, scaled: %d/%d",
    00254 //                          image->valid_rect().width(), image->valid_rect().height(),
    00255 //                          scaledrect.width(), scaledrect.height());
    00256
    00257                 // sometimes scaledrect.width/height are off by one because
    00258                 // of rounding errors. if the image is fully loaded, we
    00259                 // make sure that we don't do unnecessary resizes during painting
    00260                 QSize s(scaledrect.size());
    00261                 if(image->valid_rect().size() == QSize( intrinsicWidth(), intrinsicHeight() )) // fully loaded
    00262                     s = QSize(cWidth, cHeight);
    00263                 if(QABS(s.width() - cWidth) < 2) // rounding errors
    00264                     s.setWidth(cWidth);
    00265                 if(resizeCache.size() != s)
    00266                     resizeCache.resize(s);
    00267
    00268                 p->drawPixmap( QPoint( _tx + leftBorder + leftPad, _ty + topBorder + topPad),
    00269                                resizeCache, scaledrect );
    00270             }
    00271             else
    00272                 p->drawPixmap( QPoint( _tx + leftBorder + leftPad, _ty + topBorder + topPad), resizeCache );
    00273         }
    00274         else
    00275         {
    00276             // we might be just about switching images
    00277             // so pix contains the old one (we want to paint), but image->valid_rect is still invalid
    00278             // so use intrinsic Size instead.
    00279             // ### maybe no progressive loading for the second image ?
    00280             QRect rect(image->valid_rect().isValid() ? image->valid_rect()
    00281                        : QRect(0, 0, intrinsicWidth(), intrinsicHeight()));
    00282
    00283             QPoint offs( _tx + leftBorder + leftPad, _ty + topBorder + topPad);
    00284 //             qDebug("normal paint rect %d/%d/%d/%d", rect.x(), rect.y(), rect.width(), rect.height());
    00285 //             rect = rect & QRect( 0 , y - offs.y() - 10, w, 10 + y + h  - offs.y());
    00286
    00287 //             qDebug("normal paint rect after %d/%d/%d/%d", rect.x(), rect.y(), rect.width(), rect.height());
    00288 //             qDebug("normal paint: offs.y(): %d, y: %d, diff: %d", offs.y(), y, y - offs.y());
    00289 //             qDebug("");
    00290
    00291 //           p->setClipRect(QRect(x,y,w,h));
    00292
    00293
    00294 //             p->drawPixmap( offs.x(), y, pix, rect.x(), rect.y(), rect.width(), rect.height() );
    00295              p->drawPixmap(offs, pix, rect);
    00296
    00297         }
    00298     }
    00299     if(style()->outlineWidth())
    00300         paintOutline(p, _tx, _ty, width(), height(), style());
    00301 }
    00302
    00303 void RenderImage::layout()
    00304 {
    00305     KHTMLAssert(!layouted());
    00306     KHTMLAssert( minMaxKnown() );
    00307
    00308     short oldwidth = m_width;
    00309     int oldheight = m_height;
    00310
    00311     // minimum height
    00312     m_height = image && image->isErrorImage() ? intrinsicHeight() : 0;
    00313
    00314     calcWidth();
    00315     calcHeight();
    00316
    00317     // if they are variable width and we calculate a huge height or width, we assume they
    00318     // actually wanted the intrinsic width.
    00319     if ( m_width > 2048 && !style()->width().isFixed() )
    00320         m_width = intrinsicWidth();
    00321     if ( m_height > 2048 && !style()->height().isFixed() )
    00322         m_height = intrinsicHeight();
    00323     // limit total size to not run out of memory when doing the xform call.
    00324     if ( m_width * m_height > 2048*2048 ) {
    00325         float scale = sqrt( m_width*m_height / ( 2048.*2048. ) );
    00326         m_width = (int) (m_width/scale);
    00327         m_height = (int) (m_height/scale);
    00328     }
    00329
    00330     if ( m_width != oldwidth || m_height != oldheight )
    00331         resizeCache = QPixmap();
    00332
    00333     setLayouted();
    00334 }
    00335
    00336 void RenderImage::notifyFinished(CachedObject *finishedObj)
    00337 {
    00338     if (image == finishedObj && !loadEventSent && element()) {
    00339         loadEventSent = true;
    00340         element()->dispatchHTMLEvent(
    00341             image->isErrorImage() ? EventImpl::ERROR_EVENT : EventImpl::LOAD_EVENT,
    00342             false,false);
    00343     }
    00344 }
    00345
    00346 bool RenderImage::nodeAtPoint(NodeInfo& info, int _x, int _y, int _tx, int _ty)
    00347 {
    00348     bool inside = RenderReplaced::nodeAtPoint(info, _x, _y, _tx, _ty);
    00349
    00350     if (inside && element()) {
    00351         int tx = _tx + m_x;
    00352         int ty = _ty + m_y;
    00353         if (isRelPositioned())
    00354             relativePositionOffset(tx, ty);
    00355
    00356         HTMLImageElementImpl* i = element()->id() == ID_IMG ? static_cast<HTMLImageElementImpl*>(element()) : 0;
    00357         HTMLMapElementImpl* map;
    00358         if (i && i->getDocument()->isHTMLDocument() &&
    00359             (map = static_cast<HTMLDocumentImpl*>(i->getDocument())->getMap(i->imageMap()))) {
    00360             // we're a client side image map
    00361             inside = map->mapMouseEvent(_x - tx, _y - ty, contentWidth(), contentHeight(), info);
    00362         }
    00363     }
    00364
    00365     return inside;
    00366 }
    00367
    00368 void RenderImage::updateFromElement()
    00369 {
    00370     DOMString u = element()->id() == ID_OBJECT ?
    00371                   element()->getAttribute(ATTR_DATA) : element()->getAttribute(ATTR_SRC);
    00372
    00373     if (!u.isEmpty()) {
    00374         CachedImage *new_image = element()->getDocument()->docLoader()->
    00375                                  requestImage(khtml::parseURL(u));
    00376
    00377         if(new_image && new_image != image && (!style() || !style()->contentObject())) {
    00378             loadEventSent = false;
    00379             CachedImage* oldimage = image;
    00380             image = new_image;
    00381             image->ref(this);
    00382             if(oldimage) oldimage->deref(this);
    00383             berrorPic = image->isErrorImage();
    00384         }
    00385     }
    00386
    00387     if (element()->id() == ID_INPUT)
    00388         alt = static_cast<HTMLInputElementImpl*>(element())->altText();
    00389     else if (element()->id() == ID_IMG)
    00390         alt = static_cast<HTMLImageElementImpl*>(element())->altText();
    00391 }
    00392
    00393 bool RenderImage::complete() const
    00394 {
    00395     // "complete" means that the image has been loaded
    00396     // but also that its width/height (contentWidth(),contentHeight()) have been calculated.
    00397      return image && image->valid_rect().size() == image->pixmap_size() && layouted();
    00398 }
    00399
    00400 short RenderImage::calcReplacedWidth() const
    00401 {
    00402     const Length w = style()->width();
    00403
    00404     if (w.isVariable()) {
    00405         const Length h = style()->height();
    00406         if ( m_intrinsicHeight > 0 && ( h.isPercent() || h.isFixed() ) )
    00407             return ( ( h.isPercent() ? calcReplacedHeight() : h.value() )*intrinsicWidth() ) / m_intrinsicHeight;
    00408     }
    00409
    00410     return RenderReplaced::calcReplacedWidth();
    00411 }
    00412
    00413 int RenderImage::calcReplacedHeight() const
    00414 {
    00415     const Length h = style()->height();
    00416
    00417     if (h.isVariable()) {
    00418         const Length w = style()->width();
    00419         if( m_intrinsicWidth > 0 && ( w.isFixed() || w.isPercent() ))
    00420             return (( w.isPercent() ? calcReplacedWidth() : w.value() ) * intrinsicHeight()) / m_intrinsicWidth;
    00421
    00422     }
    00423
    00424     return RenderReplaced::calcReplacedHeight();
    00425 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:34:36 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001