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.
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.
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.
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.
24 #ifndef RenderBoxModelObject_h
25 #define RenderBoxModelObject_h
27 #include "core/rendering/RenderLayerModelObject.h"
28 #include "core/rendering/style/ShadowData.h"
29 #include "platform/geometry/LayoutRect.h"
33 // Modes for some of the line-related functions.
34 enum LinePositionMode { PositionOnContainingLine, PositionOfInteriorLineBoxes };
35 enum LineDirectionMode { HorizontalLine, VerticalLine };
36 typedef unsigned BorderEdgeFlags;
38 enum BackgroundBleedAvoidance {
40 BackgroundBleedShrinkBackground,
41 BackgroundBleedClipBackground,
42 BackgroundBleedBackgroundOverBorder
45 enum ContentChangeType {
51 class RenderTextFragment;
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
56 class RenderBoxModelObject : public RenderLayerModelObject {
58 RenderBoxModelObject(ContainerNode*);
59 virtual ~RenderBoxModelObject();
61 LayoutSize relativePositionOffset() const;
62 LayoutSize relativePositionLogicalOffset() const { return style()->isHorizontalWritingMode() ? relativePositionOffset() : relativePositionOffset().transposedSize(); }
64 LayoutSize offsetForInFlowPosition() const;
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;
73 int pixelSnappedOffsetLeft() const { return roundToInt(offsetLeft()); }
74 int pixelSnappedOffsetTop() const { return roundToInt(offsetTop()); }
75 virtual int pixelSnappedOffsetWidth() const;
76 virtual int pixelSnappedOffsetHeight() const;
78 virtual void updateFromStyle() OVERRIDE;
80 // This will work on inlines to return the bounding box of all of the lines' border boxes.
81 virtual IntRect borderBoundingBox() const = 0;
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()); }
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(); }
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(); }
113 int borderWidth() const { return borderLeft() + borderRight(); }
114 int borderHeight() const { return borderTop() + borderBottom(); }
116 LayoutUnit borderAndPaddingStart() const { return borderStart() + paddingStart(); }
117 LayoutUnit borderAndPaddingBefore() const { return borderBefore() + paddingBefore(); }
118 LayoutUnit borderAndPaddingAfter() const { return borderAfter() + paddingAfter(); }
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(); }
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(); }
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(); }
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(); }
150 bool hasInlineDirectionBordersPaddingOrMargin() const { return hasInlineDirectionBordersOrPadding() || marginStart()|| marginEnd(); }
151 bool hasInlineDirectionBordersOrPadding() const { return borderStart() || borderEnd() || paddingStart()|| paddingEnd(); }
153 virtual LayoutUnit containingBlockLogicalWidthForContent() const;
155 virtual void childBecameNonInline(RenderObject* /*child*/) { }
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);
162 virtual bool boxShadowShouldBeAppliedToBackground(BackgroundBleedAvoidance, InlineFlowBox* = 0) const;
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;
168 virtual void mapAbsoluteToLocalPoint(MapCoordinatesFlags, TransformState&) const OVERRIDE;
169 virtual const RenderObject* pushMappingToContainer(const RenderLayerModelObject* ancestorToStopAt, RenderGeometryMap&) const OVERRIDE;
171 virtual void setSelectionState(SelectionState) OVERRIDE;
173 void contentChanged(ContentChangeType);
174 bool hasAcceleratedCompositing() const;
176 virtual void computeLayerHitTestRects(LayerHitTestRects&) const OVERRIDE;
179 virtual void willBeDestroyed() OVERRIDE;
181 class BackgroundImageGeometry {
183 BackgroundImageGeometry()
184 : m_hasNonLocalGeometry(false)
187 IntPoint destOrigin() const { return m_destOrigin; }
188 void setDestOrigin(const IntPoint& destOrigin)
190 m_destOrigin = destOrigin;
193 IntRect destRect() const { return m_destRect; }
194 void setDestRect(const IntRect& destRect)
196 m_destRect = destRect;
199 // Returns the phase relative to the destination rectangle.
200 IntPoint relativePhase() const;
202 IntPoint phase() const { return m_phase; }
203 void setPhase(const IntPoint& phase)
208 IntSize tileSize() const { return m_tileSize; }
209 void setTileSize(const IntSize& tileSize)
211 m_tileSize = tileSize;
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)
219 m_repeatSpacing = repeatSpacing;
222 void setPhaseX(int x) { m_phase.setX(x); }
223 void setPhaseY(int y) { m_phase.setY(y); }
225 void setNoRepeatX(int xOffset);
226 void setNoRepeatY(int yOffset);
228 void useFixedAttachment(const IntPoint& attachmentPoint);
230 void clip(const IntRect&);
232 void setHasNonLocalGeometry(bool hasNonLocalGeometry = true) { m_hasNonLocalGeometry = hasNonLocalGeometry; }
233 bool hasNonLocalGeometry() const { return m_hasNonLocalGeometry; }
237 IntPoint m_destOrigin;
240 IntSize m_repeatSpacing;
241 bool m_hasNonLocalGeometry; // Has background-attachment: fixed. Implies that we can't always cheaply compute destRect.
244 LayoutPoint adjustedPositionRelativeToOffsetParent(const LayoutPoint&) const;
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;
254 InterpolationQuality chooseInterpolationQuality(GraphicsContext*, Image*, const void*, const LayoutSize&);
256 RenderBoxModelObject* continuation() const;
257 void setContinuation(RenderBoxModelObject*);
259 LayoutRect localCaretRectForEmptyElement(LayoutUnit width, LayoutUnit textIndentOffset);
261 static void clipRoundedInnerRect(GraphicsContext*, const LayoutRect&, const RoundedRect& clipRect);
263 bool hasAutoHeightOrContainingBlockWithAutoHeight() const;
266 static bool shouldAntialiasLines(GraphicsContext*);
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*);
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)
278 moveChildTo(toBoxModelObject, child, 0, fullRemoveInsert);
280 void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, bool fullRemoveInsert = false)
282 moveAllChildrenTo(toBoxModelObject, 0, fullRemoveInsert);
284 void moveAllChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* beforeChild, bool fullRemoveInsert = false)
286 moveChildrenTo(toBoxModelObject, slowFirstChild(), 0, beforeChild, fullRemoveInsert);
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)
292 moveChildrenTo(toBoxModelObject, startChild, endChild, 0, fullRemoveInsert);
294 void moveChildrenTo(RenderBoxModelObject* toBoxModelObject, RenderObject* startChild, RenderObject* endChild, RenderObject* beforeChild, bool fullRemoveInsert = false);
296 enum ScaleByEffectiveZoomOrNot { ScaleByEffectiveZoom, DoNotScaleByEffectiveZoom };
297 IntSize calculateImageIntrinsicDimensions(StyleImage*, const IntSize& scaledPositioningAreaSize, ScaleByEffectiveZoomOrNot) const;
300 LayoutUnit computedCSSPadding(const Length&) const;
301 virtual bool isBoxModelObject() const OVERRIDE FINAL { return true; }
303 IntSize calculateFillTileSize(const FillLayer&, const IntSize& scaledPositioningAreaSize) const;
305 RoundedRect getBackgroundRoundedRect(const LayoutRect&, InlineFlowBox*, LayoutUnit inlineBoxWidth, LayoutUnit inlineBoxHeight,
306 bool includeLogicalLeftEdge, bool includeLogicalRightEdge) const;
308 bool fixedBackgroundPaintsInLocalCoordinates() const;
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);
326 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderBoxModelObject, isBoxModelObject());
330 #endif // RenderBoxModelObject_h