[Cherry-pick] Use -webkit-clip-path shapes to clip HTML elements
[framework/web/webkit-efl.git] / Source / WebCore / rendering / RenderBoxModelObject.h
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2006, 2007, 2009 Apple Inc. All rights reserved.
5  * Copyright (C) 2010 Google Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #ifndef RenderBoxModelObject_h
25 #define RenderBoxModelObject_h
26
27 #include "RenderObject.h"
28 #include "ShadowData.h"
29
30 namespace WebCore {
31
32 // Modes for some of the line-related functions.
33 enum LinePositionMode { PositionOnContainingLine, PositionOfInteriorLineBoxes };
34 enum LineDirectionMode { HorizontalLine, VerticalLine };
35 typedef unsigned BorderEdgeFlags;
36
37 enum BackgroundBleedAvoidance {
38     BackgroundBleedNone,
39     BackgroundBleedShrinkBackground,
40     BackgroundBleedUseTransparencyLayer
41 };
42
43 enum ContentChangeType {
44     ImageChanged,
45     MaskImageChanged,
46     CanvasChanged,
47     CanvasPixelsChanged,
48     VideoChanged,
49     FullScreenChanged
50 };
51
52 class KeyframeList;
53
54 // This class is the base for all objects that adhere to the CSS box model as described
55 // at http://www.w3.org/TR/CSS21/box.html
56
57 class RenderBoxModelObject : public RenderObject {
58 public:
59     RenderBoxModelObject(Node*);
60     virtual ~RenderBoxModelObject();
61     
62     LayoutSize relativePositionOffset() const;
63     LayoutSize relativePositionLogicalOffset() const { return style()->isHorizontalWritingMode() ? relativePositionOffset() : relativePositionOffset().transposedSize(); }
64
65     // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (RenderFlow)
66     // to return the remaining width on a given line (and the height of a single line).
67     virtual LayoutUnit offsetLeft() const;
68     virtual LayoutUnit offsetTop() const;
69     virtual LayoutUnit offsetWidth() const = 0;
70     virtual LayoutUnit offsetHeight() const = 0;
71
72     int pixelSnappedOffsetLeft() const { return roundToInt(offsetLeft()); }
73     int pixelSnappedOffsetTop() const { return roundToInt(offsetTop()); }
74     int pixelSnappedOffsetWidth() const;
75     int pixelSnappedOffsetHeight() const;
76
77     virtual void styleWillChange(StyleDifference, const RenderStyle* newStyle);
78     virtual void styleDidChange(StyleDifference, const RenderStyle* oldStyle);
79     virtual void updateBoxModelInfoFromStyle();
80
81     bool hasSelfPaintingLayer() const;
82     RenderLayer* layer() const { return m_layer; }
83     
84     virtual bool requiresLayer() const { return isRoot() || isOutOfFlowPositioned() || isRelPositioned() || isTransparent() || hasClipPath() || hasTransform() || hasHiddenBackface() || hasMask() || hasReflection() || hasFilter() || style()->specifiesColumns(); }
85
86     // This will work on inlines to return the bounding box of all of the lines' border boxes.
87     virtual IntRect borderBoundingBox() const = 0;
88
89     // These return the CSS computed padding values.
90     LayoutUnit computedCSSPaddingTop() const;
91     LayoutUnit computedCSSPaddingBottom() const;
92     LayoutUnit computedCSSPaddingLeft() const;
93     LayoutUnit computedCSSPaddingRight() const;
94     LayoutUnit computedCSSPaddingBefore() const;
95     LayoutUnit computedCSSPaddingAfter() const;
96     LayoutUnit computedCSSPaddingStart() const;
97     LayoutUnit computedCSSPaddingEnd() const;
98
99     // These functions are used during layout. Table cells and the MathML
100     // code override them to include some extra intrinsic padding.
101     virtual LayoutUnit paddingTop() const { return computedCSSPaddingTop(); }
102     virtual LayoutUnit paddingBottom() const { return computedCSSPaddingBottom(); }
103     virtual LayoutUnit paddingLeft() const { return computedCSSPaddingLeft(); }
104     virtual LayoutUnit paddingRight() const { return computedCSSPaddingRight(); }
105     virtual LayoutUnit paddingBefore() const { return computedCSSPaddingBefore(); }
106     virtual LayoutUnit paddingAfter() const { return computedCSSPaddingAfter(); }
107     virtual LayoutUnit paddingStart() const { return computedCSSPaddingStart(); }
108     virtual LayoutUnit paddingEnd() const { return computedCSSPaddingEnd(); }
109
110     virtual int borderTop() const { return style()->borderTopWidth(); }
111     virtual int borderBottom() const { return style()->borderBottomWidth(); }
112     virtual int borderLeft() const { return style()->borderLeftWidth(); }
113     virtual int borderRight() const { return style()->borderRightWidth(); }
114     virtual int borderBefore() const { return style()->borderBeforeWidth(); }
115     virtual int borderAfter() const { return style()->borderAfterWidth(); }
116     virtual int borderStart() const { return style()->borderStartWidth(); }
117     virtual int borderEnd() const { return style()->borderEndWidth(); }
118
119     LayoutUnit borderAndPaddingHeight() const { return borderTop() + borderBottom() + paddingTop() + paddingBottom(); }
120     LayoutUnit borderAndPaddingWidth() const { return borderLeft() + borderRight() + paddingLeft() + paddingRight(); }
121     LayoutUnit borderAndPaddingLogicalHeight() const { return borderBefore() + borderAfter() + paddingBefore() + paddingAfter(); }
122     LayoutUnit borderAndPaddingLogicalWidth() const { return borderStart() + borderEnd() + paddingStart() + paddingEnd(); }
123     LayoutUnit borderAndPaddingLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
124
125     LayoutUnit borderAndPaddingStart() const { return borderStart() + paddingStart(); }
126     LayoutUnit borderLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() : borderTop(); }
127     LayoutUnit borderLogicalRight() const { return style()->isHorizontalWritingMode() ? borderRight() : borderBottom(); }
128
129     virtual LayoutUnit marginTop() const = 0;
130     virtual LayoutUnit marginBottom() const = 0;
131     virtual LayoutUnit marginLeft() const = 0;
132     virtual LayoutUnit marginRight() const = 0;
133     virtual LayoutUnit marginBefore(const RenderStyle* otherStyle = 0) const = 0;
134     virtual LayoutUnit marginAfter(const RenderStyle* otherStyle = 0) const = 0;
135     virtual LayoutUnit marginStart(const RenderStyle* otherStyle = 0) const = 0;
136     virtual LayoutUnit marginEnd(const RenderStyle* otherStyle = 0) const = 0;
137     LayoutUnit marginHeight() const { return marginTop() + marginBottom(); }
138     LayoutUnit marginWidth() const { return marginLeft() + marginRight(); }
139
140     bool hasInlineDirectionBordersPaddingOrMargin() const { return hasInlineDirectionBordersOrPadding() || marginStart()|| marginEnd(); }
141     bool hasInlineDirectionBordersOrPadding() const { return borderStart() || borderEnd() || paddingStart()|| paddingEnd(); }
142
143     virtual LayoutUnit containingBlockLogicalWidthForContent() const;
144
145     virtual void childBecameNonInline(RenderObject* /*child*/) { }
146
147     void paintBorder(const PaintInfo&, const LayoutRect&, const RenderStyle*, BackgroundBleedAvoidance = BackgroundBleedNone, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
148     bool paintNinePieceImage(GraphicsContext*, const LayoutRect&, const RenderStyle*, const NinePieceImage&, CompositeOperator = CompositeSourceOver);
149     void paintBoxShadow(const PaintInfo&, const LayoutRect&, const RenderStyle*, ShadowStyle, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
150     void paintFillLayerExtended(const PaintInfo&, const Color&, const FillLayer*, const LayoutRect&, BackgroundBleedAvoidance, InlineFlowBox* = 0, const LayoutSize& = LayoutSize(), CompositeOperator = CompositeSourceOver, RenderObject* backgroundObject = 0);
151     
152     virtual bool boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox* = 0) const;
153
154     // Overridden by subclasses to determine line height and baseline position.
155     virtual LayoutUnit lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
156     virtual LayoutUnit baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
157
158     virtual void mapAbsoluteToLocalPoint(bool fixed, bool useTransforms, TransformState&) const OVERRIDE;
159
160     // Called by RenderObject::willBeDestroyed() and is the only way layers should ever be destroyed
161     void destroyLayer();
162
163     void highQualityRepaintTimerFired(Timer<RenderBoxModelObject>*);
164
165     virtual void setSelectionState(SelectionState s);
166
167 #if USE(ACCELERATED_COMPOSITING)
168     void contentChanged(ContentChangeType);
169     bool hasAcceleratedCompositing() const;
170
171     bool startTransition(double, CSSPropertyID, const RenderStyle* fromStyle, const RenderStyle* toStyle);
172     void transitionPaused(double timeOffset, CSSPropertyID);
173     void transitionFinished(CSSPropertyID);
174
175     bool startAnimation(double timeOffset, const Animation*, const KeyframeList& keyframes);
176     void animationPaused(double timeOffset, const String& name);
177     void animationFinished(const String& name);
178
179     void suspendAnimations(double time = 0);
180 #endif
181
182 protected:
183     virtual void willBeDestroyed();
184
185     class BackgroundImageGeometry {
186     public:
187         IntPoint destOrigin() const { return m_destOrigin; }
188         void setDestOrigin(const IntPoint& destOrigin)
189         {
190             m_destOrigin = destOrigin;
191         }
192         
193         IntRect destRect() const { return m_destRect; }
194         void setDestRect(const IntRect& destRect)
195         {
196             m_destRect = destRect;
197         }
198
199         // Returns the phase relative to the destination rectangle.
200         IntPoint relativePhase() const;
201         
202         IntPoint phase() const { return m_phase; }   
203         void setPhase(const IntPoint& phase)
204         {
205             m_phase = phase;
206         }
207
208         IntSize tileSize() const { return m_tileSize; }    
209         void setTileSize(const IntSize& tileSize)
210         {
211             m_tileSize = tileSize;
212         }
213         
214         void setPhaseX(int x) { m_phase.setX(x); }
215         void setPhaseY(int y) { m_phase.setY(y); }
216         
217         void setNoRepeatX(int xOffset);
218         void setNoRepeatY(int yOffset);
219         
220         void useFixedAttachment(const IntPoint& attachmentPoint);
221         
222         void clip(const IntRect&);
223     private:
224         IntRect m_destRect;
225         IntPoint m_destOrigin;
226         IntPoint m_phase;
227         IntSize m_tileSize;
228     };
229
230     LayoutPoint adjustedPositionRelativeToOffsetParent(const LayoutPoint&) const;
231
232     void calculateBackgroundImageGeometry(const FillLayer*, const LayoutRect& paintRect, BackgroundImageGeometry&);
233     void getBorderEdgeInfo(class BorderEdge[], const RenderStyle*, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true) const;
234     bool borderObscuresBackgroundEdge(const FloatSize& contextScale) const;
235     bool borderObscuresBackground() const;
236
237     bool shouldPaintAtLowQuality(GraphicsContext*, Image*, const void*, const LayoutSize&);
238
239     RenderBoxModelObject* continuation() const;
240     void setContinuation(RenderBoxModelObject*);
241
242     LayoutRect localCaretRectForEmptyElement(LayoutUnit width, LayoutUnit textIndentOffset);
243
244     static bool shouldAntialiasLines(GraphicsContext*);
245
246 public:
247     // For RenderBlocks and RenderInlines with m_style->styleType() == FIRST_LETTER, this tracks their remaining text fragments
248     RenderObject* firstLetterRemainingText() const;
249     void setFirstLetterRemainingText(RenderObject*);
250
251     // These functions are only used internally to manipulate the render tree structure via remove/insert/appendChildNode.
252     // Since they are typically called only to move objects around within anonymous blocks (which only have layers in
253     // the case of column spans), the default for fullRemoveInsert is false rather than true.
254     void moveChildTo(RenderBoxModelObject* toBoxModelObject, RenderObject* child, RenderObject* beforeChild, bool fullRemoveInsert = false);
255     void moveChildTo(RenderBoxModelObject* toBoxModelObject, RenderObject* child, bool fullRemoveInsert = false)
256     {
257         moveChildTo(toBoxModelObject, child, 0, fullRemoveInsert);
258     }
259     void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, bool fullRemoveInsert = false)
260     {
261         moveAllChildrenTo(toBoxModelObject, 0, fullRemoveInsert);
262     }
263     void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* beforeChild, bool fullRemoveInsert = false)
264     {
265         moveChildrenTo(toBoxModelObject, firstChild(), 0, beforeChild, fullRemoveInsert);
266     }
267     // Move all of the kids from |startChild| up to but excluding |endChild|. 0 can be passed as the |endChild| to denote
268     // that all the kids from |startChild| onwards should be moved.
269     void moveChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* startChild, RenderObject* endChild, bool fullRemoveInsert = false)
270     {
271         moveChildrenTo(toBoxModelObject, startChild, endChild, 0, fullRemoveInsert);
272     }
273     void moveChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* startChild, RenderObject* endChild, RenderObject* beforeChild, bool fullRemoveInsert = false);
274
275 private:
276     virtual bool isBoxModelObject() const { return true; }
277
278     IntSize calculateFillTileSize(const FillLayer*, const IntSize& scaledPositioningAreaSize) const;
279
280     enum ScaleByEffectiveZoomOrNot { ScaleByEffectiveZoom, DoNotScaleByEffectiveZoom };
281     IntSize calculateImageIntrinsicDimensions(StyleImage*, const IntSize& scaledPositioningAreaSize, ScaleByEffectiveZoomOrNot) const;
282
283     RoundedRect getBackgroundRoundedRect(const LayoutRect&, InlineFlowBox*, LayoutUnit inlineBoxWidth, LayoutUnit inlineBoxHeight,
284         bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
285
286     void clipBorderSidePolygon(GraphicsContext*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
287                                BoxSide, bool firstEdgeMatches, bool secondEdgeMatches);
288     void clipBorderSideForComplexInnerPath(GraphicsContext*, const RoundedRect&, const RoundedRect&, BoxSide, const class BorderEdge[]);
289     void paintOneBorderSide(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
290                                 const IntRect& sideRect, BoxSide, BoxSide adjacentSide1, BoxSide adjacentSide2, const class BorderEdge[],
291                                 const Path*, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias, const Color* overrideColor = 0);
292     void paintTranslucentBorderSides(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
293                           const class BorderEdge[], BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false);
294     void paintBorderSides(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
295                           const class BorderEdge[], BorderEdgeFlags, BackgroundBleedAvoidance,
296                           bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false, const Color* overrideColor = 0);
297     void drawBoxSideFromPath(GraphicsContext*, const LayoutRect&, const Path&, const class BorderEdge[],
298                             float thickness, float drawThickness, BoxSide, const RenderStyle*, 
299                             Color, EBorderStyle, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
300
301     friend class RenderView;
302
303     RenderLayer* m_layer;
304     
305     // Used to store state between styleWillChange and styleDidChange
306     static bool s_wasFloating;
307     static bool s_hadLayer;
308     static bool s_hadTransform;
309     static bool s_layerWasSelfPainting;
310 };
311
312 inline RenderBoxModelObject* toRenderBoxModelObject(RenderObject* object)
313
314     ASSERT(!object || object->isBoxModelObject());
315     return static_cast<RenderBoxModelObject*>(object);
316 }
317
318 inline const RenderBoxModelObject* toRenderBoxModelObject(const RenderObject* object)
319
320     ASSERT(!object || object->isBoxModelObject());
321     return static_cast<const RenderBoxModelObject*>(object);
322 }
323
324 // This will catch anyone doing an unnecessary cast.
325 void toRenderBoxModelObject(const RenderBoxModelObject*);
326
327 } // namespace WebCore
328
329 #endif // RenderBoxModelObject_h