Bumping manifests a=b2g-bump
[gecko.git] / layout / base / nsCSSRendering.h
blobe93c6c261b55f8b08ddad0af28db6cf800da830f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* utility functions for drawing borders and backgrounds */
8 #ifndef nsCSSRendering_h___
9 #define nsCSSRendering_h___
11 #include "gfxBlur.h"
12 #include "gfxContext.h"
13 #include "nsLayoutUtils.h"
14 #include "nsStyleStruct.h"
15 #include "nsIFrame.h"
17 class nsStyleContext;
18 class nsPresContext;
19 class nsRenderingContext;
21 namespace mozilla {
23 namespace layers {
24 class ImageContainer;
27 // A CSSSizeOrRatio represents a (possibly partially specified) size for use
28 // in computing image sizes. Either or both of the width and height might be
29 // given. A ratio of width to height may also be given. If we at least two
30 // of these then we can compute a concrete size, that is a width and height.
31 struct CSSSizeOrRatio
33 CSSSizeOrRatio()
34 : mRatio(0, 0)
35 , mHasWidth(false)
36 , mHasHeight(false) {}
38 bool CanComputeConcreteSize() const
40 return mHasWidth + mHasHeight + HasRatio() >= 2;
42 bool IsConcrete() const { return mHasWidth && mHasHeight; }
43 bool HasRatio() const { return mRatio.width > 0 && mRatio.height > 0; }
44 bool IsEmpty() const
46 return (mHasWidth && mWidth <= 0) ||
47 (mHasHeight && mHeight <= 0) ||
48 mRatio.width <= 0 || mRatio.height <= 0;
51 // CanComputeConcreteSize must return true when ComputeConcreteSize is
52 // called.
53 nsSize ComputeConcreteSize() const;
55 void SetWidth(nscoord aWidth)
57 mWidth = aWidth;
58 mHasWidth = true;
59 if (mHasHeight) {
60 mRatio = nsSize(mWidth, mHeight);
63 void SetHeight(nscoord aHeight)
65 mHeight = aHeight;
66 mHasHeight = true;
67 if (mHasWidth) {
68 mRatio = nsSize(mWidth, mHeight);
71 void SetSize(const nsSize& aSize)
73 mWidth = aSize.width;
74 mHeight = aSize.height;
75 mHasWidth = true;
76 mHasHeight = true;
77 mRatio = aSize;
79 void SetRatio(const nsSize& aRatio)
81 MOZ_ASSERT(!mHasWidth || !mHasHeight,
82 "Probably shouldn't be setting a ratio if we have a concrete size");
83 mRatio = aRatio;
86 nsSize mRatio;
87 nscoord mWidth;
88 nscoord mHeight;
89 bool mHasWidth;
90 bool mHasHeight;
95 /**
96 * This is a small wrapper class to encapsulate image drawing that can draw an
97 * nsStyleImage image, which may internally be a real image, a sub image, or a
98 * CSS gradient.
100 * @note Always call the member functions in the order of PrepareImage(),
101 * SetSize(), and Draw*().
103 class nsImageRenderer {
104 public:
105 typedef mozilla::layers::LayerManager LayerManager;
106 typedef mozilla::layers::ImageContainer ImageContainer;
108 enum {
109 FLAG_SYNC_DECODE_IMAGES = 0x01,
110 FLAG_PAINTING_TO_WINDOW = 0x02
112 enum FitType
114 CONTAIN,
115 COVER
118 nsImageRenderer(nsIFrame* aForFrame, const nsStyleImage* aImage, uint32_t aFlags);
119 ~nsImageRenderer();
121 * Populates member variables to get ready for rendering.
122 * @return true iff the image is ready, and there is at least a pixel to
123 * draw.
125 bool PrepareImage();
128 * The three Compute*Size functions correspond to the sizing algorthms and
129 * definitions from the CSS Image Values and Replaced Content spec. See
130 * http://dev.w3.org/csswg/css-images-3/#sizing .
134 * Compute the intrinsic size of the image as defined in the CSS Image Values
135 * spec. The intrinsic size is the unscaled size which the image would ideally
136 * like to be in app units.
138 mozilla::CSSSizeOrRatio ComputeIntrinsicSize();
141 * Compute the size of the rendered image using either the 'cover' or
142 * 'contain' constraints (aFitType).
143 * aIntrinsicRatio may be an invalid ratio, that is one or both of its
144 * dimensions can be less than or equal to zero.
146 static nsSize ComputeConstrainedSize(const nsSize& aConstrainingSize,
147 const nsSize& aIntrinsicRatio,
148 FitType aFitType);
150 * Compute the size of the rendered image (the concrete size) where no cover/
151 * contain constraints are given. The 'default algorithm' from the CSS Image
152 * Values spec.
154 static nsSize ComputeConcreteSize(const mozilla::CSSSizeOrRatio& aSpecifiedSize,
155 const mozilla::CSSSizeOrRatio& aIntrinsicSize,
156 const nsSize& aDefaultSize);
159 * Set this image's preferred size. This will be its intrinsic size where
160 * specified and the default size where it is not. Used as the unscaled size
161 * when rendering the image.
163 void SetPreferredSize(const mozilla::CSSSizeOrRatio& aIntrinsicSize,
164 const nsSize& aDefaultSize);
167 * Draws the image to the target rendering context using background-specific
168 * arguments.
169 * @see nsLayoutUtils::DrawImage() for parameters.
171 void DrawBackground(nsPresContext* aPresContext,
172 nsRenderingContext& aRenderingContext,
173 const nsRect& aDest,
174 const nsRect& aFill,
175 const nsPoint& aAnchor,
176 const nsRect& aDirty);
179 * Draw the image to a single component of a border-image style rendering.
180 * aFill The destination rect to be drawn into
181 * aSrc is the part of the image to be rendered into a tile (aUnitSize in
182 * aFill), if aSrc and the dest tile are different sizes, the image will be
183 * scaled to map aSrc onto the dest tile.
184 * aHFill and aVFill are the repeat patterns for the component -
185 * NS_STYLE_BORDER_IMAGE_REPEAT_*
186 * aUnitSize The scaled size of a single source rect (in destination coords)
187 * aIndex identifies the component: 0 1 2
188 * 3 4 5
189 * 6 7 8
191 void
192 DrawBorderImageComponent(nsPresContext* aPresContext,
193 nsRenderingContext& aRenderingContext,
194 const nsRect& aDirtyRect,
195 const nsRect& aFill,
196 const mozilla::CSSIntRect& aSrc,
197 uint8_t aHFill,
198 uint8_t aVFill,
199 const nsSize& aUnitSize,
200 uint8_t aIndex);
202 bool IsRasterImage();
203 bool IsAnimatedImage();
204 already_AddRefed<ImageContainer> GetContainer(LayerManager* aManager);
206 bool IsReady() { return mIsReady; }
208 private:
210 * Draws the image to the target rendering context.
211 * aSrc is a rect on the source image which will be mapped to aDest; it's
212 * currently only used for gradients.
214 * @see nsLayoutUtils::DrawImage() for other parameters.
216 void Draw(nsPresContext* aPresContext,
217 nsRenderingContext& aRenderingContext,
218 const nsRect& aDirtyRect,
219 const nsRect& aDest,
220 const nsRect& aFill,
221 const nsPoint& aAnchor,
222 const mozilla::CSSIntRect& aSrc);
225 * Helper method for creating a gfxDrawable from mPaintServerFrame or
226 * mImageElementSurface.
227 * Requires mType is eStyleImageType_Element.
228 * Returns null if we cannot create the drawable.
230 already_AddRefed<gfxDrawable> DrawableForElement(const nsRect& aImageRect,
231 nsRenderingContext& aRenderingContext);
233 nsIFrame* mForFrame;
234 const nsStyleImage* mImage;
235 nsStyleImageType mType;
236 nsCOMPtr<imgIContainer> mImageContainer;
237 nsRefPtr<nsStyleGradient> mGradientData;
238 nsIFrame* mPaintServerFrame;
239 nsLayoutUtils::SurfaceFromElementResult mImageElementSurface;
240 bool mIsReady;
241 nsSize mSize; // unscaled size of the image, in app units
242 uint32_t mFlags;
246 * A struct representing all the information needed to paint a background
247 * image to some target, taking into account all CSS background-* properties.
248 * See PrepareBackgroundLayer.
250 struct nsBackgroundLayerState {
252 * @param aFlags some combination of nsCSSRendering::PAINTBG_* flags
254 nsBackgroundLayerState(nsIFrame* aForFrame, const nsStyleImage* aImage, uint32_t aFlags)
255 : mImageRenderer(aForFrame, aImage, aFlags), mCompositingOp(gfxContext::OPERATOR_OVER) {}
258 * The nsImageRenderer that will be used to draw the background.
260 nsImageRenderer mImageRenderer;
262 * A rectangle that one copy of the image tile is mapped onto. Same
263 * coordinate system as aBorderArea/aBGClipRect passed into
264 * PrepareBackgroundLayer.
266 nsRect mDestArea;
268 * The actual rectangle that should be filled with (complete or partial)
269 * image tiles. Same coordinate system as aBorderArea/aBGClipRect passed into
270 * PrepareBackgroundLayer.
272 nsRect mFillArea;
274 * The anchor point that should be snapped to a pixel corner. Same
275 * coordinate system as aBorderArea/aBGClipRect passed into
276 * PrepareBackgroundLayer.
278 nsPoint mAnchor;
280 * The compositing operation that the image should use
282 gfxContext::GraphicsOperator mCompositingOp;
285 struct nsCSSRendering {
286 typedef nsIFrame::Sides Sides;
289 * Initialize any static variables used by nsCSSRendering.
291 static void Init();
294 * Clean up any static variables used by nsCSSRendering.
296 static void Shutdown();
298 static void PaintBoxShadowInner(nsPresContext* aPresContext,
299 nsRenderingContext& aRenderingContext,
300 nsIFrame* aForFrame,
301 const nsRect& aFrameArea,
302 const nsRect& aDirtyRect);
304 static void PaintBoxShadowOuter(nsPresContext* aPresContext,
305 nsRenderingContext& aRenderingContext,
306 nsIFrame* aForFrame,
307 const nsRect& aFrameArea,
308 const nsRect& aDirtyRect,
309 float aOpacity = 1.0);
311 static void ComputePixelRadii(const nscoord *aAppUnitsRadii,
312 nscoord aAppUnitsPerPixel,
313 gfxCornerSizes *oBorderRadii);
316 * Render the border for an element using css rendering rules
317 * for borders. aSkipSides says which sides to skip
318 * when rendering, the default is to skip none.
320 static void PaintBorder(nsPresContext* aPresContext,
321 nsRenderingContext& aRenderingContext,
322 nsIFrame* aForFrame,
323 const nsRect& aDirtyRect,
324 const nsRect& aBorderArea,
325 nsStyleContext* aStyleContext,
326 Sides aSkipSides = Sides());
329 * Like PaintBorder, but taking an nsStyleBorder argument instead of
330 * getting it from aStyleContext. aSkipSides says which sides to skip
331 * when rendering, the default is to skip none.
333 static void PaintBorderWithStyleBorder(nsPresContext* aPresContext,
334 nsRenderingContext& aRenderingContext,
335 nsIFrame* aForFrame,
336 const nsRect& aDirtyRect,
337 const nsRect& aBorderArea,
338 const nsStyleBorder& aBorderStyle,
339 nsStyleContext* aStyleContext,
340 Sides aSkipSides = Sides());
344 * Render the outline for an element using css rendering rules
345 * for borders.
347 static void PaintOutline(nsPresContext* aPresContext,
348 nsRenderingContext& aRenderingContext,
349 nsIFrame* aForFrame,
350 const nsRect& aDirtyRect,
351 const nsRect& aBorderArea,
352 nsStyleContext* aStyleContext);
355 * Render keyboard focus on an element.
356 * |aFocusRect| is the outer rectangle of the focused element.
357 * Uses a fixed style equivalent to "1px dotted |aColor|".
358 * Not used for controls, because the native theme may differ.
360 static void PaintFocus(nsPresContext* aPresContext,
361 nsRenderingContext& aRenderingContext,
362 const nsRect& aFocusRect,
363 nscolor aColor);
366 * Render a gradient for an element.
367 * aDest is the rect for a single tile of the gradient on the destination.
368 * aFill is the rect on the destination to be covered by repeated tiling of
369 * the gradient.
370 * aSrc is the part of the gradient to be rendered into a tile (aDest), if
371 * aSrc and aDest are different sizes, the image will be scaled to map aSrc
372 * onto aDest.
373 * aIntrinsicSize is the size of the source gradient.
375 static void PaintGradient(nsPresContext* aPresContext,
376 nsRenderingContext& aRenderingContext,
377 nsStyleGradient* aGradient,
378 const nsRect& aDirtyRect,
379 const nsRect& aDest,
380 const nsRect& aFill,
381 const mozilla::CSSIntRect& aSrc,
382 const nsSize& aIntrinsiceSize);
385 * Find the frame whose background style should be used to draw the
386 * canvas background. aForFrame must be the frame for the root element
387 * whose background style should be used. This function will return
388 * aForFrame unless the <body> background should be propagated, in
389 * which case we return the frame associated with the <body>'s background.
391 static nsIFrame* FindBackgroundStyleFrame(nsIFrame* aForFrame);
394 * @return true if |aFrame| is a canvas frame, in the CSS sense.
396 static bool IsCanvasFrame(nsIFrame* aFrame);
399 * Fill in an aBackgroundSC to be used to paint the background
400 * for an element. This applies the rules for propagating
401 * backgrounds between BODY, the root element, and the canvas.
402 * @return true if there is some meaningful background.
404 static bool FindBackground(nsIFrame* aForFrame,
405 nsStyleContext** aBackgroundSC);
408 * As FindBackground, but the passed-in frame is known to be a root frame
409 * (returned from nsCSSFrameConstructor::GetRootElementStyleFrame())
410 * and there is always some meaningful background returned.
412 static nsStyleContext* FindRootFrameBackground(nsIFrame* aForFrame);
415 * Returns background style information for the canvas.
417 * @param aForFrame
418 * the frame used to represent the canvas, in the CSS sense (i.e.
419 * nsCSSRendering::IsCanvasFrame(aForFrame) must be true)
420 * @param aRootElementFrame
421 * the frame representing the root element of the document
422 * @param aBackground
423 * contains background style information for the canvas on return
425 static nsStyleContext*
426 FindCanvasBackground(nsIFrame* aForFrame, nsIFrame* aRootElementFrame)
428 NS_ABORT_IF_FALSE(IsCanvasFrame(aForFrame), "not a canvas frame");
429 if (aRootElementFrame)
430 return FindRootFrameBackground(aRootElementFrame);
432 // This should always give transparent, so we'll fill it in with the
433 // default color if needed. This seems to happen a bit while a page is
434 // being loaded.
435 return aForFrame->StyleContext();
439 * Find a frame which draws a non-transparent background,
440 * for various table-related and HR-related backwards-compatibility hacks.
441 * This function will also stop if it finds themed frame which might draw
442 * background.
444 * Be very hesitant if you're considering calling this function -- it's
445 * usually not what you want.
447 static nsIFrame*
448 FindNonTransparentBackgroundFrame(nsIFrame* aFrame,
449 bool aStartAtParent = false);
452 * Determine the background color to draw taking into account print settings.
454 static nscolor
455 DetermineBackgroundColor(nsPresContext* aPresContext,
456 nsStyleContext* aStyleContext,
457 nsIFrame* aFrame,
458 bool& aDrawBackgroundImage,
459 bool& aDrawBackgroundColor);
461 static nsRect
462 ComputeBackgroundPositioningArea(nsPresContext* aPresContext,
463 nsIFrame* aForFrame,
464 const nsRect& aBorderArea,
465 const nsStyleBackground::Layer& aLayer,
466 nsIFrame** aAttachedToFrame);
468 static nsBackgroundLayerState
469 PrepareBackgroundLayer(nsPresContext* aPresContext,
470 nsIFrame* aForFrame,
471 uint32_t aFlags,
472 const nsRect& aBorderArea,
473 const nsRect& aBGClipRect,
474 const nsStyleBackground::Layer& aLayer);
476 struct BackgroundClipState {
477 nsRect mBGClipArea; // Affected by mClippedRadii
478 nsRect mAdditionalBGClipArea; // Not affected by mClippedRadii
479 nsRect mDirtyRect;
480 gfxRect mDirtyRectGfx;
482 nscoord mRadii[8];
483 gfxCornerSizes mClippedRadii;
484 bool mHasRoundedCorners;
485 bool mHasAdditionalBGClipArea;
487 // Whether we are being asked to draw with a caller provided background
488 // clipping area. If this is true we also disable rounded corners.
489 bool mCustomClip;
492 static void
493 GetBackgroundClip(const nsStyleBackground::Layer& aLayer,
494 nsIFrame* aForFrame, const nsStyleBorder& aBorder, const nsRect& aBorderArea,
495 const nsRect& aCallerDirtyRect, bool aWillPaintBorder,
496 nscoord aAppUnitsPerPixel,
497 /* out */ BackgroundClipState* aClipState);
500 * Render the background for an element using css rendering rules
501 * for backgrounds.
503 enum {
505 * When this flag is passed, the element's nsDisplayBorder will be
506 * painted immediately on top of this background.
508 PAINTBG_WILL_PAINT_BORDER = 0x01,
510 * When this flag is passed, images are synchronously decoded.
512 PAINTBG_SYNC_DECODE_IMAGES = 0x02,
514 * When this flag is passed, painting will go to the screen so we can
515 * take advantage of the fact that it will be clipped to the viewport.
517 PAINTBG_TO_WINDOW = 0x04
519 static void PaintBackground(nsPresContext* aPresContext,
520 nsRenderingContext& aRenderingContext,
521 nsIFrame* aForFrame,
522 const nsRect& aDirtyRect,
523 const nsRect& aBorderArea,
524 uint32_t aFlags,
525 nsRect* aBGClipRect = nullptr,
526 int32_t aLayer = -1);
529 * Same as |PaintBackground|, except using the provided style structs.
530 * This short-circuits the code that ensures that the root element's
531 * background is drawn on the canvas.
532 * The aLayer parameter allows you to paint a single layer of the background.
533 * The default value for aLayer, -1, means that all layers will be painted.
534 * The background color will only be painted if the back-most layer is also
535 * being painted.
537 static void PaintBackgroundWithSC(nsPresContext* aPresContext,
538 nsRenderingContext& aRenderingContext,
539 nsIFrame* aForFrame,
540 const nsRect& aDirtyRect,
541 const nsRect& aBorderArea,
542 nsStyleContext *aStyleContext,
543 const nsStyleBorder& aBorder,
544 uint32_t aFlags,
545 nsRect* aBGClipRect = nullptr,
546 int32_t aLayer = -1);
549 * Returns the rectangle covered by the given background layer image, taking
550 * into account background positioning, sizing, and repetition, but not
551 * clipping.
553 static nsRect GetBackgroundLayerRect(nsPresContext* aPresContext,
554 nsIFrame* aForFrame,
555 const nsRect& aBorderArea,
556 const nsRect& aClipRect,
557 const nsStyleBackground::Layer& aLayer,
558 uint32_t aFlags);
561 * Checks if image in layer aLayer of aBackground is currently decoded.
563 static bool IsBackgroundImageDecodedForStyleContextAndLayer(
564 const nsStyleBackground *aBackground, uint32_t aLayer);
567 * Checks if all images that are part of the background for aFrame are
568 * currently decoded.
570 static bool AreAllBackgroundImagesDecodedForFrame(nsIFrame* aFrame);
573 * Called when we start creating a display list. The frame tree will not
574 * change until a matching EndFrameTreeLocked is called.
576 static void BeginFrameTreesLocked();
578 * Called when we've finished using a display list. When all
579 * BeginFrameTreeLocked calls have been balanced by an EndFrameTreeLocked,
580 * the frame tree may start changing again.
582 static void EndFrameTreesLocked();
584 // Draw a border segment in the table collapsing border model without
585 // beveling corners
586 static void DrawTableBorderSegment(nsRenderingContext& aContext,
587 uint8_t aBorderStyle,
588 nscolor aBorderColor,
589 const nsStyleBackground* aBGColor,
590 const nsRect& aBorderRect,
591 int32_t aAppUnitsPerCSSPixel,
592 uint8_t aStartBevelSide = 0,
593 nscoord aStartBevelOffset = 0,
594 uint8_t aEndBevelSide = 0,
595 nscoord aEndBevelOffset = 0);
598 * Function for painting the decoration lines for the text.
599 * NOTE: aPt, aLineSize, aAscent and aOffset are non-rounded device pixels,
600 * not app units.
601 * input:
602 * @param aFrame the frame which needs the decoration line
603 * @param aGfxContext
604 * @param aDirtyRect no need to paint outside this rect
605 * @param aColor the color of the decoration line
606 * @param aPt the top/left edge of the text
607 * @param aXInFrame the distance between aPt.x and left edge of
608 * aFrame. If the decoration line is for shadow,
609 * set the distance between the left edge of
610 * the aFrame and the position of the text as
611 * positioned without offset of the shadow.
612 * @param aLineSize the width and the height of the decoration
613 * line
614 * @param aAscent the ascent of the text
615 * @param aOffset the offset of the decoration line from
616 * the baseline of the text (if the value is
617 * positive, the line is lifted up)
618 * @param aDecoration which line will be painted. The value can be
619 * NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE or
620 * NS_STYLE_TEXT_DECORATION_LINE_OVERLINE or
621 * NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH.
622 * @param aStyle the style of the decoration line such as
623 * NS_STYLE_TEXT_DECORATION_STYLE_*.
624 * @param aDescentLimit If aDescentLimit is zero or larger and the
625 * underline overflows from the descent space,
626 * the underline should be lifted up as far as
627 * possible. Note that this does not mean the
628 * underline never overflows from this
629 * limitation. Because if the underline is
630 * positioned to the baseline or upper, it causes
631 * unreadability. Note that if this is zero
632 * or larger, the underline rect may be shrunken
633 * if it's possible. Therefore, this value is
634 * used for strikeout line and overline too.
636 static void PaintDecorationLine(nsIFrame* aFrame,
637 gfxContext* aGfxContext,
638 const gfxRect& aDirtyRect,
639 const nscolor aColor,
640 const gfxPoint& aPt,
641 const gfxFloat aXInFrame,
642 const gfxSize& aLineSize,
643 const gfxFloat aAscent,
644 const gfxFloat aOffset,
645 const uint8_t aDecoration,
646 const uint8_t aStyle,
647 const gfxFloat aDescentLimit = -1.0);
650 * Adds a path corresponding to the outline of the decoration line to
651 * the specified context. Arguments have the same meaning as for
652 * PaintDecorationLine. Currently this only works for solid
653 * decorations; for other decoration styles, an empty path is added
654 * to the context.
656 static void DecorationLineToPath(nsIFrame* aFrame,
657 gfxContext* aGfxContext,
658 const gfxRect& aDirtyRect,
659 const nscolor aColor,
660 const gfxPoint& aPt,
661 const gfxFloat aXInFrame,
662 const gfxSize& aLineSize,
663 const gfxFloat aAscent,
664 const gfxFloat aOffset,
665 const uint8_t aDecoration,
666 const uint8_t aStyle,
667 const gfxFloat aDescentLimit = -1.0);
670 * Function for getting the decoration line rect for the text.
671 * NOTE: aLineSize, aAscent and aOffset are non-rounded device pixels,
672 * not app units.
673 * input:
674 * @param aPresContext
675 * @param aLineSize the width and the height of the decoration
676 * line
677 * @param aAscent the ascent of the text
678 * @param aOffset the offset of the decoration line from
679 * the baseline of the text (if the value is
680 * positive, the line is lifted up)
681 * @param aDecoration which line will be painted. The value can be
682 * NS_STYLE_TEXT_DECORATION_LINE_UNDERLINE or
683 * NS_STYLE_TEXT_DECORATION_LINE_OVERLINE or
684 * NS_STYLE_TEXT_DECORATION_LINE_LINE_THROUGH.
685 * @param aStyle the style of the decoration line such as
686 * NS_STYLE_TEXT_DECORATION_STYLE_*.
687 * @param aDescentLimit If aDescentLimit is zero or larger and the
688 * underline overflows from the descent space,
689 * the underline should be lifted up as far as
690 * possible. Note that this does not mean the
691 * underline never overflows from this
692 * limitation. Because if the underline is
693 * positioned to the baseline or upper, it causes
694 * unreadability. Note that if this is zero
695 * or larger, the underline rect may be shrunken
696 * if it's possible. Therefore, this value is
697 * used for strikeout line and overline too.
698 * output:
699 * @return the decoration line rect for the input,
700 * the each values are app units.
702 static nsRect GetTextDecorationRect(nsPresContext* aPresContext,
703 const gfxSize& aLineSize,
704 const gfxFloat aAscent,
705 const gfxFloat aOffset,
706 const uint8_t aDecoration,
707 const uint8_t aStyle,
708 const gfxFloat aDescentLimit = -1.0);
710 static gfxContext::GraphicsOperator GetGFXBlendMode(uint8_t mBlendMode) {
711 switch (mBlendMode) {
712 case NS_STYLE_BLEND_NORMAL: return gfxContext::OPERATOR_OVER;
713 case NS_STYLE_BLEND_MULTIPLY: return gfxContext::OPERATOR_MULTIPLY;
714 case NS_STYLE_BLEND_SCREEN: return gfxContext::OPERATOR_SCREEN;
715 case NS_STYLE_BLEND_OVERLAY: return gfxContext::OPERATOR_OVERLAY;
716 case NS_STYLE_BLEND_DARKEN: return gfxContext::OPERATOR_DARKEN;
717 case NS_STYLE_BLEND_LIGHTEN: return gfxContext::OPERATOR_LIGHTEN;
718 case NS_STYLE_BLEND_COLOR_DODGE: return gfxContext::OPERATOR_COLOR_DODGE;
719 case NS_STYLE_BLEND_COLOR_BURN: return gfxContext::OPERATOR_COLOR_BURN;
720 case NS_STYLE_BLEND_HARD_LIGHT: return gfxContext::OPERATOR_HARD_LIGHT;
721 case NS_STYLE_BLEND_SOFT_LIGHT: return gfxContext::OPERATOR_SOFT_LIGHT;
722 case NS_STYLE_BLEND_DIFFERENCE: return gfxContext::OPERATOR_DIFFERENCE;
723 case NS_STYLE_BLEND_EXCLUSION: return gfxContext::OPERATOR_EXCLUSION;
724 case NS_STYLE_BLEND_HUE: return gfxContext::OPERATOR_HUE;
725 case NS_STYLE_BLEND_SATURATION: return gfxContext::OPERATOR_SATURATION;
726 case NS_STYLE_BLEND_COLOR: return gfxContext::OPERATOR_COLOR;
727 case NS_STYLE_BLEND_LUMINOSITY: return gfxContext::OPERATOR_LUMINOSITY;
728 default: MOZ_ASSERT(false); return gfxContext::OPERATOR_OVER;
731 return gfxContext::OPERATOR_OVER;
734 protected:
735 static gfxRect GetTextDecorationRectInternal(const gfxPoint& aPt,
736 const gfxSize& aLineSize,
737 const gfxFloat aAscent,
738 const gfxFloat aOffset,
739 const uint8_t aDecoration,
740 const uint8_t aStyle,
741 const gfxFloat aDscentLimit);
744 * Returns inflated rect for painting a decoration line.
745 * Complex style decoration lines should be painted from leftmost of nearest
746 * ancestor block box because that makes better look of connection of lines
747 * for different nodes. ExpandPaintingRectForDecorationLine() returns
748 * a rect for actual painting rect for the clipped rect.
750 * input:
751 * @param aFrame the frame which needs the decoration line.
752 * @param aStyle the style of the complex decoration line
753 * NS_STYLE_TEXT_DECORATION_STYLE_DOTTED or
754 * NS_STYLE_TEXT_DECORATION_STYLE_DASHED or
755 * NS_STYLE_TEXT_DECORATION_STYLE_WAVY.
756 * @param aClippedRect the clipped rect for the decoration line.
757 * in other words, visible area of the line.
758 * @param aXInFrame the distance between left edge of aFrame and
759 * aClippedRect.pos.x.
760 * @param aCycleLength the width of one cycle of the line style.
762 static gfxRect ExpandPaintingRectForDecorationLine(
763 nsIFrame* aFrame,
764 const uint8_t aStyle,
765 const gfxRect &aClippedRect,
766 const gfxFloat aXInFrame,
767 const gfxFloat aCycleLength);
771 * nsContextBoxBlur
772 * Creates an 8-bit alpha channel context for callers to draw in, blurs the
773 * contents of that context and applies it as a 1-color mask on a
774 * different existing context. Uses gfxAlphaBoxBlur as its back end.
776 * You must call Init() first to create a suitable temporary surface to draw
777 * on. You must then draw any desired content onto the given context, then
778 * call DoPaint() to apply the blurred content as a single-color mask. You
779 * can only call Init() once, so objects cannot be reused.
781 * This is very useful for creating drop shadows or silhouettes.
783 class nsContextBoxBlur {
784 public:
785 enum {
786 FORCE_MASK = 0x01
789 * Prepares a gfxContext to draw on. Do not call this twice; if you want
790 * to get the gfxContext again use GetContext().
792 * @param aRect The coordinates of the surface to create.
793 * All coordinates must be in app units.
794 * This must not include the blur radius, pass
795 * it as the second parameter and everything
796 * is taken care of.
798 * @param aBlurRadius The blur radius in app units.
800 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
801 * for conversion. Most of the time you'll
802 * pass this from the current PresContext if
803 * available.
805 * @param aDestinationCtx The graphics context to apply the blurred
806 * mask to when you call DoPaint(). Make sure
807 * it is not destroyed before you call
808 * DoPaint(). To set the color of the
809 * resulting blurred graphic mask, you must
810 * set the color on this context before
811 * calling Init().
813 * @param aDirtyRect The absolute dirty rect in app units. Used to
814 * optimize the temporary surface size and speed up blur.
816 * @param aSkipRect An area in device pixels (NOT app units!) to avoid
817 * blurring over, to prevent unnecessary work.
819 * @param aFlags FORCE_MASK to ensure that the content drawn to the
820 * returned gfxContext is used as a mask, and not
821 * drawn directly to aDestinationCtx.
823 * @return A blank 8-bit alpha-channel-only graphics context to
824 * draw on, or null on error. Must not be freed. The
825 * context has a device offset applied to it given by
826 * aRect. This means you can use coordinates as if it
827 * were at the desired position at aRect and you don't
828 * need to worry about translating any coordinates to
829 * draw on this temporary surface.
831 * If aBlurRadius is 0, the returned context is aDestinationCtx and
832 * DoPaint() does nothing, because no blurring is required. Therefore, you
833 * should prepare the destination context as if you were going to draw
834 * directly on it instead of any temporary surface created in this class.
836 gfxContext* Init(const nsRect& aRect, nscoord aSpreadRadius,
837 nscoord aBlurRadius,
838 int32_t aAppUnitsPerDevPixel, gfxContext* aDestinationCtx,
839 const nsRect& aDirtyRect, const gfxRect* aSkipRect,
840 uint32_t aFlags = 0);
843 * Does the actual blurring and mask applying. Users of this object *must*
844 * have called Init() first, then have drawn whatever they want to be
845 * blurred onto the internal gfxContext before calling this.
847 void DoPaint();
850 * Gets the internal gfxContext at any time. Must not be freed. Avoid
851 * calling this before calling Init() since the context would not be
852 * constructed at that point.
854 gfxContext* GetContext();
858 * Get the margin associated with the given blur radius, i.e., the
859 * additional area that might be painted as a result of it. (The
860 * margin for a spread radius is itself, on all sides.)
862 static nsMargin GetBlurRadiusMargin(nscoord aBlurRadius,
863 int32_t aAppUnitsPerDevPixel);
866 * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent
867 * to calling Init(), drawing a rectangle onto the returned surface
868 * and then calling DoPaint, but may let us optimize better in the
869 * backend.
871 * @param aDestinationCtx The destination to blur to.
872 * @param aRect The rectangle to blur in app units.
873 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
874 * for conversion. Most of the time you'll
875 * pass this from the current PresContext if
876 * available.
877 * @param aCornerRadii Corner radii for aRect, if it is a rounded
878 * rectangle.
879 * @param aBlurRadius The blur radius in app units.
880 * @param aShadowColor The color to draw the blurred shadow.
881 * @param aDirtyRect The absolute dirty rect in app units. Used to
882 * optimize the temporary surface size and speed up blur.
883 * @param aSkipRect An area in device pixels (NOT app units!) to avoid
884 * blurring over, to prevent unnecessary work.
886 static void BlurRectangle(gfxContext* aDestinationCtx,
887 const nsRect& aRect,
888 int32_t aAppUnitsPerDevPixel,
889 gfxCornerSizes* aCornerRadii,
890 nscoord aBlurRadius,
891 const gfxRGBA& aShadowColor,
892 const nsRect& aDirtyRect,
893 const gfxRect& aSkipRect);
895 protected:
896 gfxAlphaBoxBlur blur;
897 nsRefPtr<gfxContext> mContext;
898 gfxContext* mDestinationCtx;
900 /* This is true if the blur already has it's content transformed
901 * by mDestinationCtx's transform */
902 bool mPreTransformed;
906 #endif /* nsCSSRendering_h___ */