1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* utility functions for drawing borders and backgrounds */
9 #ifndef nsCSSRendering_h___
10 #define nsCSSRendering_h___
13 #include "gfxContext.h"
14 #include "mozilla/gfx/PathHelpers.h"
15 #include "mozilla/gfx/Rect.h"
16 #include "mozilla/TypedEnumBits.h"
17 #include "nsStyleStruct.h"
19 #include "nsImageRenderer.h"
20 #include "nsCSSRenderingBorders.h"
21 #include "gfxTextRun.h"
37 class StackingContextHelper
;
38 class WebRenderParentCommand
;
39 class WebRenderLayerManager
;
40 class RenderRootStateManager
;
44 class DisplayListBuilder
;
47 enum class PaintBorderFlags
: uint8_t { SyncDecodeImages
= 1 << 0 };
48 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(PaintBorderFlags
)
50 } // namespace mozilla
53 * A struct representing all the information needed to paint a background
54 * image to some target, taking into account all CSS background-* properties.
55 * See PrepareImageLayer.
57 struct nsBackgroundLayerState
{
58 typedef mozilla::gfx::CompositionOp CompositionOp
;
59 typedef mozilla::nsImageRenderer nsImageRenderer
;
62 * @param aFlags some combination of nsCSSRendering::PAINTBG_* flags
64 nsBackgroundLayerState(nsIFrame
* aForFrame
, const mozilla::StyleImage
* aImage
,
66 : mImageRenderer(aForFrame
, aImage
, aFlags
) {}
69 * The nsImageRenderer that will be used to draw the background.
71 nsImageRenderer mImageRenderer
;
73 * A rectangle that one copy of the image tile is mapped onto. Same
74 * coordinate system as aBorderArea/aBGClipRect passed into
79 * The actual rectangle that should be filled with (complete or partial)
80 * image tiles. Same coordinate system as aBorderArea/aBGClipRect passed into
85 * The anchor point that should be snapped to a pixel corner. Same
86 * coordinate system as aBorderArea/aBGClipRect passed into
91 * The background-repeat property space keyword computes the
92 * repeat size which is image size plus spacing.
97 struct nsCSSRendering
{
98 typedef mozilla::gfx::sRGBColor sRGBColor
;
99 typedef mozilla::gfx::CompositionOp CompositionOp
;
100 typedef mozilla::gfx::DrawTarget DrawTarget
;
101 typedef mozilla::gfx::Float Float
;
102 typedef mozilla::gfx::Point Point
;
103 typedef mozilla::gfx::Rect Rect
;
104 typedef mozilla::gfx::Size Size
;
105 typedef mozilla::gfx::RectCornerRadii RectCornerRadii
;
106 typedef mozilla::layers::WebRenderLayerManager WebRenderLayerManager
;
107 typedef mozilla::image::ImgDrawResult ImgDrawResult
;
108 typedef nsIFrame::Sides Sides
;
111 * Initialize any static variables used by nsCSSRendering.
116 * Clean up any static variables used by nsCSSRendering.
118 static void Shutdown();
120 static bool IsBoxDecorationSlice(const nsStyleBorder
& aStyleBorder
);
121 static nsRect
BoxDecorationRectForBorder(
122 nsIFrame
* aFrame
, const nsRect
& aBorderArea
, Sides aSkipSides
,
123 const nsStyleBorder
* aStyleBorder
= nullptr);
124 static nsRect
BoxDecorationRectForBackground(
125 nsIFrame
* aFrame
, const nsRect
& aBorderArea
, Sides aSkipSides
,
126 const nsStyleBorder
* aStyleBorder
= nullptr);
128 static bool GetShadowInnerRadii(nsIFrame
* aFrame
, const nsRect
& aFrameArea
,
129 RectCornerRadii
& aOutInnerRadii
);
130 static nsRect
GetBoxShadowInnerPaddingRect(nsIFrame
* aFrame
,
131 const nsRect
& aFrameArea
);
132 static bool ShouldPaintBoxShadowInner(nsIFrame
* aFrame
);
133 static void PaintBoxShadowInner(nsPresContext
* aPresContext
,
134 gfxContext
& aRenderingContext
,
136 const nsRect
& aFrameArea
);
138 static bool GetBorderRadii(const nsRect
& aFrameRect
,
139 const nsRect
& aBorderRect
, nsIFrame
* aFrame
,
140 RectCornerRadii
& aOutRadii
);
141 static nsRect
GetShadowRect(const nsRect
& aFrameArea
, bool aNativeTheme
,
142 nsIFrame
* aForFrame
);
143 static mozilla::gfx::sRGBColor
GetShadowColor(
144 const mozilla::StyleSimpleShadow
&, nsIFrame
* aFrame
, float aOpacity
);
145 // Returns if the frame has a themed frame.
146 // aMaybeHasBorderRadius will return false if we can early detect
147 // that we don't have a border radius.
148 static bool HasBoxShadowNativeTheme(nsIFrame
* aFrame
,
149 bool& aMaybeHasBorderRadius
);
150 static void PaintBoxShadowOuter(nsPresContext
* aPresContext
,
151 gfxContext
& aRenderingContext
,
152 nsIFrame
* aForFrame
, const nsRect
& aFrameArea
,
153 const nsRect
& aDirtyRect
,
154 float aOpacity
= 1.0);
156 static void ComputePixelRadii(const nscoord
* aAppUnitsRadii
,
157 nscoord aAppUnitsPerPixel
,
158 RectCornerRadii
* oBorderRadii
);
161 * Render the border for an element using css rendering rules
162 * for borders. aSkipSides says which sides to skip
163 * when rendering, the default is to skip none.
165 static ImgDrawResult
PaintBorder(
166 nsPresContext
* aPresContext
, gfxContext
& aRenderingContext
,
167 nsIFrame
* aForFrame
, const nsRect
& aDirtyRect
, const nsRect
& aBorderArea
,
168 mozilla::ComputedStyle
* aStyle
, mozilla::PaintBorderFlags aFlags
,
169 Sides aSkipSides
= Sides());
172 * Like PaintBorder, but taking an nsStyleBorder argument instead of
173 * getting it from aStyle. aSkipSides says which sides to skip
174 * when rendering, the default is to skip none.
176 static ImgDrawResult
PaintBorderWithStyleBorder(
177 nsPresContext
* aPresContext
, gfxContext
& aRenderingContext
,
178 nsIFrame
* aForFrame
, const nsRect
& aDirtyRect
, const nsRect
& aBorderArea
,
179 const nsStyleBorder
& aBorderStyle
, mozilla::ComputedStyle
* aStyle
,
180 mozilla::PaintBorderFlags aFlags
, Sides aSkipSides
= Sides());
182 static mozilla::Maybe
<nsCSSBorderRenderer
> CreateBorderRenderer(
183 nsPresContext
* aPresContext
, DrawTarget
* aDrawTarget
, nsIFrame
* aForFrame
,
184 const nsRect
& aDirtyRect
, const nsRect
& aBorderArea
,
185 mozilla::ComputedStyle
* aStyle
, bool* aOutBorderIsEmpty
,
186 Sides aSkipSides
= Sides());
188 static mozilla::Maybe
<nsCSSBorderRenderer
>
189 CreateBorderRendererWithStyleBorder(
190 nsPresContext
* aPresContext
, DrawTarget
* aDrawTarget
, nsIFrame
* aForFrame
,
191 const nsRect
& aDirtyRect
, const nsRect
& aBorderArea
,
192 const nsStyleBorder
& aBorderStyle
, mozilla::ComputedStyle
* aStyle
,
193 bool* aOutBorderIsEmpty
, Sides aSkipSides
= Sides());
195 static mozilla::Maybe
<nsCSSBorderRenderer
>
196 CreateNullBorderRendererWithStyleBorder(
197 nsPresContext
* aPresContext
, DrawTarget
* aDrawTarget
, nsIFrame
* aForFrame
,
198 const nsRect
& aDirtyRect
, const nsRect
& aBorderArea
,
199 const nsStyleBorder
& aBorderStyle
, mozilla::ComputedStyle
* aStyle
,
200 bool* aOutBorderIsEmpty
, Sides aSkipSides
= Sides());
202 static mozilla::Maybe
<nsCSSBorderRenderer
>
203 CreateBorderRendererForNonThemedOutline(nsPresContext
* aPresContext
,
204 DrawTarget
* aDrawTarget
,
206 const nsRect
& aDirtyRect
,
207 const nsRect
& aInnerRect
,
208 mozilla::ComputedStyle
* aStyle
);
210 static ImgDrawResult
CreateWebRenderCommandsForBorder(
211 mozilla::nsDisplayItem
* aItem
, nsIFrame
* aForFrame
,
212 const nsRect
& aBorderArea
, mozilla::wr::DisplayListBuilder
& aBuilder
,
213 mozilla::wr::IpcResourceUpdateQueue
& aResources
,
214 const mozilla::layers::StackingContextHelper
& aSc
,
215 mozilla::layers::RenderRootStateManager
* aManager
,
216 mozilla::nsDisplayListBuilder
* aDisplayListBuilder
);
218 static void CreateWebRenderCommandsForNullBorder(
219 mozilla::nsDisplayItem
* aItem
, nsIFrame
* aForFrame
,
220 const nsRect
& aBorderArea
, mozilla::wr::DisplayListBuilder
& aBuilder
,
221 mozilla::wr::IpcResourceUpdateQueue
& aResources
,
222 const mozilla::layers::StackingContextHelper
& aSc
,
223 const nsStyleBorder
& aStyleBorder
);
225 static ImgDrawResult
CreateWebRenderCommandsForBorderWithStyleBorder(
226 mozilla::nsDisplayItem
* aItem
, nsIFrame
* aForFrame
,
227 const nsRect
& aBorderArea
, mozilla::wr::DisplayListBuilder
& aBuilder
,
228 mozilla::wr::IpcResourceUpdateQueue
& aResources
,
229 const mozilla::layers::StackingContextHelper
& aSc
,
230 mozilla::layers::RenderRootStateManager
* aManager
,
231 mozilla::nsDisplayListBuilder
* aDisplayListBuilder
,
232 const nsStyleBorder
& aStyleBorder
);
235 * Render the outline for an element using css rendering rules for borders.
237 static void PaintNonThemedOutline(nsPresContext
* aPresContext
,
238 gfxContext
& aRenderingContext
,
240 const nsRect
& aDirtyRect
,
241 const nsRect
& aInnerRect
,
242 mozilla::ComputedStyle
* aStyle
);
245 * Render keyboard focus on an element.
246 * |aFocusRect| is the outer rectangle of the focused element.
247 * Uses a fixed style equivalent to "1px dotted |aColor|".
248 * Not used for controls, because the native theme may differ.
250 static void PaintFocus(nsPresContext
* aPresContext
, DrawTarget
* aDrawTarget
,
251 const nsRect
& aFocusRect
, nscolor aColor
);
254 * Render a gradient for an element.
255 * aDest is the rect for a single tile of the gradient on the destination.
256 * aFill is the rect on the destination to be covered by repeated tiling of
258 * aSrc is the part of the gradient to be rendered into a tile (aDest), if
259 * aSrc and aDest are different sizes, the image will be scaled to map aSrc
261 * aIntrinsicSize is the size of the source gradient.
263 static void PaintGradient(nsPresContext
* aPresContext
, gfxContext
& aContext
,
264 const mozilla::StyleGradient
& aGradient
,
265 const nsRect
& aDirtyRect
, const nsRect
& aDest
,
266 const nsRect
& aFill
, const nsSize
& aRepeatSize
,
267 const mozilla::CSSIntRect
& aSrc
,
268 const nsSize
& aIntrinsiceSize
,
269 float aOpacity
= 1.0);
272 * Find the frame whose background style should be used to draw the
273 * canvas background. aForFrame must be the frame for the root element
274 * whose background style should be used. This function will return
275 * aForFrame unless the <body> background should be propagated, in
276 * which case we return the frame associated with the <body>'s background.
278 static nsIFrame
* FindBackgroundStyleFrame(nsIFrame
* aForFrame
);
281 * @return true if |aFrame| is a canvas frame, in the CSS sense.
283 static bool IsCanvasFrame(const nsIFrame
* aFrame
);
286 * Returns the ComputedStyle to be used to paint the background for the given
287 * frame, if its element has a meaningful background. This applies the rules
288 * for propagating backgrounds between BODY, the root element, and the
291 * @return the ComputedStyle (if any) to be used for painting aForFrame's
294 static mozilla::ComputedStyle
* FindBackground(const nsIFrame
* aForFrame
);
295 static nsIFrame
* FindBackgroundFrame(const nsIFrame
* aForFrame
);
298 * As FindBackground, but the passed-in frame is known to be a root frame
299 * (returned from nsCSSFrameConstructor::GetRootElementStyleFrame())
300 * and there is always some meaningful background returned.
302 static mozilla::ComputedStyle
* FindRootFrameBackground(nsIFrame
* aForFrame
);
305 * Returns background style information for the canvas.
308 * the frame used to represent the canvas, in the CSS sense (i.e.
309 * nsCSSRendering::IsCanvasFrame(aForFrame) must be true)
310 * @param aRootElementFrame
311 * the frame representing the root element of the document
313 * contains background style information for the canvas on return
316 static nsIFrame
* FindCanvasBackgroundFrame(const nsIFrame
* aForFrame
,
317 nsIFrame
* aRootElementFrame
) {
318 MOZ_ASSERT(IsCanvasFrame(aForFrame
), "not a canvas frame");
319 if (aRootElementFrame
) {
320 return FindBackgroundStyleFrame(aRootElementFrame
);
323 // This should always give transparent, so we'll fill it in with the
324 // default color if needed. This seems to happen a bit while a page is
326 return const_cast<nsIFrame
*>(aForFrame
);
329 static mozilla::ComputedStyle
* FindCanvasBackground(
330 nsIFrame
* aForFrame
, nsIFrame
* aRootElementFrame
) {
331 return FindCanvasBackgroundFrame(aForFrame
, aRootElementFrame
)->Style();
335 * Find a non-transparent background color on an ancestor, for various
336 * contrast checks. Note that this only accounts for background-color and
337 * might stop at themed frames (depending on the argument), so it might not be
338 * what you want. Note that if we stop at themed frames we might, in fact, end
339 * up returning a transparent color (but then mIsThemed will be set to true).
341 * For semi-transparent colors, right now we blend with the default
342 * background-color rather than with all ancestor backgrounds.
344 * If aPreferBodyToCanvas is true, we prefer the background color of the
345 * <body> frame, even though we found a canvas background, because the body
346 * background color is most likely what will be visible as the background
347 * color of the page, even if the html element has a different background
348 * color which prevents that of the body frame to propagate to the viewport.
350 struct EffectiveBackgroundColor
{
352 bool mIsThemed
= false;
354 static EffectiveBackgroundColor
FindEffectiveBackgroundColor(
355 nsIFrame
* aFrame
, bool aStopAtThemed
= true,
356 bool aPreferBodyToCanvas
= false);
359 * Determine the background color to draw taking into account print settings.
361 static nscolor
DetermineBackgroundColor(nsPresContext
* aPresContext
,
362 const mozilla::ComputedStyle
* aStyle
,
364 bool& aDrawBackgroundImage
,
365 bool& aDrawBackgroundColor
);
367 static nsRect
ComputeImageLayerPositioningArea(
368 nsPresContext
* aPresContext
, nsIFrame
* aForFrame
,
369 const nsRect
& aBorderArea
, const nsStyleImageLayers::Layer
& aLayer
,
370 nsIFrame
** aAttachedToFrame
, bool* aOutTransformedFixed
);
372 // Implementation of the formula for computation of background-repeat round
373 // See http://dev.w3.org/csswg/css3-background/#the-background-size
374 // This function returns the adjusted size of the background image.
375 static nscoord
ComputeRoundedSize(nscoord aCurrentSize
,
376 nscoord aPositioningSize
);
378 /* ComputeBorderSpacedRepeatSize
379 * aImageDimension: the image width/height
380 * aAvailableSpace: the background positioning area width/height
381 * aSpace: the space between each image
382 * Returns the image size plus gap size of app units for use as spacing
384 static nscoord
ComputeBorderSpacedRepeatSize(nscoord aImageDimension
,
385 nscoord aAvailableSpace
,
388 static nsBackgroundLayerState
PrepareImageLayer(
389 nsPresContext
* aPresContext
, nsIFrame
* aForFrame
, uint32_t aFlags
,
390 const nsRect
& aBorderArea
, const nsRect
& aBGClipRect
,
391 const nsStyleImageLayers::Layer
& aLayer
,
392 bool* aOutIsTransformedFixed
= nullptr);
394 struct ImageLayerClipState
{
395 nsRect mBGClipArea
; // Affected by mClippedRadii
396 nsRect mAdditionalBGClipArea
; // Not affected by mClippedRadii
397 nsRect mDirtyRectInAppUnits
;
398 gfxRect mDirtyRectInDevPx
;
401 RectCornerRadii mClippedRadii
;
402 bool mHasRoundedCorners
;
403 bool mHasAdditionalBGClipArea
;
405 // Whether we are being asked to draw with a caller provided background
406 // clipping area. If this is true we also disable rounded corners.
409 ImageLayerClipState()
410 : mHasRoundedCorners(false),
411 mHasAdditionalBGClipArea(false),
413 memset(mRadii
, 0, sizeof(nscoord
) * 8);
416 bool IsValid() const;
419 static void GetImageLayerClip(const nsStyleImageLayers::Layer
& aLayer
,
421 const nsStyleBorder
& aBorder
,
422 const nsRect
& aBorderArea
,
423 const nsRect
& aCallerDirtyRect
,
424 bool aWillPaintBorder
,
425 nscoord aAppUnitsPerPixel
,
426 /* out */ ImageLayerClipState
* aClipState
);
429 * Render the background for an element using css rendering rules
430 * for backgrounds or mask.
434 * When this flag is passed, the element's nsDisplayBorder will be
435 * painted immediately on top of this background.
437 PAINTBG_WILL_PAINT_BORDER
= 0x01,
439 * When this flag is passed, images are synchronously decoded.
441 PAINTBG_SYNC_DECODE_IMAGES
= 0x02,
443 * When this flag is passed, painting will go to the screen so we can
444 * take advantage of the fact that it will be clipped to the viewport.
446 PAINTBG_TO_WINDOW
= 0x04,
448 * When this flag is passed, painting will read properties of mask-image
449 * style, instead of background-image.
451 PAINTBG_MASK_IMAGE
= 0x08,
453 * When this flag is passed, images are downscaled during decode. This
454 * is also implied by PAINTBG_TO_WINDOW.
456 PAINTBG_HIGH_QUALITY_SCALING
= 0x16,
459 struct PaintBGParams
{
460 nsPresContext
& presCtx
;
465 nsRect
* bgClipRect
= nullptr;
466 int32_t layer
; // -1 means painting all layers; other
467 // value means painting one specific
469 CompositionOp compositionOp
;
472 static PaintBGParams
ForAllLayers(nsPresContext
& aPresCtx
,
473 const nsRect
& aDirtyRect
,
474 const nsRect
& aBorderArea
,
475 nsIFrame
* aFrame
, uint32_t aPaintFlags
,
476 float aOpacity
= 1.0);
477 static PaintBGParams
ForSingleLayer(
478 nsPresContext
& aPresCtx
, const nsRect
& aDirtyRect
,
479 const nsRect
& aBorderArea
, nsIFrame
* aFrame
, uint32_t aPaintFlags
,
480 int32_t aLayer
, CompositionOp aCompositionOp
= CompositionOp::OP_OVER
,
481 float aOpacity
= 1.0);
484 PaintBGParams(nsPresContext
& aPresCtx
, const nsRect
& aDirtyRect
,
485 const nsRect
& aBorderArea
, nsIFrame
* aFrame
,
486 uint32_t aPaintFlags
, int32_t aLayer
,
487 CompositionOp aCompositionOp
, float aOpacity
)
489 dirtyRect(aDirtyRect
),
490 borderArea(aBorderArea
),
492 paintFlags(aPaintFlags
),
494 compositionOp(aCompositionOp
),
498 static ImgDrawResult
PaintStyleImageLayer(const PaintBGParams
& aParams
,
499 gfxContext
& aRenderingCtx
);
502 * Same as |PaintStyleImageLayer|, except using the provided style structs.
503 * This short-circuits the code that ensures that the root element's
504 * {background|mask} is drawn on the canvas.
505 * The aLayer parameter allows you to paint a single layer of the
507 * The default value for aLayer, -1, means that all layers will be painted.
508 * The background color will only be painted if the back-most layer is also
509 * being painted and (aParams.paintFlags & PAINTBG_MASK_IMAGE) is false.
510 * aCompositionOp is only respected if a single layer is specified (aLayer !=
511 * -1). If all layers are painted, the image layer's blend mode (or the mask
512 * layer's composition mode) will be used.
514 static ImgDrawResult
PaintStyleImageLayerWithSC(
515 const PaintBGParams
& aParams
, gfxContext
& aRenderingCtx
,
516 const mozilla::ComputedStyle
* aBackgroundSC
,
517 const nsStyleBorder
& aBorder
);
519 static bool CanBuildWebRenderDisplayItemsForStyleImageLayer(
520 WebRenderLayerManager
* aManager
, nsPresContext
& aPresCtx
,
521 nsIFrame
* aFrame
, const nsStyleBackground
* aBackgroundStyle
,
522 int32_t aLayer
, uint32_t aPaintFlags
);
523 static ImgDrawResult
BuildWebRenderDisplayItemsForStyleImageLayer(
524 const PaintBGParams
& aParams
, mozilla::wr::DisplayListBuilder
& aBuilder
,
525 mozilla::wr::IpcResourceUpdateQueue
& aResources
,
526 const mozilla::layers::StackingContextHelper
& aSc
,
527 mozilla::layers::RenderRootStateManager
* aManager
,
528 mozilla::nsDisplayItem
* aItem
);
530 static ImgDrawResult
BuildWebRenderDisplayItemsForStyleImageLayerWithSC(
531 const PaintBGParams
& aParams
, mozilla::wr::DisplayListBuilder
& aBuilder
,
532 mozilla::wr::IpcResourceUpdateQueue
& aResources
,
533 const mozilla::layers::StackingContextHelper
& aSc
,
534 mozilla::layers::RenderRootStateManager
* aManager
,
535 mozilla::nsDisplayItem
* aItem
, mozilla::ComputedStyle
* mBackgroundSC
,
536 const nsStyleBorder
& aBorder
);
539 * Returns the rectangle covered by the given background layer image, taking
540 * into account background positioning, sizing, and repetition, but not
543 static nsRect
GetBackgroundLayerRect(nsPresContext
* aPresContext
,
545 const nsRect
& aBorderArea
,
546 const nsRect
& aClipRect
,
547 const nsStyleImageLayers::Layer
& aLayer
,
551 * Called when we start creating a display list. The frame tree will not
552 * change until a matching EndFrameTreeLocked is called.
554 static void BeginFrameTreesLocked();
556 * Called when we've finished using a display list. When all
557 * BeginFrameTreeLocked calls have been balanced by an EndFrameTreeLocked,
558 * the frame tree may start changing again.
560 static void EndFrameTreesLocked();
562 // Draw a border segment in the table collapsing border model with beveling
564 static void DrawTableBorderSegment(
565 DrawTarget
& aDrawTarget
, mozilla::StyleBorderStyle aBorderStyle
,
566 nscolor aBorderColor
, const nsRect
& aBorderRect
,
567 int32_t aAppUnitsPerDevPixel
, mozilla::Side aStartBevelSide
,
568 nscoord aStartBevelOffset
, mozilla::Side aEndBevelSide
,
569 nscoord aEndBevelOffset
);
571 // A single border bevel.
577 // A single solid beveled border segment.
578 struct SolidBeveledBorderSegment
{
585 // Collect the table border segments with beveling. Can't be called with
586 // dashed / dotted borders, since we don't support beveling those.
587 static void GetTableBorderSolidSegments(
588 nsTArray
<SolidBeveledBorderSegment
>& aSegments
,
589 mozilla::StyleBorderStyle aBorderStyle
, nscolor aBorderColor
,
590 const nsRect
& aBorderRect
, int32_t aAppUnitsPerDevPixel
,
591 mozilla::Side aStartBevelSide
, nscoord aStartBevelOffset
,
592 mozilla::Side aEndBevelSide
, nscoord aEndBevelOffset
);
594 // NOTE: pt, dirtyRect, lineSize, ascent, offset in the following
595 // structs are non-rounded device pixels, not app units.
596 struct DecorationRectParams
{
597 // The width [length] and the height [thickness] of the decoration
598 // line. This is a "logical" size in textRun orientation, so that
599 // for a vertical textrun, width will actually be a physical height;
600 // and conversely, height will be a physical width.
602 // The default height [thickness] of the line given by the font metrics.
603 // This is used for obtaining the correct offset for the decoration line
604 // when CSS specifies a unique thickness for a text-decoration,
605 // since the offset given by the font is derived from the font metric's
606 // assumed line height
607 Float defaultLineThickness
= 0.0f
;
608 // The ascent of the text.
610 // The offset of the decoration line from the baseline of the text
611 // (if the value is positive, the line is lifted up).
613 // If descentLimit is zero or larger and the underline overflows
614 // from the descent space, the underline should be lifted up as far
615 // as possible. Note that this does not mean the underline never
616 // overflows from this limitation, because if the underline is
617 // positioned to the baseline or upper, it causes unreadability.
618 // Note that if this is zero or larger, the underline rect may be
619 // shrunken if it's possible. Therefore, this value is used for
620 // strikeout line and overline too.
621 Float descentLimit
= -1.0f
;
622 // Which line will be painted. The value can be
623 // UNDERLINE or OVERLINE or LINE_THROUGH.
624 mozilla::StyleTextDecorationLine decoration
=
625 mozilla::StyleTextDecorationLine::UNDERLINE
;
626 // The style of the decoration line such as
627 // NS_STYLE_TEXT_DECORATION_STYLE_*.
628 uint8_t style
= NS_STYLE_TEXT_DECORATION_STYLE_NONE
;
629 bool vertical
= false;
630 bool sidewaysLeft
= false;
631 gfxTextRun::Range glyphRange
;
632 gfxTextRun::PropertyProvider
* provider
;
635 struct PaintDecorationLineParams
: DecorationRectParams
{
636 // No need to paint outside this rect.
638 // The top/left edge of the text.
640 // The color of the decoration line.
641 nscolor color
= NS_RGBA(0, 0, 0, 0);
642 // The distance between the left edge of the given frame and the
643 // position of the text as positioned without offset of the shadow.
644 Float icoordInFrame
= 0.0f
;
645 // Baseline offset being applied to this text (block-direction adjustment
646 // applied to glyph positions when computing skip-ink intercepts).
647 Float baselineOffset
= 0.0f
;
651 * Function for painting the clipped decoration lines for the text.
652 * Takes into account the rect clipping that occurs when
653 * text-decoration-skip-ink is being used for underlines or overlines
656 * @param aFrame the frame which needs the decoration line
657 * @param aDrawTarget the target/backend being drawn to
658 * @param aParams the parameters for the decoration line
660 * @param aRect the rect representing the decoration line
662 static void PaintDecorationLineInternal(
663 nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
664 const PaintDecorationLineParams
& aParams
, Rect aRect
);
667 * Function for painting the decoration lines for the text.
670 * @param aFrame the frame which needs the decoration line
671 * @param aDrawTarget the target/backend being drawn to
672 * @param aParams the parameters for the decoration line
675 static void PaintDecorationLine(nsIFrame
* aFrame
, DrawTarget
& aDrawTarget
,
676 const PaintDecorationLineParams
& aParams
);
679 * Returns a Rect corresponding to the outline of the decoration line for the
680 * given text metrics. Arguments have the same meaning as for
681 * PaintDecorationLine. Currently this only works for solid
682 * decorations; for other decoration styles the returned Rect will be empty.
684 static Rect
DecorationLineToPath(const PaintDecorationLineParams
& aParams
);
687 * Function for getting the decoration line rect for the text.
688 * NOTE: aLineSize, aAscent and aOffset are non-rounded device pixels,
691 * @param aPresContext
693 * @return the decoration line rect for the input,
694 * the each values are app units.
696 static nsRect
GetTextDecorationRect(nsPresContext
* aPresContext
,
697 const DecorationRectParams
& aParams
);
699 static CompositionOp
GetGFXBlendMode(mozilla::StyleBlend aBlendMode
) {
700 switch (aBlendMode
) {
701 case mozilla::StyleBlend::Normal
:
702 return CompositionOp::OP_OVER
;
703 case mozilla::StyleBlend::Multiply
:
704 return CompositionOp::OP_MULTIPLY
;
705 case mozilla::StyleBlend::Screen
:
706 return CompositionOp::OP_SCREEN
;
707 case mozilla::StyleBlend::Overlay
:
708 return CompositionOp::OP_OVERLAY
;
709 case mozilla::StyleBlend::Darken
:
710 return CompositionOp::OP_DARKEN
;
711 case mozilla::StyleBlend::Lighten
:
712 return CompositionOp::OP_LIGHTEN
;
713 case mozilla::StyleBlend::ColorDodge
:
714 return CompositionOp::OP_COLOR_DODGE
;
715 case mozilla::StyleBlend::ColorBurn
:
716 return CompositionOp::OP_COLOR_BURN
;
717 case mozilla::StyleBlend::HardLight
:
718 return CompositionOp::OP_HARD_LIGHT
;
719 case mozilla::StyleBlend::SoftLight
:
720 return CompositionOp::OP_SOFT_LIGHT
;
721 case mozilla::StyleBlend::Difference
:
722 return CompositionOp::OP_DIFFERENCE
;
723 case mozilla::StyleBlend::Exclusion
:
724 return CompositionOp::OP_EXCLUSION
;
725 case mozilla::StyleBlend::Hue
:
726 return CompositionOp::OP_HUE
;
727 case mozilla::StyleBlend::Saturation
:
728 return CompositionOp::OP_SATURATION
;
729 case mozilla::StyleBlend::Color
:
730 return CompositionOp::OP_COLOR
;
731 case mozilla::StyleBlend::Luminosity
:
732 return CompositionOp::OP_LUMINOSITY
;
733 case mozilla::StyleBlend::PlusLighter
:
734 return CompositionOp::OP_ADD
;
737 return CompositionOp::OP_OVER
;
741 static CompositionOp
GetGFXCompositeMode(
742 mozilla::StyleMaskComposite aCompositeMode
) {
743 switch (aCompositeMode
) {
744 case mozilla::StyleMaskComposite::Add
:
745 return CompositionOp::OP_OVER
;
746 case mozilla::StyleMaskComposite::Subtract
:
747 return CompositionOp::OP_OUT
;
748 case mozilla::StyleMaskComposite::Intersect
:
749 return CompositionOp::OP_IN
;
750 case mozilla::StyleMaskComposite::Exclude
:
751 return CompositionOp::OP_XOR
;
754 return CompositionOp::OP_OVER
;
759 static gfxRect
GetTextDecorationRectInternal(
760 const Point
& aPt
, const DecorationRectParams
& aParams
);
763 * Returns inflated rect for painting a decoration line.
764 * Complex style decoration lines should be painted from leftmost of nearest
765 * ancestor block box because that makes better look of connection of lines
766 * for different nodes. ExpandPaintingRectForDecorationLine() returns
767 * a rect for actual painting rect for the clipped rect.
770 * @param aFrame the frame which needs the decoration line.
771 * @param aStyle the style of the complex decoration line
772 * NS_STYLE_TEXT_DECORATION_STYLE_DOTTED or
773 * NS_STYLE_TEXT_DECORATION_STYLE_DASHED or
774 * NS_STYLE_TEXT_DECORATION_STYLE_WAVY.
775 * @param aClippedRect the clipped rect for the decoration line.
776 * in other words, visible area of the line.
777 * @param aICoordInFrame the distance between inline-start edge of aFrame
778 * and aClippedRect.pos.
779 * @param aCycleLength the width of one cycle of the line style.
781 static Rect
ExpandPaintingRectForDecorationLine(
782 nsIFrame
* aFrame
, const uint8_t aStyle
, const Rect
& aClippedRect
,
783 const Float aICoordInFrame
, const Float aCycleLength
, bool aVertical
);
788 * Creates an 8-bit alpha channel context for callers to draw in, blurs the
789 * contents of that context and applies it as a 1-color mask on a
790 * different existing context. Uses gfxAlphaBoxBlur as its back end.
792 * You must call Init() first to create a suitable temporary surface to draw
793 * on. You must then draw any desired content onto the given context, then
794 * call DoPaint() to apply the blurred content as a single-color mask. You
795 * can only call Init() once, so objects cannot be reused.
797 * This is very useful for creating drop shadows or silhouettes.
799 class nsContextBoxBlur
{
800 typedef mozilla::gfx::sRGBColor sRGBColor
;
801 typedef mozilla::gfx::DrawTarget DrawTarget
;
802 typedef mozilla::gfx::RectCornerRadii RectCornerRadii
;
805 enum { FORCE_MASK
= 0x01, DISABLE_HARDWARE_ACCELERATION_BLUR
= 0x02 };
807 * Prepares a gfxContext to draw on. Do not call this twice; if you want
808 * to get the gfxContext again use GetContext().
810 * @param aRect The coordinates of the surface to create.
811 * All coordinates must be in app units.
812 * This must not include the blur radius, pass
813 * it as the second parameter and everything
816 * @param aBlurRadius The blur radius in app units.
818 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
819 * for conversion. Most of the time you'll
820 * pass this from the current PresContext if
823 * @param aDestinationCtx The graphics context to apply the blurred
824 * mask to when you call DoPaint(). Make sure
825 * it is not destroyed before you call
826 * DoPaint(). To set the color of the
827 * resulting blurred graphic mask, you must
828 * set the color on this context before
831 * @param aDirtyRect The absolute dirty rect in app units. Used to
832 * optimize the temporary surface size and speed
835 * @param aSkipRect An area in device pixels (NOT app units!) to
836 * avoid blurring over, to prevent unnecessary work.
838 * @param aFlags FORCE_MASK to ensure that the content drawn to
839 * the returned gfxContext is used as a mask, and not drawn directly to
842 * @return A blank 8-bit alpha-channel-only graphics context to
843 * draw on, or null on error. Must not be freed. The
844 * context has a device offset applied to it given by
845 * aRect. This means you can use coordinates as if it
846 * were at the desired position at aRect and you don't
847 * need to worry about translating any coordinates to
848 * draw on this temporary surface.
850 * If aBlurRadius is 0, the returned context is aDestinationCtx and
851 * DoPaint() does nothing, because no blurring is required. Therefore, you
852 * should prepare the destination context as if you were going to draw
853 * directly on it instead of any temporary surface created in this class.
855 gfxContext
* Init(const nsRect
& aRect
, nscoord aSpreadRadius
,
856 nscoord aBlurRadius
, int32_t aAppUnitsPerDevPixel
,
857 gfxContext
* aDestinationCtx
, const nsRect
& aDirtyRect
,
858 const gfxRect
* aSkipRect
, uint32_t aFlags
= 0);
861 * Does the actual blurring and mask applying. Users of this object *must*
862 * have called Init() first, then have drawn whatever they want to be
863 * blurred onto the internal gfxContext before calling this.
868 * Gets the internal gfxContext at any time. Must not be freed. Avoid
869 * calling this before calling Init() since the context would not be
870 * constructed at that point.
872 gfxContext
* GetContext();
875 * Get the margin associated with the given blur radius, i.e., the
876 * additional area that might be painted as a result of it. (The
877 * margin for a spread radius is itself, on all sides.)
879 static nsMargin
GetBlurRadiusMargin(nscoord aBlurRadius
,
880 int32_t aAppUnitsPerDevPixel
);
883 * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent
884 * to calling Init(), drawing a rectangle onto the returned surface
885 * and then calling DoPaint, but may let us optimize better in the
888 * @param aDestinationCtx The destination to blur to.
889 * @param aRect The rectangle to blur in app units.
890 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
891 * for conversion. Most of the time you'll
892 * pass this from the current PresContext if
894 * @param aCornerRadii Corner radii for aRect, if it is a rounded
896 * @param aBlurRadius The blur radius in app units.
897 * @param aShadowColor The color to draw the blurred shadow.
898 * @param aDirtyRect The absolute dirty rect in app units. Used to
899 * optimize the temporary surface size and speed
901 * @param aSkipRect An area in device pixels (NOT app units!) to
902 * avoid blurring over, to prevent unnecessary work.
904 static void BlurRectangle(gfxContext
* aDestinationCtx
, const nsRect
& aRect
,
905 int32_t aAppUnitsPerDevPixel
,
906 RectCornerRadii
* aCornerRadii
, nscoord aBlurRadius
,
907 const sRGBColor
& aShadowColor
,
908 const nsRect
& aDirtyRect
, const gfxRect
& aSkipRect
);
911 * Draws a blurred inset box shadow shape onto the destination surface.
912 * Like BlurRectangle, this is equivalent to calling Init(),
913 * drawing a rectangle onto the returned surface
914 * and then calling DoPaint, but may let us optimize better in the
917 * @param aDestinationCtx The destination to blur to.
918 * @param aDestinationRect The rectangle to blur in app units.
919 * @param aShadowClipRect The inside clip rect that creates the path.
920 * @param aShadowColor The color of the blur
921 * @param aBlurRadiusAppUnits The blur radius in app units
922 * @param aSpreadRadiusAppUnits The spread radius in app units.
923 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
924 * for conversion. Most of the time you'll
925 * pass this from the current PresContext if
927 * @param aHasBorderRadius If this inset box blur has a border radius
928 * @param aInnerClipRectRadii The clip rect radii used for the inside rect's
930 * @param aSkipRect An area in device pixels (NOT app units!) to
931 * avoid blurring over, to prevent unnecessary work.
933 bool InsetBoxBlur(gfxContext
* aDestinationCtx
,
934 mozilla::gfx::Rect aDestinationRect
,
935 mozilla::gfx::Rect aShadowClipRect
,
936 mozilla::gfx::sRGBColor
& aShadowColor
,
937 nscoord aBlurRadiusAppUnits
, nscoord aSpreadRadiusAppUnits
,
938 int32_t aAppUnitsPerDevPixel
, bool aHasBorderRadius
,
939 RectCornerRadii
& aInnerClipRectRadii
,
940 mozilla::gfx::Rect aSkipRect
,
941 mozilla::gfx::Point aShadowOffset
);
944 static void GetBlurAndSpreadRadius(DrawTarget
* aDestDrawTarget
,
945 int32_t aAppUnitsPerDevPixel
,
946 nscoord aBlurRadius
, nscoord aSpreadRadius
,
947 mozilla::gfx::IntSize
& aOutBlurRadius
,
948 mozilla::gfx::IntSize
& aOutSpreadRadius
,
949 bool aConstrainSpreadRadius
= true);
951 gfxAlphaBoxBlur mAlphaBoxBlur
;
952 RefPtr
<gfxContext
> mContext
;
953 gfxContext
* mDestinationCtx
;
955 /* This is true if the blur already has it's content transformed
956 * by mDestinationCtx's transform */
957 bool mPreTransformed
;
960 #endif /* nsCSSRendering_h___ */