Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / 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 "core/rendering/RenderLayerModelObject.h"
28 #include "core/rendering/style/ShadowData.h"
29 #include "platform/geometry/LayoutRect.h"
30
31 namespace blink {
32
33 // Modes for some of the line-related functions.
34 enum LinePositionMode { PositionOnContainingLine, PositionOfInteriorLineBoxes };
35 enum LineDirectionMode { HorizontalLine, VerticalLine };
36 typedef unsigned BorderEdgeFlags;
37
38 enum BackgroundBleedAvoidance {
39     BackgroundBleedNone,
40     BackgroundBleedShrinkBackground,
41     BackgroundBleedClipBackground,
42     BackgroundBleedBackgroundOverBorder
43 };
44
45 enum ContentChangeType {
46     ImageChanged,
47     CanvasChanged,
48     CanvasContextChanged
49 };
50
51 class RenderTextFragment;
52
53 // This class is the base for all objects that adhere to the CSS box model as described
54 // at http://www.w3.org/TR/CSS21/box.html
55
56 class RenderBoxModelObject : public RenderLayerModelObject {
57 public:
58     RenderBoxModelObject(ContainerNode*);
59     virtual ~RenderBoxModelObject();
60
61     LayoutSize relativePositionOffset() const;
62     LayoutSize relativePositionLogicalOffset() const { return style()->isHorizontalWritingMode() ? relativePositionOffset() : relativePositionOffset().transposedSize(); }
63
64     LayoutSize offsetForInFlowPosition() const;
65
66     // IE extensions. Used to calculate offsetWidth/Height.  Overridden by inlines (RenderFlow)
67     // to return the remaining width on a given line (and the height of a single line).
68     virtual LayoutUnit offsetLeft() const;
69     virtual LayoutUnit offsetTop() const;
70     virtual LayoutUnit offsetWidth() const = 0;
71     virtual LayoutUnit offsetHeight() const = 0;
72
73     int pixelSnappedOffsetLeft() const { return roundToInt(offsetLeft()); }
74     int pixelSnappedOffsetTop() const { return roundToInt(offsetTop()); }
75     virtual int pixelSnappedOffsetWidth() const;
76     virtual int pixelSnappedOffsetHeight() const;
77
78     virtual void updateFromStyle() OVERRIDE;
79
80     // This will work on inlines to return the bounding box of all of the lines' border boxes.
81     virtual IntRect borderBoundingBox() const = 0;
82
83     // These return the CSS computed padding values.
84     LayoutUnit computedCSSPaddingTop() const { return computedCSSPadding(style()->paddingTop()); }
85     LayoutUnit computedCSSPaddingBottom() const { return computedCSSPadding(style()->paddingBottom()); }
86     LayoutUnit computedCSSPaddingLeft() const { return computedCSSPadding(style()->paddingLeft()); }
87     LayoutUnit computedCSSPaddingRight() const { return computedCSSPadding(style()->paddingRight()); }
88     LayoutUnit computedCSSPaddingBefore() const { return computedCSSPadding(style()->paddingBefore()); }
89     LayoutUnit computedCSSPaddingAfter() const { return computedCSSPadding(style()->paddingAfter()); }
90     LayoutUnit computedCSSPaddingStart() const { return computedCSSPadding(style()->paddingStart()); }
91     LayoutUnit computedCSSPaddingEnd() const { return computedCSSPadding(style()->paddingEnd()); }
92
93     // These functions are used during layout. Table cells
94     // override them to include some extra intrinsic padding.
95     virtual LayoutUnit paddingTop() const { return computedCSSPaddingTop(); }
96     virtual LayoutUnit paddingBottom() const { return computedCSSPaddingBottom(); }
97     virtual LayoutUnit paddingLeft() const { return computedCSSPaddingLeft(); }
98     virtual LayoutUnit paddingRight() const { return computedCSSPaddingRight(); }
99     virtual LayoutUnit paddingBefore() const { return computedCSSPaddingBefore(); }
100     virtual LayoutUnit paddingAfter() const { return computedCSSPaddingAfter(); }
101     virtual LayoutUnit paddingStart() const { return computedCSSPaddingStart(); }
102     virtual LayoutUnit paddingEnd() const { return computedCSSPaddingEnd(); }
103
104     virtual int borderTop() const { return style()->borderTopWidth(); }
105     virtual int borderBottom() const { return style()->borderBottomWidth(); }
106     virtual int borderLeft() const { return style()->borderLeftWidth(); }
107     virtual int borderRight() const { return style()->borderRightWidth(); }
108     virtual int borderBefore() const { return style()->borderBeforeWidth(); }
109     virtual int borderAfter() const { return style()->borderAfterWidth(); }
110     virtual int borderStart() const { return style()->borderStartWidth(); }
111     virtual int borderEnd() const { return style()->borderEndWidth(); }
112
113     int borderWidth() const { return borderLeft() + borderRight(); }
114     int borderHeight() const { return borderTop() + borderBottom(); }
115
116     LayoutUnit borderAndPaddingStart() const { return borderStart() + paddingStart(); }
117     LayoutUnit borderAndPaddingBefore() const { return borderBefore() + paddingBefore(); }
118     LayoutUnit borderAndPaddingAfter() const { return borderAfter() + paddingAfter(); }
119
120     LayoutUnit borderAndPaddingHeight() const { return borderTop() + borderBottom() + paddingTop() + paddingBottom(); }
121     LayoutUnit borderAndPaddingWidth() const { return borderLeft() + borderRight() + paddingLeft() + paddingRight(); }
122     LayoutUnit borderAndPaddingLogicalHeight() const { return borderAndPaddingBefore() + borderAndPaddingAfter(); }
123     LayoutUnit borderAndPaddingLogicalWidth() const { return borderStart() + borderEnd() + paddingStart() + paddingEnd(); }
124     LayoutUnit borderAndPaddingLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() + paddingLeft() : borderTop() + paddingTop(); }
125
126
127     LayoutUnit borderLogicalLeft() const { return style()->isHorizontalWritingMode() ? borderLeft() : borderTop(); }
128     LayoutUnit borderLogicalRight() const { return style()->isHorizontalWritingMode() ? borderRight() : borderBottom(); }
129     LayoutUnit borderLogicalWidth() const { return borderStart() + borderEnd(); }
130     LayoutUnit borderLogicalHeight() const { return borderBefore() + borderAfter(); }
131
132     LayoutUnit paddingLogicalLeft() const { return style()->isHorizontalWritingMode() ? paddingLeft() : paddingTop(); }
133     LayoutUnit paddingLogicalRight() const { return style()->isHorizontalWritingMode() ? paddingRight() : paddingBottom(); }
134     LayoutUnit paddingLogicalWidth() const { return paddingStart() + paddingEnd(); }
135     LayoutUnit paddingLogicalHeight() const { return paddingBefore() + paddingAfter(); }
136
137     virtual LayoutUnit marginTop() const = 0;
138     virtual LayoutUnit marginBottom() const = 0;
139     virtual LayoutUnit marginLeft() const = 0;
140     virtual LayoutUnit marginRight() const = 0;
141     virtual LayoutUnit marginBefore(const RenderStyle* otherStyle = 0) const = 0;
142     virtual LayoutUnit marginAfter(const RenderStyle* otherStyle = 0) const = 0;
143     virtual LayoutUnit marginStart(const RenderStyle* otherStyle = 0) const = 0;
144     virtual LayoutUnit marginEnd(const RenderStyle* otherStyle = 0) const = 0;
145     LayoutUnit marginHeight() const { return marginTop() + marginBottom(); }
146     LayoutUnit marginWidth() const { return marginLeft() + marginRight(); }
147     LayoutUnit marginLogicalHeight() const { return marginBefore() + marginAfter(); }
148     LayoutUnit marginLogicalWidth() const { return marginStart() + marginEnd(); }
149
150     bool hasInlineDirectionBordersPaddingOrMargin() const { return hasInlineDirectionBordersOrPadding() || marginStart()|| marginEnd(); }
151     bool hasInlineDirectionBordersOrPadding() const { return borderStart() || borderEnd() || paddingStart()|| paddingEnd(); }
152
153     virtual LayoutUnit containingBlockLogicalWidthForContent() const;
154
155     virtual void childBecameNonInline(RenderObject* /*child*/) { }
156
157     void paintBorder(const PaintInfo&, const LayoutRect&, const RenderStyle*, BackgroundBleedAvoidance = BackgroundBleedNone, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
158     bool paintNinePieceImage(GraphicsContext*, const LayoutRect&, const RenderStyle*, const NinePieceImage&, CompositeOperator = CompositeSourceOver);
159     void paintBoxShadow(const PaintInfo&, const LayoutRect&, const RenderStyle*, ShadowStyle, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true);
160     void paintFillLayerExtended(const PaintInfo&, const Color&, const FillLayer&, const LayoutRect&, BackgroundBleedAvoidance, InlineFlowBox* = 0, const LayoutSize& = LayoutSize(), CompositeOperator = CompositeSourceOver, RenderObject* backgroundObject = 0);
161
162     virtual bool boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox* = 0) const;
163
164     // Overridden by subclasses to determine line height and baseline position.
165     virtual LayoutUnit lineHeight(bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
166     virtual int baselinePosition(FontBaseline, bool firstLine, LineDirectionMode, LinePositionMode = PositionOnContainingLine) const = 0;
167
168     virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const OVERRIDE;
169     virtual const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const OVERRIDE;
170
171     virtual void setSelectionState(SelectionState) OVERRIDE;
172
173     void contentChanged(ContentChangeType);
174     bool hasAcceleratedCompositing() const;
175
176     virtual void computeLayerHitTestRects(LayerHitTestRects&) const OVERRIDE;
177
178 protected:
179     virtual void willBeDestroyed() OVERRIDE;
180
181     class BackgroundImageGeometry {
182     public:
183         BackgroundImageGeometry()
184             : m_hasNonLocalGeometry(false)
185         { }
186
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         // Space-size represents extra width and height that may be added to
215         // the image if used as a pattern with repeat: space
216         IntSize spaceSize() const { return m_repeatSpacing; }
217         void setSpaceSize(const IntSize& repeatSpacing)
218         {
219             m_repeatSpacing = repeatSpacing;
220         }
221
222         void setPhaseX(int x) { m_phase.setX(x); }
223         void setPhaseY(int y) { m_phase.setY(y); }
224
225         void setNoRepeatX(int xOffset);
226         void setNoRepeatY(int yOffset);
227
228         void useFixedAttachment(const IntPoint& attachmentPoint);
229
230         void clip(const IntRect&);
231
232         void setHasNonLocalGeometry(bool hasNonLocalGeometry = true) { m_hasNonLocalGeometry = hasNonLocalGeometry; }
233         bool hasNonLocalGeometry() const { return m_hasNonLocalGeometry; }
234
235     private:
236         IntRect m_destRect;
237         IntPoint m_destOrigin;
238         IntPoint m_phase;
239         IntSize m_tileSize;
240         IntSize m_repeatSpacing;
241         bool m_hasNonLocalGeometry; // Has background-attachment: fixed. Implies that we can't always cheaply compute destRect.
242     };
243
244     LayoutPoint adjustedPositionRelativeToOffsetParent(const LayoutPoint&) const;
245
246     bool calculateHasBoxDecorations() const;
247     void calculateBackgroundImageGeometry(const RenderLayerModelObject* paintContainer, const FillLayer&, const LayoutRect& paintRect, BackgroundImageGeometry&, RenderObject* = 0) const;
248     void getBorderEdgeInfo(class BorderEdge[], const RenderStyle*, bool includeLogicalLeftEdge = true, bool includeLogicalRightEdge = true) const;
249     bool borderObscuresBackgroundEdge(const FloatSize& contextScale) const;
250     bool borderObscuresBackground() const;
251     RoundedRect backgroundRoundedRectAdjustedForBleedAvoidance(GraphicsContext*, const LayoutRect&, BackgroundBleedAvoidance, InlineFlowBox*, const LayoutSize&, bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const;
252     LayoutRect borderInnerRectAdjustedForBleedAvoidance(GraphicsContext*, const LayoutRect&, BackgroundBleedAvoidance) const;
253
254     InterpolationQuality chooseInterpolationQuality(GraphicsContext*, Image*, const void*, const LayoutSize&);
255
256     RenderBoxModelObject* continuation() const;
257     void setContinuation(RenderBoxModelObject*);
258
259     LayoutRect localCaretRectForEmptyElement(LayoutUnit width, LayoutUnit textIndentOffset);
260
261     static void clipRoundedInnerRect(GraphicsContext*, const LayoutRect&, const RoundedRect& clipRect);
262
263     bool hasAutoHeightOrContainingBlockWithAutoHeight() const;
264
265 public:
266     static bool shouldAntialiasLines(GraphicsContext*);
267
268     // For RenderBlocks and RenderInlines with m_style->styleType() == FIRST_LETTER, this tracks their remaining text fragments
269     RenderTextFragment* firstLetterRemainingText() const;
270     void setFirstLetterRemainingText(RenderTextFragment*);
271
272     // These functions are only used internally to manipulate the render tree structure via remove/insert/appendChildNode.
273     // Since they are typically called only to move objects around within anonymous blocks (which only have layers in
274     // the case of column spans), the default for fullRemoveInsert is false rather than true.
275     void moveChildTo(RenderBoxModelObject* toBoxModelObject, RenderObject* child, RenderObject* beforeChild, bool fullRemoveInsert = false);
276     void moveChildTo(RenderBoxModelObject* toBoxModelObject, RenderObject* child, bool fullRemoveInsert = false)
277     {
278         moveChildTo(toBoxModelObject, child, 0, fullRemoveInsert);
279     }
280     void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, bool fullRemoveInsert = false)
281     {
282         moveAllChildrenTo(toBoxModelObject, 0, fullRemoveInsert);
283     }
284     void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* beforeChild, bool fullRemoveInsert = false)
285     {
286         moveChildrenTo(toBoxModelObject, slowFirstChild(), 0, beforeChild, fullRemoveInsert);
287     }
288     // Move all of the kids from |startChild| up to but excluding |endChild|. 0 can be passed as the |endChild| to denote
289     // that all the kids from |startChild| onwards should be moved.
290     void moveChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* startChild, RenderObject* endChild, bool fullRemoveInsert = false)
291     {
292         moveChildrenTo(toBoxModelObject, startChild, endChild, 0, fullRemoveInsert);
293     }
294     void moveChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* startChild, RenderObject* endChild, RenderObject* beforeChild, bool fullRemoveInsert = false);
295
296     enum ScaleByEffectiveZoomOrNot { ScaleByEffectiveZoom, DoNotScaleByEffectiveZoom };
297     IntSize calculateImageIntrinsicDimensions(StyleImage*, const IntSize& scaledPositioningAreaSize, ScaleByEffectiveZoomOrNot) const;
298
299 private:
300     LayoutUnit computedCSSPadding(const Length&) const;
301     virtual bool isBoxModelObject() const OVERRIDE FINAL { return true; }
302
303     IntSize calculateFillTileSize(const FillLayer&, const IntSize& scaledPositioningAreaSize) const;
304
305     RoundedRect getBackgroundRoundedRect(const LayoutRect&, InlineFlowBox*, LayoutUnit inlineBoxWidth, LayoutUnit inlineBoxHeight,
306         bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const;
307
308     bool fixedBackgroundPaintsInLocalCoordinates() const;
309
310     void clipBorderSidePolygon(GraphicsContext*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
311                                BoxSide, bool firstEdgeMatches, bool secondEdgeMatches);
312     void clipBorderSideForComplexInnerPath(GraphicsContext*, const RoundedRect&, const RoundedRect&, BoxSide, const class BorderEdge[]);
313     void paintOneBorderSide(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
314                                 const IntRect& sideRect, BoxSide, BoxSide adjacentSide1, BoxSide adjacentSide2, const class BorderEdge[],
315                                 const Path*, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias, const Color* overrideColor = 0);
316     void paintTranslucentBorderSides(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder, const IntPoint& innerBorderAdjustment,
317         const class BorderEdge[], BorderEdgeFlags, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false);
318     void paintBorderSides(GraphicsContext*, const RenderStyle*, const RoundedRect& outerBorder, const RoundedRect& innerBorder,
319         const IntPoint& innerBorderAdjustment, const class BorderEdge[], BorderEdgeFlags, BackgroundBleedAvoidance,
320         bool includeLogicalLeftEdge, bool includeLogicalRightEdge, bool antialias = false, const Color* overrideColor = 0);
321     void drawBoxSideFromPath(GraphicsContext*, const LayoutRect&, const Path&, const class BorderEdge[],
322                             float thickness, float drawThickness, BoxSide, const RenderStyle*,
323                             Color, EBorderStyle, BackgroundBleedAvoidance, bool includeLogicalLeftEdge, bool includeLogicalRightEdge);
324 };
325
326 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderBoxModelObject, isBoxModelObject());
327
328 } // namespace blink
329
330 #endif // RenderBoxModelObject_h