/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /* * structs that contain the data provided by nsStyleContext, the * internal API for computed style data for an element */ #ifndef nsStyleStruct_h___ #define nsStyleStruct_h___ #include "mozilla/ArenaObjectID.h" #include "mozilla/Attributes.h" #include "mozilla/CSSVariableValues.h" #include "mozilla/Maybe.h" #include "mozilla/SheetType.h" #include "mozilla/StaticPtr.h" #include "mozilla/StyleComplexColor.h" #include "mozilla/UniquePtr.h" #include "nsColor.h" #include "nsCoord.h" #include "nsMargin.h" #include "nsFont.h" #include "nsStyleAutoArray.h" #include "nsStyleCoord.h" #include "nsStyleConsts.h" #include "nsThemeConstants.h" #include "nsChangeHint.h" #include "nsPresContext.h" #include "nsCOMPtr.h" #include "nsCOMArray.h" #include "nsTArray.h" #include "nsCSSValue.h" #include "imgRequestProxy.h" #include "Orientation.h" #include "CounterStyleManager.h" #include // offsetof() #include #include "X11UndefineNone.h" class nsIFrame; class nsIURI; class nsStyleContext; class nsTextFrame; class imgIContainer; struct nsStyleVisibility; namespace mozilla { namespace dom { class ImageTracker; } // namespace dom } // namespace mozilla // Includes nsStyleStructID. #include "nsStyleStructFwd.h" // Bits for each struct. // NS_STYLE_INHERIT_BIT defined in nsStyleStructFwd.h #define NS_STYLE_INHERIT_MASK 0x000ffffff // Bits for inherited structs. #define NS_STYLE_INHERITED_STRUCT_MASK \ ((nsStyleStructID_size_t(1) << nsStyleStructID_Inherited_Count) - 1) // Bits for reset structs. #define NS_STYLE_RESET_STRUCT_MASK \ (((nsStyleStructID_size_t(1) << nsStyleStructID_Reset_Count) - 1) \ << nsStyleStructID_Inherited_Count) // Additional bits for nsStyleContext's mBits: // See nsStyleContext::HasTextDecorationLines #define NS_STYLE_HAS_TEXT_DECORATION_LINES 0x001000000 // See nsStyleContext::HasPseudoElementData. #define NS_STYLE_HAS_PSEUDO_ELEMENT_DATA 0x002000000 // See nsStyleContext::RelevantLinkIsVisited #define NS_STYLE_RELEVANT_LINK_VISITED 0x004000000 // See nsStyleContext::IsStyleIfVisited #define NS_STYLE_IS_STYLE_IF_VISITED 0x008000000 // See nsStyleContext::HasChildThatUsesGrandancestorStyle #define NS_STYLE_CHILD_USES_GRANDANCESTOR_STYLE 0x010000000 // See nsStyleContext::IsShared #define NS_STYLE_IS_SHARED 0x020000000 // See nsStyleContext::AssertStructsNotUsedElsewhere // (This bit is currently only used in #ifdef DEBUG code.) #define NS_STYLE_IS_GOING_AWAY 0x040000000 // See nsStyleContext::ShouldSuppressLineBreak #define NS_STYLE_SUPPRESS_LINEBREAK 0x080000000 // See nsStyleContext::IsInDisplayNoneSubtree #define NS_STYLE_IN_DISPLAY_NONE_SUBTREE 0x100000000 // See nsStyleContext::FindChildWithRules #define NS_STYLE_INELIGIBLE_FOR_SHARING 0x200000000 // See nsStyleContext::HasChildThatUsesResetStyle #define NS_STYLE_HAS_CHILD_THAT_USES_RESET_STYLE 0x400000000 // See nsStyleContext::IsTextCombined #define NS_STYLE_IS_TEXT_COMBINED 0x800000000 // See nsStyleContext::GetPseudoEnum #define NS_STYLE_CONTEXT_TYPE_SHIFT 36 // Additional bits for nsRuleNode's mDependentBits: #define NS_RULE_NODE_IS_ANIMATION_RULE 0x01000000 // Free bit 0x02000000 #define NS_RULE_NODE_USED_DIRECTLY 0x04000000 #define NS_RULE_NODE_IS_IMPORTANT 0x08000000 #define NS_RULE_NODE_LEVEL_MASK 0xf0000000 #define NS_RULE_NODE_LEVEL_SHIFT 28 // Additional bits for nsRuleNode's mNoneBits: #define NS_RULE_NODE_HAS_ANIMATION_DATA 0x80000000 static_assert(int(mozilla::SheetType::Count) - 1 <= (NS_RULE_NODE_LEVEL_MASK >> NS_RULE_NODE_LEVEL_SHIFT), "NS_RULE_NODE_LEVEL_MASK cannot fit SheetType"); static_assert(NS_STYLE_INHERIT_MASK == (1 << nsStyleStructID_Length) - 1, "NS_STYLE_INHERIT_MASK is not correct"); static_assert((NS_RULE_NODE_IS_ANIMATION_RULE & NS_STYLE_INHERIT_MASK) == 0, "NS_RULE_NODE_IS_ANIMATION_RULE must not overlap the style struct bits."); namespace mozilla { struct Position { using Coord = nsStyleCoord::CalcValue; Coord mXPosition, mYPosition; // Initialize nothing Position() {} // Sets both mXPosition and mYPosition to the given percent value for the // initial property-value (e.g. 0.0f for "0% 0%", or 0.5f for "50% 50%") void SetInitialPercentValues(float aPercentVal); // Sets both mXPosition and mYPosition to 0 (app units) for the // initial property-value as a length with no percentage component. void SetInitialZeroValues(); // True if the effective background image position described by this depends // on the size of the corresponding frame. bool DependsOnPositioningAreaSize() const { return mXPosition.mPercent != 0.0f || mYPosition.mPercent != 0.0f; } bool operator==(const Position& aOther) const { return mXPosition == aOther.mXPosition && mYPosition == aOther.mYPosition; } bool operator!=(const Position& aOther) const { return !(*this == aOther); } }; } // namespace mozilla // The lifetime of these objects is managed by the presshell's arena. struct MOZ_NEEDS_MEMMOVABLE_MEMBERS nsStyleFont { nsStyleFont(const nsFont& aFont, const nsPresContext* aContext); nsStyleFont(const nsStyleFont& aStyleFont); explicit nsStyleFont(const nsPresContext* aContext); ~nsStyleFont() { MOZ_COUNT_DTOR(nsStyleFont); } void FinishStyle(nsPresContext* aPresContext) {} nsChangeHint CalcDifference(const nsStyleFont& aNewData) const; /** * Return aSize multiplied by the current text zoom factor (in aPresContext). * aSize is allowed to be negative, but the caller is expected to deal with * negative results. The result is clamped to nscoord_MIN .. nscoord_MAX. */ static nscoord ZoomText(const nsPresContext* aPresContext, nscoord aSize); /** * Return aSize divided by the current text zoom factor (in aPresContext). * aSize is allowed to be negative, but the caller is expected to deal with * negative results. The result is clamped to nscoord_MIN .. nscoord_MAX. */ static nscoord UnZoomText(nsPresContext* aPresContext, nscoord aSize); static already_AddRefed GetLanguage(const nsPresContext* aPresContext); void* operator new(size_t sz, nsStyleFont* aSelf) { return aSelf; } void* operator new(size_t sz, nsPresContext* aContext) { return aContext->PresShell()-> AllocateByObjectID(mozilla::eArenaObjectID_nsStyleFont, sz); } void Destroy(nsPresContext* aContext); void EnableZoom(nsPresContext* aContext, bool aEnable); nsFont mFont; // [inherited] nscoord mSize; // [inherited] Our "computed size". Can be different // from mFont.size which is our "actual size" and is // enforced to be >= the user's preferred min-size. // mFont.size should be used for display purposes // while mSize is the value to return in // getComputedStyle() for example. uint8_t mGenericID; // [inherited] generic CSS font family, if any; // value is a kGenericFont_* constant, see nsFont.h. // MathML scriptlevel support int8_t mScriptLevel; // [inherited] // MathML mathvariant support uint8_t mMathVariant; // [inherited] // MathML displaystyle support uint8_t mMathDisplay; // [inherited] // allow different min font-size for certain cases uint8_t mMinFontSizeRatio; // [inherited] percent * 100 // was mLanguage set based on a lang attribute in the document? bool mExplicitLanguage; // [inherited] // should calls to ZoomText() and UnZoomText() be made to the font // size on this nsStyleFont? bool mAllowZoom; // [inherited] // The value mSize would have had if scriptminsize had never been applied nscoord mScriptUnconstrainedSize; nscoord mScriptMinSize; // [inherited] length float mScriptSizeMultiplier; // [inherited] nsCOMPtr mLanguage; // [inherited] }; struct nsStyleGradientStop { nsStyleCoord mLocation; // percent, coord, calc, none nscolor mColor; bool mIsInterpolationHint; // Use ==/!= on nsStyleGradient instead of on the gradient stop. bool operator==(const nsStyleGradientStop&) const = delete; bool operator!=(const nsStyleGradientStop&) const = delete; }; class nsStyleGradient final { public: nsStyleGradient(); uint8_t mShape; // NS_STYLE_GRADIENT_SHAPE_* uint8_t mSize; // NS_STYLE_GRADIENT_SIZE_*; // not used (must be FARTHEST_CORNER) for linear shape bool mRepeating; bool mLegacySyntax; // If true, serialization should use a vendor prefix. // XXXdholbert This will hopefully be going away soon, if bug 1337655 sticks: bool mMozLegacySyntax; // (Only makes sense when mLegacySyntax is true.) // If true, serialization should use -moz prefix. // Else, serialization should use -webkit prefix. nsStyleCoord mBgPosX; // percent, coord, calc, none nsStyleCoord mBgPosY; // percent, coord, calc, none nsStyleCoord mAngle; // none, angle nsStyleCoord mRadiusX; // percent, coord, calc, none nsStyleCoord mRadiusY; // percent, coord, calc, none // stops are in the order specified in the stylesheet nsTArray mStops; bool operator==(const nsStyleGradient& aOther) const; bool operator!=(const nsStyleGradient& aOther) const { return !(*this == aOther); } bool IsOpaque(); bool HasCalc(); uint32_t Hash(PLDHashNumber aHash); NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsStyleGradient) private: // Private destructor, to discourage deletion outside of Release(): ~nsStyleGradient() {} nsStyleGradient(const nsStyleGradient& aOther) = delete; nsStyleGradient& operator=(const nsStyleGradient& aOther) = delete; }; /** * A wrapper for an imgRequestProxy that supports off-main-thread creation * and equality comparison. * * An nsStyleImageRequest can be created in two ways: * * 1. Using the constructor that takes an imgRequestProxy. This must * be called from the main thread. The nsStyleImageRequest is * immediately considered "resolved", and the get() method that * returns the imgRequestProxy can be called. * * 2. Using the constructor that takes the URL, base URI, referrer * and principal that can be used to inititiate an image load and * produce an imgRequestProxy later. This can be called from * any thread. The nsStyleImageRequest is not considered "resolved" * at this point, and the Resolve() method must be called later * to initiate the image load and make calls to get() valid. * * Calls to TrackImage(), UntrackImage(), LockImage(), UnlockImage() and * RequestDiscard() are made to the imgRequestProxy and ImageTracker as * appropriate, according to the mode flags passed in to the constructor. * * The main thread constructor takes a pointer to the css::ImageValue that * is the specified url() value, while the off-main-thread constructor * creates a new css::ImageValue to represent the url() information passed * to the constructor. This ImageValue is held on to for the comparisons done * in DefinitelyEquals(), so that we don't need to call into the non-OMT-safe * Equals() on the nsIURI objects returned from imgRequestProxy::GetURI(). */ class nsStyleImageRequest { public: typedef mozilla::css::URLValueData URLValueData; // Flags describing whether the imgRequestProxy must be tracked in the // ImageTracker, whether LockImage/UnlockImage calls will be made // when obtaining and releasing the imgRequestProxy, and whether // RequestDiscard will be called on release. enum class Mode : uint8_t { // The imgRequestProxy will be added to the ImageTracker when resolved // Without this flag, the nsStyleImageRequest itself will call LockImage/ // UnlockImage on the imgRequestProxy, rather than leaving locking to the // ImageTracker to manage. // // This flag is currently used by all nsStyleImageRequests except // those for list-style-image and cursor. Track = 0x1, // The imgRequestProxy will have its RequestDiscard method called when // the nsStyleImageRequest is going away. // // This is currently used only for cursor images. Discard = 0x2, }; // Must be called from the main thread. // // aImageTracker must be non-null iff aModeFlags contains Track. nsStyleImageRequest(Mode aModeFlags, imgRequestProxy* aRequestProxy, mozilla::css::ImageValue* aImageValue, mozilla::dom::ImageTracker* aImageTracker); // Can be called from any thread, but Resolve() must be called later // on the main thread before get() can be used. nsStyleImageRequest( Mode aModeFlags, const nsAString& aURL, already_AddRefed aExtraData); bool Resolve(nsPresContext* aPresContext); bool IsResolved() const { return mResolved; } imgRequestProxy* get() { MOZ_ASSERT(IsResolved(), "Resolve() must be called first"); MOZ_ASSERT(NS_IsMainThread()); return mRequestProxy.get(); } const imgRequestProxy* get() const { return const_cast(this)->get(); } // Returns whether the ImageValue objects in the two nsStyleImageRequests // return true from URLValueData::DefinitelyEqualURIs. bool DefinitelyEquals(const nsStyleImageRequest& aOther) const; mozilla::css::ImageValue* GetImageValue() const { return mImageValue; } already_AddRefed GetImageURI() const; NS_INLINE_DECL_THREADSAFE_REFCOUNTING(nsStyleImageRequest); private: ~nsStyleImageRequest(); nsStyleImageRequest& operator=(const nsStyleImageRequest& aOther) = delete; void MaybeTrackAndLock(); RefPtr mRequestProxy; RefPtr mImageValue; RefPtr mImageTracker; // Cache DocGroup for dispatching events in the destructor. RefPtr mDocGroup; Mode mModeFlags; bool mResolved; }; MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(nsStyleImageRequest::Mode) enum nsStyleImageType { eStyleImageType_Null, eStyleImageType_Image, eStyleImageType_Gradient, eStyleImageType_Element, eStyleImageType_URL }; struct CachedBorderImageData { // Caller are expected to ensure that the value of aSVGViewportSize is // different from the cached one since the method won't do the check. void SetCachedSVGViewportSize(const mozilla::Maybe& aSVGViewportSize); const mozilla::Maybe& GetCachedSVGViewportSize(); void PurgeCachedImages(); void SetSubImage(uint8_t aIndex, imgIContainer* aSubImage); imgIContainer* GetSubImage(uint8_t aIndex); private: // If this is a SVG border-image, we save the size of the SVG viewport that // we used when rasterizing any cached border-image subimages. (The viewport // size matters for percent-valued sizes & positions in inner SVG doc). mozilla::Maybe mCachedSVGViewportSize; nsCOMArray mSubImages; }; /** * Represents a paintable image of one of the following types. * (1) A real image loaded from an external source. * (2) A CSS linear or radial gradient. * (3) An element within a document, or an ,