Bug 1687263: part 4) Defer and in some cases avoid removing spellchecking-ranges...
[gecko.git] / layout / painting / nsCSSRendering.h
blob17d06f2cd00ceaf30bb4359cfad041a9115bb343
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___
12 #include "gfxBlur.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"
18 #include "nsIFrame.h"
19 #include "nsImageRenderer.h"
20 #include "nsCSSRenderingBorders.h"
21 #include "gfxTextRun.h"
23 class gfxContext;
24 class nsPresContext;
26 namespace mozilla {
28 class ComputedStyle;
30 namespace gfx {
31 struct sRGBColor;
32 class DrawTarget;
33 } // namespace gfx
35 namespace layers {
36 class ImageContainer;
37 class StackingContextHelper;
38 class WebRenderParentCommand;
39 class LayerManager;
40 class RenderRootStateManager;
41 } // namespace layers
43 namespace wr {
44 class DisplayListBuilder;
45 } // namespace wr
47 enum class PaintBorderFlags : uint8_t { SyncDecodeImages = 1 << 0 };
48 MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(PaintBorderFlags)
50 } // namespace mozilla
52 /**
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;
61 /**
62 * @param aFlags some combination of nsCSSRendering::PAINTBG_* flags
64 nsBackgroundLayerState(nsIFrame* aForFrame, const mozilla::StyleImage* aImage,
65 uint32_t aFlags)
66 : mImageRenderer(aForFrame, aImage, aFlags) {}
68 /**
69 * The nsImageRenderer that will be used to draw the background.
71 nsImageRenderer mImageRenderer;
72 /**
73 * A rectangle that one copy of the image tile is mapped onto. Same
74 * coordinate system as aBorderArea/aBGClipRect passed into
75 * PrepareImageLayer.
77 nsRect mDestArea;
78 /**
79 * The actual rectangle that should be filled with (complete or partial)
80 * image tiles. Same coordinate system as aBorderArea/aBGClipRect passed into
81 * PrepareImageLayer.
83 nsRect mFillArea;
84 /**
85 * The anchor point that should be snapped to a pixel corner. Same
86 * coordinate system as aBorderArea/aBGClipRect passed into
87 * PrepareImageLayer.
89 nsPoint mAnchor;
90 /**
91 * The background-repeat property space keyword computes the
92 * repeat size which is image size plus spacing.
94 nsSize mRepeatSize;
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::LayerManager LayerManager;
107 typedef mozilla::image::ImgDrawResult ImgDrawResult;
108 typedef nsIFrame::Sides Sides;
111 * Initialize any static variables used by nsCSSRendering.
113 static void Init();
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,
135 nsIFrame* aForFrame,
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,
205 nsIFrame* aForFrame,
206 const nsRect& aDirtyRect,
207 const nsRect& aInnerRect,
208 mozilla::ComputedStyle* aStyle);
210 static ImgDrawResult CreateWebRenderCommandsForBorder(
211 nsDisplayItem* aItem, nsIFrame* aForFrame, const nsRect& aBorderArea,
212 mozilla::wr::DisplayListBuilder& aBuilder,
213 mozilla::wr::IpcResourceUpdateQueue& aResources,
214 const mozilla::layers::StackingContextHelper& aSc,
215 mozilla::layers::RenderRootStateManager* aManager,
216 nsDisplayListBuilder* aDisplayListBuilder);
218 static void CreateWebRenderCommandsForNullBorder(
219 nsDisplayItem* aItem, nsIFrame* aForFrame, const nsRect& aBorderArea,
220 mozilla::wr::DisplayListBuilder& aBuilder,
221 mozilla::wr::IpcResourceUpdateQueue& aResources,
222 const mozilla::layers::StackingContextHelper& aSc,
223 const nsStyleBorder& aStyleBorder);
225 static ImgDrawResult CreateWebRenderCommandsForBorderWithStyleBorder(
226 nsDisplayItem* aItem, nsIFrame* aForFrame, const nsRect& aBorderArea,
227 mozilla::wr::DisplayListBuilder& aBuilder,
228 mozilla::wr::IpcResourceUpdateQueue& aResources,
229 const mozilla::layers::StackingContextHelper& aSc,
230 mozilla::layers::RenderRootStateManager* aManager,
231 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,
239 nsIFrame* aForFrame,
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
257 * the gradient.
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
260 * onto aDest.
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 * Fill in an aBackgroundSC to be used to paint the background
287 * for an element. This applies the rules for propagating
288 * backgrounds between BODY, the root element, and the canvas.
289 * @return true if there is some meaningful background.
291 static bool FindBackground(const nsIFrame* aForFrame,
292 mozilla::ComputedStyle** aBackgroundSC);
293 static bool FindBackgroundFrame(const nsIFrame* aForFrame,
294 nsIFrame** aBackgroundFrame);
297 * As FindBackground, but the passed-in frame is known to be a root frame
298 * (returned from nsCSSFrameConstructor::GetRootElementStyleFrame())
299 * and there is always some meaningful background returned.
301 static mozilla::ComputedStyle* FindRootFrameBackground(nsIFrame* aForFrame);
304 * Returns background style information for the canvas.
306 * @param aForFrame
307 * the frame used to represent the canvas, in the CSS sense (i.e.
308 * nsCSSRendering::IsCanvasFrame(aForFrame) must be true)
309 * @param aRootElementFrame
310 * the frame representing the root element of the document
311 * @param aBackground
312 * contains background style information for the canvas on return
315 static nsIFrame* FindCanvasBackgroundFrame(const nsIFrame* aForFrame,
316 nsIFrame* aRootElementFrame) {
317 MOZ_ASSERT(IsCanvasFrame(aForFrame), "not a canvas frame");
318 if (aRootElementFrame) {
319 return FindBackgroundStyleFrame(aRootElementFrame);
322 // This should always give transparent, so we'll fill it in with the
323 // default color if needed. This seems to happen a bit while a page is
324 // being loaded.
325 return const_cast<nsIFrame*>(aForFrame);
328 static mozilla::ComputedStyle* FindCanvasBackground(
329 nsIFrame* aForFrame, nsIFrame* aRootElementFrame) {
330 return FindCanvasBackgroundFrame(aForFrame, aRootElementFrame)->Style();
334 * Find a frame which draws a non-transparent background, for various contrast
335 * checks. Note that this only accounts for background-color and might stop at
336 * themed frames (depending on the argument), so it might not be what you
337 * want.
339 struct NonTransparentBackgroundFrame {
340 nsIFrame* mFrame = nullptr;
341 bool mIsThemed = false;
342 bool mIsForCanvas = false;
344 static NonTransparentBackgroundFrame FindNonTransparentBackgroundFrame(
345 nsIFrame* aFrame, bool aStopAtThemed = true);
348 * Determine the background color to draw taking into account print settings.
350 static nscolor DetermineBackgroundColor(nsPresContext* aPresContext,
351 mozilla::ComputedStyle* aStyle,
352 nsIFrame* aFrame,
353 bool& aDrawBackgroundImage,
354 bool& aDrawBackgroundColor);
356 static nsRect ComputeImageLayerPositioningArea(
357 nsPresContext* aPresContext, nsIFrame* aForFrame,
358 const nsRect& aBorderArea, const nsStyleImageLayers::Layer& aLayer,
359 nsIFrame** aAttachedToFrame, bool* aOutTransformedFixed);
361 // Implementation of the formula for computation of background-repeat round
362 // See http://dev.w3.org/csswg/css3-background/#the-background-size
363 // This function returns the adjusted size of the background image.
364 static nscoord ComputeRoundedSize(nscoord aCurrentSize,
365 nscoord aPositioningSize);
367 /* ComputeBorderSpacedRepeatSize
368 * aImageDimension: the image width/height
369 * aAvailableSpace: the background positioning area width/height
370 * aSpace: the space between each image
371 * Returns the image size plus gap size of app units for use as spacing
373 static nscoord ComputeBorderSpacedRepeatSize(nscoord aImageDimension,
374 nscoord aAvailableSpace,
375 nscoord& aSpace);
377 static nsBackgroundLayerState PrepareImageLayer(
378 nsPresContext* aPresContext, nsIFrame* aForFrame, uint32_t aFlags,
379 const nsRect& aBorderArea, const nsRect& aBGClipRect,
380 const nsStyleImageLayers::Layer& aLayer,
381 bool* aOutIsTransformedFixed = nullptr);
383 struct ImageLayerClipState {
384 nsRect mBGClipArea; // Affected by mClippedRadii
385 nsRect mAdditionalBGClipArea; // Not affected by mClippedRadii
386 nsRect mDirtyRectInAppUnits;
387 gfxRect mDirtyRectInDevPx;
389 nscoord mRadii[8];
390 RectCornerRadii mClippedRadii;
391 bool mHasRoundedCorners;
392 bool mHasAdditionalBGClipArea;
394 // Whether we are being asked to draw with a caller provided background
395 // clipping area. If this is true we also disable rounded corners.
396 bool mCustomClip;
398 ImageLayerClipState()
399 : mHasRoundedCorners(false),
400 mHasAdditionalBGClipArea(false),
401 mCustomClip(false) {
402 memset(mRadii, 0, sizeof(nscoord) * 8);
405 bool IsValid() const;
408 static void GetImageLayerClip(const nsStyleImageLayers::Layer& aLayer,
409 nsIFrame* aForFrame,
410 const nsStyleBorder& aBorder,
411 const nsRect& aBorderArea,
412 const nsRect& aCallerDirtyRect,
413 bool aWillPaintBorder,
414 nscoord aAppUnitsPerPixel,
415 /* out */ ImageLayerClipState* aClipState);
418 * Render the background for an element using css rendering rules
419 * for backgrounds or mask.
421 enum {
423 * When this flag is passed, the element's nsDisplayBorder will be
424 * painted immediately on top of this background.
426 PAINTBG_WILL_PAINT_BORDER = 0x01,
428 * When this flag is passed, images are synchronously decoded.
430 PAINTBG_SYNC_DECODE_IMAGES = 0x02,
432 * When this flag is passed, painting will go to the screen so we can
433 * take advantage of the fact that it will be clipped to the viewport.
435 PAINTBG_TO_WINDOW = 0x04,
437 * When this flag is passed, painting will read properties of mask-image
438 * style, instead of background-image.
440 PAINTBG_MASK_IMAGE = 0x08,
442 * When this flag is passed, images are downscaled during decode. This
443 * is also implied by PAINTBG_TO_WINDOW.
445 PAINTBG_HIGH_QUALITY_SCALING = 0x16,
448 struct PaintBGParams {
449 nsPresContext& presCtx;
450 nsRect dirtyRect;
451 nsRect borderArea;
452 nsIFrame* frame;
453 uint32_t paintFlags;
454 nsRect* bgClipRect = nullptr;
455 int32_t layer; // -1 means painting all layers; other
456 // value means painting one specific
457 // layer only.
458 CompositionOp compositionOp;
459 float opacity;
461 static PaintBGParams ForAllLayers(nsPresContext& aPresCtx,
462 const nsRect& aDirtyRect,
463 const nsRect& aBorderArea,
464 nsIFrame* aFrame, uint32_t aPaintFlags,
465 float aOpacity = 1.0);
466 static PaintBGParams ForSingleLayer(
467 nsPresContext& aPresCtx, const nsRect& aDirtyRect,
468 const nsRect& aBorderArea, nsIFrame* aFrame, uint32_t aPaintFlags,
469 int32_t aLayer, CompositionOp aCompositionOp = CompositionOp::OP_OVER,
470 float aOpacity = 1.0);
472 private:
473 PaintBGParams(nsPresContext& aPresCtx, const nsRect& aDirtyRect,
474 const nsRect& aBorderArea, nsIFrame* aFrame,
475 uint32_t aPaintFlags, int32_t aLayer,
476 CompositionOp aCompositionOp, float aOpacity)
477 : presCtx(aPresCtx),
478 dirtyRect(aDirtyRect),
479 borderArea(aBorderArea),
480 frame(aFrame),
481 paintFlags(aPaintFlags),
482 layer(aLayer),
483 compositionOp(aCompositionOp),
484 opacity(aOpacity) {}
487 static ImgDrawResult PaintStyleImageLayer(const PaintBGParams& aParams,
488 gfxContext& aRenderingCtx);
491 * Same as |PaintStyleImageLayer|, except using the provided style structs.
492 * This short-circuits the code that ensures that the root element's
493 * {background|mask} is drawn on the canvas.
494 * The aLayer parameter allows you to paint a single layer of the
495 * {background|mask}.
496 * The default value for aLayer, -1, means that all layers will be painted.
497 * The background color will only be painted if the back-most layer is also
498 * being painted and (aParams.paintFlags & PAINTBG_MASK_IMAGE) is false.
499 * aCompositionOp is only respected if a single layer is specified (aLayer !=
500 * -1). If all layers are painted, the image layer's blend mode (or the mask
501 * layer's composition mode) will be used.
503 static ImgDrawResult PaintStyleImageLayerWithSC(
504 const PaintBGParams& aParams, gfxContext& aRenderingCtx,
505 mozilla::ComputedStyle* mBackgroundSC, const nsStyleBorder& aBorder);
507 static bool CanBuildWebRenderDisplayItemsForStyleImageLayer(
508 LayerManager* aManager, nsPresContext& aPresCtx, nsIFrame* aFrame,
509 const nsStyleBackground* aBackgroundStyle, int32_t aLayer,
510 uint32_t aPaintFlags);
511 static ImgDrawResult BuildWebRenderDisplayItemsForStyleImageLayer(
512 const PaintBGParams& aParams, mozilla::wr::DisplayListBuilder& aBuilder,
513 mozilla::wr::IpcResourceUpdateQueue& aResources,
514 const mozilla::layers::StackingContextHelper& aSc,
515 mozilla::layers::RenderRootStateManager* aManager, nsDisplayItem* aItem);
517 static ImgDrawResult BuildWebRenderDisplayItemsForStyleImageLayerWithSC(
518 const PaintBGParams& aParams, mozilla::wr::DisplayListBuilder& aBuilder,
519 mozilla::wr::IpcResourceUpdateQueue& aResources,
520 const mozilla::layers::StackingContextHelper& aSc,
521 mozilla::layers::RenderRootStateManager* aManager, nsDisplayItem* aItem,
522 mozilla::ComputedStyle* mBackgroundSC, const nsStyleBorder& aBorder);
525 * Returns the rectangle covered by the given background layer image, taking
526 * into account background positioning, sizing, and repetition, but not
527 * clipping.
529 static nsRect GetBackgroundLayerRect(nsPresContext* aPresContext,
530 nsIFrame* aForFrame,
531 const nsRect& aBorderArea,
532 const nsRect& aClipRect,
533 const nsStyleImageLayers::Layer& aLayer,
534 uint32_t aFlags);
537 * Called when we start creating a display list. The frame tree will not
538 * change until a matching EndFrameTreeLocked is called.
540 static void BeginFrameTreesLocked();
542 * Called when we've finished using a display list. When all
543 * BeginFrameTreeLocked calls have been balanced by an EndFrameTreeLocked,
544 * the frame tree may start changing again.
546 static void EndFrameTreesLocked();
548 // Draw a border segment in the table collapsing border model with beveling
549 // corners.
550 static void DrawTableBorderSegment(
551 DrawTarget& aDrawTarget, mozilla::StyleBorderStyle aBorderStyle,
552 nscolor aBorderColor, const nsRect& aBorderRect,
553 int32_t aAppUnitsPerDevPixel, mozilla::Side aStartBevelSide,
554 nscoord aStartBevelOffset, mozilla::Side aEndBevelSide,
555 nscoord aEndBevelOffset);
557 // A single border bevel.
558 struct Bevel {
559 mozilla::Side mSide;
560 nscoord mOffset;
563 // A single solid beveled border segment.
564 struct SolidBeveledBorderSegment {
565 nsRect mRect;
566 nscolor mColor;
567 Bevel mStartBevel;
568 Bevel mEndBevel;
571 // Collect the table border segments with beveling. Can't be called with
572 // dashed / dotted borders, since we don't support beveling those.
573 static void GetTableBorderSolidSegments(
574 nsTArray<SolidBeveledBorderSegment>& aSegments,
575 mozilla::StyleBorderStyle aBorderStyle, nscolor aBorderColor,
576 const nsRect& aBorderRect, int32_t aAppUnitsPerDevPixel,
577 mozilla::Side aStartBevelSide, nscoord aStartBevelOffset,
578 mozilla::Side aEndBevelSide, nscoord aEndBevelOffset);
580 // NOTE: pt, dirtyRect, lineSize, ascent, offset in the following
581 // structs are non-rounded device pixels, not app units.
582 struct DecorationRectParams {
583 // The width [length] and the height [thickness] of the decoration
584 // line. This is a "logical" size in textRun orientation, so that
585 // for a vertical textrun, width will actually be a physical height;
586 // and conversely, height will be a physical width.
587 Size lineSize;
588 // The default height [thickness] of the line given by the font metrics.
589 // This is used for obtaining the correct offset for the decoration line
590 // when CSS specifies a unique thickness for a text-decoration,
591 // since the offset given by the font is derived from the font metric's
592 // assumed line height
593 Float defaultLineThickness = 0.0f;
594 // The ascent of the text.
595 Float ascent = 0.0f;
596 // The offset of the decoration line from the baseline of the text
597 // (if the value is positive, the line is lifted up).
598 Float offset = 0.0f;
599 // If descentLimit is zero or larger and the underline overflows
600 // from the descent space, the underline should be lifted up as far
601 // as possible. Note that this does not mean the underline never
602 // overflows from this limitation, because if the underline is
603 // positioned to the baseline or upper, it causes unreadability.
604 // Note that if this is zero or larger, the underline rect may be
605 // shrunken if it's possible. Therefore, this value is used for
606 // strikeout line and overline too.
607 Float descentLimit = -1.0f;
608 // Which line will be painted. The value can be
609 // UNDERLINE or OVERLINE or LINE_THROUGH.
610 mozilla::StyleTextDecorationLine decoration =
611 mozilla::StyleTextDecorationLine::UNDERLINE;
612 // The style of the decoration line such as
613 // NS_STYLE_TEXT_DECORATION_STYLE_*.
614 uint8_t style = NS_STYLE_TEXT_DECORATION_STYLE_NONE;
615 bool vertical = false;
616 bool sidewaysLeft = false;
617 gfxTextRun::Range glyphRange;
618 gfxTextRun::PropertyProvider* provider;
621 struct PaintDecorationLineParams : DecorationRectParams {
622 // No need to paint outside this rect.
623 Rect dirtyRect;
624 // The top/left edge of the text.
625 Point pt;
626 // The color of the decoration line.
627 nscolor color = NS_RGBA(0, 0, 0, 0);
628 // The distance between the left edge of the given frame and the
629 // position of the text as positioned without offset of the shadow.
630 Float icoordInFrame = 0.0f;
631 // Baseline offset being applied to this text (block-direction adjustment
632 // applied to glyph positions when computing skip-ink intercepts).
633 Float baselineOffset = 0.0f;
637 * Function for painting the clipped decoration lines for the text.
638 * Takes into account the rect clipping that occurs when
639 * text-decoration-skip-ink is being used for underlines or overlines
641 * input:
642 * @param aFrame the frame which needs the decoration line
643 * @param aDrawTarget the target/backend being drawn to
644 * @param aParams the parameters for the decoration line
645 * being drawn
646 * @param aRect the rect representing the decoration line
648 static void PaintDecorationLineInternal(
649 nsIFrame* aFrame, DrawTarget& aDrawTarget,
650 const PaintDecorationLineParams& aParams, Rect aRect);
653 * Function for painting the decoration lines for the text.
655 * input:
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
659 * being drawn
661 static void PaintDecorationLine(nsIFrame* aFrame, DrawTarget& aDrawTarget,
662 const PaintDecorationLineParams& aParams);
665 * Returns a Rect corresponding to the outline of the decoration line for the
666 * given text metrics. Arguments have the same meaning as for
667 * PaintDecorationLine. Currently this only works for solid
668 * decorations; for other decoration styles the returned Rect will be empty.
670 static Rect DecorationLineToPath(const PaintDecorationLineParams& aParams);
673 * Function for getting the decoration line rect for the text.
674 * NOTE: aLineSize, aAscent and aOffset are non-rounded device pixels,
675 * not app units.
676 * input:
677 * @param aPresContext
678 * output:
679 * @return the decoration line rect for the input,
680 * the each values are app units.
682 static nsRect GetTextDecorationRect(nsPresContext* aPresContext,
683 const DecorationRectParams& aParams);
685 static CompositionOp GetGFXBlendMode(mozilla::StyleBlend mBlendMode) {
686 switch (mBlendMode) {
687 case mozilla::StyleBlend::Normal:
688 return CompositionOp::OP_OVER;
689 case mozilla::StyleBlend::Multiply:
690 return CompositionOp::OP_MULTIPLY;
691 case mozilla::StyleBlend::Screen:
692 return CompositionOp::OP_SCREEN;
693 case mozilla::StyleBlend::Overlay:
694 return CompositionOp::OP_OVERLAY;
695 case mozilla::StyleBlend::Darken:
696 return CompositionOp::OP_DARKEN;
697 case mozilla::StyleBlend::Lighten:
698 return CompositionOp::OP_LIGHTEN;
699 case mozilla::StyleBlend::ColorDodge:
700 return CompositionOp::OP_COLOR_DODGE;
701 case mozilla::StyleBlend::ColorBurn:
702 return CompositionOp::OP_COLOR_BURN;
703 case mozilla::StyleBlend::HardLight:
704 return CompositionOp::OP_HARD_LIGHT;
705 case mozilla::StyleBlend::SoftLight:
706 return CompositionOp::OP_SOFT_LIGHT;
707 case mozilla::StyleBlend::Difference:
708 return CompositionOp::OP_DIFFERENCE;
709 case mozilla::StyleBlend::Exclusion:
710 return CompositionOp::OP_EXCLUSION;
711 case mozilla::StyleBlend::Hue:
712 return CompositionOp::OP_HUE;
713 case mozilla::StyleBlend::Saturation:
714 return CompositionOp::OP_SATURATION;
715 case mozilla::StyleBlend::Color:
716 return CompositionOp::OP_COLOR;
717 case mozilla::StyleBlend::Luminosity:
718 return CompositionOp::OP_LUMINOSITY;
719 default:
720 MOZ_ASSERT(false);
721 return CompositionOp::OP_OVER;
725 static CompositionOp GetGFXCompositeMode(
726 mozilla::StyleMaskComposite aCompositeMode) {
727 switch (aCompositeMode) {
728 case mozilla::StyleMaskComposite::Add:
729 return CompositionOp::OP_OVER;
730 case mozilla::StyleMaskComposite::Subtract:
731 return CompositionOp::OP_OUT;
732 case mozilla::StyleMaskComposite::Intersect:
733 return CompositionOp::OP_IN;
734 case mozilla::StyleMaskComposite::Exclude:
735 return CompositionOp::OP_XOR;
736 default:
737 MOZ_ASSERT(false);
738 return CompositionOp::OP_OVER;
742 protected:
743 static gfxRect GetTextDecorationRectInternal(
744 const Point& aPt, const DecorationRectParams& aParams);
747 * Returns inflated rect for painting a decoration line.
748 * Complex style decoration lines should be painted from leftmost of nearest
749 * ancestor block box because that makes better look of connection of lines
750 * for different nodes. ExpandPaintingRectForDecorationLine() returns
751 * a rect for actual painting rect for the clipped rect.
753 * input:
754 * @param aFrame the frame which needs the decoration line.
755 * @param aStyle the style of the complex decoration line
756 * NS_STYLE_TEXT_DECORATION_STYLE_DOTTED or
757 * NS_STYLE_TEXT_DECORATION_STYLE_DASHED or
758 * NS_STYLE_TEXT_DECORATION_STYLE_WAVY.
759 * @param aClippedRect the clipped rect for the decoration line.
760 * in other words, visible area of the line.
761 * @param aICoordInFrame the distance between inline-start edge of aFrame
762 * and aClippedRect.pos.
763 * @param aCycleLength the width of one cycle of the line style.
765 static Rect ExpandPaintingRectForDecorationLine(
766 nsIFrame* aFrame, const uint8_t aStyle, const Rect& aClippedRect,
767 const Float aICoordInFrame, const Float aCycleLength, bool aVertical);
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 typedef mozilla::gfx::sRGBColor sRGBColor;
785 typedef mozilla::gfx::DrawTarget DrawTarget;
786 typedef mozilla::gfx::RectCornerRadii RectCornerRadii;
788 public:
789 enum { FORCE_MASK = 0x01, DISABLE_HARDWARE_ACCELERATION_BLUR = 0x02 };
791 * Prepares a gfxContext to draw on. Do not call this twice; if you want
792 * to get the gfxContext again use GetContext().
794 * @param aRect The coordinates of the surface to create.
795 * All coordinates must be in app units.
796 * This must not include the blur radius, pass
797 * it as the second parameter and everything
798 * is taken care of.
800 * @param aBlurRadius The blur radius in app units.
802 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
803 * for conversion. Most of the time you'll
804 * pass this from the current PresContext if
805 * available.
807 * @param aDestinationCtx The graphics context to apply the blurred
808 * mask to when you call DoPaint(). Make sure
809 * it is not destroyed before you call
810 * DoPaint(). To set the color of the
811 * resulting blurred graphic mask, you must
812 * set the color on this context before
813 * calling Init().
815 * @param aDirtyRect The absolute dirty rect in app units. Used to
816 * optimize the temporary surface size and speed
817 * up blur.
819 * @param aSkipRect An area in device pixels (NOT app units!) to
820 * avoid blurring over, to prevent unnecessary work.
822 * @param aFlags FORCE_MASK to ensure that the content drawn to
823 * the returned gfxContext is used as a mask, and not drawn directly to
824 * aDestinationCtx.
826 * @return A blank 8-bit alpha-channel-only graphics context to
827 * draw on, or null on error. Must not be freed. The
828 * context has a device offset applied to it given by
829 * aRect. This means you can use coordinates as if it
830 * were at the desired position at aRect and you don't
831 * need to worry about translating any coordinates to
832 * draw on this temporary surface.
834 * If aBlurRadius is 0, the returned context is aDestinationCtx and
835 * DoPaint() does nothing, because no blurring is required. Therefore, you
836 * should prepare the destination context as if you were going to draw
837 * directly on it instead of any temporary surface created in this class.
839 gfxContext* Init(const nsRect& aRect, nscoord aSpreadRadius,
840 nscoord aBlurRadius, int32_t aAppUnitsPerDevPixel,
841 gfxContext* aDestinationCtx, const nsRect& aDirtyRect,
842 const gfxRect* aSkipRect, uint32_t aFlags = 0);
845 * Does the actual blurring and mask applying. Users of this object *must*
846 * have called Init() first, then have drawn whatever they want to be
847 * blurred onto the internal gfxContext before calling this.
849 void DoPaint();
852 * Gets the internal gfxContext at any time. Must not be freed. Avoid
853 * calling this before calling Init() since the context would not be
854 * constructed at that point.
856 gfxContext* GetContext();
859 * Get the margin associated with the given blur radius, i.e., the
860 * additional area that might be painted as a result of it. (The
861 * margin for a spread radius is itself, on all sides.)
863 static nsMargin GetBlurRadiusMargin(nscoord aBlurRadius,
864 int32_t aAppUnitsPerDevPixel);
867 * Blurs a coloured rectangle onto aDestinationCtx. This is equivalent
868 * to calling Init(), drawing a rectangle onto the returned surface
869 * and then calling DoPaint, but may let us optimize better in the
870 * backend.
872 * @param aDestinationCtx The destination to blur to.
873 * @param aRect The rectangle to blur in app units.
874 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
875 * for conversion. Most of the time you'll
876 * pass this from the current PresContext if
877 * available.
878 * @param aCornerRadii Corner radii for aRect, if it is a rounded
879 * rectangle.
880 * @param aBlurRadius The blur radius in app units.
881 * @param aShadowColor The color to draw the blurred shadow.
882 * @param aDirtyRect The absolute dirty rect in app units. Used to
883 * optimize the temporary surface size and speed
884 * up blur.
885 * @param aSkipRect An area in device pixels (NOT app units!) to
886 * avoid blurring over, to prevent unnecessary work.
888 static void BlurRectangle(gfxContext* aDestinationCtx, const nsRect& aRect,
889 int32_t aAppUnitsPerDevPixel,
890 RectCornerRadii* aCornerRadii, nscoord aBlurRadius,
891 const sRGBColor& aShadowColor,
892 const nsRect& aDirtyRect, const gfxRect& aSkipRect);
895 * Draws a blurred inset box shadow shape onto the destination surface.
896 * Like BlurRectangle, this is equivalent to calling Init(),
897 * drawing a rectangle onto the returned surface
898 * and then calling DoPaint, but may let us optimize better in the
899 * backend.
901 * @param aDestinationCtx The destination to blur to.
902 * @param aDestinationRect The rectangle to blur in app units.
903 * @param aShadowClipRect The inside clip rect that creates the path.
904 * @param aShadowColor The color of the blur
905 * @param aBlurRadiusAppUnits The blur radius in app units
906 * @param aSpreadRadiusAppUnits The spread radius in app units.
907 * @param aAppUnitsPerDevPixel The number of app units in a device pixel,
908 * for conversion. Most of the time you'll
909 * pass this from the current PresContext if
910 * available.
911 * @param aHasBorderRadius If this inset box blur has a border radius
912 * @param aInnerClipRectRadii The clip rect radii used for the inside rect's
913 * path.
914 * @param aSkipRect An area in device pixels (NOT app units!) to
915 * avoid blurring over, to prevent unnecessary work.
917 bool InsetBoxBlur(gfxContext* aDestinationCtx,
918 mozilla::gfx::Rect aDestinationRect,
919 mozilla::gfx::Rect aShadowClipRect,
920 mozilla::gfx::sRGBColor& aShadowColor,
921 nscoord aBlurRadiusAppUnits, nscoord aSpreadRadiusAppUnits,
922 int32_t aAppUnitsPerDevPixel, bool aHasBorderRadius,
923 RectCornerRadii& aInnerClipRectRadii,
924 mozilla::gfx::Rect aSkipRect,
925 mozilla::gfx::Point aShadowOffset);
927 protected:
928 static void GetBlurAndSpreadRadius(DrawTarget* aDestDrawTarget,
929 int32_t aAppUnitsPerDevPixel,
930 nscoord aBlurRadius, nscoord aSpreadRadius,
931 mozilla::gfx::IntSize& aOutBlurRadius,
932 mozilla::gfx::IntSize& aOutSpreadRadius,
933 bool aConstrainSpreadRadius = true);
935 gfxAlphaBoxBlur mAlphaBoxBlur;
936 RefPtr<gfxContext> mContext;
937 gfxContext* mDestinationCtx;
939 /* This is true if the blur already has it's content transformed
940 * by mDestinationCtx's transform */
941 bool mPreTransformed;
944 #endif /* nsCSSRendering_h___ */