1 /*
2  * Copyright (C) 2004 Apple Computer, Inc.  All rights reserved.
3  * Copyright (C) 2005 Zack Rusin <zack@kde.org>
4  * Copyright (C) 2007 Maksim Orlovich <maksim@kde.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
16  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 //#define DEBUG_LAYOUT
29 
30 #include "render_canvasimage.h"
31 #include "render_canvas.h"
32 
33 #include <QDebug>
34 
35 #include <css/csshelper.h>
36 #include <misc/helper.h>
37 #include <html/html_formimpl.h>
38 #include <html/html_canvasimpl.h>
39 #include <xml/dom2_eventsimpl.h>
40 #include <html/html_documentimpl.h>
41 #include <imload/canvasimage.h>
42 
43 #include <math.h>
44 
45 using namespace DOM;
46 using namespace khtml;
47 
48 // -------------------------------------------------------------------------
49 
RenderCanvasImage(DOM::HTMLCanvasElementImpl * canvasEl)50 RenderCanvasImage::RenderCanvasImage(DOM::HTMLCanvasElementImpl *canvasEl)
51     : RenderReplaced(canvasEl), imagePainter(canvasEl->getCanvasImage())
52 {
53     setIntrinsicWidth(element()->width());
54     setIntrinsicHeight(element()->height());
55 }
56 
paint(PaintInfo & i,int _tx,int _ty)57 void RenderCanvasImage::paint(PaintInfo &i, int _tx, int _ty)
58 {
59     int x = _tx + m_x;
60     int y = _ty + m_y;
61 
62     if (shouldPaintBackgroundOrBorder() && i.phase != PaintActionOutline) {
63         paintBoxDecorations(i, x, y);
64     }
65 
66     QPainter *p = i.p;
67 
68     if (i.phase == PaintActionOutline && style()->outlineWidth() && style()->visibility() == VISIBLE) {
69         paintOutline(p, x, y, width(), height(), style());
70     }
71 
72     if (i.phase != PaintActionForeground && i.phase != PaintActionSelection) {
73         return;
74     }
75 
76     //bool isPrinting = (i.p->device()->devType() == QInternal::Printer);
77     //bool drawSelectionTint = (selectionState() != SelectionNone) && !isPrinting;
78     if (i.phase == PaintActionSelection) {
79         if (selectionState() == SelectionNone) {
80             return;
81         }
82         //drawSelectionTint = false;
83     }
84 
85     int cWidth = contentWidth();
86     int cHeight = contentHeight();
87     if (!cWidth) {
88         cWidth = 300;
89     }
90     if (!cHeight) {
91         cHeight = 150;
92     }
93     int leftBorder = borderLeft();
94     int topBorder = borderTop();
95     int leftPad = paddingLeft();
96     int topPad = paddingTop();
97 
98     x += leftBorder + leftPad;
99     y += topBorder + topPad;
100 
101     element()->getContext2D()->commit(); // Make sure everything is up-to-date
102     imagePainter.setSize(QSize(cWidth, cHeight));
103     imagePainter.paint(x, y, p, 0, 0);
104 
105     // if (drawSelectionTint) {
106 //         QBrush brush(selectionColor(p));
107 //         QRect selRect(selectionRect());
108 //         p->fillRect(selRect.x(), selRect.y(), selRect.width(), selRect.height(), brush);
109 //     }
110 }
111 
layout()112 void RenderCanvasImage::layout()
113 {
114     KHTMLAssert(needsLayout());
115     KHTMLAssert(minMaxKnown());
116 
117     calcWidth();
118     calcHeight();
119 
120     setNeedsLayout(false);
121 }
122 
updateFromElement()123 void RenderCanvasImage::updateFromElement()
124 {
125     int newWidth  = element()->width();
126     int newHeight = element()->height();
127     if (intrinsicHeight() != newHeight || intrinsicWidth()  != newWidth) {
128         setIntrinsicWidth(newWidth);
129         setIntrinsicHeight(newHeight);
130         setNeedsLayoutAndMinMaxRecalc();
131     }
132 
133     if (!needsLayout()) {
134         repaint();
135     }
136 }
137 
138