1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 /* utility functions for drawing borders and backgrounds */
40 #ifndef nsCSSRendering_h___
41 #define nsCSSRendering_h___
43 #include "nsIRenderingContext.h"
44 #include "nsStyleConsts.h"
46 #include "gfxContext.h"
47 #include "gfxImageSurface.h"
53 struct nsCSSRendering
{
55 * Initialize any static variables used by nsCSSRendering.
57 static nsresult
Init();
60 * Clean up any static variables used by nsCSSRendering.
62 static void Shutdown();
64 static void PaintBoxShadow(nsPresContext
* aPresContext
,
65 nsIRenderingContext
& aRenderingContext
,
67 const nsPoint
& aForFramePt
,
68 const nsRect
& aDirtyRect
);
71 * Render the border for an element using css rendering rules
72 * for borders. aSkipSides is a bitmask of the sides to skip
73 * when rendering. If 0 then no sides are skipped.
75 * Both aDirtyRect and aBorderArea are in the local coordinate space
78 static void PaintBorder(nsPresContext
* aPresContext
,
79 nsIRenderingContext
& aRenderingContext
,
81 const nsRect
& aDirtyRect
,
82 const nsRect
& aBorderArea
,
83 const nsStyleBorder
& aBorderStyle
,
84 nsStyleContext
* aStyleContext
,
85 PRIntn aSkipSides
= 0);
88 * Render the outline for an element using css rendering rules
89 * for borders. aSkipSides is a bitmask of the sides to skip
90 * when rendering. If 0 then no sides are skipped.
92 * Both aDirtyRect and aBorderArea are in the local coordinate space
95 static void PaintOutline(nsPresContext
* aPresContext
,
96 nsIRenderingContext
& aRenderingContext
,
98 const nsRect
& aDirtyRect
,
99 const nsRect
& aBorderArea
,
100 const nsStyleBorder
& aBorderStyle
,
101 const nsStyleOutline
& aOutlineStyle
,
102 nsStyleContext
* aStyleContext
);
105 * Render keyboard focus on an element.
106 * |aFocusRect| is the outer rectangle of the focused element.
107 * Uses a fixed style equivalent to "1px dotted |aColor|".
108 * Not used for controls, because the native theme may differ.
110 static void PaintFocus(nsPresContext
* aPresContext
,
111 nsIRenderingContext
& aRenderingContext
,
112 const nsRect
& aFocusRect
,
116 * Fill in an nsStyleBackground to be used to paint the background for
117 * an element. The nsStyleBackground should first be initialized
118 * using the pres context. This applies the rules for propagating
119 * backgrounds between BODY, the root element, and the canvas.
120 * @return PR_TRUE if there is some meaningful background.
122 static PRBool
FindBackground(nsPresContext
* aPresContext
,
124 const nsStyleBackground
** aBackground
,
128 * Find a non-transparent background, for various table-related and
129 * HR-related backwards-compatibility hacks. Be very hesitant if
130 * you're considering calling this function -- it's usually not what
133 static const nsStyleBackground
*
134 FindNonTransparentBackground(nsStyleContext
* aContext
,
135 PRBool aStartAtParent
= PR_FALSE
);
138 * Render the background for an element using css rendering rules
141 * Both aDirtyRect and aBorderArea are in the local coordinate space
144 static void PaintBackground(nsPresContext
* aPresContext
,
145 nsIRenderingContext
& aRenderingContext
,
147 const nsRect
& aDirtyRect
,
148 const nsRect
& aBorderArea
,
149 PRBool aUsePrintSettings
,
150 nsRect
* aBGClipRect
= nsnull
);
153 * Same as |PaintBackground|, except using the provided style structs.
154 * This short-circuits the code that ensures that the root element's
155 * background is drawn on the canvas.
157 static void PaintBackgroundWithSC(nsPresContext
* aPresContext
,
158 nsIRenderingContext
& aRenderingContext
,
160 const nsRect
& aDirtyRect
,
161 const nsRect
& aBorderArea
,
162 const nsStyleBackground
& aColor
,
163 const nsStyleBorder
& aBorder
,
164 PRBool aUsePrintSettings
= PR_FALSE
,
165 nsRect
* aBGClipRect
= nsnull
);
168 * Called by the presShell when painting is finished, so we can clear our
169 * inline background data cache.
171 static void DidPaint();
173 // Draw a border segment in the table collapsing border model without
175 static void DrawTableBorderSegment(nsIRenderingContext
& aContext
,
176 PRUint8 aBorderStyle
,
177 nscolor aBorderColor
,
178 const nsStyleBackground
* aBGColor
,
179 const nsRect
& aBorderRect
,
180 PRInt32 aAppUnitsPerCSSPixel
,
181 PRUint8 aStartBevelSide
= 0,
182 nscoord aStartBevelOffset
= 0,
183 PRUint8 aEndBevelSide
= 0,
184 nscoord aEndBevelOffset
= 0);
187 * Function for painting the decoration lines for the text.
188 * NOTE: aPt, aLineSize, aAscent and aOffset are non-rounded device pixels,
192 * @param aColor the color of the decoration line
193 * @param aPt the top/left edge of the text
194 * @param aLineSize the width and the height of the decoration
196 * @param aAscent the ascent of the text
197 * @param aOffset the offset of the decoration line from
198 * the baseline of the text (if the value is
199 * positive, the line is lifted up)
200 * @param aDecoration which line will be painted. The value can be
201 * NS_STYLE_TEXT_DECORATION_UNDERLINE or
202 * NS_STYLE_TEXT_DECORATION_OVERLINE or
203 * NS_STYLE_TEXT_DECORATION_LINE_THROUGH.
204 * @param aStyle the style of the decoration line. The value
205 * can be NS_STYLE_BORDER_STYLE_SOLID or
206 * NS_STYLE_BORDER_STYLE_DOTTED or
207 * NS_STYLE_BORDER_STYLE_DASHED or
208 * NS_STYLE_BORDER_STYLE_DOUBLE.
210 static void PaintDecorationLine(gfxContext
* aGfxContext
,
211 const nscolor aColor
,
213 const gfxSize
& aLineSize
,
214 const gfxFloat aAscent
,
215 const gfxFloat aOffset
,
216 const PRUint8 aDecoration
,
217 const PRUint8 aStyle
);
220 * Function for getting the decoration line rect for the text.
221 * NOTE: aLineSize, aAscent and aOffset are non-rounded device pixels,
224 * @param aPresContext
225 * @param aLineSize the width and the height of the decoration
227 * @param aAscent the ascent of the text
228 * @param aOffset the offset of the decoration line from
229 * the baseline of the text (if the value is
230 * positive, the line is lifted up)
231 * @param aDecoration which line will be painted. The value can be
232 * NS_STYLE_TEXT_DECORATION_UNDERLINE or
233 * NS_STYLE_TEXT_DECORATION_OVERLINE or
234 * NS_STYLE_TEXT_DECORATION_LINE_THROUGH.
235 * @param aStyle the style of the decoration line. The value
236 * can be NS_STYLE_BORDER_STYLE_SOLID or
237 * NS_STYLE_BORDER_STYLE_DOTTED or
238 * NS_STYLE_BORDER_STYLE_DASHED or
239 * NS_STYLE_BORDER_STYLE_DOUBLE.
241 * @return the decoration line rect for the input,
242 * the each values are app units.
244 static nsRect
GetTextDecorationRect(nsPresContext
* aPresContext
,
245 const gfxSize
& aLineSize
,
246 const gfxFloat aAscent
,
247 const gfxFloat aOffset
,
248 const PRUint8 aDecoration
,
249 const PRUint8 aStyle
);
254 * Creates an 8-bit alpha channel context for callers to draw in, blurs the
255 * contents of that context and applies it as a 1-color mask on a
256 * different existing context. Uses gfxAlphaBoxBlur as its back end.
258 * You must call Init() first to create a suitable temporary surface to draw
259 * on. You must then draw any desired content onto the given context, then
260 * call DoPaint() to apply the blurred content as a single-color mask. You
261 * can only call Init() once, so objects cannot be reused.
263 * This is very useful for creating drop shadows or silhouettes.
265 class nsContextBoxBlur
{
268 * Prepares a gfxContext to draw on. Do not call this twice; if you want
269 * to get the gfxContext again use GetContext().
271 * @param aRect The coordinates of the surface to create.
272 * All coordinates must be in app units.
273 * This must not include the blur radius, pass
274 * it as the second parameter and everything
277 * @param aBlurRadius The blur radius in app units.
279 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
280 * for conversion. Most of the time you'll
281 * pass this from the current PresContext if
284 * @param aDestinationCtx The graphics context to apply the blurred
285 * mask to when you call DoPaint(). Make sure
286 * it is not destroyed before you call
287 * DoPaint(). To set the color of the
288 * resulting blurred graphic mask, you must
289 * set the color on this context before
292 * @param aDirtyRect The absolute dirty rect in app units. Used to
293 * optimize the temporary surface size and speed up blur.
295 * @return A blank 8-bit alpha-channel-only graphics context to
296 * draw on, or null on error. Must not be freed. The
297 * context has a device offset applied to it given by
298 * aRect. This means you can use coordinates as if it
299 * were at the desired position at aRect and you don't
300 * need to worry about translating any coordinates to
301 * draw on this temporary surface.
303 * If aBlurRadius is 0, the returned context is aDestinationCtx and
304 * DoPaint() does nothing, because no blurring is required. Therefore, you
305 * should prepare the destination context as if you were going to draw
306 * directly on it instead of any temporary surface created in this class.
308 gfxContext
* Init(const gfxRect
& aRect
, nscoord aBlurRadius
,
309 PRInt32 aAppUnitsPerDevPixel
, gfxContext
* aDestinationCtx
,
310 const gfxRect
& aDirtyRect
);
313 * Does the actual blurring and mask applying. Users of this object *must*
314 * have called Init() first, then have drawn whatever they want to be
315 * blurred onto the internal gfxContext before calling this.
320 * Gets the internal gfxContext at any time. Must not be freed. Avoid
321 * calling this before calling Init() since the context would not be
322 * constructed at that point.
324 gfxContext
* GetContext();
327 gfxAlphaBoxBlur blur
;
328 nsRefPtr
<gfxContext
> mContext
;
329 gfxContext
* mDestinationCtx
;
333 #endif /* nsCSSRendering_h___ */