michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=2 sw=2 et tw=78: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsLayoutUtils.h" michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/BasicEvents.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDOMHTMLDocument.h" michael@0: #include "nsIDOMHTMLElement.h" michael@0: #include "nsFrameList.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsCSSPseudoElements.h" michael@0: #include "nsCSSAnonBoxes.h" michael@0: #include "nsCSSColorUtils.h" michael@0: #include "nsView.h" michael@0: #include "nsPlaceholderFrame.h" michael@0: #include "nsIScrollableFrame.h" michael@0: #include "nsIDOMEvent.h" michael@0: #include "nsDisplayList.h" michael@0: #include "nsRegion.h" michael@0: #include "nsFrameManager.h" michael@0: #include "nsBlockFrame.h" michael@0: #include "nsBidiPresUtils.h" michael@0: #include "imgIContainer.h" michael@0: #include "ImageOps.h" michael@0: #include "gfxRect.h" michael@0: #include "gfxContext.h" michael@0: #include "nsRenderingContext.h" michael@0: #include "nsIInterfaceRequestorUtils.h" michael@0: #include "nsCSSRendering.h" michael@0: #include "nsThemeConstants.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsIDocShell.h" michael@0: #include "nsIWidget.h" michael@0: #include "gfxMatrix.h" michael@0: #include "gfxPoint3D.h" michael@0: #include "gfxPrefs.h" michael@0: #include "gfxTypes.h" michael@0: #include "nsTArray.h" michael@0: #include "mozilla/dom/HTMLCanvasElement.h" michael@0: #include "nsICanvasRenderingContextInternal.h" michael@0: #include "gfxPlatform.h" michael@0: #include michael@0: #include "mozilla/dom/HTMLVideoElement.h" michael@0: #include "mozilla/dom/HTMLImageElement.h" michael@0: #include "mozilla/dom/DOMRect.h" michael@0: #include "imgIRequest.h" michael@0: #include "nsIImageLoadingContent.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsCSSProps.h" michael@0: #include "nsListControlFrame.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "nsCanvasFrame.h" michael@0: #include "gfxDrawable.h" michael@0: #include "gfxUtils.h" michael@0: #include "nsDataHashtable.h" michael@0: #include "nsTextFrame.h" michael@0: #include "nsFontFaceList.h" michael@0: #include "nsFontInflationData.h" michael@0: #include "nsSVGUtils.h" michael@0: #include "SVGTextFrame.h" michael@0: #include "nsStyleStructInlines.h" michael@0: #include "nsStyleTransformMatrix.h" michael@0: #include "nsIFrameInlines.h" michael@0: #include "ImageContainer.h" michael@0: #include "nsComputedDOMStyle.h" michael@0: #include "ActiveLayerTracker.h" michael@0: #include "mozilla/gfx/2D.h" michael@0: #include "gfx2DGlue.h" michael@0: #include "mozilla/LookAndFeel.h" michael@0: #include "UnitTransforms.h" michael@0: #include "TiledLayerBuffer.h" // For TILEDLAYERBUFFER_TILE_SIZE michael@0: michael@0: #include "mozilla/Preferences.h" michael@0: michael@0: #include "mozilla/LookAndFeel.h" michael@0: michael@0: #ifdef MOZ_XUL michael@0: #include "nsXULPopupManager.h" michael@0: #endif michael@0: michael@0: #include "GeckoProfiler.h" michael@0: #include "nsAnimationManager.h" michael@0: #include "nsTransitionManager.h" michael@0: #include "RestyleManager.h" michael@0: michael@0: // Additional includes used on B2G by code in GetOrMaybeCreateDisplayPort(). michael@0: #ifdef MOZ_WIDGET_GONK michael@0: #include "mozilla/layers/AsyncPanZoomController.h" michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::css; michael@0: using namespace mozilla::dom; michael@0: using namespace mozilla::layers; michael@0: using namespace mozilla::layout; michael@0: using namespace mozilla::gfx; michael@0: michael@0: using mozilla::image::Angle; michael@0: using mozilla::image::Flip; michael@0: using mozilla::image::ImageOps; michael@0: using mozilla::image::Orientation; michael@0: michael@0: #define GRID_ENABLED_PREF_NAME "layout.css.grid.enabled" michael@0: #define STICKY_ENABLED_PREF_NAME "layout.css.sticky.enabled" michael@0: #define TEXT_ALIGN_TRUE_ENABLED_PREF_NAME "layout.css.text-align-true-value.enabled" michael@0: michael@0: #ifdef DEBUG michael@0: // TODO: remove, see bug 598468. michael@0: bool nsLayoutUtils::gPreventAssertInCompareTreePosition = false; michael@0: #endif // DEBUG michael@0: michael@0: typedef FrameMetrics::ViewID ViewID; michael@0: michael@0: /* static */ uint32_t nsLayoutUtils::sFontSizeInflationEmPerLine; michael@0: /* static */ uint32_t nsLayoutUtils::sFontSizeInflationMinTwips; michael@0: /* static */ uint32_t nsLayoutUtils::sFontSizeInflationLineThreshold; michael@0: /* static */ int32_t nsLayoutUtils::sFontSizeInflationMappingIntercept; michael@0: /* static */ uint32_t nsLayoutUtils::sFontSizeInflationMaxRatio; michael@0: /* static */ bool nsLayoutUtils::sFontSizeInflationForceEnabled; michael@0: /* static */ bool nsLayoutUtils::sFontSizeInflationDisabledInMasterProcess; michael@0: /* static */ bool nsLayoutUtils::sInvalidationDebuggingIsEnabled; michael@0: /* static */ bool nsLayoutUtils::sCSSVariablesEnabled; michael@0: /* static */ bool nsLayoutUtils::sInterruptibleReflowEnabled; michael@0: michael@0: static ViewID sScrollIdCounter = FrameMetrics::START_SCROLL_ID; michael@0: michael@0: typedef nsDataHashtable ContentMap; michael@0: static ContentMap* sContentMap = nullptr; michael@0: static ContentMap& GetContentMap() { michael@0: if (!sContentMap) { michael@0: sContentMap = new ContentMap(); michael@0: } michael@0: return *sContentMap; michael@0: } michael@0: michael@0: // When the pref "layout.css.grid.enabled" changes, this function is invoked michael@0: // to let us update kDisplayKTable, to selectively disable or restore the michael@0: // entries for "grid" and "inline-grid" in that table. michael@0: static void michael@0: GridEnabledPrefChangeCallback(const char* aPrefName, void* aClosure) michael@0: { michael@0: MOZ_ASSERT(strncmp(aPrefName, GRID_ENABLED_PREF_NAME, michael@0: ArrayLength(GRID_ENABLED_PREF_NAME)) == 0, michael@0: "We only registered this callback for a single pref, so it " michael@0: "should only be called for that pref"); michael@0: michael@0: static int32_t sIndexOfGridInDisplayTable; michael@0: static int32_t sIndexOfInlineGridInDisplayTable; michael@0: static bool sAreGridKeywordIndicesInitialized; // initialized to false michael@0: michael@0: bool isGridEnabled = michael@0: Preferences::GetBool(GRID_ENABLED_PREF_NAME, false); michael@0: if (!sAreGridKeywordIndicesInitialized) { michael@0: // First run: find the position of "grid" and "inline-grid" in michael@0: // kDisplayKTable. michael@0: sIndexOfGridInDisplayTable = michael@0: nsCSSProps::FindIndexOfKeyword(eCSSKeyword_grid, michael@0: nsCSSProps::kDisplayKTable); michael@0: MOZ_ASSERT(sIndexOfGridInDisplayTable >= 0, michael@0: "Couldn't find grid in kDisplayKTable"); michael@0: sIndexOfInlineGridInDisplayTable = michael@0: nsCSSProps::FindIndexOfKeyword(eCSSKeyword_inline_grid, michael@0: nsCSSProps::kDisplayKTable); michael@0: MOZ_ASSERT(sIndexOfInlineGridInDisplayTable >= 0, michael@0: "Couldn't find inline-grid in kDisplayKTable"); michael@0: sAreGridKeywordIndicesInitialized = true; michael@0: } michael@0: michael@0: // OK -- now, stomp on or restore the "grid" entries in kDisplayKTable, michael@0: // depending on whether the grid pref is enabled vs. disabled. michael@0: if (sIndexOfGridInDisplayTable >= 0) { michael@0: nsCSSProps::kDisplayKTable[sIndexOfGridInDisplayTable] = michael@0: isGridEnabled ? eCSSKeyword_grid : eCSSKeyword_UNKNOWN; michael@0: } michael@0: if (sIndexOfInlineGridInDisplayTable >= 0) { michael@0: nsCSSProps::kDisplayKTable[sIndexOfInlineGridInDisplayTable] = michael@0: isGridEnabled ? eCSSKeyword_inline_grid : eCSSKeyword_UNKNOWN; michael@0: } michael@0: } michael@0: michael@0: // When the pref "layout.css.sticky.enabled" changes, this function is invoked michael@0: // to let us update kPositionKTable, to selectively disable or restore the michael@0: // entry for "sticky" in that table. michael@0: static void michael@0: StickyEnabledPrefChangeCallback(const char* aPrefName, void* aClosure) michael@0: { michael@0: MOZ_ASSERT(strncmp(aPrefName, STICKY_ENABLED_PREF_NAME, michael@0: ArrayLength(STICKY_ENABLED_PREF_NAME)) == 0, michael@0: "We only registered this callback for a single pref, so it " michael@0: "should only be called for that pref"); michael@0: michael@0: static int32_t sIndexOfStickyInPositionTable; michael@0: static bool sIsStickyKeywordIndexInitialized; // initialized to false michael@0: michael@0: bool isStickyEnabled = michael@0: Preferences::GetBool(STICKY_ENABLED_PREF_NAME, false); michael@0: michael@0: if (!sIsStickyKeywordIndexInitialized) { michael@0: // First run: find the position of "sticky" in kPositionKTable. michael@0: sIndexOfStickyInPositionTable = michael@0: nsCSSProps::FindIndexOfKeyword(eCSSKeyword_sticky, michael@0: nsCSSProps::kPositionKTable); michael@0: MOZ_ASSERT(sIndexOfStickyInPositionTable >= 0, michael@0: "Couldn't find sticky in kPositionKTable"); michael@0: sIsStickyKeywordIndexInitialized = true; michael@0: } michael@0: michael@0: // OK -- now, stomp on or restore the "sticky" entry in kPositionKTable, michael@0: // depending on whether the sticky pref is enabled vs. disabled. michael@0: nsCSSProps::kPositionKTable[sIndexOfStickyInPositionTable] = michael@0: isStickyEnabled ? eCSSKeyword_sticky : eCSSKeyword_UNKNOWN; michael@0: } michael@0: michael@0: // When the pref "layout.css.text-align-true-value.enabled" changes, this michael@0: // function is called to let us update kTextAlignKTable & kTextAlignLastKTable, michael@0: // to selectively disable or restore the entries for "true" in those tables. michael@0: static void michael@0: TextAlignTrueEnabledPrefChangeCallback(const char* aPrefName, void* aClosure) michael@0: { michael@0: NS_ASSERTION(strcmp(aPrefName, TEXT_ALIGN_TRUE_ENABLED_PREF_NAME) == 0, michael@0: "Did you misspell " TEXT_ALIGN_TRUE_ENABLED_PREF_NAME " ?"); michael@0: michael@0: static bool sIsInitialized; michael@0: static int32_t sIndexOfTrueInTextAlignTable; michael@0: static int32_t sIndexOfTrueInTextAlignLastTable; michael@0: bool isTextAlignTrueEnabled = michael@0: Preferences::GetBool(TEXT_ALIGN_TRUE_ENABLED_PREF_NAME, false); michael@0: michael@0: if (!sIsInitialized) { michael@0: // First run: find the position of "true" in kTextAlignKTable. michael@0: sIndexOfTrueInTextAlignTable = michael@0: nsCSSProps::FindIndexOfKeyword(eCSSKeyword_true, michael@0: nsCSSProps::kTextAlignKTable); michael@0: // First run: find the position of "true" in kTextAlignLastKTable. michael@0: sIndexOfTrueInTextAlignLastTable = michael@0: nsCSSProps::FindIndexOfKeyword(eCSSKeyword_true, michael@0: nsCSSProps::kTextAlignLastKTable); michael@0: sIsInitialized = true; michael@0: } michael@0: michael@0: // OK -- now, stomp on or restore the "true" entry in the keyword tables, michael@0: // depending on whether the pref is enabled vs. disabled. michael@0: MOZ_ASSERT(sIndexOfTrueInTextAlignTable >= 0); michael@0: nsCSSProps::kTextAlignKTable[sIndexOfTrueInTextAlignTable] = michael@0: isTextAlignTrueEnabled ? eCSSKeyword_true : eCSSKeyword_UNKNOWN; michael@0: MOZ_ASSERT(sIndexOfTrueInTextAlignLastTable >= 0); michael@0: nsCSSProps::kTextAlignLastKTable[sIndexOfTrueInTextAlignLastTable] = michael@0: isTextAlignTrueEnabled ? eCSSKeyword_true : eCSSKeyword_UNKNOWN; michael@0: } michael@0: michael@0: template michael@0: static AnimationsOrTransitions* michael@0: HasAnimationOrTransitionForCompositor(nsIContent* aContent, michael@0: nsIAtom* aAnimationProperty, michael@0: nsCSSProperty aProperty) michael@0: { michael@0: AnimationsOrTransitions* animations = michael@0: static_cast(aContent->GetProperty(aAnimationProperty)); michael@0: if (animations) { michael@0: bool propertyMatches = animations->HasAnimationOfProperty(aProperty); michael@0: if (propertyMatches && michael@0: animations->CanPerformOnCompositorThread( michael@0: CommonElementAnimationData::CanAnimate_AllowPartial)) { michael@0: return animations; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::HasAnimationsForCompositor(nsIContent* aContent, michael@0: nsCSSProperty aProperty) michael@0: { michael@0: if (!aContent->MayHaveAnimations()) michael@0: return false; michael@0: return HasAnimationOrTransitionForCompositor michael@0: (aContent, nsGkAtoms::animationsProperty, aProperty) || michael@0: HasAnimationOrTransitionForCompositor michael@0: (aContent, nsGkAtoms::transitionsProperty, aProperty); michael@0: } michael@0: michael@0: template michael@0: AnimationsOrTransitions* michael@0: mozilla::HasAnimationOrTransition(nsIContent* aContent, michael@0: nsIAtom* aAnimationProperty, michael@0: nsCSSProperty aProperty) michael@0: { michael@0: AnimationsOrTransitions* animations = michael@0: static_cast(aContent->GetProperty(aAnimationProperty)); michael@0: if (animations) { michael@0: bool propertyMatches = animations->HasAnimationOfProperty(aProperty); michael@0: if (propertyMatches) { michael@0: return animations; michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: template ElementAnimations* michael@0: mozilla::HasAnimationOrTransition(nsIContent* aContent, michael@0: nsIAtom* aAnimationProperty, michael@0: nsCSSProperty aProperty); michael@0: michael@0: template ElementTransitions* michael@0: mozilla::HasAnimationOrTransition(nsIContent* aContent, michael@0: nsIAtom* aAnimationProperty, michael@0: nsCSSProperty aProperty); michael@0: michael@0: michael@0: bool michael@0: nsLayoutUtils::HasAnimations(nsIContent* aContent, michael@0: nsCSSProperty aProperty) michael@0: { michael@0: if (!aContent->MayHaveAnimations()) michael@0: return false; michael@0: return HasAnimationOrTransition michael@0: (aContent, nsGkAtoms::animationsProperty, aProperty) || michael@0: HasAnimationOrTransition michael@0: (aContent, nsGkAtoms::transitionsProperty, aProperty); michael@0: } michael@0: michael@0: static gfxSize michael@0: GetScaleForValue(const nsStyleAnimation::Value& aValue, michael@0: nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) { michael@0: NS_WARNING("No frame."); michael@0: return gfxSize(); michael@0: } michael@0: if (aValue.GetUnit() != nsStyleAnimation::eUnit_Transform) { michael@0: NS_WARNING("Expected a transform."); michael@0: return gfxSize(); michael@0: } michael@0: michael@0: nsCSSValueSharedList* list = aValue.GetCSSValueSharedListValue(); michael@0: MOZ_ASSERT(list->mHead); michael@0: michael@0: if (list->mHead->mValue.GetUnit() == eCSSUnit_None) { michael@0: // There is an animation, but no actual transform yet. michael@0: return gfxSize(); michael@0: } michael@0: michael@0: nsRect frameBounds = aFrame->GetRect(); michael@0: bool dontCare; michael@0: gfx3DMatrix transform = nsStyleTransformMatrix::ReadTransforms( michael@0: list->mHead, michael@0: aFrame->StyleContext(), michael@0: aFrame->PresContext(), dontCare, frameBounds, michael@0: aFrame->PresContext()->AppUnitsPerDevPixel()); michael@0: michael@0: gfxMatrix transform2d; michael@0: bool canDraw2D = transform.CanDraw2D(&transform2d); michael@0: if (!canDraw2D) { michael@0: return gfxSize(); michael@0: } michael@0: michael@0: return transform2d.ScaleFactors(true); michael@0: } michael@0: michael@0: float michael@0: GetSuitableScale(float aMaxScale, float aMinScale) michael@0: { michael@0: // If the minimum scale >= 1.0f, use it; if the maximum <= 1.0f, use it; michael@0: // otherwise use 1.0f. michael@0: if (aMinScale >= 1.0f) { michael@0: return aMinScale; michael@0: } michael@0: else if (aMaxScale <= 1.0f) { michael@0: return aMaxScale; michael@0: } michael@0: michael@0: return 1.0f; michael@0: } michael@0: michael@0: gfxSize michael@0: nsLayoutUtils::ComputeSuitableScaleForAnimation(nsIContent* aContent) michael@0: { michael@0: gfxSize maxScale(1.0f, 1.0f); michael@0: gfxSize minScale(1.0f, 1.0f); michael@0: michael@0: ElementAnimations* animations = HasAnimationOrTransitionForCompositor michael@0: (aContent, nsGkAtoms::animationsProperty, eCSSProperty_transform); michael@0: if (animations) { michael@0: for (uint32_t animIdx = animations->mAnimations.Length(); animIdx-- != 0; ) { michael@0: mozilla::StyleAnimation& anim = animations->mAnimations[animIdx]; michael@0: for (uint32_t propIdx = anim.mProperties.Length(); propIdx-- != 0; ) { michael@0: AnimationProperty& prop = anim.mProperties[propIdx]; michael@0: if (prop.mProperty == eCSSProperty_transform) { michael@0: for (uint32_t segIdx = prop.mSegments.Length(); segIdx-- != 0; ) { michael@0: AnimationPropertySegment& segment = prop.mSegments[segIdx]; michael@0: gfxSize from = GetScaleForValue(segment.mFromValue, michael@0: aContent->GetPrimaryFrame()); michael@0: maxScale.width = std::max(maxScale.width, from.width); michael@0: maxScale.height = std::max(maxScale.height, from.height); michael@0: minScale.width = std::min(minScale.width, from.width); michael@0: minScale.height = std::min(minScale.height, from.height); michael@0: gfxSize to = GetScaleForValue(segment.mToValue, michael@0: aContent->GetPrimaryFrame()); michael@0: maxScale.width = std::max(maxScale.width, to.width); michael@0: maxScale.height = std::max(maxScale.height, to.height); michael@0: minScale.width = std::min(minScale.width, to.width); michael@0: minScale.height = std::min(minScale.height, to.height); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: ElementTransitions* transitions = HasAnimationOrTransitionForCompositor michael@0: (aContent, nsGkAtoms::transitionsProperty, eCSSProperty_transform); michael@0: if (transitions) { michael@0: for (uint32_t i = 0, i_end = transitions->mPropertyTransitions.Length(); michael@0: i < i_end; ++i){ michael@0: ElementPropertyTransition &pt = transitions->mPropertyTransitions[i]; michael@0: if (pt.IsRemovedSentinel()) { michael@0: continue; michael@0: } michael@0: MOZ_ASSERT(pt.mProperties.Length() == 1, michael@0: "Should have one animation property for a transition"); michael@0: MOZ_ASSERT(pt.mProperties[0].mSegments.Length() == 1, michael@0: "Animation property should have one segment for a transition"); michael@0: const AnimationPropertySegment& segment = pt.mProperties[0].mSegments[0]; michael@0: michael@0: if (pt.mProperties[0].mProperty == eCSSProperty_transform) { michael@0: gfxSize start = GetScaleForValue(segment.mFromValue, michael@0: aContent->GetPrimaryFrame()); michael@0: maxScale.width = std::max(maxScale.width, start.width); michael@0: maxScale.height = std::max(maxScale.height, start.height); michael@0: minScale.width = std::min(minScale.width, start.width); michael@0: minScale.height = std::min(minScale.height, start.height); michael@0: gfxSize end = GetScaleForValue(segment.mToValue, michael@0: aContent->GetPrimaryFrame()); michael@0: maxScale.width = std::max(maxScale.width, end.width); michael@0: maxScale.height = std::max(maxScale.height, end.height); michael@0: minScale.width = std::min(minScale.width, end.width); michael@0: minScale.height = std::min(minScale.height, end.height); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return gfxSize(GetSuitableScale(maxScale.width, minScale.width), michael@0: GetSuitableScale(maxScale.height, minScale.height)); michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::AreAsyncAnimationsEnabled() michael@0: { michael@0: static bool sAreAsyncAnimationsEnabled; michael@0: static bool sAsyncPrefCached = false; michael@0: michael@0: if (!sAsyncPrefCached) { michael@0: sAsyncPrefCached = true; michael@0: Preferences::AddBoolVarCache(&sAreAsyncAnimationsEnabled, michael@0: "layers.offmainthreadcomposition.async-animations"); michael@0: } michael@0: michael@0: return sAreAsyncAnimationsEnabled && michael@0: gfxPlatform::OffMainThreadCompositingEnabled(); michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::IsAnimationLoggingEnabled() michael@0: { michael@0: static bool sShouldLog; michael@0: static bool sShouldLogPrefCached; michael@0: michael@0: if (!sShouldLogPrefCached) { michael@0: sShouldLogPrefCached = true; michael@0: Preferences::AddBoolVarCache(&sShouldLog, michael@0: "layers.offmainthreadcomposition.log-animations"); michael@0: } michael@0: michael@0: return sShouldLog; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::UseBackgroundNearestFiltering() michael@0: { michael@0: static bool sUseBackgroundNearestFilteringEnabled; michael@0: static bool sUseBackgroundNearestFilteringPrefInitialised = false; michael@0: michael@0: if (!sUseBackgroundNearestFilteringPrefInitialised) { michael@0: sUseBackgroundNearestFilteringPrefInitialised = true; michael@0: sUseBackgroundNearestFilteringEnabled = michael@0: Preferences::GetBool("gfx.filter.nearest.force-enabled", false); michael@0: } michael@0: michael@0: return sUseBackgroundNearestFilteringEnabled; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::GPUImageScalingEnabled() michael@0: { michael@0: static bool sGPUImageScalingEnabled; michael@0: static bool sGPUImageScalingPrefInitialised = false; michael@0: michael@0: if (!sGPUImageScalingPrefInitialised) { michael@0: sGPUImageScalingPrefInitialised = true; michael@0: sGPUImageScalingEnabled = michael@0: Preferences::GetBool("layout.gpu-image-scaling.enabled", false); michael@0: } michael@0: michael@0: return sGPUImageScalingEnabled; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::AnimatedImageLayersEnabled() michael@0: { michael@0: static bool sAnimatedImageLayersEnabled; michael@0: static bool sAnimatedImageLayersPrefCached = false; michael@0: michael@0: if (!sAnimatedImageLayersPrefCached) { michael@0: sAnimatedImageLayersPrefCached = true; michael@0: Preferences::AddBoolVarCache(&sAnimatedImageLayersEnabled, michael@0: "layout.animated-image-layers.enabled", michael@0: false); michael@0: } michael@0: michael@0: return sAnimatedImageLayersEnabled; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::CSSFiltersEnabled() michael@0: { michael@0: static bool sCSSFiltersEnabled; michael@0: static bool sCSSFiltersPrefCached = false; michael@0: michael@0: if (!sCSSFiltersPrefCached) { michael@0: sCSSFiltersPrefCached = true; michael@0: Preferences::AddBoolVarCache(&sCSSFiltersEnabled, michael@0: "layout.css.filters.enabled", michael@0: false); michael@0: } michael@0: michael@0: return sCSSFiltersEnabled; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::UnsetValueEnabled() michael@0: { michael@0: static bool sUnsetValueEnabled; michael@0: static bool sUnsetValuePrefCached = false; michael@0: michael@0: if (!sUnsetValuePrefCached) { michael@0: sUnsetValuePrefCached = true; michael@0: Preferences::AddBoolVarCache(&sUnsetValueEnabled, michael@0: "layout.css.unset-value.enabled", michael@0: false); michael@0: } michael@0: michael@0: return sUnsetValueEnabled; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::IsTextAlignTrueValueEnabled() michael@0: { michael@0: static bool sTextAlignTrueValueEnabled; michael@0: static bool sTextAlignTrueValueEnabledPrefCached = false; michael@0: michael@0: if (!sTextAlignTrueValueEnabledPrefCached) { michael@0: sTextAlignTrueValueEnabledPrefCached = true; michael@0: Preferences::AddBoolVarCache(&sTextAlignTrueValueEnabled, michael@0: TEXT_ALIGN_TRUE_ENABLED_PREF_NAME, michael@0: false); michael@0: } michael@0: michael@0: return sTextAlignTrueValueEnabled; michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::UnionChildOverflow(nsIFrame* aFrame, michael@0: nsOverflowAreas& aOverflowAreas, michael@0: FrameChildListIDs aSkipChildLists) michael@0: { michael@0: // Iterate over all children except pop-ups. michael@0: FrameChildListIDs skip = aSkipChildLists | michael@0: nsIFrame::kSelectPopupList | nsIFrame::kPopupList; michael@0: for (nsIFrame::ChildListIterator childLists(aFrame); michael@0: !childLists.IsDone(); childLists.Next()) { michael@0: if (skip.Contains(childLists.CurrentID())) { michael@0: continue; michael@0: } michael@0: michael@0: nsFrameList children = childLists.CurrentList(); michael@0: for (nsFrameList::Enumerator e(children); !e.AtEnd(); e.Next()) { michael@0: nsIFrame* child = e.get(); michael@0: nsOverflowAreas childOverflow = michael@0: child->GetOverflowAreas() + child->GetPosition(); michael@0: aOverflowAreas.UnionWith(childOverflow); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static void DestroyViewID(void* aObject, nsIAtom* aPropertyName, michael@0: void* aPropertyValue, void* aData) michael@0: { michael@0: ViewID* id = static_cast(aPropertyValue); michael@0: GetContentMap().Remove(*id); michael@0: delete id; michael@0: } michael@0: michael@0: /** michael@0: * A namespace class for static layout utilities. michael@0: */ michael@0: michael@0: bool michael@0: nsLayoutUtils::FindIDFor(const nsIContent* aContent, ViewID* aOutViewId) michael@0: { michael@0: void* scrollIdProperty = aContent->GetProperty(nsGkAtoms::RemoteId); michael@0: if (scrollIdProperty) { michael@0: *aOutViewId = *static_cast(scrollIdProperty); michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: ViewID michael@0: nsLayoutUtils::FindOrCreateIDFor(nsIContent* aContent) michael@0: { michael@0: ViewID scrollId; michael@0: michael@0: if (!FindIDFor(aContent, &scrollId)) { michael@0: scrollId = sScrollIdCounter++; michael@0: aContent->SetProperty(nsGkAtoms::RemoteId, new ViewID(scrollId), michael@0: DestroyViewID); michael@0: GetContentMap().Put(scrollId, aContent); michael@0: } michael@0: michael@0: return scrollId; michael@0: } michael@0: michael@0: nsIContent* michael@0: nsLayoutUtils::FindContentFor(ViewID aId) michael@0: { michael@0: NS_ABORT_IF_FALSE(aId != FrameMetrics::NULL_SCROLL_ID, michael@0: "Cannot find a content element in map for null IDs."); michael@0: nsIContent* content; michael@0: bool exists = GetContentMap().Get(aId, &content); michael@0: michael@0: if (exists) { michael@0: return content; michael@0: } else { michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: nsIScrollableFrame* michael@0: nsLayoutUtils::FindScrollableFrameFor(ViewID aId) michael@0: { michael@0: nsIContent* content = FindContentFor(aId); michael@0: if (!content) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIFrame* scrolledFrame = content->GetPrimaryFrame(); michael@0: if (scrolledFrame && content->OwnerDoc()->GetRootElement() == content) { michael@0: // The content is the root element of a subdocument, so return the root scrollable michael@0: // for the subdocument. michael@0: scrolledFrame = scrolledFrame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: } michael@0: return scrolledFrame ? scrolledFrame->GetScrollTargetFrame() : nullptr; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::GetDisplayPort(nsIContent* aContent, nsRect *aResult) michael@0: { michael@0: DisplayPortPropertyData* rectData = michael@0: static_cast(aContent->GetProperty(nsGkAtoms::DisplayPort)); michael@0: DisplayPortMarginsPropertyData* marginsData = michael@0: static_cast(aContent->GetProperty(nsGkAtoms::DisplayPortMargins)); michael@0: if (!rectData && !marginsData) { michael@0: return false; michael@0: } michael@0: michael@0: if (aResult) { michael@0: if (rectData && marginsData) { michael@0: // choose margins if equal priority michael@0: if (rectData->mPriority > marginsData->mPriority) { michael@0: marginsData = nullptr; michael@0: } else { michael@0: rectData = nullptr; michael@0: } michael@0: } michael@0: michael@0: if (rectData) { michael@0: *aResult = rectData->mRect; michael@0: } else { michael@0: nsRect* baseData = michael@0: static_cast(aContent->GetProperty(nsGkAtoms::DisplayPortBase)); michael@0: nsRect base; michael@0: if (baseData) { michael@0: base = *baseData; michael@0: } michael@0: michael@0: nsIFrame* frame = aContent->GetPrimaryFrame(); michael@0: if (frame) { michael@0: bool isRoot = false; michael@0: if (aContent->OwnerDoc()->GetRootElement() == aContent) { michael@0: // We want the scroll frame, the root scroll frame differs from all michael@0: // others in that the primary frame is not the scroll frame. michael@0: frame = frame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: isRoot = true; michael@0: } michael@0: michael@0: // first convert the base rect to layer pixels michael@0: nsPresContext* presContext = frame->PresContext(); michael@0: int32_t auPerDevPixel = presContext->AppUnitsPerDevPixel(); michael@0: gfxSize res = presContext->PresShell()->GetCumulativeResolution(); michael@0: gfxSize parentRes = res; michael@0: if (isRoot) { michael@0: // the base rect for root scroll frames is specified in the parent document michael@0: // coordinate space, so it doesn't include the local resolution. michael@0: gfxSize localRes = presContext->PresShell()->GetResolution(); michael@0: parentRes.width /= localRes.width; michael@0: parentRes.height /= localRes.height; michael@0: } michael@0: LayerRect rect; michael@0: rect.x = parentRes.width * NSAppUnitsToFloatPixels(base.x, auPerDevPixel); michael@0: rect.y = parentRes.height * NSAppUnitsToFloatPixels(base.y, auPerDevPixel); michael@0: rect.width = michael@0: parentRes.width * NSAppUnitsToFloatPixels(base.width, auPerDevPixel); michael@0: rect.height = michael@0: parentRes.height * NSAppUnitsToFloatPixels(base.height, auPerDevPixel); michael@0: michael@0: rect.Inflate(marginsData->mMargins); michael@0: michael@0: nsIScrollableFrame* scrollableFrame = frame->GetScrollTargetFrame(); michael@0: nsPoint scrollPos( michael@0: scrollableFrame ? scrollableFrame->GetScrollPosition() : nsPoint(0,0)); michael@0: if (marginsData->mAlignmentX > 0 || marginsData->mAlignmentY > 0) { michael@0: // Avoid division by zero. michael@0: if (marginsData->mAlignmentX == 0) { michael@0: marginsData->mAlignmentX = 1; michael@0: } michael@0: if (marginsData->mAlignmentY == 0) { michael@0: marginsData->mAlignmentY = 1; michael@0: } michael@0: michael@0: LayerPoint scrollPosLayer( michael@0: res.width * NSAppUnitsToFloatPixels(scrollPos.x, auPerDevPixel), michael@0: res.height * NSAppUnitsToFloatPixels(scrollPos.y, auPerDevPixel)); michael@0: rect += scrollPosLayer; michael@0: michael@0: // Inflate the rectangle by 1 so that we always push to the next tile michael@0: // boundary. This is desirable to stop from having a rectangle with a michael@0: // moving origin occasionally being smaller when it coincidentally lines michael@0: // up to tile boundaries. michael@0: rect.Inflate(1); michael@0: michael@0: float left = michael@0: marginsData->mAlignmentX * floor(rect.x / marginsData->mAlignmentX); michael@0: float top = michael@0: marginsData->mAlignmentY * floor(rect.y / marginsData->mAlignmentY); michael@0: float right = michael@0: marginsData->mAlignmentX * ceil(rect.XMost() / marginsData->mAlignmentX); michael@0: float bottom = michael@0: marginsData->mAlignmentY * ceil(rect.YMost() / marginsData->mAlignmentY); michael@0: rect = LayerRect(left, top, right - left, bottom - top); michael@0: rect -= scrollPosLayer; michael@0: } michael@0: michael@0: nsRect result; michael@0: result.x = NSFloatPixelsToAppUnits(rect.x / res.width, auPerDevPixel); michael@0: result.y = NSFloatPixelsToAppUnits(rect.y / res.height, auPerDevPixel); michael@0: result.width = michael@0: NSFloatPixelsToAppUnits(rect.width / res.width, auPerDevPixel); michael@0: result.height = michael@0: NSFloatPixelsToAppUnits(rect.height / res.height, auPerDevPixel); michael@0: michael@0: // Finally, clamp the display port to the expanded scrollable rect. michael@0: nsRect expandedScrollableRect = CalculateExpandedScrollableRect(frame); michael@0: result = expandedScrollableRect.Intersect(result + scrollPos) - scrollPos; michael@0: michael@0: *aResult = result; michael@0: } michael@0: } michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::SetDisplayPortMargins(nsIContent* aContent, michael@0: nsIPresShell* aPresShell, michael@0: const LayerMargin& aMargins, michael@0: uint32_t aAlignmentX, michael@0: uint32_t aAlignmentY, michael@0: uint32_t aPriority, michael@0: RepaintMode aRepaintMode) michael@0: { michael@0: DisplayPortMarginsPropertyData* currentData = michael@0: static_cast(aContent->GetProperty(nsGkAtoms::DisplayPortMargins)); michael@0: if (currentData && currentData->mPriority > aPriority) { michael@0: return; michael@0: } michael@0: michael@0: aContent->SetProperty(nsGkAtoms::DisplayPortMargins, michael@0: new DisplayPortMarginsPropertyData( michael@0: aMargins, aAlignmentX, aAlignmentY, aPriority), michael@0: nsINode::DeleteProperty); michael@0: michael@0: nsIFrame* rootScrollFrame = aPresShell->GetRootScrollFrame(); michael@0: if (rootScrollFrame && aContent == rootScrollFrame->GetContent()) { michael@0: // We are setting a root displayport for a document. michael@0: // The pres shell needs a special flag set. michael@0: aPresShell->SetIgnoreViewportScrolling(true); michael@0: } michael@0: michael@0: if (aRepaintMode == RepaintMode::Repaint) { michael@0: nsIFrame* rootFrame = aPresShell->FrameManager()->GetRootFrame(); michael@0: if (rootFrame) { michael@0: rootFrame->SchedulePaint(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::SetDisplayPortBase(nsIContent* aContent, const nsRect& aBase) michael@0: { michael@0: aContent->SetProperty(nsGkAtoms::DisplayPortBase, new nsRect(aBase), michael@0: nsINode::DeleteProperty); michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::SetDisplayPortBaseIfNotSet(nsIContent* aContent, const nsRect& aBase) michael@0: { michael@0: if (!aContent->GetProperty(nsGkAtoms::DisplayPortBase)) { michael@0: SetDisplayPortBase(aContent, aBase); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::GetCriticalDisplayPort(nsIContent* aContent, nsRect* aResult) michael@0: { michael@0: void* property = aContent->GetProperty(nsGkAtoms::CriticalDisplayPort); michael@0: if (!property) { michael@0: return false; michael@0: } michael@0: michael@0: if (aResult) { michael@0: *aResult = *static_cast(property); michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::LastContinuationWithChild(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "NULL frame pointer"); michael@0: aFrame = aFrame->LastContinuation(); michael@0: while (!aFrame->GetFirstPrincipalChild() && michael@0: aFrame->GetPrevContinuation()) { michael@0: aFrame = aFrame->GetPrevContinuation(); michael@0: } michael@0: return aFrame; michael@0: } michael@0: michael@0: /** michael@0: * GetFirstChildFrame returns the first "real" child frame of a michael@0: * given frame. It will descend down into pseudo-frames (unless the michael@0: * pseudo-frame is the :before generated frame). michael@0: * @param aFrame the frame michael@0: * @param aFrame the frame's content node michael@0: */ michael@0: static nsIFrame* michael@0: GetFirstChildFrame(nsIFrame* aFrame, michael@0: nsIContent* aContent) michael@0: { michael@0: NS_PRECONDITION(aFrame, "NULL frame pointer"); michael@0: michael@0: // Get the first child frame michael@0: nsIFrame* childFrame = aFrame->GetFirstPrincipalChild(); michael@0: michael@0: // If the child frame is a pseudo-frame, then return its first child. michael@0: // Note that the frame we create for the generated content is also a michael@0: // pseudo-frame and so don't drill down in that case michael@0: if (childFrame && michael@0: childFrame->IsPseudoFrame(aContent) && michael@0: !childFrame->IsGeneratedContentFrame()) { michael@0: return GetFirstChildFrame(childFrame, aContent); michael@0: } michael@0: michael@0: return childFrame; michael@0: } michael@0: michael@0: /** michael@0: * GetLastChildFrame returns the last "real" child frame of a michael@0: * given frame. It will descend down into pseudo-frames (unless the michael@0: * pseudo-frame is the :after generated frame). michael@0: * @param aFrame the frame michael@0: * @param aFrame the frame's content node michael@0: */ michael@0: static nsIFrame* michael@0: GetLastChildFrame(nsIFrame* aFrame, michael@0: nsIContent* aContent) michael@0: { michael@0: NS_PRECONDITION(aFrame, "NULL frame pointer"); michael@0: michael@0: // Get the last continuation frame that's a parent michael@0: nsIFrame* lastParentContinuation = michael@0: nsLayoutUtils::LastContinuationWithChild(aFrame); michael@0: nsIFrame* lastChildFrame = michael@0: lastParentContinuation->GetLastChild(nsIFrame::kPrincipalList); michael@0: if (lastChildFrame) { michael@0: // Get the frame's first continuation. This matters in case the frame has michael@0: // been continued across multiple lines or split by BiDi resolution. michael@0: lastChildFrame = lastChildFrame->FirstContinuation(); michael@0: michael@0: // If the last child frame is a pseudo-frame, then return its last child. michael@0: // Note that the frame we create for the generated content is also a michael@0: // pseudo-frame and so don't drill down in that case michael@0: if (lastChildFrame && michael@0: lastChildFrame->IsPseudoFrame(aContent) && michael@0: !lastChildFrame->IsGeneratedContentFrame()) { michael@0: return GetLastChildFrame(lastChildFrame, aContent); michael@0: } michael@0: michael@0: return lastChildFrame; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: //static michael@0: FrameChildListID michael@0: nsLayoutUtils::GetChildListNameFor(nsIFrame* aChildFrame) michael@0: { michael@0: nsIFrame::ChildListID id = nsIFrame::kPrincipalList; michael@0: michael@0: if (aChildFrame->GetStateBits() & NS_FRAME_IS_OVERFLOW_CONTAINER) { michael@0: nsIFrame* pif = aChildFrame->GetPrevInFlow(); michael@0: if (pif->GetParent() == aChildFrame->GetParent()) { michael@0: id = nsIFrame::kExcessOverflowContainersList; michael@0: } michael@0: else { michael@0: id = nsIFrame::kOverflowContainersList; michael@0: } michael@0: } michael@0: // See if the frame is moved out of the flow michael@0: else if (aChildFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) { michael@0: // Look at the style information to tell michael@0: const nsStyleDisplay* disp = aChildFrame->StyleDisplay(); michael@0: michael@0: if (NS_STYLE_POSITION_ABSOLUTE == disp->mPosition) { michael@0: id = nsIFrame::kAbsoluteList; michael@0: } else if (NS_STYLE_POSITION_FIXED == disp->mPosition) { michael@0: if (nsLayoutUtils::IsReallyFixedPos(aChildFrame)) { michael@0: id = nsIFrame::kFixedList; michael@0: } else { michael@0: id = nsIFrame::kAbsoluteList; michael@0: } michael@0: #ifdef MOZ_XUL michael@0: } else if (NS_STYLE_DISPLAY_POPUP == disp->mDisplay) { michael@0: // Out-of-flows that are DISPLAY_POPUP must be kids of the root popup set michael@0: #ifdef DEBUG michael@0: nsIFrame* parent = aChildFrame->GetParent(); michael@0: NS_ASSERTION(parent && parent->GetType() == nsGkAtoms::popupSetFrame, michael@0: "Unexpected parent"); michael@0: #endif // DEBUG michael@0: michael@0: id = nsIFrame::kPopupList; michael@0: #endif // MOZ_XUL michael@0: } else { michael@0: NS_ASSERTION(aChildFrame->IsFloating(), "not a floated frame"); michael@0: id = nsIFrame::kFloatList; michael@0: } michael@0: michael@0: } else { michael@0: nsIAtom* childType = aChildFrame->GetType(); michael@0: if (nsGkAtoms::menuPopupFrame == childType) { michael@0: nsIFrame* parent = aChildFrame->GetParent(); michael@0: MOZ_ASSERT(parent, "nsMenuPopupFrame can't be the root frame"); michael@0: if (parent) { michael@0: if (parent->GetType() == nsGkAtoms::popupSetFrame) { michael@0: id = nsIFrame::kPopupList; michael@0: } else { michael@0: nsIFrame* firstPopup = parent->GetFirstChild(nsIFrame::kPopupList); michael@0: MOZ_ASSERT(!firstPopup || !firstPopup->GetNextSibling(), michael@0: "We assume popupList only has one child, but it has more."); michael@0: id = firstPopup == aChildFrame michael@0: ? nsIFrame::kPopupList michael@0: : nsIFrame::kPrincipalList; michael@0: } michael@0: } else { michael@0: id = nsIFrame::kPrincipalList; michael@0: } michael@0: } else if (nsGkAtoms::tableColGroupFrame == childType) { michael@0: id = nsIFrame::kColGroupList; michael@0: } else if (nsGkAtoms::tableCaptionFrame == childType) { michael@0: id = nsIFrame::kCaptionList; michael@0: } else { michael@0: id = nsIFrame::kPrincipalList; michael@0: } michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: // Verify that the frame is actually in that child list or in the michael@0: // corresponding overflow list. michael@0: nsIFrame* parent = aChildFrame->GetParent(); michael@0: bool found = parent->GetChildList(id).ContainsFrame(aChildFrame); michael@0: if (!found) { michael@0: if (!(aChildFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW)) { michael@0: found = parent->GetChildList(nsIFrame::kOverflowList) michael@0: .ContainsFrame(aChildFrame); michael@0: } michael@0: else if (aChildFrame->IsFloating()) { michael@0: found = parent->GetChildList(nsIFrame::kOverflowOutOfFlowList) michael@0: .ContainsFrame(aChildFrame); michael@0: if (!found) { michael@0: found = parent->GetChildList(nsIFrame::kPushedFloatsList) michael@0: .ContainsFrame(aChildFrame); michael@0: } michael@0: } michael@0: // else it's positioned and should have been on the 'id' child list. michael@0: NS_POSTCONDITION(found, "not in child list"); michael@0: } michael@0: #endif michael@0: michael@0: return id; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::GetBeforeFrame(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "NULL frame pointer"); michael@0: NS_ASSERTION(!aFrame->GetPrevContinuation(), michael@0: "aFrame must be first continuation"); michael@0: michael@0: nsIFrame* cif = aFrame->GetContentInsertionFrame(); michael@0: nsIFrame* firstFrame = GetFirstChildFrame(cif, aFrame->GetContent()); michael@0: michael@0: if (firstFrame && IsGeneratedContentFor(nullptr, firstFrame, michael@0: nsCSSPseudoElements::before)) { michael@0: return firstFrame; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::GetAfterFrame(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "NULL frame pointer"); michael@0: michael@0: nsIFrame* cif = aFrame->GetContentInsertionFrame(); michael@0: nsIFrame* lastFrame = GetLastChildFrame(cif, aFrame->GetContent()); michael@0: michael@0: if (lastFrame && IsGeneratedContentFor(nullptr, lastFrame, michael@0: nsCSSPseudoElements::after)) { michael@0: return lastFrame; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::GetClosestFrameOfType(nsIFrame* aFrame, nsIAtom* aFrameType) michael@0: { michael@0: for (nsIFrame* frame = aFrame; frame; frame = frame->GetParent()) { michael@0: if (frame->GetType() == aFrameType) { michael@0: return frame; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::GetStyleFrame(nsIFrame* aFrame) michael@0: { michael@0: if (aFrame->GetType() == nsGkAtoms::tableOuterFrame) { michael@0: nsIFrame* inner = aFrame->GetFirstPrincipalChild(); michael@0: NS_ASSERTION(inner, "Outer table must have an inner"); michael@0: return inner; michael@0: } michael@0: michael@0: return aFrame; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetStyleFrame(const nsIContent* aContent) michael@0: { michael@0: nsIFrame *frame = aContent->GetPrimaryFrame(); michael@0: if (!frame) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return nsLayoutUtils::GetStyleFrame(frame); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetFloatFromPlaceholder(nsIFrame* aFrame) { michael@0: NS_ASSERTION(nsGkAtoms::placeholderFrame == aFrame->GetType(), michael@0: "Must have a placeholder here"); michael@0: if (aFrame->GetStateBits() & PLACEHOLDER_FOR_FLOAT) { michael@0: nsIFrame *outOfFlowFrame = michael@0: nsPlaceholderFrame::GetRealFrameForPlaceholder(aFrame); michael@0: NS_ASSERTION(outOfFlowFrame->IsFloating(), michael@0: "How did that happen?"); michael@0: return outOfFlowFrame; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: nsLayoutUtils::IsGeneratedContentFor(nsIContent* aContent, michael@0: nsIFrame* aFrame, michael@0: nsIAtom* aPseudoElement) michael@0: { michael@0: NS_PRECONDITION(aFrame, "Must have a frame"); michael@0: NS_PRECONDITION(aPseudoElement, "Must have a pseudo name"); michael@0: michael@0: if (!aFrame->IsGeneratedContentFrame()) { michael@0: return false; michael@0: } michael@0: nsIFrame* parent = aFrame->GetParent(); michael@0: NS_ASSERTION(parent, "Generated content can't be root frame"); michael@0: if (parent->IsGeneratedContentFrame()) { michael@0: // Not the root of the generated content michael@0: return false; michael@0: } michael@0: michael@0: if (aContent && parent->GetContent() != aContent) { michael@0: return false; michael@0: } michael@0: michael@0: return (aFrame->GetContent()->Tag() == nsGkAtoms::mozgeneratedcontentbefore) == michael@0: (aPseudoElement == nsCSSPseudoElements::before); michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::GetCrossDocParentFrame(const nsIFrame* aFrame, michael@0: nsPoint* aExtraOffset) michael@0: { michael@0: nsIFrame* p = aFrame->GetParent(); michael@0: if (p) michael@0: return p; michael@0: michael@0: nsView* v = aFrame->GetView(); michael@0: if (!v) michael@0: return nullptr; michael@0: v = v->GetParent(); // anonymous inner view michael@0: if (!v) michael@0: return nullptr; michael@0: if (aExtraOffset) { michael@0: *aExtraOffset += v->GetPosition(); michael@0: } michael@0: v = v->GetParent(); // subdocumentframe's view michael@0: return v ? v->GetFrame() : nullptr; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: nsLayoutUtils::IsProperAncestorFrameCrossDoc(nsIFrame* aAncestorFrame, nsIFrame* aFrame, michael@0: nsIFrame* aCommonAncestor) michael@0: { michael@0: if (aFrame == aAncestorFrame) michael@0: return false; michael@0: return IsAncestorFrameCrossDoc(aAncestorFrame, aFrame, aCommonAncestor); michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: nsLayoutUtils::IsAncestorFrameCrossDoc(const nsIFrame* aAncestorFrame, const nsIFrame* aFrame, michael@0: const nsIFrame* aCommonAncestor) michael@0: { michael@0: for (const nsIFrame* f = aFrame; f != aCommonAncestor; michael@0: f = GetCrossDocParentFrame(f)) { michael@0: if (f == aAncestorFrame) michael@0: return true; michael@0: } michael@0: return aCommonAncestor == aAncestorFrame; michael@0: } michael@0: michael@0: // static michael@0: bool michael@0: nsLayoutUtils::IsProperAncestorFrame(nsIFrame* aAncestorFrame, nsIFrame* aFrame, michael@0: nsIFrame* aCommonAncestor) michael@0: { michael@0: if (aFrame == aAncestorFrame) michael@0: return false; michael@0: for (nsIFrame* f = aFrame; f != aCommonAncestor; f = f->GetParent()) { michael@0: if (f == aAncestorFrame) michael@0: return true; michael@0: } michael@0: return aCommonAncestor == aAncestorFrame; michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: nsLayoutUtils::DoCompareTreePosition(nsIContent* aContent1, michael@0: nsIContent* aContent2, michael@0: int32_t aIf1Ancestor, michael@0: int32_t aIf2Ancestor, michael@0: const nsIContent* aCommonAncestor) michael@0: { michael@0: NS_PRECONDITION(aContent1, "aContent1 must not be null"); michael@0: NS_PRECONDITION(aContent2, "aContent2 must not be null"); michael@0: michael@0: nsAutoTArray content1Ancestors; michael@0: nsINode* c1; michael@0: for (c1 = aContent1; c1 && c1 != aCommonAncestor; c1 = c1->GetParentNode()) { michael@0: content1Ancestors.AppendElement(c1); michael@0: } michael@0: if (!c1 && aCommonAncestor) { michael@0: // So, it turns out aCommonAncestor was not an ancestor of c1. Oops. michael@0: // Never mind. We can continue as if aCommonAncestor was null. michael@0: aCommonAncestor = nullptr; michael@0: } michael@0: michael@0: nsAutoTArray content2Ancestors; michael@0: nsINode* c2; michael@0: for (c2 = aContent2; c2 && c2 != aCommonAncestor; c2 = c2->GetParentNode()) { michael@0: content2Ancestors.AppendElement(c2); michael@0: } michael@0: if (!c2 && aCommonAncestor) { michael@0: // So, it turns out aCommonAncestor was not an ancestor of c2. michael@0: // We need to retry with no common ancestor hint. michael@0: return DoCompareTreePosition(aContent1, aContent2, michael@0: aIf1Ancestor, aIf2Ancestor, nullptr); michael@0: } michael@0: michael@0: int last1 = content1Ancestors.Length() - 1; michael@0: int last2 = content2Ancestors.Length() - 1; michael@0: nsINode* content1Ancestor = nullptr; michael@0: nsINode* content2Ancestor = nullptr; michael@0: while (last1 >= 0 && last2 >= 0 michael@0: && ((content1Ancestor = content1Ancestors.ElementAt(last1)) == michael@0: (content2Ancestor = content2Ancestors.ElementAt(last2)))) { michael@0: last1--; michael@0: last2--; michael@0: } michael@0: michael@0: if (last1 < 0) { michael@0: if (last2 < 0) { michael@0: NS_ASSERTION(aContent1 == aContent2, "internal error?"); michael@0: return 0; michael@0: } michael@0: // aContent1 is an ancestor of aContent2 michael@0: return aIf1Ancestor; michael@0: } michael@0: michael@0: if (last2 < 0) { michael@0: // aContent2 is an ancestor of aContent1 michael@0: return aIf2Ancestor; michael@0: } michael@0: michael@0: // content1Ancestor != content2Ancestor, so they must be siblings with the same parent michael@0: nsINode* parent = content1Ancestor->GetParentNode(); michael@0: #ifdef DEBUG michael@0: // TODO: remove the uglyness, see bug 598468. michael@0: NS_ASSERTION(gPreventAssertInCompareTreePosition || parent, michael@0: "no common ancestor at all???"); michael@0: #endif // DEBUG michael@0: if (!parent) { // different documents?? michael@0: return 0; michael@0: } michael@0: michael@0: int32_t index1 = parent->IndexOf(content1Ancestor); michael@0: int32_t index2 = parent->IndexOf(content2Ancestor); michael@0: if (index1 < 0 || index2 < 0) { michael@0: // one of them must be anonymous; we can't determine the order michael@0: return 0; michael@0: } michael@0: michael@0: return index1 - index2; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* michael@0: nsLayoutUtils::FillAncestors(nsIFrame* aFrame, michael@0: nsIFrame* aStopAtAncestor, michael@0: nsTArray* aAncestors) michael@0: { michael@0: while (aFrame && aFrame != aStopAtAncestor) { michael@0: aAncestors->AppendElement(aFrame); michael@0: aFrame = nsLayoutUtils::GetParentOrPlaceholderFor(aFrame); michael@0: } michael@0: return aFrame; michael@0: } michael@0: michael@0: // Return true if aFrame1 is after aFrame2 michael@0: static bool IsFrameAfter(nsIFrame* aFrame1, nsIFrame* aFrame2) michael@0: { michael@0: nsIFrame* f = aFrame2; michael@0: do { michael@0: f = f->GetNextSibling(); michael@0: if (f == aFrame1) michael@0: return true; michael@0: } while (f); michael@0: return false; michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: nsLayoutUtils::DoCompareTreePosition(nsIFrame* aFrame1, michael@0: nsIFrame* aFrame2, michael@0: int32_t aIf1Ancestor, michael@0: int32_t aIf2Ancestor, michael@0: nsIFrame* aCommonAncestor) michael@0: { michael@0: NS_PRECONDITION(aFrame1, "aFrame1 must not be null"); michael@0: NS_PRECONDITION(aFrame2, "aFrame2 must not be null"); michael@0: michael@0: nsAutoTArray frame2Ancestors; michael@0: nsIFrame* nonCommonAncestor = michael@0: FillAncestors(aFrame2, aCommonAncestor, &frame2Ancestors); michael@0: michael@0: return DoCompareTreePosition(aFrame1, aFrame2, frame2Ancestors, michael@0: aIf1Ancestor, aIf2Ancestor, michael@0: nonCommonAncestor ? aCommonAncestor : nullptr); michael@0: } michael@0: michael@0: // static michael@0: int32_t michael@0: nsLayoutUtils::DoCompareTreePosition(nsIFrame* aFrame1, michael@0: nsIFrame* aFrame2, michael@0: nsTArray& aFrame2Ancestors, michael@0: int32_t aIf1Ancestor, michael@0: int32_t aIf2Ancestor, michael@0: nsIFrame* aCommonAncestor) michael@0: { michael@0: NS_PRECONDITION(aFrame1, "aFrame1 must not be null"); michael@0: NS_PRECONDITION(aFrame2, "aFrame2 must not be null"); michael@0: michael@0: nsPresContext* presContext = aFrame1->PresContext(); michael@0: if (presContext != aFrame2->PresContext()) { michael@0: NS_ERROR("no common ancestor at all, different documents"); michael@0: return 0; michael@0: } michael@0: michael@0: nsAutoTArray frame1Ancestors; michael@0: if (aCommonAncestor && michael@0: !FillAncestors(aFrame1, aCommonAncestor, &frame1Ancestors)) { michael@0: // We reached the root of the frame tree ... if aCommonAncestor was set, michael@0: // it is wrong michael@0: return DoCompareTreePosition(aFrame1, aFrame2, michael@0: aIf1Ancestor, aIf2Ancestor, nullptr); michael@0: } michael@0: michael@0: int32_t last1 = int32_t(frame1Ancestors.Length()) - 1; michael@0: int32_t last2 = int32_t(aFrame2Ancestors.Length()) - 1; michael@0: while (last1 >= 0 && last2 >= 0 && michael@0: frame1Ancestors[last1] == aFrame2Ancestors[last2]) { michael@0: last1--; michael@0: last2--; michael@0: } michael@0: michael@0: if (last1 < 0) { michael@0: if (last2 < 0) { michael@0: NS_ASSERTION(aFrame1 == aFrame2, "internal error?"); michael@0: return 0; michael@0: } michael@0: // aFrame1 is an ancestor of aFrame2 michael@0: return aIf1Ancestor; michael@0: } michael@0: michael@0: if (last2 < 0) { michael@0: // aFrame2 is an ancestor of aFrame1 michael@0: return aIf2Ancestor; michael@0: } michael@0: michael@0: nsIFrame* ancestor1 = frame1Ancestors[last1]; michael@0: nsIFrame* ancestor2 = aFrame2Ancestors[last2]; michael@0: // Now we should be able to walk sibling chains to find which one is first michael@0: if (IsFrameAfter(ancestor2, ancestor1)) michael@0: return -1; michael@0: if (IsFrameAfter(ancestor1, ancestor2)) michael@0: return 1; michael@0: NS_WARNING("Frames were in different child lists???"); michael@0: return 0; michael@0: } michael@0: michael@0: // static michael@0: nsIFrame* nsLayoutUtils::GetLastSibling(nsIFrame* aFrame) { michael@0: if (!aFrame) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIFrame* next; michael@0: while ((next = aFrame->GetNextSibling()) != nullptr) { michael@0: aFrame = next; michael@0: } michael@0: return aFrame; michael@0: } michael@0: michael@0: // static michael@0: nsView* michael@0: nsLayoutUtils::FindSiblingViewFor(nsView* aParentView, nsIFrame* aFrame) { michael@0: nsIFrame* parentViewFrame = aParentView->GetFrame(); michael@0: nsIContent* parentViewContent = parentViewFrame ? parentViewFrame->GetContent() : nullptr; michael@0: for (nsView* insertBefore = aParentView->GetFirstChild(); insertBefore; michael@0: insertBefore = insertBefore->GetNextSibling()) { michael@0: nsIFrame* f = insertBefore->GetFrame(); michael@0: if (!f) { michael@0: // this view could be some anonymous view attached to a meaningful parent michael@0: for (nsView* searchView = insertBefore->GetParent(); searchView; michael@0: searchView = searchView->GetParent()) { michael@0: f = searchView->GetFrame(); michael@0: if (f) { michael@0: break; michael@0: } michael@0: } michael@0: NS_ASSERTION(f, "Can't find a frame anywhere!"); michael@0: } michael@0: if (!f || !aFrame->GetContent() || !f->GetContent() || michael@0: CompareTreePosition(aFrame->GetContent(), f->GetContent(), parentViewContent) > 0) { michael@0: // aFrame's content is after f's content (or we just don't know), michael@0: // so put our view before f's view michael@0: return insertBefore; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: //static michael@0: nsIScrollableFrame* michael@0: nsLayoutUtils::GetScrollableFrameFor(const nsIFrame *aScrolledFrame) michael@0: { michael@0: nsIFrame *frame = aScrolledFrame->GetParent(); michael@0: nsIScrollableFrame *sf = do_QueryFrame(frame); michael@0: return sf; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::SetFixedPositionLayerData(Layer* aLayer, michael@0: const nsIFrame* aViewportFrame, michael@0: const nsRect& aAnchorRect, michael@0: const nsIFrame* aFixedPosFrame, michael@0: nsPresContext* aPresContext, michael@0: const ContainerLayerParameters& aContainerParameters) { michael@0: // Find out the rect of the viewport frame relative to the reference frame. michael@0: // This, in conjunction with the container scale, will correspond to the michael@0: // coordinate-space of the built layer. michael@0: float factor = aPresContext->AppUnitsPerDevPixel(); michael@0: Rect anchorRect(NSAppUnitsToFloatPixels(aAnchorRect.x, factor) * michael@0: aContainerParameters.mXScale, michael@0: NSAppUnitsToFloatPixels(aAnchorRect.y, factor) * michael@0: aContainerParameters.mYScale, michael@0: NSAppUnitsToFloatPixels(aAnchorRect.width, factor) * michael@0: aContainerParameters.mXScale, michael@0: NSAppUnitsToFloatPixels(aAnchorRect.height, factor) * michael@0: aContainerParameters.mYScale); michael@0: // Need to transform anchorRect from the container layer's coordinate system michael@0: // into aLayer's coordinate system. michael@0: Matrix transform2d; michael@0: if (aLayer->GetTransform().Is2D(&transform2d)) { michael@0: transform2d.Invert(); michael@0: anchorRect = transform2d.TransformBounds(anchorRect); michael@0: } else { michael@0: NS_ERROR("3D transform found between fixedpos content and its viewport (should never happen)"); michael@0: anchorRect = Rect(0,0,0,0); michael@0: } michael@0: michael@0: // Work out the anchor point for this fixed position layer. We assume that michael@0: // any positioning set (left/top/right/bottom) indicates that the michael@0: // corresponding side of its container should be the anchor point, michael@0: // defaulting to top-left. michael@0: LayerPoint anchor(anchorRect.x, anchorRect.y); michael@0: // Make sure the layer is aware of any fixed position margins that have michael@0: // been set. michael@0: nsMargin fixedMargins = aPresContext->PresShell()->GetContentDocumentFixedPositionMargins(); michael@0: LayerMargin fixedLayerMargins(NSAppUnitsToFloatPixels(fixedMargins.top, factor) * michael@0: aContainerParameters.mYScale, michael@0: NSAppUnitsToFloatPixels(fixedMargins.right, factor) * michael@0: aContainerParameters.mXScale, michael@0: NSAppUnitsToFloatPixels(fixedMargins.bottom, factor) * michael@0: aContainerParameters.mYScale, michael@0: NSAppUnitsToFloatPixels(fixedMargins.left, factor) * michael@0: aContainerParameters.mXScale); michael@0: michael@0: if (aFixedPosFrame != aViewportFrame) { michael@0: const nsStylePosition* position = aFixedPosFrame->StylePosition(); michael@0: if (position->mOffset.GetRightUnit() != eStyleUnit_Auto) { michael@0: if (position->mOffset.GetLeftUnit() != eStyleUnit_Auto) { michael@0: anchor.x = anchorRect.x + anchorRect.width / 2.f; michael@0: } else { michael@0: anchor.x = anchorRect.XMost(); michael@0: } michael@0: } michael@0: if (position->mOffset.GetBottomUnit() != eStyleUnit_Auto) { michael@0: if (position->mOffset.GetTopUnit() != eStyleUnit_Auto) { michael@0: anchor.y = anchorRect.y + anchorRect.height / 2.f; michael@0: } else { michael@0: anchor.y = anchorRect.YMost(); michael@0: } michael@0: } michael@0: michael@0: // If the frame is auto-positioned on either axis, set the top/left layer michael@0: // margins to -1, to indicate to the compositor that this layer is michael@0: // unaffected by fixed margins. michael@0: if (position->mOffset.GetLeftUnit() == eStyleUnit_Auto && michael@0: position->mOffset.GetRightUnit() == eStyleUnit_Auto) { michael@0: fixedLayerMargins.left = -1; michael@0: } michael@0: if (position->mOffset.GetTopUnit() == eStyleUnit_Auto && michael@0: position->mOffset.GetBottomUnit() == eStyleUnit_Auto) { michael@0: fixedLayerMargins.top = -1; michael@0: } michael@0: } michael@0: michael@0: aLayer->SetFixedPositionAnchor(anchor); michael@0: aLayer->SetFixedPositionMargins(fixedLayerMargins); michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::ViewportHasDisplayPort(nsPresContext* aPresContext, nsRect* aDisplayPort) michael@0: { michael@0: nsIFrame* rootScrollFrame = michael@0: aPresContext->PresShell()->GetRootScrollFrame(); michael@0: return rootScrollFrame && michael@0: nsLayoutUtils::GetDisplayPort(rootScrollFrame->GetContent(), aDisplayPort); michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::IsFixedPosFrameInDisplayPort(const nsIFrame* aFrame, nsRect* aDisplayPort) michael@0: { michael@0: // Fixed-pos frames are parented by the viewport frame or the page content frame. michael@0: // We'll assume that printing/print preview don't have displayports for their michael@0: // pages! michael@0: nsIFrame* parent = aFrame->GetParent(); michael@0: if (!parent || parent->GetParent() || michael@0: aFrame->StyleDisplay()->mPosition != NS_STYLE_POSITION_FIXED) { michael@0: return false; michael@0: } michael@0: return ViewportHasDisplayPort(aFrame->PresContext(), aDisplayPort); michael@0: } michael@0: michael@0: static nsIFrame* michael@0: GetAnimatedGeometryRootForFrame(nsIFrame* aFrame, michael@0: const nsIFrame* aStopAtAncestor) michael@0: { michael@0: nsIFrame* f = aFrame; michael@0: nsIFrame* stickyFrame = nullptr; michael@0: while (f != aStopAtAncestor) { michael@0: if (nsLayoutUtils::IsPopup(f)) michael@0: break; michael@0: if (ActiveLayerTracker::IsOffsetOrMarginStyleAnimated(f)) michael@0: break; michael@0: if (!f->GetParent() && michael@0: nsLayoutUtils::ViewportHasDisplayPort(f->PresContext())) { michael@0: // Viewport frames in a display port need to be animated geometry roots michael@0: // for background-attachment:fixed elements. michael@0: break; michael@0: } michael@0: nsIFrame* parent = nsLayoutUtils::GetCrossDocParentFrame(f); michael@0: if (!parent) michael@0: break; michael@0: nsIAtom* parentType = parent->GetType(); michael@0: #ifdef ANDROID michael@0: // Treat the slider thumb as being as an active scrolled root michael@0: // on mobile so that it can move without repainting. michael@0: if (parentType == nsGkAtoms::sliderFrame) michael@0: break; michael@0: #endif michael@0: // Sticky frames are active if their nearest scrollable frame michael@0: // is also active, just keep a record of sticky frames that we michael@0: // encounter for now. michael@0: if (f->StyleDisplay()->mPosition == NS_STYLE_POSITION_STICKY && michael@0: !stickyFrame) { michael@0: stickyFrame = f; michael@0: } michael@0: if (parentType == nsGkAtoms::scrollFrame) { michael@0: nsIScrollableFrame* sf = do_QueryFrame(parent); michael@0: if (sf->IsScrollingActive() && sf->GetScrolledFrame() == f) { michael@0: // If we found a sticky frame inside this active scroll frame, michael@0: // then use that. Otherwise use the scroll frame. michael@0: if (stickyFrame) { michael@0: return stickyFrame; michael@0: } michael@0: return f; michael@0: } else { michael@0: stickyFrame = nullptr; michael@0: } michael@0: } michael@0: // Fixed-pos frames are parented by the viewport frame, which has no parent michael@0: if (nsLayoutUtils::IsFixedPosFrameInDisplayPort(f)) { michael@0: return f; michael@0: } michael@0: f = parent; michael@0: } michael@0: return f; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetAnimatedGeometryRootFor(nsDisplayItem* aItem, michael@0: nsDisplayListBuilder* aBuilder) michael@0: { michael@0: nsIFrame* f = aItem->Frame(); michael@0: if (aItem->GetType() == nsDisplayItem::TYPE_SCROLL_LAYER) { michael@0: nsDisplayScrollLayer* scrollLayerItem = michael@0: static_cast(aItem); michael@0: nsIFrame* scrolledFrame = scrollLayerItem->GetScrolledFrame(); michael@0: return GetAnimatedGeometryRootForFrame(scrolledFrame, michael@0: aBuilder->FindReferenceFrameFor(scrolledFrame)); michael@0: } michael@0: if (aItem->ShouldFixToViewport(aBuilder)) { michael@0: // Make its active scrolled root be the active scrolled root of michael@0: // the enclosing viewport, since it shouldn't be scrolled by scrolled michael@0: // frames in its document. InvalidateFixedBackgroundFramesFromList in michael@0: // nsGfxScrollFrame will not repaint this item when scrolling occurs. michael@0: nsIFrame* viewportFrame = michael@0: nsLayoutUtils::GetClosestFrameOfType(f, nsGkAtoms::viewportFrame); michael@0: NS_ASSERTION(viewportFrame, "no viewport???"); michael@0: return GetAnimatedGeometryRootForFrame(viewportFrame, michael@0: aBuilder->FindReferenceFrameFor(viewportFrame)); michael@0: } michael@0: return GetAnimatedGeometryRootForFrame(f, aItem->ReferenceFrame()); michael@0: } michael@0: michael@0: // static michael@0: nsIScrollableFrame* michael@0: nsLayoutUtils::GetNearestScrollableFrameForDirection(nsIFrame* aFrame, michael@0: Direction aDirection) michael@0: { michael@0: NS_ASSERTION(aFrame, "GetNearestScrollableFrameForDirection expects a non-null frame"); michael@0: for (nsIFrame* f = aFrame; f; f = nsLayoutUtils::GetCrossDocParentFrame(f)) { michael@0: nsIScrollableFrame* scrollableFrame = do_QueryFrame(f); michael@0: if (scrollableFrame) { michael@0: ScrollbarStyles ss = scrollableFrame->GetScrollbarStyles(); michael@0: uint32_t directions = scrollableFrame->GetPerceivedScrollingDirections(); michael@0: if (aDirection == eVertical ? michael@0: (ss.mVertical != NS_STYLE_OVERFLOW_HIDDEN && michael@0: (directions & nsIScrollableFrame::VERTICAL)) : michael@0: (ss.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN && michael@0: (directions & nsIScrollableFrame::HORIZONTAL))) michael@0: return scrollableFrame; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: nsIScrollableFrame* michael@0: nsLayoutUtils::GetNearestScrollableFrame(nsIFrame* aFrame, uint32_t aFlags) michael@0: { michael@0: NS_ASSERTION(aFrame, "GetNearestScrollableFrame expects a non-null frame"); michael@0: for (nsIFrame* f = aFrame; f; f = (aFlags & SCROLLABLE_SAME_DOC) ? michael@0: f->GetParent() : nsLayoutUtils::GetCrossDocParentFrame(f)) { michael@0: nsIScrollableFrame* scrollableFrame = do_QueryFrame(f); michael@0: if (scrollableFrame) { michael@0: ScrollbarStyles ss = scrollableFrame->GetScrollbarStyles(); michael@0: if ((aFlags & SCROLLABLE_INCLUDE_HIDDEN) || michael@0: ss.mVertical != NS_STYLE_OVERFLOW_HIDDEN || michael@0: ss.mHorizontal != NS_STYLE_OVERFLOW_HIDDEN) michael@0: return scrollableFrame; michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: // static michael@0: nsRect michael@0: nsLayoutUtils::GetScrolledRect(nsIFrame* aScrolledFrame, michael@0: const nsRect& aScrolledFrameOverflowArea, michael@0: const nsSize& aScrollPortSize, michael@0: uint8_t aDirection) michael@0: { michael@0: nscoord x1 = aScrolledFrameOverflowArea.x, michael@0: x2 = aScrolledFrameOverflowArea.XMost(), michael@0: y1 = aScrolledFrameOverflowArea.y, michael@0: y2 = aScrolledFrameOverflowArea.YMost(); michael@0: if (y1 < 0) { michael@0: y1 = 0; michael@0: } michael@0: if (aDirection != NS_STYLE_DIRECTION_RTL) { michael@0: if (x1 < 0) { michael@0: x1 = 0; michael@0: } michael@0: } else { michael@0: if (x2 > aScrollPortSize.width) { michael@0: x2 = aScrollPortSize.width; michael@0: } michael@0: // When the scrolled frame chooses a size larger than its available width (because michael@0: // its padding alone is larger than the available width), we need to keep the michael@0: // start-edge of the scroll frame anchored to the start-edge of the scrollport. michael@0: // When the scrolled frame is RTL, this means moving it in our left-based michael@0: // coordinate system, so we need to compensate for its extra width here by michael@0: // effectively repositioning the frame. michael@0: nscoord extraWidth = std::max(0, aScrolledFrame->GetSize().width - aScrollPortSize.width); michael@0: x2 += extraWidth; michael@0: } michael@0: return nsRect(x1, y1, x2 - x1, y2 - y1); michael@0: } michael@0: michael@0: //static michael@0: bool michael@0: nsLayoutUtils::HasPseudoStyle(nsIContent* aContent, michael@0: nsStyleContext* aStyleContext, michael@0: nsCSSPseudoElements::Type aPseudoElement, michael@0: nsPresContext* aPresContext) michael@0: { michael@0: NS_PRECONDITION(aPresContext, "Must have a prescontext"); michael@0: michael@0: nsRefPtr pseudoContext; michael@0: if (aContent) { michael@0: pseudoContext = aPresContext->StyleSet()-> michael@0: ProbePseudoElementStyle(aContent->AsElement(), aPseudoElement, michael@0: aStyleContext); michael@0: } michael@0: return pseudoContext != nullptr; michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::GetDOMEventCoordinatesRelativeTo(nsIDOMEvent* aDOMEvent, nsIFrame* aFrame) michael@0: { michael@0: if (!aDOMEvent) michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: WidgetEvent* event = aDOMEvent->GetInternalNSEvent(); michael@0: if (!event) michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: return GetEventCoordinatesRelativeTo(event, aFrame); michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::GetEventCoordinatesRelativeTo(const WidgetEvent* aEvent, michael@0: nsIFrame* aFrame) michael@0: { michael@0: if (!aEvent || (aEvent->eventStructType != NS_MOUSE_EVENT && michael@0: aEvent->eventStructType != NS_MOUSE_SCROLL_EVENT && michael@0: aEvent->eventStructType != NS_WHEEL_EVENT && michael@0: aEvent->eventStructType != NS_DRAG_EVENT && michael@0: aEvent->eventStructType != NS_SIMPLE_GESTURE_EVENT && michael@0: aEvent->eventStructType != NS_POINTER_EVENT && michael@0: aEvent->eventStructType != NS_GESTURENOTIFY_EVENT && michael@0: aEvent->eventStructType != NS_TOUCH_EVENT && michael@0: aEvent->eventStructType != NS_QUERY_CONTENT_EVENT)) michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: michael@0: return GetEventCoordinatesRelativeTo(aEvent, michael@0: LayoutDeviceIntPoint::ToUntyped(aEvent->AsGUIEvent()->refPoint), michael@0: aFrame); michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::GetEventCoordinatesRelativeTo(const WidgetEvent* aEvent, michael@0: const nsIntPoint aPoint, michael@0: nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: nsIWidget* widget = aEvent->AsGUIEvent()->widget; michael@0: if (!widget) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: return GetEventCoordinatesRelativeTo(widget, aPoint, aFrame); michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::GetEventCoordinatesRelativeTo(nsIWidget* aWidget, michael@0: const nsIntPoint aPoint, michael@0: nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame || !aWidget) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: nsView* view = aFrame->GetView(); michael@0: if (view) { michael@0: nsIWidget* frameWidget = view->GetWidget(); michael@0: if (frameWidget && frameWidget == aWidget) { michael@0: // Special case this cause it happens a lot. michael@0: // This also fixes bug 664707, events in the extra-special case of select michael@0: // dropdown popups that are transformed. michael@0: nsPresContext* presContext = aFrame->PresContext(); michael@0: nsPoint pt(presContext->DevPixelsToAppUnits(aPoint.x), michael@0: presContext->DevPixelsToAppUnits(aPoint.y)); michael@0: return pt - view->ViewToWidgetOffset(); michael@0: } michael@0: } michael@0: michael@0: /* If we walk up the frame tree and discover that any of the frames are michael@0: * transformed, we need to do extra work to convert from the global michael@0: * space to the local space. michael@0: */ michael@0: nsIFrame* rootFrame = aFrame; michael@0: bool transformFound = false; michael@0: for (nsIFrame* f = aFrame; f; f = GetCrossDocParentFrame(f)) { michael@0: if (f->IsTransformed()) { michael@0: transformFound = true; michael@0: } michael@0: michael@0: rootFrame = f; michael@0: } michael@0: michael@0: nsView* rootView = rootFrame->GetView(); michael@0: if (!rootView) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: nsPoint widgetToView = TranslateWidgetToView(rootFrame->PresContext(), michael@0: aWidget, aPoint, rootView); michael@0: michael@0: if (widgetToView == nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE)) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: // Convert from root document app units to app units of the document aFrame michael@0: // is in. michael@0: int32_t rootAPD = rootFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: int32_t localAPD = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: widgetToView = widgetToView.ConvertAppUnits(rootAPD, localAPD); michael@0: michael@0: /* If we encountered a transform, we can't do simple arithmetic to figure michael@0: * out how to convert back to aFrame's coordinates and must use the CTM. michael@0: */ michael@0: if (transformFound || aFrame->IsSVGText()) { michael@0: return TransformRootPointToFrame(aFrame, widgetToView); michael@0: } michael@0: michael@0: /* Otherwise, all coordinate systems are translations of one another, michael@0: * so we can just subtract out the difference. michael@0: */ michael@0: return widgetToView - aFrame->GetOffsetToCrossDoc(rootFrame); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetPopupFrameForEventCoordinates(nsPresContext* aPresContext, michael@0: const WidgetEvent* aEvent) michael@0: { michael@0: #ifdef MOZ_XUL michael@0: nsXULPopupManager* pm = nsXULPopupManager::GetInstance(); michael@0: if (!pm) { michael@0: return nullptr; michael@0: } michael@0: nsTArray popups; michael@0: pm->GetVisiblePopups(popups); michael@0: uint32_t i; michael@0: // Search from top to bottom michael@0: for (i = 0; i < popups.Length(); i++) { michael@0: nsIFrame* popup = popups[i]; michael@0: if (popup->PresContext()->GetRootPresContext() == aPresContext && michael@0: popup->GetScrollableOverflowRect().Contains( michael@0: GetEventCoordinatesRelativeTo(aEvent, popup))) { michael@0: return popup; michael@0: } michael@0: } michael@0: #endif michael@0: return nullptr; michael@0: } michael@0: michael@0: gfx3DMatrix michael@0: nsLayoutUtils::ChangeMatrixBasis(const gfxPoint3D &aOrigin, michael@0: const gfx3DMatrix &aMatrix) michael@0: { michael@0: gfx3DMatrix result = aMatrix; michael@0: michael@0: /* Translate to the origin before aMatrix */ michael@0: result.Translate(-aOrigin); michael@0: michael@0: /* Translate back into position after aMatrix */ michael@0: result.TranslatePost(aOrigin); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static void ConstrainToCoordValues(float& aStart, float& aSize) michael@0: { michael@0: MOZ_ASSERT(aSize >= 0); michael@0: michael@0: // Here we try to make sure that the resulting nsRect will continue to cover michael@0: // as much of the area that was covered by the original gfx Rect as possible. michael@0: michael@0: // We clamp the bounds of the rect to {nscoord_MIN,nscoord_MAX} since michael@0: // nsRect::X/Y() and nsRect::XMost/YMost() can't return values outwith this michael@0: // range: michael@0: float end = aStart + aSize; michael@0: aStart = clamped(aStart, float(nscoord_MIN), float(nscoord_MAX)); michael@0: end = clamped(end, float(nscoord_MIN), float(nscoord_MAX)); michael@0: michael@0: aSize = end - aStart; michael@0: michael@0: // We must also clamp aSize to {0,nscoord_MAX} since nsRect::Width/Height() michael@0: // can't return a value greater than nscoord_MAX. If aSize is greater than michael@0: // nscoord_MAX then we reduce it to nscoord_MAX while keeping the rect michael@0: // centered: michael@0: if (aSize > nscoord_MAX) { michael@0: float excess = aSize - nscoord_MAX; michael@0: excess /= 2; michael@0: aStart += excess; michael@0: aSize = nscoord_MAX; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Given a gfxFloat, constrains its value to be between nscoord_MIN and nscoord_MAX. michael@0: * michael@0: * @param aVal The value to constrain (in/out) michael@0: */ michael@0: static void ConstrainToCoordValues(gfxFloat& aVal) michael@0: { michael@0: if (aVal <= nscoord_MIN) michael@0: aVal = nscoord_MIN; michael@0: else if (aVal >= nscoord_MAX) michael@0: aVal = nscoord_MAX; michael@0: } michael@0: michael@0: static void ConstrainToCoordValues(gfxFloat& aStart, gfxFloat& aSize) michael@0: { michael@0: gfxFloat max = aStart + aSize; michael@0: michael@0: // Clamp the end points to within nscoord range michael@0: ConstrainToCoordValues(aStart); michael@0: ConstrainToCoordValues(max); michael@0: michael@0: aSize = max - aStart; michael@0: // If the width if still greater than the max nscoord, then bring both michael@0: // endpoints in by the same amount until it fits. michael@0: if (aSize > nscoord_MAX) { michael@0: gfxFloat excess = aSize - nscoord_MAX; michael@0: excess /= 2; michael@0: michael@0: aStart += excess; michael@0: aSize = nscoord_MAX; michael@0: } else if (aSize < nscoord_MIN) { michael@0: gfxFloat excess = aSize - nscoord_MIN; michael@0: excess /= 2; michael@0: michael@0: aStart -= excess; michael@0: aSize = nscoord_MIN; michael@0: } michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::RoundGfxRectToAppRect(const Rect &aRect, float aFactor) michael@0: { michael@0: /* Get a new Rect whose units are app units by scaling by the specified factor. */ michael@0: Rect scaledRect = aRect; michael@0: scaledRect.ScaleRoundOut(aFactor); michael@0: michael@0: /* We now need to constrain our results to the max and min values for coords. */ michael@0: ConstrainToCoordValues(scaledRect.x, scaledRect.width); michael@0: ConstrainToCoordValues(scaledRect.y, scaledRect.height); michael@0: michael@0: /* Now typecast everything back. This is guaranteed to be safe. */ michael@0: return nsRect(nscoord(scaledRect.X()), nscoord(scaledRect.Y()), michael@0: nscoord(scaledRect.Width()), nscoord(scaledRect.Height())); michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::RoundGfxRectToAppRect(const gfxRect &aRect, float aFactor) michael@0: { michael@0: /* Get a new gfxRect whose units are app units by scaling by the specified factor. */ michael@0: gfxRect scaledRect = aRect; michael@0: scaledRect.ScaleRoundOut(aFactor); michael@0: michael@0: /* We now need to constrain our results to the max and min values for coords. */ michael@0: ConstrainToCoordValues(scaledRect.x, scaledRect.width); michael@0: ConstrainToCoordValues(scaledRect.y, scaledRect.height); michael@0: michael@0: /* Now typecast everything back. This is guaranteed to be safe. */ michael@0: return nsRect(nscoord(scaledRect.X()), nscoord(scaledRect.Y()), michael@0: nscoord(scaledRect.Width()), nscoord(scaledRect.Height())); michael@0: } michael@0: michael@0: michael@0: nsRegion michael@0: nsLayoutUtils::RoundedRectIntersectRect(const nsRect& aRoundedRect, michael@0: const nscoord aRadii[8], michael@0: const nsRect& aContainedRect) michael@0: { michael@0: // rectFullHeight and rectFullWidth together will approximately contain michael@0: // the total area of the frame minus the rounded corners. michael@0: nsRect rectFullHeight = aRoundedRect; michael@0: nscoord xDiff = std::max(aRadii[NS_CORNER_TOP_LEFT_X], aRadii[NS_CORNER_BOTTOM_LEFT_X]); michael@0: rectFullHeight.x += xDiff; michael@0: rectFullHeight.width -= std::max(aRadii[NS_CORNER_TOP_RIGHT_X], michael@0: aRadii[NS_CORNER_BOTTOM_RIGHT_X]) + xDiff; michael@0: nsRect r1; michael@0: r1.IntersectRect(rectFullHeight, aContainedRect); michael@0: michael@0: nsRect rectFullWidth = aRoundedRect; michael@0: nscoord yDiff = std::max(aRadii[NS_CORNER_TOP_LEFT_Y], aRadii[NS_CORNER_TOP_RIGHT_Y]); michael@0: rectFullWidth.y += yDiff; michael@0: rectFullWidth.height -= std::max(aRadii[NS_CORNER_BOTTOM_LEFT_Y], michael@0: aRadii[NS_CORNER_BOTTOM_RIGHT_Y]) + yDiff; michael@0: nsRect r2; michael@0: r2.IntersectRect(rectFullWidth, aContainedRect); michael@0: michael@0: nsRegion result; michael@0: result.Or(r1, r2); michael@0: return result; michael@0: } michael@0: michael@0: // Helper for RoundedRectIntersectsRect. michael@0: static bool michael@0: CheckCorner(nscoord aXOffset, nscoord aYOffset, michael@0: nscoord aXRadius, nscoord aYRadius) michael@0: { michael@0: NS_ABORT_IF_FALSE(aXOffset > 0 && aYOffset > 0, michael@0: "must not pass nonpositives to CheckCorner"); michael@0: NS_ABORT_IF_FALSE(aXRadius >= 0 && aYRadius >= 0, michael@0: "must not pass negatives to CheckCorner"); michael@0: michael@0: // Avoid floating point math unless we're either (1) within the michael@0: // quarter-ellipse area at the rounded corner or (2) outside the michael@0: // rounding. michael@0: if (aXOffset >= aXRadius || aYOffset >= aYRadius) michael@0: return true; michael@0: michael@0: // Convert coordinates to a unit circle with (0,0) as the center of michael@0: // curvature, and see if we're inside the circle or outside. michael@0: float scaledX = float(aXRadius - aXOffset) / float(aXRadius); michael@0: float scaledY = float(aYRadius - aYOffset) / float(aYRadius); michael@0: return scaledX * scaledX + scaledY * scaledY < 1.0f; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::RoundedRectIntersectsRect(const nsRect& aRoundedRect, michael@0: const nscoord aRadii[8], michael@0: const nsRect& aTestRect) michael@0: { michael@0: if (!aTestRect.Intersects(aRoundedRect)) michael@0: return false; michael@0: michael@0: // distances from this edge of aRoundedRect to opposite edge of aTestRect, michael@0: // which we know are positive due to the Intersects check above. michael@0: nsMargin insets; michael@0: insets.top = aTestRect.YMost() - aRoundedRect.y; michael@0: insets.right = aRoundedRect.XMost() - aTestRect.x; michael@0: insets.bottom = aRoundedRect.YMost() - aTestRect.y; michael@0: insets.left = aTestRect.XMost() - aRoundedRect.x; michael@0: michael@0: // Check whether the bottom-right corner of aTestRect is inside the michael@0: // top left corner of aBounds when rounded by aRadii, etc. If any michael@0: // corner is not, then fail; otherwise succeed. michael@0: return CheckCorner(insets.left, insets.top, michael@0: aRadii[NS_CORNER_TOP_LEFT_X], michael@0: aRadii[NS_CORNER_TOP_LEFT_Y]) && michael@0: CheckCorner(insets.right, insets.top, michael@0: aRadii[NS_CORNER_TOP_RIGHT_X], michael@0: aRadii[NS_CORNER_TOP_RIGHT_Y]) && michael@0: CheckCorner(insets.right, insets.bottom, michael@0: aRadii[NS_CORNER_BOTTOM_RIGHT_X], michael@0: aRadii[NS_CORNER_BOTTOM_RIGHT_Y]) && michael@0: CheckCorner(insets.left, insets.bottom, michael@0: aRadii[NS_CORNER_BOTTOM_LEFT_X], michael@0: aRadii[NS_CORNER_BOTTOM_LEFT_Y]); michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::MatrixTransformRectOut(const nsRect &aBounds, michael@0: const gfx3DMatrix &aMatrix, float aFactor) michael@0: { michael@0: nsRect outside = aBounds; michael@0: outside.ScaleRoundOut(1/aFactor); michael@0: gfxRect image = aMatrix.TransformBounds(gfxRect(outside.x, michael@0: outside.y, michael@0: outside.width, michael@0: outside.height)); michael@0: return RoundGfxRectToAppRect(image, aFactor); michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::MatrixTransformRect(const nsRect &aBounds, michael@0: const gfx3DMatrix &aMatrix, float aFactor) michael@0: { michael@0: gfxRect image = aMatrix.TransformBounds(gfxRect(NSAppUnitsToDoublePixels(aBounds.x, aFactor), michael@0: NSAppUnitsToDoublePixels(aBounds.y, aFactor), michael@0: NSAppUnitsToDoublePixels(aBounds.width, aFactor), michael@0: NSAppUnitsToDoublePixels(aBounds.height, aFactor))); michael@0: michael@0: return RoundGfxRectToAppRect(image, aFactor); michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::MatrixTransformPoint(const nsPoint &aPoint, michael@0: const gfx3DMatrix &aMatrix, float aFactor) michael@0: { michael@0: gfxPoint image = aMatrix.Transform(gfxPoint(NSAppUnitsToFloatPixels(aPoint.x, aFactor), michael@0: NSAppUnitsToFloatPixels(aPoint.y, aFactor))); michael@0: return nsPoint(NSFloatPixelsToAppUnits(float(image.x), aFactor), michael@0: NSFloatPixelsToAppUnits(float(image.y), aFactor)); michael@0: } michael@0: michael@0: gfx3DMatrix michael@0: nsLayoutUtils::GetTransformToAncestor(nsIFrame *aFrame, const nsIFrame *aAncestor) michael@0: { michael@0: nsIFrame* parent; michael@0: gfx3DMatrix ctm; michael@0: if (aFrame == aAncestor) { michael@0: return ctm; michael@0: } michael@0: ctm = aFrame->GetTransformMatrix(aAncestor, &parent); michael@0: while (parent && parent != aAncestor) { michael@0: if (!parent->Preserves3DChildren()) { michael@0: ctm.ProjectTo2D(); michael@0: } michael@0: ctm = ctm * parent->GetTransformMatrix(aAncestor, &parent); michael@0: } michael@0: return ctm; michael@0: } michael@0: michael@0: static nsIFrame* michael@0: FindNearestCommonAncestorFrame(nsIFrame* aFrame1, nsIFrame* aFrame2) michael@0: { michael@0: nsAutoTArray ancestors1; michael@0: nsAutoTArray ancestors2; michael@0: nsIFrame* commonAncestor = nullptr; michael@0: if (aFrame1->PresContext() == aFrame2->PresContext()) { michael@0: commonAncestor = aFrame1->PresContext()->PresShell()->GetRootFrame(); michael@0: } michael@0: for (nsIFrame* f = aFrame1; f != commonAncestor; michael@0: f = nsLayoutUtils::GetCrossDocParentFrame(f)) { michael@0: ancestors1.AppendElement(f); michael@0: } michael@0: for (nsIFrame* f = aFrame2; f != commonAncestor; michael@0: f = nsLayoutUtils::GetCrossDocParentFrame(f)) { michael@0: ancestors2.AppendElement(f); michael@0: } michael@0: uint32_t minLengths = std::min(ancestors1.Length(), ancestors2.Length()); michael@0: for (uint32_t i = 1; i <= minLengths; ++i) { michael@0: if (ancestors1[ancestors1.Length() - i] == ancestors2[ancestors2.Length() - i]) { michael@0: commonAncestor = ancestors1[ancestors1.Length() - i]; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: return commonAncestor; michael@0: } michael@0: michael@0: nsLayoutUtils::TransformResult michael@0: nsLayoutUtils::TransformPoints(nsIFrame* aFromFrame, nsIFrame* aToFrame, michael@0: uint32_t aPointCount, CSSPoint* aPoints) michael@0: { michael@0: nsIFrame* nearestCommonAncestor = FindNearestCommonAncestorFrame(aFromFrame, aToFrame); michael@0: if (!nearestCommonAncestor) { michael@0: return NO_COMMON_ANCESTOR; michael@0: } michael@0: gfx3DMatrix downToDest = GetTransformToAncestor(aToFrame, nearestCommonAncestor); michael@0: if (downToDest.IsSingular()) { michael@0: return NONINVERTIBLE_TRANSFORM; michael@0: } michael@0: downToDest.Invert(); michael@0: gfx3DMatrix upToAncestor = GetTransformToAncestor(aFromFrame, nearestCommonAncestor); michael@0: CSSToLayoutDeviceScale devPixelsPerCSSPixelFromFrame( michael@0: double(nsPresContext::AppUnitsPerCSSPixel())/ michael@0: aFromFrame->PresContext()->AppUnitsPerDevPixel()); michael@0: CSSToLayoutDeviceScale devPixelsPerCSSPixelToFrame( michael@0: double(nsPresContext::AppUnitsPerCSSPixel())/ michael@0: aToFrame->PresContext()->AppUnitsPerDevPixel()); michael@0: for (uint32_t i = 0; i < aPointCount; ++i) { michael@0: LayoutDevicePoint devPixels = aPoints[i] * devPixelsPerCSSPixelFromFrame; michael@0: gfxPoint toDevPixels = downToDest.ProjectPoint( michael@0: upToAncestor.ProjectPoint(gfxPoint(devPixels.x, devPixels.y))); michael@0: // Divide here so that when the devPixelsPerCSSPixels are the same, we get the correct michael@0: // answer instead of some inaccuracy multiplying a number by its reciprocal. michael@0: aPoints[i] = LayoutDevicePoint(toDevPixels.x, toDevPixels.y) / michael@0: devPixelsPerCSSPixelToFrame; michael@0: } michael@0: return TRANSFORM_SUCCEEDED; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::GetLayerTransformForFrame(nsIFrame* aFrame, michael@0: gfx3DMatrix* aTransform) michael@0: { michael@0: // FIXME/bug 796690: we can sometimes compute a transform in these michael@0: // cases, it just increases complexity considerably. Punt for now. michael@0: if (aFrame->Preserves3DChildren() || aFrame->HasTransformGetter()) { michael@0: return false; michael@0: } michael@0: michael@0: nsIFrame* root = nsLayoutUtils::GetDisplayRootFrame(aFrame); michael@0: if (root->HasAnyStateBits(NS_FRAME_UPDATE_LAYER_TREE)) { michael@0: // Content may have been invalidated, so we can't reliably compute michael@0: // the "layer transform" in general. michael@0: return false; michael@0: } michael@0: // If the caller doesn't care about the value, early-return to skip michael@0: // overhead below. michael@0: if (!aTransform) { michael@0: return true; michael@0: } michael@0: michael@0: nsDisplayListBuilder builder(root, nsDisplayListBuilder::OTHER, michael@0: false/*don't build caret*/); michael@0: nsDisplayList list; michael@0: nsDisplayTransform* item = michael@0: new (&builder) nsDisplayTransform(&builder, aFrame, &list); michael@0: michael@0: *aTransform = michael@0: item->GetTransform(); michael@0: item->~nsDisplayTransform(); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: static bool michael@0: TransformGfxPointFromAncestor(nsIFrame *aFrame, michael@0: const gfxPoint &aPoint, michael@0: nsIFrame *aAncestor, michael@0: gfxPoint* aOut) michael@0: { michael@0: gfx3DMatrix ctm = nsLayoutUtils::GetTransformToAncestor(aFrame, aAncestor); michael@0: michael@0: float factor = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: nsRect childBounds = aFrame->GetVisualOverflowRectRelativeToSelf(); michael@0: gfxRect childGfxBounds(NSAppUnitsToFloatPixels(childBounds.x, factor), michael@0: NSAppUnitsToFloatPixels(childBounds.y, factor), michael@0: NSAppUnitsToFloatPixels(childBounds.width, factor), michael@0: NSAppUnitsToFloatPixels(childBounds.height, factor)); michael@0: return ctm.UntransformPoint(aPoint, childGfxBounds, aOut); michael@0: } michael@0: michael@0: static gfxRect michael@0: TransformGfxRectToAncestor(nsIFrame *aFrame, michael@0: const gfxRect &aRect, michael@0: const nsIFrame *aAncestor, michael@0: bool* aPreservesAxisAlignedRectangles = nullptr) michael@0: { michael@0: gfx3DMatrix ctm = nsLayoutUtils::GetTransformToAncestor(aFrame, aAncestor); michael@0: if (aPreservesAxisAlignedRectangles) { michael@0: gfxMatrix matrix2d; michael@0: *aPreservesAxisAlignedRectangles = michael@0: ctm.Is2D(&matrix2d) && matrix2d.PreservesAxisAlignedRectangles(); michael@0: } michael@0: return ctm.TransformBounds(aRect); michael@0: } michael@0: michael@0: static SVGTextFrame* michael@0: GetContainingSVGTextFrame(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame->IsSVGText()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return static_cast michael@0: (nsLayoutUtils::GetClosestFrameOfType(aFrame->GetParent(), michael@0: nsGkAtoms::svgTextFrame)); michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::TransformAncestorPointToFrame(nsIFrame* aFrame, michael@0: const nsPoint& aPoint, michael@0: nsIFrame* aAncestor) michael@0: { michael@0: SVGTextFrame* text = GetContainingSVGTextFrame(aFrame); michael@0: michael@0: float factor = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: gfxPoint result(NSAppUnitsToFloatPixels(aPoint.x, factor), michael@0: NSAppUnitsToFloatPixels(aPoint.y, factor)); michael@0: michael@0: if (text) { michael@0: if (!TransformGfxPointFromAncestor(text, result, aAncestor, &result)) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: result = text->TransformFramePointToTextChild(result, aFrame); michael@0: } else { michael@0: if (!TransformGfxPointFromAncestor(aFrame, result, nullptr, &result)) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: } michael@0: michael@0: return nsPoint(NSFloatPixelsToAppUnits(float(result.x), factor), michael@0: NSFloatPixelsToAppUnits(float(result.y), factor)); michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::TransformFrameRectToAncestor(nsIFrame* aFrame, michael@0: const nsRect& aRect, michael@0: const nsIFrame* aAncestor, michael@0: bool* aPreservesAxisAlignedRectangles /* = nullptr */) michael@0: { michael@0: SVGTextFrame* text = GetContainingSVGTextFrame(aFrame); michael@0: michael@0: float srcAppUnitsPerDevPixel = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: gfxRect result; michael@0: michael@0: if (text) { michael@0: result = text->TransformFrameRectFromTextChild(aRect, aFrame); michael@0: result = TransformGfxRectToAncestor(text, result, aAncestor); michael@0: // TransformFrameRectFromTextChild could involve any kind of transform, we michael@0: // could drill down into it to get an answer out of it but we don't yet. michael@0: if (aPreservesAxisAlignedRectangles) michael@0: *aPreservesAxisAlignedRectangles = false; michael@0: } else { michael@0: result = gfxRect(NSAppUnitsToFloatPixels(aRect.x, srcAppUnitsPerDevPixel), michael@0: NSAppUnitsToFloatPixels(aRect.y, srcAppUnitsPerDevPixel), michael@0: NSAppUnitsToFloatPixels(aRect.width, srcAppUnitsPerDevPixel), michael@0: NSAppUnitsToFloatPixels(aRect.height, srcAppUnitsPerDevPixel)); michael@0: result = TransformGfxRectToAncestor(aFrame, result, aAncestor, aPreservesAxisAlignedRectangles); michael@0: } michael@0: michael@0: float destAppUnitsPerDevPixel = aAncestor->PresContext()->AppUnitsPerDevPixel(); michael@0: return nsRect(NSFloatPixelsToAppUnits(float(result.x), destAppUnitsPerDevPixel), michael@0: NSFloatPixelsToAppUnits(float(result.y), destAppUnitsPerDevPixel), michael@0: NSFloatPixelsToAppUnits(float(result.width), destAppUnitsPerDevPixel), michael@0: NSFloatPixelsToAppUnits(float(result.height), destAppUnitsPerDevPixel)); michael@0: } michael@0: michael@0: static nsIntPoint GetWidgetOffset(nsIWidget* aWidget, nsIWidget*& aRootWidget) { michael@0: nsIntPoint offset(0, 0); michael@0: nsIWidget* parent = aWidget->GetParent(); michael@0: while (parent) { michael@0: nsIntRect bounds; michael@0: aWidget->GetBounds(bounds); michael@0: offset += bounds.TopLeft(); michael@0: aWidget = parent; michael@0: parent = aWidget->GetParent(); michael@0: } michael@0: aRootWidget = aWidget; michael@0: return offset; michael@0: } michael@0: michael@0: nsPoint michael@0: nsLayoutUtils::TranslateWidgetToView(nsPresContext* aPresContext, michael@0: nsIWidget* aWidget, nsIntPoint aPt, michael@0: nsView* aView) michael@0: { michael@0: nsPoint viewOffset; michael@0: nsIWidget* viewWidget = aView->GetNearestWidget(&viewOffset); michael@0: if (!viewWidget) { michael@0: return nsPoint(NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE); michael@0: } michael@0: michael@0: nsIWidget* fromRoot; michael@0: nsIntPoint fromOffset = GetWidgetOffset(aWidget, fromRoot); michael@0: nsIWidget* toRoot; michael@0: nsIntPoint toOffset = GetWidgetOffset(viewWidget, toRoot); michael@0: michael@0: nsIntPoint widgetPoint; michael@0: if (fromRoot == toRoot) { michael@0: widgetPoint = aPt + fromOffset - toOffset; michael@0: } else { michael@0: nsIntPoint screenPoint = aWidget->WidgetToScreenOffset(); michael@0: widgetPoint = aPt + screenPoint - viewWidget->WidgetToScreenOffset(); michael@0: } michael@0: michael@0: nsPoint widgetAppUnits(aPresContext->DevPixelsToAppUnits(widgetPoint.x), michael@0: aPresContext->DevPixelsToAppUnits(widgetPoint.y)); michael@0: return widgetAppUnits - viewOffset; michael@0: } michael@0: michael@0: // Combine aNewBreakType with aOrigBreakType, but limit the break types michael@0: // to NS_STYLE_CLEAR_LEFT, RIGHT, BOTH. michael@0: uint8_t michael@0: nsLayoutUtils::CombineBreakType(uint8_t aOrigBreakType, michael@0: uint8_t aNewBreakType) michael@0: { michael@0: uint8_t breakType = aOrigBreakType; michael@0: switch(breakType) { michael@0: case NS_STYLE_CLEAR_LEFT: michael@0: if (NS_STYLE_CLEAR_RIGHT == aNewBreakType || michael@0: NS_STYLE_CLEAR_BOTH == aNewBreakType) { michael@0: breakType = NS_STYLE_CLEAR_BOTH; michael@0: } michael@0: break; michael@0: case NS_STYLE_CLEAR_RIGHT: michael@0: if (NS_STYLE_CLEAR_LEFT == aNewBreakType || michael@0: NS_STYLE_CLEAR_BOTH == aNewBreakType) { michael@0: breakType = NS_STYLE_CLEAR_BOTH; michael@0: } michael@0: break; michael@0: case NS_STYLE_CLEAR_NONE: michael@0: if (NS_STYLE_CLEAR_LEFT == aNewBreakType || michael@0: NS_STYLE_CLEAR_RIGHT == aNewBreakType || michael@0: NS_STYLE_CLEAR_BOTH == aNewBreakType) { michael@0: breakType = aNewBreakType; michael@0: } michael@0: } michael@0: return breakType; michael@0: } michael@0: michael@0: #ifdef MOZ_DUMP_PAINTING michael@0: #include michael@0: michael@0: static bool gDumpEventList = false; michael@0: int gPaintCount = 0; michael@0: #endif michael@0: michael@0: nsresult michael@0: nsLayoutUtils::GetRemoteContentIds(nsIFrame* aFrame, michael@0: const nsRect& aTarget, michael@0: nsTArray &aOutIDs, michael@0: bool aIgnoreRootScrollFrame) michael@0: { michael@0: nsDisplayListBuilder builder(aFrame, nsDisplayListBuilder::EVENT_DELIVERY, michael@0: false); michael@0: nsDisplayList list; michael@0: michael@0: if (aIgnoreRootScrollFrame) { michael@0: nsIFrame* rootScrollFrame = michael@0: aFrame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: if (rootScrollFrame) { michael@0: builder.SetIgnoreScrollFrame(rootScrollFrame); michael@0: } michael@0: } michael@0: michael@0: builder.EnterPresShell(aFrame, aTarget); michael@0: aFrame->BuildDisplayListForStackingContext(&builder, aTarget, &list); michael@0: builder.LeavePresShell(aFrame, aTarget); michael@0: michael@0: nsAutoTArray outFrames; michael@0: nsDisplayItem::HitTestState hitTestState(&aOutIDs); michael@0: list.HitTest(&builder, aTarget, &hitTestState, &outFrames); michael@0: list.DeleteAll(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetFrameForPoint(nsIFrame* aFrame, nsPoint aPt, uint32_t aFlags) michael@0: { michael@0: PROFILER_LABEL("nsLayoutUtils", "GetFrameForPoint"); michael@0: nsresult rv; michael@0: nsAutoTArray outFrames; michael@0: rv = GetFramesForArea(aFrame, nsRect(aPt, nsSize(1, 1)), outFrames, aFlags); michael@0: NS_ENSURE_SUCCESS(rv, nullptr); michael@0: return outFrames.Length() ? outFrames.ElementAt(0) : nullptr; michael@0: } michael@0: michael@0: nsresult michael@0: nsLayoutUtils::GetFramesForArea(nsIFrame* aFrame, const nsRect& aRect, michael@0: nsTArray &aOutFrames, michael@0: uint32_t aFlags) michael@0: { michael@0: PROFILER_LABEL("nsLayoutUtils","GetFramesForArea"); michael@0: nsDisplayListBuilder builder(aFrame, nsDisplayListBuilder::EVENT_DELIVERY, michael@0: false); michael@0: nsDisplayList list; michael@0: nsRect target(aRect); michael@0: michael@0: if (aFlags & IGNORE_PAINT_SUPPRESSION) { michael@0: builder.IgnorePaintSuppression(); michael@0: } michael@0: michael@0: if (aFlags & IGNORE_ROOT_SCROLL_FRAME) { michael@0: nsIFrame* rootScrollFrame = michael@0: aFrame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: if (rootScrollFrame) { michael@0: builder.SetIgnoreScrollFrame(rootScrollFrame); michael@0: } michael@0: } michael@0: if (aFlags & IGNORE_CROSS_DOC) { michael@0: builder.SetDescendIntoSubdocuments(false); michael@0: } michael@0: michael@0: builder.EnterPresShell(aFrame, target); michael@0: aFrame->BuildDisplayListForStackingContext(&builder, target, &list); michael@0: builder.LeavePresShell(aFrame, target); michael@0: michael@0: #ifdef MOZ_DUMP_PAINTING michael@0: if (gDumpEventList) { michael@0: fprintf_stderr(stderr, "Event handling --- (%d,%d):\n", aRect.x, aRect.y); michael@0: nsFrame::PrintDisplayList(&builder, list); michael@0: } michael@0: #endif michael@0: michael@0: nsDisplayItem::HitTestState hitTestState; michael@0: list.HitTest(&builder, target, &hitTestState, &aOutFrames); michael@0: list.DeleteAll(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: // This function is only used on B2G, and some compilers complain about michael@0: // unused static functions, so we need to #ifdef it. michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // aScrollFrame and aScrollFrameAsScrollable must be non-nullptr michael@0: static FrameMetrics michael@0: CalculateFrameMetricsForDisplayPort(nsIFrame* aScrollFrame, michael@0: nsIScrollableFrame* aScrollFrameAsScrollable) { michael@0: // Calculate the metrics necessary for calculating the displayport. michael@0: // This code has a lot in common with the code in RecordFrameMetrics(); michael@0: // we may want to refactor this at some point. michael@0: FrameMetrics metrics; michael@0: nsPresContext* presContext = aScrollFrame->PresContext(); michael@0: nsIPresShell* presShell = presContext->PresShell(); michael@0: CSSToLayoutDeviceScale deviceScale(float(nsPresContext::AppUnitsPerCSSPixel()) michael@0: / presContext->AppUnitsPerDevPixel()); michael@0: ParentLayerToLayerScale resolution(presShell->GetResolution().width); michael@0: LayoutDeviceToLayerScale cumulativeResolution(presShell->GetCumulativeResolution().width); michael@0: michael@0: metrics.mDevPixelsPerCSSPixel = deviceScale; michael@0: metrics.mResolution = resolution; michael@0: metrics.mCumulativeResolution = cumulativeResolution; michael@0: metrics.SetZoom(deviceScale * cumulativeResolution * LayerToScreenScale(1)); michael@0: michael@0: // Only the size of the composition bounds is relevant to the michael@0: // displayport calculation, not its origin. michael@0: nsSize compositionSize = nsLayoutUtils::CalculateCompositionSizeForFrame(aScrollFrame); michael@0: metrics.mCompositionBounds michael@0: = RoundedToInt(LayoutDeviceRect::FromAppUnits(nsRect(nsPoint(0, 0), compositionSize), michael@0: presContext->AppUnitsPerDevPixel()) michael@0: * (cumulativeResolution / resolution)); michael@0: michael@0: // This function is used for setting a display port for subframes, so michael@0: // aScrollFrame will not be the root content document's root scroll frame. michael@0: metrics.SetRootCompositionSize( michael@0: nsLayoutUtils::CalculateRootCompositionSize(aScrollFrame, false, metrics)); michael@0: michael@0: metrics.SetScrollOffset(CSSPoint::FromAppUnits( michael@0: aScrollFrameAsScrollable->GetScrollPosition())); michael@0: michael@0: metrics.mScrollableRect = CSSRect::FromAppUnits( michael@0: nsLayoutUtils::CalculateScrollableRectForFrame(aScrollFrameAsScrollable, nullptr)); michael@0: michael@0: return metrics; michael@0: } michael@0: #endif michael@0: michael@0: bool michael@0: nsLayoutUtils::GetOrMaybeCreateDisplayPort(nsDisplayListBuilder& aBuilder, michael@0: nsIFrame* aScrollFrame, michael@0: nsRect aDisplayPortBase, michael@0: nsRect* aOutDisplayport) { michael@0: nsIContent* content = aScrollFrame->GetContent(); michael@0: nsIScrollableFrame* scrollableFrame = do_QueryFrame(aScrollFrame); michael@0: if (!content || !scrollableFrame) { michael@0: return false; michael@0: } michael@0: michael@0: // Set the base rect. Note that this will not influence 'haveDisplayPort', michael@0: // which is based on either the whole rect or margins being set, but it michael@0: // will affect what is returned in 'aOutDisplayPort' if margins are set. michael@0: SetDisplayPortBase(content, aDisplayPortBase); michael@0: michael@0: bool haveDisplayPort = GetDisplayPort(content, aOutDisplayport); michael@0: michael@0: #ifdef MOZ_WIDGET_GONK michael@0: // On B2G, we perform an optimization where we ensure that at least one michael@0: // async-scrollable frame (i.e. one that WantsAsyncScroll()) has a displayport. michael@0: // If that's not the case yet, and we are async-scrollable, we will get a michael@0: // displayport. michael@0: // Note: we only do this in processes where we do subframe scrolling to michael@0: // begin with (i.e., not in the parent process on B2G). michael@0: if (WantSubAPZC() && michael@0: !aBuilder.HaveScrollableDisplayPort() && michael@0: scrollableFrame->WantAsyncScroll()) { michael@0: michael@0: // If we don't already have a displayport, calculate and set one. michael@0: if (!haveDisplayPort) { michael@0: FrameMetrics metrics = CalculateFrameMetricsForDisplayPort(aScrollFrame, scrollableFrame); michael@0: LayerMargin displayportMargins = AsyncPanZoomController::CalculatePendingDisplayPort( michael@0: metrics, ScreenPoint(0.0f, 0.0f), 0.0); michael@0: nsIPresShell* presShell = aScrollFrame->PresContext()->GetPresShell(); michael@0: gfx::IntSize alignment = gfxPrefs::LayersTilesEnabled() michael@0: ? gfx::IntSize(gfxPrefs::LayersTileWidth(), gfxPrefs::LayersTileHeight()) : michael@0: gfx::IntSize(0, 0); michael@0: nsLayoutUtils::SetDisplayPortMargins( michael@0: content, presShell, displayportMargins, alignment.width, michael@0: alignment.height, 0, nsLayoutUtils::RepaintMode::DoNotRepaint); michael@0: haveDisplayPort = GetDisplayPort(content, aOutDisplayport); michael@0: NS_ASSERTION(haveDisplayPort, "should have a displayport after having just set it"); michael@0: } michael@0: michael@0: // Record that the we now have a scrollable display port. michael@0: aBuilder.SetHaveScrollableDisplayPort(); michael@0: } michael@0: #endif michael@0: michael@0: return haveDisplayPort; michael@0: } michael@0: michael@0: nsresult michael@0: nsLayoutUtils::PaintFrame(nsRenderingContext* aRenderingContext, nsIFrame* aFrame, michael@0: const nsRegion& aDirtyRegion, nscolor aBackstop, michael@0: uint32_t aFlags) michael@0: { michael@0: PROFILER_LABEL("nsLayoutUtils","PaintFrame"); michael@0: if (aFlags & PAINT_WIDGET_LAYERS) { michael@0: nsView* view = aFrame->GetView(); michael@0: if (!(view && view->GetWidget() && GetDisplayRootFrame(aFrame) == aFrame)) { michael@0: aFlags &= ~PAINT_WIDGET_LAYERS; michael@0: NS_ASSERTION(aRenderingContext, "need a rendering context"); michael@0: } michael@0: } michael@0: michael@0: nsPresContext* presContext = aFrame->PresContext(); michael@0: nsIPresShell* presShell = presContext->PresShell(); michael@0: nsRootPresContext* rootPresContext = presContext->GetRootPresContext(); michael@0: if (!rootPresContext) { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsDisplayListBuilder builder(aFrame, nsDisplayListBuilder::PAINTING, michael@0: !(aFlags & PAINT_HIDE_CARET)); michael@0: michael@0: nsIFrame* rootScrollFrame = presShell->GetRootScrollFrame(); michael@0: bool usingDisplayPort = false; michael@0: nsRect displayport; michael@0: if (rootScrollFrame && !aFrame->GetParent()) { michael@0: nsRect displayportBase( michael@0: nsPoint(0,0), michael@0: nsLayoutUtils::CalculateCompositionSizeForFrame(rootScrollFrame)); michael@0: usingDisplayPort = nsLayoutUtils::GetOrMaybeCreateDisplayPort( michael@0: builder, rootScrollFrame, displayportBase, &displayport); michael@0: } michael@0: michael@0: nsRegion visibleRegion; michael@0: if (aFlags & PAINT_WIDGET_LAYERS) { michael@0: // This layer tree will be reused, so we'll need to calculate it michael@0: // for the whole "visible" area of the window michael@0: // michael@0: // |ignoreViewportScrolling| and |usingDisplayPort| are persistent michael@0: // document-rendering state. We rely on PresShell to flush michael@0: // retained layers as needed when that persistent state changes. michael@0: if (!usingDisplayPort) { michael@0: visibleRegion = aFrame->GetVisualOverflowRectRelativeToSelf(); michael@0: } else { michael@0: visibleRegion = displayport; michael@0: } michael@0: } else { michael@0: visibleRegion = aDirtyRegion; michael@0: } michael@0: michael@0: // If we're going to display something different from what we'd normally michael@0: // paint in a window then we will flush out any retained layer trees before michael@0: // *and after* we draw. michael@0: bool willFlushRetainedLayers = (aFlags & PAINT_HIDE_CARET) != 0; michael@0: michael@0: nsDisplayList list; michael@0: if (aFlags & PAINT_IN_TRANSFORM) { michael@0: builder.SetInTransform(true); michael@0: } michael@0: if (aFlags & PAINT_SYNC_DECODE_IMAGES) { michael@0: builder.SetSyncDecodeImages(true); michael@0: } michael@0: if (aFlags & (PAINT_WIDGET_LAYERS | PAINT_TO_WINDOW)) { michael@0: builder.SetPaintingToWindow(true); michael@0: } michael@0: if (aFlags & PAINT_IGNORE_SUPPRESSION) { michael@0: builder.IgnorePaintSuppression(); michael@0: } michael@0: // Windowed plugins aren't allowed in popups michael@0: if ((aFlags & PAINT_WIDGET_LAYERS) && michael@0: !willFlushRetainedLayers && michael@0: !(aFlags & PAINT_DOCUMENT_RELATIVE) && michael@0: rootPresContext->NeedToComputePluginGeometryUpdates()) { michael@0: builder.SetWillComputePluginGeometry(true); michael@0: } michael@0: nsRect canvasArea(nsPoint(0, 0), aFrame->GetSize()); michael@0: michael@0: bool ignoreViewportScrolling = michael@0: aFrame->GetParent() ? false : presShell->IgnoringViewportScrolling(); michael@0: if (ignoreViewportScrolling && rootScrollFrame) { michael@0: nsIScrollableFrame* rootScrollableFrame = michael@0: presShell->GetRootScrollFrameAsScrollable(); michael@0: if (aFlags & PAINT_DOCUMENT_RELATIVE) { michael@0: // Make visibleRegion and aRenderingContext relative to the michael@0: // scrolled frame instead of the root frame. michael@0: nsPoint pos = rootScrollableFrame->GetScrollPosition(); michael@0: visibleRegion.MoveBy(-pos); michael@0: if (aRenderingContext) { michael@0: aRenderingContext->Translate(pos); michael@0: } michael@0: } michael@0: builder.SetIgnoreScrollFrame(rootScrollFrame); michael@0: michael@0: nsCanvasFrame* canvasFrame = michael@0: do_QueryFrame(rootScrollableFrame->GetScrolledFrame()); michael@0: if (canvasFrame) { michael@0: // Use UnionRect here to ensure that areas where the scrollbars michael@0: // were are still filled with the background color. michael@0: canvasArea.UnionRect(canvasArea, michael@0: canvasFrame->CanvasArea() + builder.ToReferenceFrame(canvasFrame)); michael@0: } michael@0: } michael@0: michael@0: nsRect dirtyRect = visibleRegion.GetBounds(); michael@0: builder.EnterPresShell(aFrame, dirtyRect); michael@0: { michael@0: PROFILER_LABEL("nsLayoutUtils","PaintFrame::BuildDisplayList"); michael@0: aFrame->BuildDisplayListForStackingContext(&builder, dirtyRect, &list); michael@0: } michael@0: const bool paintAllContinuations = aFlags & PAINT_ALL_CONTINUATIONS; michael@0: NS_ASSERTION(!paintAllContinuations || !aFrame->GetPrevContinuation(), michael@0: "If painting all continuations, the frame must be " michael@0: "first-continuation"); michael@0: michael@0: nsIAtom* frameType = aFrame->GetType(); michael@0: michael@0: if (paintAllContinuations) { michael@0: nsIFrame* currentFrame = aFrame; michael@0: while ((currentFrame = currentFrame->GetNextContinuation()) != nullptr) { michael@0: PROFILER_LABEL("nsLayoutUtils","PaintFrame::ContinuationsBuildDisplayList"); michael@0: nsRect frameDirty = dirtyRect - builder.ToReferenceFrame(currentFrame); michael@0: currentFrame->BuildDisplayListForStackingContext(&builder, michael@0: frameDirty, &list); michael@0: } michael@0: } michael@0: michael@0: // For the viewport frame in print preview/page layout we want to paint michael@0: // the grey background behind the page, not the canvas color. michael@0: if (frameType == nsGkAtoms::viewportFrame && michael@0: nsLayoutUtils::NeedsPrintPreviewBackground(presContext)) { michael@0: nsRect bounds = nsRect(builder.ToReferenceFrame(aFrame), michael@0: aFrame->GetSize()); michael@0: presShell->AddPrintPreviewBackgroundItem(builder, list, aFrame, bounds); michael@0: } else if (frameType != nsGkAtoms::pageFrame) { michael@0: // For printing, this function is first called on an nsPageFrame, which michael@0: // creates a display list with a PageContent item. The PageContent item's michael@0: // paint function calls this function on the nsPageFrame's child which is michael@0: // an nsPageContentFrame. We only want to add the canvas background color michael@0: // item once, for the nsPageContentFrame. michael@0: michael@0: // Add the canvas background color to the bottom of the list. This michael@0: // happens after we've built the list so that AddCanvasBackgroundColorItem michael@0: // can monkey with the contents if necessary. michael@0: canvasArea.IntersectRect(canvasArea, visibleRegion.GetBounds()); michael@0: presShell->AddCanvasBackgroundColorItem( michael@0: builder, list, aFrame, canvasArea, aBackstop); michael@0: michael@0: // If the passed in backstop color makes us draw something different from michael@0: // normal, we need to flush layers. michael@0: if ((aFlags & PAINT_WIDGET_LAYERS) && !willFlushRetainedLayers) { michael@0: nsView* view = aFrame->GetView(); michael@0: if (view) { michael@0: nscolor backstop = presShell->ComputeBackstopColor(view); michael@0: // The PresShell's canvas background color doesn't get updated until michael@0: // EnterPresShell, so this check has to be done after that. michael@0: nscolor canvasColor = presShell->GetCanvasBackground(); michael@0: if (NS_ComposeColors(aBackstop, canvasColor) != michael@0: NS_ComposeColors(backstop, canvasColor)) { michael@0: willFlushRetainedLayers = true; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: builder.LeavePresShell(aFrame, dirtyRect); michael@0: michael@0: if (builder.GetHadToIgnorePaintSuppression()) { michael@0: willFlushRetainedLayers = true; michael@0: } michael@0: michael@0: #ifdef MOZ_DUMP_PAINTING michael@0: FILE* savedDumpFile = gfxUtils::sDumpPaintFile; michael@0: if (gfxUtils::sDumpPaintList || gfxUtils::sDumpPainting) { michael@0: if (gfxUtils::sDumpPaintingToFile) { michael@0: nsCString string("dump-"); michael@0: string.AppendInt(gPaintCount); michael@0: string.Append(".html"); michael@0: gfxUtils::sDumpPaintFile = fopen(string.BeginReading(), "w"); michael@0: } else { michael@0: gfxUtils::sDumpPaintFile = stderr; michael@0: } michael@0: if (gfxUtils::sDumpPaintingToFile) { michael@0: fprintf_stderr(gfxUtils::sDumpPaintFile, ""); michael@0: } michael@0: fprintf_stderr(gfxUtils::sDumpPaintFile, "Painting --- before optimization (dirty %d,%d,%d,%d):\n", michael@0: dirtyRect.x, dirtyRect.y, dirtyRect.width, dirtyRect.height); michael@0: nsFrame::PrintDisplayList(&builder, list, gfxUtils::sDumpPaintFile, gfxUtils::sDumpPaintingToFile); michael@0: if (gfxUtils::sDumpPaintingToFile) { michael@0: fprintf_stderr(gfxUtils::sDumpPaintFile, ""); michael@0: } michael@0: fprintf_stderr(gfxUtils::sDumpPaintFile, "Painting --- after optimization:\n"); michael@0: nsFrame::PrintDisplayList(&builder, list, gfxUtils::sDumpPaintFile, gfxUtils::sDumpPaintingToFile); michael@0: michael@0: fprintf_stderr(gfxUtils::sDumpPaintFile, "Painting --- retained layer tree:\n"); michael@0: nsIWidget* widget = aFrame->GetNearestWidget(); michael@0: if (widget) { michael@0: nsRefPtr layerManager = widget->GetLayerManager(); michael@0: if (layerManager) { michael@0: FrameLayerBuilder::DumpRetainedLayerTree(layerManager, gfxUtils::sDumpPaintFile, michael@0: gfxUtils::sDumpPaintingToFile); michael@0: } michael@0: } michael@0: if (gfxUtils::sDumpPaintingToFile) { michael@0: fprintf(gfxUtils::sDumpPaintFile, ""); michael@0: fclose(gfxUtils::sDumpPaintFile); michael@0: } michael@0: gfxUtils::sDumpPaintFile = savedDumpFile; michael@0: gPaintCount++; michael@0: } michael@0: #endif michael@0: michael@0: // Update the widget's opaque region information. This sets michael@0: // glass boundaries on Windows. Also set up plugin clip regions and bounds. michael@0: if ((aFlags & PAINT_WIDGET_LAYERS) && michael@0: !willFlushRetainedLayers && michael@0: !(aFlags & PAINT_DOCUMENT_RELATIVE)) { michael@0: nsIWidget *widget = aFrame->GetNearestWidget(); michael@0: if (widget) { michael@0: nsRegion excludedRegion = builder.GetExcludedGlassRegion(); michael@0: excludedRegion.Sub(excludedRegion, visibleRegion); michael@0: nsIntRegion windowRegion(excludedRegion.ToNearestPixels(presContext->AppUnitsPerDevPixel())); michael@0: widget->UpdateOpaqueRegion(windowRegion); michael@0: } michael@0: } michael@0: michael@0: if (builder.WillComputePluginGeometry()) { michael@0: nsRefPtr layerManager; michael@0: nsIWidget* widget = aFrame->GetNearestWidget(); michael@0: if (widget) { michael@0: layerManager = widget->GetLayerManager(); michael@0: } michael@0: michael@0: rootPresContext->ComputePluginGeometryUpdates(aFrame, &builder, &list); michael@0: michael@0: // We're not going to get a WillPaintWindow event here if we didn't do michael@0: // widget invalidation, so just apply the plugin geometry update here instead. michael@0: // We could instead have the compositor send back an equivalent to WillPaintWindow, michael@0: // but it should be close enough to now not to matter. michael@0: if (layerManager && !layerManager->NeedsWidgetInvalidation()) { michael@0: rootPresContext->ApplyPluginGeometryUpdates(); michael@0: } michael@0: michael@0: // We told the compositor thread not to composite when it received the transaction because michael@0: // we wanted to update plugins first. Schedule the composite now. michael@0: if (layerManager) { michael@0: layerManager->Composite(); michael@0: } michael@0: } michael@0: michael@0: michael@0: // Flush the list so we don't trigger the IsEmpty-on-destruction assertion michael@0: list.DeleteAll(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /** michael@0: * Uses a binary search for find where the cursor falls in the line of text michael@0: * It also keeps track of the part of the string that has already been measured michael@0: * so it doesn't have to keep measuring the same text over and over michael@0: * michael@0: * @param "aBaseWidth" contains the width in twips of the portion michael@0: * of the text that has already been measured, and aBaseInx contains michael@0: * the index of the text that has already been measured. michael@0: * michael@0: * @param aTextWidth returns the (in twips) the length of the text that falls michael@0: * before the cursor aIndex contains the index of the text where the cursor falls michael@0: */ michael@0: bool michael@0: nsLayoutUtils::BinarySearchForPosition(nsRenderingContext* aRendContext, michael@0: const char16_t* aText, michael@0: int32_t aBaseWidth, michael@0: int32_t aBaseInx, michael@0: int32_t aStartInx, michael@0: int32_t aEndInx, michael@0: int32_t aCursorPos, michael@0: int32_t& aIndex, michael@0: int32_t& aTextWidth) michael@0: { michael@0: int32_t range = aEndInx - aStartInx; michael@0: if ((range == 1) || (range == 2 && NS_IS_HIGH_SURROGATE(aText[aStartInx]))) { michael@0: aIndex = aStartInx + aBaseInx; michael@0: aTextWidth = aRendContext->GetWidth(aText, aIndex); michael@0: return true; michael@0: } michael@0: michael@0: int32_t inx = aStartInx + (range / 2); michael@0: michael@0: // Make sure we don't leave a dangling low surrogate michael@0: if (NS_IS_HIGH_SURROGATE(aText[inx-1])) michael@0: inx++; michael@0: michael@0: int32_t textWidth = aRendContext->GetWidth(aText, inx); michael@0: michael@0: int32_t fullWidth = aBaseWidth + textWidth; michael@0: if (fullWidth == aCursorPos) { michael@0: aTextWidth = textWidth; michael@0: aIndex = inx; michael@0: return true; michael@0: } else if (aCursorPos < fullWidth) { michael@0: aTextWidth = aBaseWidth; michael@0: if (BinarySearchForPosition(aRendContext, aText, aBaseWidth, aBaseInx, aStartInx, inx, aCursorPos, aIndex, aTextWidth)) { michael@0: return true; michael@0: } michael@0: } else { michael@0: aTextWidth = fullWidth; michael@0: if (BinarySearchForPosition(aRendContext, aText, aBaseWidth, aBaseInx, inx, aEndInx, aCursorPos, aIndex, aTextWidth)) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static void michael@0: AddBoxesForFrame(nsIFrame* aFrame, michael@0: nsLayoutUtils::BoxCallback* aCallback) michael@0: { michael@0: nsIAtom* pseudoType = aFrame->StyleContext()->GetPseudo(); michael@0: michael@0: if (pseudoType == nsCSSAnonBoxes::tableOuter) { michael@0: AddBoxesForFrame(aFrame->GetFirstPrincipalChild(), aCallback); michael@0: nsIFrame* kid = aFrame->GetFirstChild(nsIFrame::kCaptionList); michael@0: if (kid) { michael@0: AddBoxesForFrame(kid, aCallback); michael@0: } michael@0: } else if (pseudoType == nsCSSAnonBoxes::mozAnonymousBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozAnonymousPositionedBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozMathMLAnonymousBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozXULAnonymousBlock) { michael@0: for (nsIFrame* kid = aFrame->GetFirstPrincipalChild(); kid; kid = kid->GetNextSibling()) { michael@0: AddBoxesForFrame(kid, aCallback); michael@0: } michael@0: } else { michael@0: aCallback->AddBox(aFrame); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::GetAllInFlowBoxes(nsIFrame* aFrame, BoxCallback* aCallback) michael@0: { michael@0: while (aFrame) { michael@0: AddBoxesForFrame(aFrame, aCallback); michael@0: aFrame = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(aFrame); michael@0: } michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetFirstNonAnonymousFrame(nsIFrame* aFrame) michael@0: { michael@0: while (aFrame) { michael@0: nsIAtom* pseudoType = aFrame->StyleContext()->GetPseudo(); michael@0: michael@0: if (pseudoType == nsCSSAnonBoxes::tableOuter) { michael@0: nsIFrame* f = GetFirstNonAnonymousFrame(aFrame->GetFirstPrincipalChild()); michael@0: if (f) { michael@0: return f; michael@0: } michael@0: nsIFrame* kid = aFrame->GetFirstChild(nsIFrame::kCaptionList); michael@0: if (kid) { michael@0: f = GetFirstNonAnonymousFrame(kid); michael@0: if (f) { michael@0: return f; michael@0: } michael@0: } michael@0: } else if (pseudoType == nsCSSAnonBoxes::mozAnonymousBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozAnonymousPositionedBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozMathMLAnonymousBlock || michael@0: pseudoType == nsCSSAnonBoxes::mozXULAnonymousBlock) { michael@0: for (nsIFrame* kid = aFrame->GetFirstPrincipalChild(); kid; kid = kid->GetNextSibling()) { michael@0: nsIFrame* f = GetFirstNonAnonymousFrame(kid); michael@0: if (f) { michael@0: return f; michael@0: } michael@0: } michael@0: } else { michael@0: return aFrame; michael@0: } michael@0: michael@0: aFrame = nsLayoutUtils::GetNextContinuationOrIBSplitSibling(aFrame); michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: struct BoxToRect : public nsLayoutUtils::BoxCallback { michael@0: nsIFrame* mRelativeTo; michael@0: nsLayoutUtils::RectCallback* mCallback; michael@0: uint32_t mFlags; michael@0: michael@0: BoxToRect(nsIFrame* aRelativeTo, nsLayoutUtils::RectCallback* aCallback, michael@0: uint32_t aFlags) michael@0: : mRelativeTo(aRelativeTo), mCallback(aCallback), mFlags(aFlags) {} michael@0: michael@0: virtual void AddBox(nsIFrame* aFrame) MOZ_OVERRIDE { michael@0: nsRect r; michael@0: nsIFrame* outer = nsSVGUtils::GetOuterSVGFrameAndCoveredRegion(aFrame, &r); michael@0: if (!outer) { michael@0: outer = aFrame; michael@0: switch (mFlags & nsLayoutUtils::RECTS_WHICH_BOX_MASK) { michael@0: case nsLayoutUtils::RECTS_USE_CONTENT_BOX: michael@0: r = aFrame->GetContentRectRelativeToSelf(); michael@0: break; michael@0: case nsLayoutUtils::RECTS_USE_PADDING_BOX: michael@0: r = aFrame->GetPaddingRectRelativeToSelf(); michael@0: break; michael@0: case nsLayoutUtils::RECTS_USE_MARGIN_BOX: michael@0: r = aFrame->GetMarginRectRelativeToSelf(); michael@0: break; michael@0: default: // Use the border box michael@0: r = aFrame->GetRectRelativeToSelf(); michael@0: } michael@0: } michael@0: if (mFlags & nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS) { michael@0: r = nsLayoutUtils::TransformFrameRectToAncestor(outer, r, mRelativeTo); michael@0: } else { michael@0: r += outer->GetOffsetTo(mRelativeTo); michael@0: } michael@0: mCallback->AddRect(r); michael@0: } michael@0: }; michael@0: michael@0: void michael@0: nsLayoutUtils::GetAllInFlowRects(nsIFrame* aFrame, nsIFrame* aRelativeTo, michael@0: RectCallback* aCallback, uint32_t aFlags) michael@0: { michael@0: BoxToRect converter(aRelativeTo, aCallback, aFlags); michael@0: GetAllInFlowBoxes(aFrame, &converter); michael@0: } michael@0: michael@0: nsLayoutUtils::RectAccumulator::RectAccumulator() : mSeenFirstRect(false) {} michael@0: michael@0: void nsLayoutUtils::RectAccumulator::AddRect(const nsRect& aRect) { michael@0: mResultRect.UnionRect(mResultRect, aRect); michael@0: if (!mSeenFirstRect) { michael@0: mSeenFirstRect = true; michael@0: mFirstRect = aRect; michael@0: } michael@0: } michael@0: michael@0: nsLayoutUtils::RectListBuilder::RectListBuilder(DOMRectList* aList) michael@0: : mRectList(aList) michael@0: { michael@0: } michael@0: michael@0: void nsLayoutUtils::RectListBuilder::AddRect(const nsRect& aRect) { michael@0: nsRefPtr rect = new DOMRect(mRectList); michael@0: michael@0: rect->SetLayoutRect(aRect); michael@0: mRectList->Append(rect); michael@0: } michael@0: michael@0: nsIFrame* nsLayoutUtils::GetContainingBlockForClientRect(nsIFrame* aFrame) michael@0: { michael@0: return aFrame->PresContext()->PresShell()->GetRootFrame(); michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::GetAllInFlowRectsUnion(nsIFrame* aFrame, nsIFrame* aRelativeTo, michael@0: uint32_t aFlags) { michael@0: RectAccumulator accumulator; michael@0: GetAllInFlowRects(aFrame, aRelativeTo, &accumulator, aFlags); michael@0: return accumulator.mResultRect.IsEmpty() ? accumulator.mFirstRect michael@0: : accumulator.mResultRect; michael@0: } michael@0: michael@0: nsRect michael@0: nsLayoutUtils::GetTextShadowRectsUnion(const nsRect& aTextAndDecorationsRect, michael@0: nsIFrame* aFrame, michael@0: uint32_t aFlags) michael@0: { michael@0: const nsStyleText* textStyle = aFrame->StyleText(); michael@0: if (!textStyle->HasTextShadow()) michael@0: return aTextAndDecorationsRect; michael@0: michael@0: nsRect resultRect = aTextAndDecorationsRect; michael@0: int32_t A2D = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: for (uint32_t i = 0; i < textStyle->mTextShadow->Length(); ++i) { michael@0: nsCSSShadowItem* shadow = textStyle->mTextShadow->ShadowAt(i); michael@0: nsMargin blur = nsContextBoxBlur::GetBlurRadiusMargin(shadow->mRadius, A2D); michael@0: if ((aFlags & EXCLUDE_BLUR_SHADOWS) && blur != nsMargin(0, 0, 0, 0)) michael@0: continue; michael@0: michael@0: nsRect tmpRect(aTextAndDecorationsRect); michael@0: michael@0: tmpRect.MoveBy(nsPoint(shadow->mXOffset, shadow->mYOffset)); michael@0: tmpRect.Inflate(blur); michael@0: michael@0: resultRect.UnionRect(resultRect, tmpRect); michael@0: } michael@0: return resultRect; michael@0: } michael@0: michael@0: nsresult michael@0: nsLayoutUtils::GetFontMetricsForFrame(const nsIFrame* aFrame, michael@0: nsFontMetrics** aFontMetrics, michael@0: float aInflation) michael@0: { michael@0: return nsLayoutUtils::GetFontMetricsForStyleContext(aFrame->StyleContext(), michael@0: aFontMetrics, michael@0: aInflation); michael@0: } michael@0: michael@0: nsresult michael@0: nsLayoutUtils::GetFontMetricsForStyleContext(nsStyleContext* aStyleContext, michael@0: nsFontMetrics** aFontMetrics, michael@0: float aInflation) michael@0: { michael@0: // pass the user font set object into the device context to pass along to CreateFontGroup michael@0: nsPresContext* pc = aStyleContext->PresContext(); michael@0: gfxUserFontSet* fs = pc->GetUserFontSet(); michael@0: gfxTextPerfMetrics* tp = pc->GetTextPerfMetrics(); michael@0: michael@0: nsFont font = aStyleContext->StyleFont()->mFont; michael@0: // We need to not run font.size through floats when it's large since michael@0: // doing so would be lossy. Fortunately, in such cases, aInflation is michael@0: // guaranteed to be 1.0f. michael@0: if (aInflation != 1.0f) { michael@0: font.size = NSToCoordRound(font.size * aInflation); michael@0: } michael@0: return pc->DeviceContext()->GetMetricsFor( michael@0: font, aStyleContext->StyleFont()->mLanguage, michael@0: fs, tp, *aFontMetrics); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::FindChildContainingDescendant(nsIFrame* aParent, nsIFrame* aDescendantFrame) michael@0: { michael@0: nsIFrame* result = aDescendantFrame; michael@0: michael@0: while (result) { michael@0: nsIFrame* parent = result->GetParent(); michael@0: if (parent == aParent) { michael@0: break; michael@0: } michael@0: michael@0: // The frame is not an immediate child of aParent so walk up another level michael@0: result = parent; michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsBlockFrame* michael@0: nsLayoutUtils::GetAsBlock(nsIFrame* aFrame) michael@0: { michael@0: nsBlockFrame* block = do_QueryFrame(aFrame); michael@0: return block; michael@0: } michael@0: michael@0: nsBlockFrame* michael@0: nsLayoutUtils::FindNearestBlockAncestor(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame* nextAncestor; michael@0: for (nextAncestor = aFrame->GetParent(); nextAncestor; michael@0: nextAncestor = nextAncestor->GetParent()) { michael@0: nsBlockFrame* block = GetAsBlock(nextAncestor); michael@0: if (block) michael@0: return block; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetNonGeneratedAncestor(nsIFrame* aFrame) michael@0: { michael@0: if (!(aFrame->GetStateBits() & NS_FRAME_GENERATED_CONTENT)) michael@0: return aFrame; michael@0: michael@0: nsIFrame* f = aFrame; michael@0: do { michael@0: f = GetParentOrPlaceholderFor(f); michael@0: } while (f->GetStateBits() & NS_FRAME_GENERATED_CONTENT); michael@0: return f; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetParentOrPlaceholderFor(nsIFrame* aFrame) michael@0: { michael@0: if ((aFrame->GetStateBits() & NS_FRAME_OUT_OF_FLOW) michael@0: && !aFrame->GetPrevInFlow()) { michael@0: return aFrame->PresContext()->PresShell()->FrameManager()-> michael@0: GetPlaceholderFrameFor(aFrame); michael@0: } michael@0: return aFrame->GetParent(); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetParentOrPlaceholderForCrossDoc(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame* f = GetParentOrPlaceholderFor(aFrame); michael@0: if (f) michael@0: return f; michael@0: return GetCrossDocParentFrame(aFrame); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::GetNextContinuationOrIBSplitSibling(nsIFrame *aFrame) michael@0: { michael@0: nsIFrame *result = aFrame->GetNextContinuation(); michael@0: if (result) michael@0: return result; michael@0: michael@0: if ((aFrame->GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) != 0) { michael@0: // We only store the ib-split sibling annotation with the first michael@0: // frame in the continuation chain. Walk back to find that frame now. michael@0: aFrame = aFrame->FirstContinuation(); michael@0: michael@0: void* value = aFrame->Properties().Get(nsIFrame::IBSplitSibling()); michael@0: return static_cast(value); michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsLayoutUtils::FirstContinuationOrIBSplitSibling(nsIFrame *aFrame) michael@0: { michael@0: nsIFrame *result = aFrame->FirstContinuation(); michael@0: if (result->GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) { michael@0: while (true) { michael@0: nsIFrame *f = static_cast michael@0: (result->Properties().Get(nsIFrame::IBSplitPrevSibling())); michael@0: if (!f) michael@0: break; michael@0: result = f; michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::IsFirstContinuationOrIBSplitSibling(nsIFrame *aFrame) michael@0: { michael@0: if (aFrame->GetPrevContinuation()) { michael@0: return false; michael@0: } michael@0: if ((aFrame->GetStateBits() & NS_FRAME_PART_OF_IBSPLIT) && michael@0: aFrame->Properties().Get(nsIFrame::IBSplitPrevSibling())) { michael@0: return false; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::IsViewportScrollbarFrame(nsIFrame* aFrame) michael@0: { michael@0: if (!aFrame) michael@0: return false; michael@0: michael@0: nsIFrame* rootScrollFrame = michael@0: aFrame->PresContext()->PresShell()->GetRootScrollFrame(); michael@0: if (!rootScrollFrame) michael@0: return false; michael@0: michael@0: nsIScrollableFrame* rootScrollableFrame = do_QueryFrame(rootScrollFrame); michael@0: NS_ASSERTION(rootScrollableFrame, "The root scorollable frame is null"); michael@0: michael@0: if (!IsProperAncestorFrame(rootScrollFrame, aFrame)) michael@0: return false; michael@0: michael@0: nsIFrame* rootScrolledFrame = rootScrollableFrame->GetScrolledFrame(); michael@0: return !(rootScrolledFrame == aFrame || michael@0: IsProperAncestorFrame(rootScrolledFrame, aFrame)); michael@0: } michael@0: michael@0: static nscoord AddPercents(nsLayoutUtils::IntrinsicWidthType aType, michael@0: nscoord aCurrent, float aPercent) michael@0: { michael@0: nscoord result = aCurrent; michael@0: if (aPercent > 0.0f && aType == nsLayoutUtils::PREF_WIDTH) { michael@0: // XXX Should we also consider percentages for min widths, up to a michael@0: // limit? michael@0: if (aPercent >= 1.0f) michael@0: result = nscoord_MAX; michael@0: else michael@0: result = NSToCoordRound(float(result) / (1.0f - aPercent)); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: // Use only for widths/heights (or their min/max), since it clamps michael@0: // negative calc() results to 0. michael@0: static bool GetAbsoluteCoord(const nsStyleCoord& aStyle, nscoord& aResult) michael@0: { michael@0: if (aStyle.IsCalcUnit()) { michael@0: if (aStyle.CalcHasPercent()) { michael@0: return false; michael@0: } michael@0: // If it has no percents, we can pass 0 for the percentage basis. michael@0: aResult = nsRuleNode::ComputeComputedCalc(aStyle, 0); michael@0: if (aResult < 0) michael@0: aResult = 0; michael@0: return true; michael@0: } michael@0: michael@0: if (eStyleUnit_Coord != aStyle.GetUnit()) michael@0: return false; michael@0: michael@0: aResult = aStyle.GetCoordValue(); michael@0: NS_ASSERTION(aResult >= 0, "negative widths not allowed"); michael@0: return true; michael@0: } michael@0: michael@0: // Only call on style coords for which GetAbsoluteCoord returned false. michael@0: static bool michael@0: GetPercentHeight(const nsStyleCoord& aStyle, michael@0: nsIFrame* aFrame, michael@0: nscoord& aResult) michael@0: { michael@0: if (eStyleUnit_Percent != aStyle.GetUnit() && michael@0: !aStyle.IsCalcUnit()) michael@0: return false; michael@0: michael@0: MOZ_ASSERT(!aStyle.IsCalcUnit() || aStyle.CalcHasPercent(), michael@0: "GetAbsoluteCoord should have handled this"); michael@0: michael@0: nsIFrame *f = aFrame->GetContainingBlock(); michael@0: if (!f) { michael@0: NS_NOTREACHED("top of frame tree not a containing block"); michael@0: return false; michael@0: } michael@0: michael@0: // During reflow, nsHTMLScrollFrame::ReflowScrolledFrame uses michael@0: // SetComputedHeight on the reflow state for its child to propagate its michael@0: // computed height to the scrolled content. So here we skip to the scroll michael@0: // frame that contains this scrolled content in order to get the same michael@0: // behavior as layout when computing percentage heights. michael@0: if (f->StyleContext()->GetPseudo() == nsCSSAnonBoxes::scrolledContent) { michael@0: f = f->GetParent(); michael@0: } michael@0: michael@0: const nsStylePosition *pos = f->StylePosition(); michael@0: nscoord h; michael@0: if (!GetAbsoluteCoord(pos->mHeight, h) && michael@0: !GetPercentHeight(pos->mHeight, f, h)) { michael@0: NS_ASSERTION(pos->mHeight.GetUnit() == eStyleUnit_Auto || michael@0: pos->mHeight.HasPercent(), michael@0: "unknown height unit"); michael@0: nsIAtom* fType = f->GetType(); michael@0: if (fType != nsGkAtoms::viewportFrame && fType != nsGkAtoms::canvasFrame && michael@0: fType != nsGkAtoms::pageContentFrame) { michael@0: // There's no basis for the percentage height, so it acts like auto. michael@0: // Should we consider a max-height < min-height pair a basis for michael@0: // percentage heights? The spec is somewhat unclear, and not doing michael@0: // so is simpler and avoids troubling discontinuities in behavior, michael@0: // so I'll choose not to. -LDB michael@0: return false; michael@0: } michael@0: michael@0: NS_ASSERTION(pos->mHeight.GetUnit() == eStyleUnit_Auto, michael@0: "Unexpected height unit for viewport or canvas or page-content"); michael@0: // For the viewport, canvas, and page-content kids, the percentage michael@0: // basis is just the parent height. michael@0: h = f->GetSize().height; michael@0: if (h == NS_UNCONSTRAINEDSIZE) { michael@0: // We don't have a percentage basis after all michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: nscoord maxh; michael@0: if (GetAbsoluteCoord(pos->mMaxHeight, maxh) || michael@0: GetPercentHeight(pos->mMaxHeight, f, maxh)) { michael@0: if (maxh < h) michael@0: h = maxh; michael@0: } else { michael@0: NS_ASSERTION(pos->mMaxHeight.GetUnit() == eStyleUnit_None || michael@0: pos->mMaxHeight.HasPercent(), michael@0: "unknown max-height unit"); michael@0: } michael@0: michael@0: nscoord minh; michael@0: if (GetAbsoluteCoord(pos->mMinHeight, minh) || michael@0: GetPercentHeight(pos->mMinHeight, f, minh)) { michael@0: if (minh > h) michael@0: h = minh; michael@0: } else { michael@0: NS_ASSERTION(pos->mMinHeight.HasPercent(), michael@0: "unknown min-height unit"); michael@0: } michael@0: michael@0: if (aStyle.IsCalcUnit()) { michael@0: aResult = std::max(nsRuleNode::ComputeComputedCalc(aStyle, h), 0); michael@0: return true; michael@0: } michael@0: michael@0: aResult = NSToCoordRound(aStyle.GetPercentValue() * h); michael@0: return true; michael@0: } michael@0: michael@0: // Handles only -moz-max-content and -moz-min-content, and michael@0: // -moz-fit-content for min-width and max-width, since the others michael@0: // (-moz-fit-content for width, and -moz-available) have no effect on michael@0: // intrinsic widths. michael@0: enum eWidthProperty { PROP_WIDTH, PROP_MAX_WIDTH, PROP_MIN_WIDTH }; michael@0: static bool michael@0: GetIntrinsicCoord(const nsStyleCoord& aStyle, michael@0: nsRenderingContext* aRenderingContext, michael@0: nsIFrame* aFrame, michael@0: eWidthProperty aProperty, michael@0: nscoord& aResult) michael@0: { michael@0: NS_PRECONDITION(aProperty == PROP_WIDTH || aProperty == PROP_MAX_WIDTH || michael@0: aProperty == PROP_MIN_WIDTH, "unexpected property"); michael@0: if (aStyle.GetUnit() != eStyleUnit_Enumerated) michael@0: return false; michael@0: int32_t val = aStyle.GetIntValue(); michael@0: NS_ASSERTION(val == NS_STYLE_WIDTH_MAX_CONTENT || michael@0: val == NS_STYLE_WIDTH_MIN_CONTENT || michael@0: val == NS_STYLE_WIDTH_FIT_CONTENT || michael@0: val == NS_STYLE_WIDTH_AVAILABLE, michael@0: "unexpected enumerated value for width property"); michael@0: if (val == NS_STYLE_WIDTH_AVAILABLE) michael@0: return false; michael@0: if (val == NS_STYLE_WIDTH_FIT_CONTENT) { michael@0: if (aProperty == PROP_WIDTH) michael@0: return false; // handle like 'width: auto' michael@0: if (aProperty == PROP_MAX_WIDTH) michael@0: // constrain large 'width' values down to -moz-max-content michael@0: val = NS_STYLE_WIDTH_MAX_CONTENT; michael@0: else michael@0: // constrain small 'width' or 'max-width' values up to -moz-min-content michael@0: val = NS_STYLE_WIDTH_MIN_CONTENT; michael@0: } michael@0: michael@0: NS_ASSERTION(val == NS_STYLE_WIDTH_MAX_CONTENT || michael@0: val == NS_STYLE_WIDTH_MIN_CONTENT, michael@0: "should have reduced everything remaining to one of these"); michael@0: michael@0: // If aFrame is a container for font size inflation, then shrink michael@0: // wrapping inside of it should not apply font size inflation. michael@0: AutoMaybeDisableFontInflation an(aFrame); michael@0: michael@0: if (val == NS_STYLE_WIDTH_MAX_CONTENT) michael@0: aResult = aFrame->GetPrefWidth(aRenderingContext); michael@0: else michael@0: aResult = aFrame->GetMinWidth(aRenderingContext); michael@0: return true; michael@0: } michael@0: michael@0: #undef DEBUG_INTRINSIC_WIDTH michael@0: michael@0: #ifdef DEBUG_INTRINSIC_WIDTH michael@0: static int32_t gNoiseIndent = 0; michael@0: #endif michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::IntrinsicForContainer(nsRenderingContext *aRenderingContext, michael@0: nsIFrame *aFrame, michael@0: IntrinsicWidthType aType, michael@0: uint32_t aFlags) michael@0: { michael@0: NS_PRECONDITION(aFrame, "null frame"); michael@0: NS_PRECONDITION(aType == MIN_WIDTH || aType == PREF_WIDTH, "bad type"); michael@0: michael@0: #ifdef DEBUG_INTRINSIC_WIDTH michael@0: nsFrame::IndentBy(stderr, gNoiseIndent); michael@0: static_cast(aFrame)->ListTag(stderr); michael@0: printf_stderr(" %s intrinsic width for container:\n", michael@0: aType == MIN_WIDTH ? "min" : "pref"); michael@0: #endif michael@0: michael@0: // If aFrame is a container for font size inflation, then shrink michael@0: // wrapping inside of it should not apply font size inflation. michael@0: AutoMaybeDisableFontInflation an(aFrame); michael@0: michael@0: nsIFrame::IntrinsicWidthOffsetData offsets = michael@0: aFrame->IntrinsicWidthOffsets(aRenderingContext); michael@0: michael@0: const nsStylePosition *stylePos = aFrame->StylePosition(); michael@0: uint8_t boxSizing = stylePos->mBoxSizing; michael@0: const nsStyleCoord &styleWidth = stylePos->mWidth; michael@0: const nsStyleCoord &styleMinWidth = stylePos->mMinWidth; michael@0: const nsStyleCoord &styleMaxWidth = stylePos->mMaxWidth; michael@0: michael@0: // We build up two values starting with the content box, and then michael@0: // adding padding, border and margin. The result is normally michael@0: // |result|. Then, when we handle 'width', 'min-width', and michael@0: // 'max-width', we use the results we've been building in |min| as a michael@0: // minimum, overriding 'min-width'. This ensures two things: michael@0: // * that we don't let a value of 'box-sizing' specifying a width michael@0: // smaller than the padding/border inside the box-sizing box give michael@0: // a content width less than zero michael@0: // * that we prevent tables from becoming smaller than their michael@0: // intrinsic minimum width michael@0: nscoord result = 0, min = 0; michael@0: michael@0: nscoord maxw; michael@0: bool haveFixedMaxWidth = GetAbsoluteCoord(styleMaxWidth, maxw); michael@0: nscoord minw; michael@0: bool haveFixedMinWidth = GetAbsoluteCoord(styleMinWidth, minw); michael@0: michael@0: // If we have a specified width (or a specified 'min-width' greater michael@0: // than the specified 'max-width', which works out to the same thing), michael@0: // don't even bother getting the frame's intrinsic width, because in michael@0: // this case GetAbsoluteCoord(styleWidth, w) will always succeed, so michael@0: // we'll never need the intrinsic dimensions. michael@0: if (styleWidth.GetUnit() == eStyleUnit_Enumerated && michael@0: (styleWidth.GetIntValue() == NS_STYLE_WIDTH_MAX_CONTENT || michael@0: styleWidth.GetIntValue() == NS_STYLE_WIDTH_MIN_CONTENT)) { michael@0: // -moz-fit-content and -moz-available enumerated widths compute intrinsic michael@0: // widths just like auto. michael@0: // For -moz-max-content and -moz-min-content, we handle them like michael@0: // specified widths, but ignore box-sizing. michael@0: boxSizing = NS_STYLE_BOX_SIZING_CONTENT; michael@0: } else if (!styleWidth.ConvertsToLength() && michael@0: !(haveFixedMinWidth && haveFixedMaxWidth && maxw <= minw)) { michael@0: #ifdef DEBUG_INTRINSIC_WIDTH michael@0: ++gNoiseIndent; michael@0: #endif michael@0: if (aType == MIN_WIDTH) michael@0: result = aFrame->GetMinWidth(aRenderingContext); michael@0: else michael@0: result = aFrame->GetPrefWidth(aRenderingContext); michael@0: #ifdef DEBUG_INTRINSIC_WIDTH michael@0: --gNoiseIndent; michael@0: nsFrame::IndentBy(stderr, gNoiseIndent); michael@0: static_cast(aFrame)->ListTag(stderr); michael@0: printf_stderr(" %s intrinsic width from frame is %d.\n", michael@0: aType == MIN_WIDTH ? "min" : "pref", result); michael@0: #endif michael@0: michael@0: // Handle elements with an intrinsic ratio (or size) and a specified michael@0: // height, min-height, or max-height. michael@0: const nsStyleCoord &styleHeight = stylePos->mHeight; michael@0: const nsStyleCoord &styleMinHeight = stylePos->mMinHeight; michael@0: const nsStyleCoord &styleMaxHeight = stylePos->mMaxHeight; michael@0: if (styleHeight.GetUnit() != eStyleUnit_Auto || michael@0: !(styleMinHeight.GetUnit() == eStyleUnit_Coord && michael@0: styleMinHeight.GetCoordValue() == 0) || michael@0: styleMaxHeight.GetUnit() != eStyleUnit_None) { michael@0: michael@0: nsSize ratio = aFrame->GetIntrinsicRatio(); michael@0: michael@0: if (ratio.height != 0) { michael@0: nscoord heightTakenByBoxSizing = 0; michael@0: switch (boxSizing) { michael@0: case NS_STYLE_BOX_SIZING_BORDER: { michael@0: const nsStyleBorder* styleBorder = aFrame->StyleBorder(); michael@0: heightTakenByBoxSizing += michael@0: styleBorder->GetComputedBorder().TopBottom(); michael@0: // fall through michael@0: } michael@0: case NS_STYLE_BOX_SIZING_PADDING: { michael@0: if (!(aFlags & IGNORE_PADDING)) { michael@0: const nsStylePadding* stylePadding = aFrame->StylePadding(); michael@0: nscoord pad; michael@0: if (GetAbsoluteCoord(stylePadding->mPadding.GetTop(), pad) || michael@0: GetPercentHeight(stylePadding->mPadding.GetTop(), aFrame, pad)) { michael@0: heightTakenByBoxSizing += pad; michael@0: } michael@0: if (GetAbsoluteCoord(stylePadding->mPadding.GetBottom(), pad) || michael@0: GetPercentHeight(stylePadding->mPadding.GetBottom(), aFrame, pad)) { michael@0: heightTakenByBoxSizing += pad; michael@0: } michael@0: } michael@0: // fall through michael@0: } michael@0: case NS_STYLE_BOX_SIZING_CONTENT: michael@0: default: michael@0: break; michael@0: } michael@0: michael@0: nscoord h; michael@0: if (GetAbsoluteCoord(styleHeight, h) || michael@0: GetPercentHeight(styleHeight, aFrame, h)) { michael@0: h = std::max(0, h - heightTakenByBoxSizing); michael@0: result = michael@0: NSToCoordRound(h * (float(ratio.width) / float(ratio.height))); michael@0: } michael@0: michael@0: if (GetAbsoluteCoord(styleMaxHeight, h) || michael@0: GetPercentHeight(styleMaxHeight, aFrame, h)) { michael@0: h = std::max(0, h - heightTakenByBoxSizing); michael@0: nscoord maxWidth = michael@0: NSToCoordRound(h * (float(ratio.width) / float(ratio.height))); michael@0: if (maxWidth < result) michael@0: result = maxWidth; michael@0: } michael@0: michael@0: if (GetAbsoluteCoord(styleMinHeight, h) || michael@0: GetPercentHeight(styleMinHeight, aFrame, h)) { michael@0: h = std::max(0, h - heightTakenByBoxSizing); michael@0: nscoord minWidth = michael@0: NSToCoordRound(h * (float(ratio.width) / float(ratio.height))); michael@0: if (minWidth > result) michael@0: result = minWidth; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (aFrame->GetType() == nsGkAtoms::tableFrame) { michael@0: // Tables can't shrink smaller than their intrinsic minimum width, michael@0: // no matter what. michael@0: min = aFrame->GetMinWidth(aRenderingContext); michael@0: } michael@0: michael@0: // We also need to track what has been added on outside of the box michael@0: // (controlled by 'box-sizing') where 'width', 'min-width' and michael@0: // 'max-width' are applied. We have to account for these properties michael@0: // after getting all the offsets (margin, border, padding) because michael@0: // percentages do not operate linearly. michael@0: // Doing this is ok because although percentages aren't handled michael@0: // linearly, they are handled monotonically. michael@0: nscoord coordOutsideWidth = 0; michael@0: float pctOutsideWidth = 0; michael@0: float pctTotal = 0.0f; michael@0: michael@0: if (!(aFlags & IGNORE_PADDING)) { michael@0: coordOutsideWidth += offsets.hPadding; michael@0: pctOutsideWidth += offsets.hPctPadding; michael@0: michael@0: if (boxSizing == NS_STYLE_BOX_SIZING_PADDING) { michael@0: min += coordOutsideWidth; michael@0: result = NSCoordSaturatingAdd(result, coordOutsideWidth); michael@0: pctTotal += pctOutsideWidth; michael@0: michael@0: coordOutsideWidth = 0; michael@0: pctOutsideWidth = 0.0f; michael@0: } michael@0: } michael@0: michael@0: coordOutsideWidth += offsets.hBorder; michael@0: michael@0: if (boxSizing == NS_STYLE_BOX_SIZING_BORDER) { michael@0: min += coordOutsideWidth; michael@0: result = NSCoordSaturatingAdd(result, coordOutsideWidth); michael@0: pctTotal += pctOutsideWidth; michael@0: michael@0: coordOutsideWidth = 0; michael@0: pctOutsideWidth = 0.0f; michael@0: } michael@0: michael@0: coordOutsideWidth += offsets.hMargin; michael@0: pctOutsideWidth += offsets.hPctMargin; michael@0: michael@0: min += coordOutsideWidth; michael@0: result = NSCoordSaturatingAdd(result, coordOutsideWidth); michael@0: pctTotal += pctOutsideWidth; michael@0: michael@0: nscoord w; michael@0: if (GetAbsoluteCoord(styleWidth, w) || michael@0: GetIntrinsicCoord(styleWidth, aRenderingContext, aFrame, michael@0: PROP_WIDTH, w)) { michael@0: result = AddPercents(aType, w + coordOutsideWidth, pctOutsideWidth); michael@0: } michael@0: else if (aType == MIN_WIDTH && michael@0: // The only cases of coord-percent-calc() units that michael@0: // GetAbsoluteCoord didn't handle are percent and calc()s michael@0: // containing percent. michael@0: styleWidth.IsCoordPercentCalcUnit() && michael@0: aFrame->IsFrameOfType(nsIFrame::eReplaced)) { michael@0: // A percentage width on replaced elements means they can shrink to 0. michael@0: result = 0; // let |min| handle padding/border/margin michael@0: } michael@0: else { michael@0: // NOTE: We could really do a lot better for percents and for some michael@0: // cases of calc() containing percent (certainly including any where michael@0: // the coefficient on the percent is positive and there are no max() michael@0: // expressions). However, doing better for percents wouldn't be michael@0: // backwards compatible. michael@0: result = AddPercents(aType, result, pctTotal); michael@0: } michael@0: michael@0: if (haveFixedMaxWidth || michael@0: GetIntrinsicCoord(styleMaxWidth, aRenderingContext, aFrame, michael@0: PROP_MAX_WIDTH, maxw)) { michael@0: maxw = AddPercents(aType, maxw + coordOutsideWidth, pctOutsideWidth); michael@0: if (result > maxw) michael@0: result = maxw; michael@0: } michael@0: michael@0: if (haveFixedMinWidth || michael@0: GetIntrinsicCoord(styleMinWidth, aRenderingContext, aFrame, michael@0: PROP_MIN_WIDTH, minw)) { michael@0: minw = AddPercents(aType, minw + coordOutsideWidth, pctOutsideWidth); michael@0: if (result < minw) michael@0: result = minw; michael@0: } michael@0: michael@0: min = AddPercents(aType, min, pctTotal); michael@0: if (result < min) michael@0: result = min; michael@0: michael@0: const nsStyleDisplay *disp = aFrame->StyleDisplay(); michael@0: if (aFrame->IsThemed(disp)) { michael@0: nsIntSize size(0, 0); michael@0: bool canOverride = true; michael@0: nsPresContext *presContext = aFrame->PresContext(); michael@0: presContext->GetTheme()-> michael@0: GetMinimumWidgetSize(aRenderingContext, aFrame, disp->mAppearance, michael@0: &size, &canOverride); michael@0: michael@0: nscoord themeWidth = presContext->DevPixelsToAppUnits(size.width); michael@0: michael@0: // GMWS() returns a border-box width michael@0: themeWidth += offsets.hMargin; michael@0: themeWidth = AddPercents(aType, themeWidth, offsets.hPctMargin); michael@0: michael@0: if (themeWidth > result || !canOverride) michael@0: result = themeWidth; michael@0: } michael@0: michael@0: #ifdef DEBUG_INTRINSIC_WIDTH michael@0: nsFrame::IndentBy(stderr, gNoiseIndent); michael@0: static_cast(aFrame)->ListTag(stderr); michael@0: printf_stderr(" %s intrinsic width for container is %d twips.\n", michael@0: aType == MIN_WIDTH ? "min" : "pref", result); michael@0: #endif michael@0: michael@0: return result; michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::ComputeCBDependentValue(nscoord aPercentBasis, michael@0: const nsStyleCoord& aCoord) michael@0: { michael@0: NS_WARN_IF_FALSE(aPercentBasis != NS_UNCONSTRAINEDSIZE, michael@0: "have unconstrained width or height; this should only " michael@0: "result from very large sizes, not attempts at intrinsic " michael@0: "size calculation"); michael@0: michael@0: if (aCoord.IsCoordPercentCalcUnit()) { michael@0: return nsRuleNode::ComputeCoordPercentCalc(aCoord, aPercentBasis); michael@0: } michael@0: NS_ASSERTION(aCoord.GetUnit() == eStyleUnit_None || michael@0: aCoord.GetUnit() == eStyleUnit_Auto, michael@0: "unexpected width value"); michael@0: return 0; michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::ComputeWidthValue( michael@0: nsRenderingContext* aRenderingContext, michael@0: nsIFrame* aFrame, michael@0: nscoord aContainingBlockWidth, michael@0: nscoord aContentEdgeToBoxSizing, michael@0: nscoord aBoxSizingToMarginEdge, michael@0: const nsStyleCoord& aCoord) michael@0: { michael@0: NS_PRECONDITION(aFrame, "non-null frame expected"); michael@0: NS_PRECONDITION(aRenderingContext, "non-null rendering context expected"); michael@0: NS_WARN_IF_FALSE(aContainingBlockWidth != NS_UNCONSTRAINEDSIZE, michael@0: "have unconstrained width; this should only result from " michael@0: "very large sizes, not attempts at intrinsic width " michael@0: "calculation"); michael@0: NS_PRECONDITION(aContainingBlockWidth >= 0, michael@0: "width less than zero"); michael@0: michael@0: nscoord result; michael@0: if (aCoord.IsCoordPercentCalcUnit()) { michael@0: result = nsRuleNode::ComputeCoordPercentCalc(aCoord, michael@0: aContainingBlockWidth); michael@0: // The result of a calc() expression might be less than 0; we michael@0: // should clamp at runtime (below). (Percentages and coords that michael@0: // are less than 0 have already been dropped by the parser.) michael@0: result -= aContentEdgeToBoxSizing; michael@0: } else { michael@0: MOZ_ASSERT(eStyleUnit_Enumerated == aCoord.GetUnit()); michael@0: // If aFrame is a container for font size inflation, then shrink michael@0: // wrapping inside of it should not apply font size inflation. michael@0: AutoMaybeDisableFontInflation an(aFrame); michael@0: michael@0: int32_t val = aCoord.GetIntValue(); michael@0: switch (val) { michael@0: case NS_STYLE_WIDTH_MAX_CONTENT: michael@0: result = aFrame->GetPrefWidth(aRenderingContext); michael@0: NS_ASSERTION(result >= 0, "width less than zero"); michael@0: break; michael@0: case NS_STYLE_WIDTH_MIN_CONTENT: michael@0: result = aFrame->GetMinWidth(aRenderingContext); michael@0: NS_ASSERTION(result >= 0, "width less than zero"); michael@0: break; michael@0: case NS_STYLE_WIDTH_FIT_CONTENT: michael@0: { michael@0: nscoord pref = aFrame->GetPrefWidth(aRenderingContext), michael@0: min = aFrame->GetMinWidth(aRenderingContext), michael@0: fill = aContainingBlockWidth - michael@0: (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing); michael@0: result = std::max(min, std::min(pref, fill)); michael@0: NS_ASSERTION(result >= 0, "width less than zero"); michael@0: } michael@0: break; michael@0: case NS_STYLE_WIDTH_AVAILABLE: michael@0: result = aContainingBlockWidth - michael@0: (aBoxSizingToMarginEdge + aContentEdgeToBoxSizing); michael@0: } michael@0: } michael@0: michael@0: return std::max(0, result); michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::ComputeHeightDependentValue( michael@0: nscoord aContainingBlockHeight, michael@0: const nsStyleCoord& aCoord) michael@0: { michael@0: // XXXldb Some callers explicitly check aContainingBlockHeight michael@0: // against NS_AUTOHEIGHT *and* unit against eStyleUnit_Percent or michael@0: // calc()s containing percents before calling this function. michael@0: // However, it would be much more likely to catch problems without michael@0: // the unit conditions. michael@0: // XXXldb Many callers pass a non-'auto' containing block height when michael@0: // according to CSS2.1 they should be passing 'auto'. michael@0: NS_PRECONDITION(NS_AUTOHEIGHT != aContainingBlockHeight || michael@0: !aCoord.HasPercent(), michael@0: "unexpected containing block height"); michael@0: michael@0: if (aCoord.IsCoordPercentCalcUnit()) { michael@0: return nsRuleNode::ComputeCoordPercentCalc(aCoord, aContainingBlockHeight); michael@0: } michael@0: michael@0: NS_ASSERTION(aCoord.GetUnit() == eStyleUnit_None || michael@0: aCoord.GetUnit() == eStyleUnit_Auto, michael@0: "unexpected height value"); michael@0: return 0; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::MarkDescendantsDirty(nsIFrame *aSubtreeRoot) michael@0: { michael@0: nsAutoTArray subtrees; michael@0: subtrees.AppendElement(aSubtreeRoot); michael@0: michael@0: // dirty descendants, iterating over subtrees that may include michael@0: // additional subtrees associated with placeholders michael@0: do { michael@0: nsIFrame *subtreeRoot = subtrees.ElementAt(subtrees.Length() - 1); michael@0: subtrees.RemoveElementAt(subtrees.Length() - 1); michael@0: michael@0: // Mark all descendants dirty (using an nsTArray stack rather than michael@0: // recursion). michael@0: // Note that nsHTMLReflowState::InitResizeFlags has some similar michael@0: // code; see comments there for how and why it differs. michael@0: nsAutoTArray stack; michael@0: stack.AppendElement(subtreeRoot); michael@0: michael@0: do { michael@0: nsIFrame *f = stack.ElementAt(stack.Length() - 1); michael@0: stack.RemoveElementAt(stack.Length() - 1); michael@0: michael@0: f->MarkIntrinsicWidthsDirty(); michael@0: michael@0: if (f->GetType() == nsGkAtoms::placeholderFrame) { michael@0: nsIFrame *oof = nsPlaceholderFrame::GetRealFrameForPlaceholder(f); michael@0: if (!nsLayoutUtils::IsProperAncestorFrame(subtreeRoot, oof)) { michael@0: // We have another distinct subtree we need to mark. michael@0: subtrees.AppendElement(oof); michael@0: } michael@0: } michael@0: michael@0: nsIFrame::ChildListIterator lists(f); michael@0: for (; !lists.IsDone(); lists.Next()) { michael@0: nsFrameList::Enumerator childFrames(lists.CurrentList()); michael@0: for (; !childFrames.AtEnd(); childFrames.Next()) { michael@0: nsIFrame* kid = childFrames.get(); michael@0: stack.AppendElement(kid); michael@0: } michael@0: } michael@0: } while (stack.Length() != 0); michael@0: } while (subtrees.Length() != 0); michael@0: } michael@0: michael@0: #define MULDIV(a,b,c) (nscoord(int64_t(a) * int64_t(b) / int64_t(c))) michael@0: michael@0: /* static */ nsSize michael@0: nsLayoutUtils::ComputeSizeWithIntrinsicDimensions( michael@0: nsRenderingContext* aRenderingContext, nsIFrame* aFrame, michael@0: const IntrinsicSize& aIntrinsicSize, michael@0: nsSize aIntrinsicRatio, nsSize aCBSize, michael@0: nsSize aMargin, nsSize aBorder, nsSize aPadding) michael@0: { michael@0: const nsStylePosition* stylePos = aFrame->StylePosition(); michael@0: michael@0: // If we're a flex item, we'll compute our size a bit differently. michael@0: const nsStyleCoord* widthStyleCoord = &(stylePos->mWidth); michael@0: const nsStyleCoord* heightStyleCoord = &(stylePos->mHeight); michael@0: michael@0: bool isFlexItem = aFrame->IsFlexItem(); michael@0: bool isHorizontalFlexItem = false; michael@0: michael@0: if (isFlexItem) { michael@0: // Flex items use their "flex-basis" property in place of their main-size michael@0: // property (e.g. "width") for sizing purposes, *unless* they have michael@0: // "flex-basis:auto", in which case they use their main-size property after michael@0: // all. michael@0: uint32_t flexDirection = michael@0: aFrame->GetParent()->StylePosition()->mFlexDirection; michael@0: isHorizontalFlexItem = michael@0: flexDirection == NS_STYLE_FLEX_DIRECTION_ROW || michael@0: flexDirection == NS_STYLE_FLEX_DIRECTION_ROW_REVERSE; michael@0: michael@0: // NOTE: The logic here should match the similar chunk for determining michael@0: // widthStyleCoord and heightStyleCoord in nsFrame::ComputeSize(). michael@0: const nsStyleCoord* flexBasis = &(stylePos->mFlexBasis); michael@0: if (flexBasis->GetUnit() != eStyleUnit_Auto) { michael@0: if (isHorizontalFlexItem) { michael@0: widthStyleCoord = flexBasis; michael@0: } else { michael@0: // One caveat for vertical flex items: We don't support enumerated michael@0: // values (e.g. "max-content") for height properties yet. So, if our michael@0: // computed flex-basis is an enumerated value, we'll just behave as if michael@0: // it were "auto", which means "use the main-size property after all" michael@0: // (which is "height", in this case). michael@0: // NOTE: Once we support intrinsic sizing keywords for "height", michael@0: // we should remove this check. michael@0: if (flexBasis->GetUnit() != eStyleUnit_Enumerated) { michael@0: heightStyleCoord = flexBasis; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Handle intrinsic sizes and their interaction with michael@0: // {min-,max-,}{width,height} according to the rules in michael@0: // http://www.w3.org/TR/CSS21/visudet.html#min-max-widths michael@0: michael@0: // Note: throughout the following section of the function, I avoid michael@0: // a * (b / c) because of its reduced accuracy relative to a * b / c michael@0: // or (a * b) / c (which are equivalent). michael@0: michael@0: const bool isAutoWidth = widthStyleCoord->GetUnit() == eStyleUnit_Auto; michael@0: const bool isAutoHeight = IsAutoHeight(*heightStyleCoord, aCBSize.height); michael@0: michael@0: nsSize boxSizingAdjust(0,0); michael@0: switch (stylePos->mBoxSizing) { michael@0: case NS_STYLE_BOX_SIZING_BORDER: michael@0: boxSizingAdjust += aBorder; michael@0: // fall through michael@0: case NS_STYLE_BOX_SIZING_PADDING: michael@0: boxSizingAdjust += aPadding; michael@0: } michael@0: nscoord boxSizingToMarginEdgeWidth = michael@0: aMargin.width + aBorder.width + aPadding.width - boxSizingAdjust.width; michael@0: michael@0: nscoord width, minWidth, maxWidth, height, minHeight, maxHeight; michael@0: michael@0: if (!isAutoWidth) { michael@0: width = nsLayoutUtils::ComputeWidthValue(aRenderingContext, michael@0: aFrame, aCBSize.width, boxSizingAdjust.width, michael@0: boxSizingToMarginEdgeWidth, *widthStyleCoord); michael@0: } michael@0: michael@0: if (stylePos->mMaxWidth.GetUnit() != eStyleUnit_None && michael@0: !(isFlexItem && isHorizontalFlexItem)) { michael@0: maxWidth = nsLayoutUtils::ComputeWidthValue(aRenderingContext, michael@0: aFrame, aCBSize.width, boxSizingAdjust.width, michael@0: boxSizingToMarginEdgeWidth, stylePos->mMaxWidth); michael@0: } else { michael@0: // NOTE: Flex items ignore their min & max sizing properties in their michael@0: // flex container's main-axis. (Those properties get applied later in michael@0: // the flexbox algorithm.) michael@0: maxWidth = nscoord_MAX; michael@0: } michael@0: michael@0: if (!(isFlexItem && isHorizontalFlexItem)) { michael@0: minWidth = nsLayoutUtils::ComputeWidthValue(aRenderingContext, michael@0: aFrame, aCBSize.width, boxSizingAdjust.width, michael@0: boxSizingToMarginEdgeWidth, stylePos->mMinWidth); michael@0: } else { michael@0: // NOTE: Flex items ignore their min & max sizing properties in their michael@0: // flex container's main-axis. (Those properties get applied later in michael@0: // the flexbox algorithm.) michael@0: minWidth = 0; michael@0: } michael@0: michael@0: if (!isAutoHeight) { michael@0: height = nsLayoutUtils::ComputeHeightValue(aCBSize.height, michael@0: boxSizingAdjust.height, michael@0: *heightStyleCoord); michael@0: } michael@0: michael@0: if (!IsAutoHeight(stylePos->mMaxHeight, aCBSize.height) && michael@0: !(isFlexItem && !isHorizontalFlexItem)) { michael@0: maxHeight = nsLayoutUtils::ComputeHeightValue(aCBSize.height, michael@0: boxSizingAdjust.height, michael@0: stylePos->mMaxHeight); michael@0: } else { michael@0: maxHeight = nscoord_MAX; michael@0: } michael@0: michael@0: if (!IsAutoHeight(stylePos->mMinHeight, aCBSize.height) && michael@0: !(isFlexItem && !isHorizontalFlexItem)) { michael@0: minHeight = nsLayoutUtils::ComputeHeightValue(aCBSize.height, michael@0: boxSizingAdjust.height, michael@0: stylePos->mMinHeight); michael@0: } else { michael@0: minHeight = 0; michael@0: } michael@0: michael@0: // Resolve percentage intrinsic width/height as necessary: michael@0: michael@0: NS_ASSERTION(aCBSize.width != NS_UNCONSTRAINEDSIZE, michael@0: "Our containing block must not have unconstrained width!"); michael@0: michael@0: bool hasIntrinsicWidth, hasIntrinsicHeight; michael@0: nscoord intrinsicWidth, intrinsicHeight; michael@0: michael@0: if (aIntrinsicSize.width.GetUnit() == eStyleUnit_Coord) { michael@0: hasIntrinsicWidth = true; michael@0: intrinsicWidth = aIntrinsicSize.width.GetCoordValue(); michael@0: if (intrinsicWidth < 0) michael@0: intrinsicWidth = 0; michael@0: } else { michael@0: NS_ASSERTION(aIntrinsicSize.width.GetUnit() == eStyleUnit_None, michael@0: "unexpected unit"); michael@0: hasIntrinsicWidth = false; michael@0: intrinsicWidth = 0; michael@0: } michael@0: michael@0: if (aIntrinsicSize.height.GetUnit() == eStyleUnit_Coord) { michael@0: hasIntrinsicHeight = true; michael@0: intrinsicHeight = aIntrinsicSize.height.GetCoordValue(); michael@0: if (intrinsicHeight < 0) michael@0: intrinsicHeight = 0; michael@0: } else { michael@0: NS_ASSERTION(aIntrinsicSize.height.GetUnit() == eStyleUnit_None, michael@0: "unexpected unit"); michael@0: hasIntrinsicHeight = false; michael@0: intrinsicHeight = 0; michael@0: } michael@0: michael@0: NS_ASSERTION(aIntrinsicRatio.width >= 0 && aIntrinsicRatio.height >= 0, michael@0: "Intrinsic ratio has a negative component!"); michael@0: michael@0: // Now calculate the used values for width and height: michael@0: michael@0: if (isAutoWidth) { michael@0: if (isAutoHeight) { michael@0: michael@0: // 'auto' width, 'auto' height michael@0: michael@0: // Get tentative values - CSS 2.1 sections 10.3.2 and 10.6.2: michael@0: michael@0: nscoord tentWidth, tentHeight; michael@0: michael@0: if (hasIntrinsicWidth) { michael@0: tentWidth = intrinsicWidth; michael@0: } else if (hasIntrinsicHeight && aIntrinsicRatio.height > 0) { michael@0: tentWidth = MULDIV(intrinsicHeight, aIntrinsicRatio.width, aIntrinsicRatio.height); michael@0: } else if (aIntrinsicRatio.width > 0) { michael@0: tentWidth = aCBSize.width - boxSizingToMarginEdgeWidth; // XXX scrollbar? michael@0: if (tentWidth < 0) tentWidth = 0; michael@0: } else { michael@0: tentWidth = nsPresContext::CSSPixelsToAppUnits(300); michael@0: } michael@0: michael@0: if (hasIntrinsicHeight) { michael@0: tentHeight = intrinsicHeight; michael@0: } else if (aIntrinsicRatio.width > 0) { michael@0: tentHeight = MULDIV(tentWidth, aIntrinsicRatio.height, aIntrinsicRatio.width); michael@0: } else { michael@0: tentHeight = nsPresContext::CSSPixelsToAppUnits(150); michael@0: } michael@0: michael@0: return ComputeAutoSizeWithIntrinsicDimensions(minWidth, minHeight, michael@0: maxWidth, maxHeight, michael@0: tentWidth, tentHeight); michael@0: } else { michael@0: michael@0: // 'auto' width, non-'auto' height michael@0: height = NS_CSS_MINMAX(height, minHeight, maxHeight); michael@0: if (aIntrinsicRatio.height > 0) { michael@0: width = MULDIV(height, aIntrinsicRatio.width, aIntrinsicRatio.height); michael@0: } else if (hasIntrinsicWidth) { michael@0: width = intrinsicWidth; michael@0: } else { michael@0: width = nsPresContext::CSSPixelsToAppUnits(300); michael@0: } michael@0: width = NS_CSS_MINMAX(width, minWidth, maxWidth); michael@0: michael@0: } michael@0: } else { michael@0: if (isAutoHeight) { michael@0: michael@0: // non-'auto' width, 'auto' height michael@0: width = NS_CSS_MINMAX(width, minWidth, maxWidth); michael@0: if (aIntrinsicRatio.width > 0) { michael@0: height = MULDIV(width, aIntrinsicRatio.height, aIntrinsicRatio.width); michael@0: } else if (hasIntrinsicHeight) { michael@0: height = intrinsicHeight; michael@0: } else { michael@0: height = nsPresContext::CSSPixelsToAppUnits(150); michael@0: } michael@0: height = NS_CSS_MINMAX(height, minHeight, maxHeight); michael@0: michael@0: } else { michael@0: michael@0: // non-'auto' width, non-'auto' height michael@0: width = NS_CSS_MINMAX(width, minWidth, maxWidth); michael@0: height = NS_CSS_MINMAX(height, minHeight, maxHeight); michael@0: michael@0: } michael@0: } michael@0: michael@0: return nsSize(width, height); michael@0: } michael@0: michael@0: nsSize michael@0: nsLayoutUtils::ComputeAutoSizeWithIntrinsicDimensions(nscoord minWidth, nscoord minHeight, michael@0: nscoord maxWidth, nscoord maxHeight, michael@0: nscoord tentWidth, nscoord tentHeight) michael@0: { michael@0: // Now apply min/max-width/height - CSS 2.1 sections 10.4 and 10.7: michael@0: michael@0: if (minWidth > maxWidth) michael@0: maxWidth = minWidth; michael@0: if (minHeight > maxHeight) michael@0: maxHeight = minHeight; michael@0: michael@0: nscoord heightAtMaxWidth, heightAtMinWidth, michael@0: widthAtMaxHeight, widthAtMinHeight; michael@0: michael@0: if (tentWidth > 0) { michael@0: heightAtMaxWidth = MULDIV(maxWidth, tentHeight, tentWidth); michael@0: if (heightAtMaxWidth < minHeight) michael@0: heightAtMaxWidth = minHeight; michael@0: heightAtMinWidth = MULDIV(minWidth, tentHeight, tentWidth); michael@0: if (heightAtMinWidth > maxHeight) michael@0: heightAtMinWidth = maxHeight; michael@0: } else { michael@0: heightAtMaxWidth = heightAtMinWidth = NS_CSS_MINMAX(tentHeight, minHeight, maxHeight); michael@0: } michael@0: michael@0: if (tentHeight > 0) { michael@0: widthAtMaxHeight = MULDIV(maxHeight, tentWidth, tentHeight); michael@0: if (widthAtMaxHeight < minWidth) michael@0: widthAtMaxHeight = minWidth; michael@0: widthAtMinHeight = MULDIV(minHeight, tentWidth, tentHeight); michael@0: if (widthAtMinHeight > maxWidth) michael@0: widthAtMinHeight = maxWidth; michael@0: } else { michael@0: widthAtMaxHeight = widthAtMinHeight = NS_CSS_MINMAX(tentWidth, minWidth, maxWidth); michael@0: } michael@0: michael@0: // The table at http://www.w3.org/TR/CSS21/visudet.html#min-max-widths : michael@0: michael@0: nscoord width, height; michael@0: michael@0: if (tentWidth > maxWidth) { michael@0: if (tentHeight > maxHeight) { michael@0: if (int64_t(maxWidth) * int64_t(tentHeight) <= michael@0: int64_t(maxHeight) * int64_t(tentWidth)) { michael@0: width = maxWidth; michael@0: height = heightAtMaxWidth; michael@0: } else { michael@0: width = widthAtMaxHeight; michael@0: height = maxHeight; michael@0: } michael@0: } else { michael@0: // This also covers "(w > max-width) and (h < min-height)" since in michael@0: // that case (max-width/w < 1), and with (h < min-height): michael@0: // max(max-width * h/w, min-height) == min-height michael@0: width = maxWidth; michael@0: height = heightAtMaxWidth; michael@0: } michael@0: } else if (tentWidth < minWidth) { michael@0: if (tentHeight < minHeight) { michael@0: if (int64_t(minWidth) * int64_t(tentHeight) <= michael@0: int64_t(minHeight) * int64_t(tentWidth)) { michael@0: width = widthAtMinHeight; michael@0: height = minHeight; michael@0: } else { michael@0: width = minWidth; michael@0: height = heightAtMinWidth; michael@0: } michael@0: } else { michael@0: // This also covers "(w < min-width) and (h > max-height)" since in michael@0: // that case (min-width/w > 1), and with (h > max-height): michael@0: // min(min-width * h/w, max-height) == max-height michael@0: width = minWidth; michael@0: height = heightAtMinWidth; michael@0: } michael@0: } else { michael@0: if (tentHeight > maxHeight) { michael@0: width = widthAtMaxHeight; michael@0: height = maxHeight; michael@0: } else if (tentHeight < minHeight) { michael@0: width = widthAtMinHeight; michael@0: height = minHeight; michael@0: } else { michael@0: width = tentWidth; michael@0: height = tentHeight; michael@0: } michael@0: } michael@0: michael@0: return nsSize(width, height); michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::MinWidthFromInline(nsIFrame* aFrame, michael@0: nsRenderingContext* aRenderingContext) michael@0: { michael@0: NS_ASSERTION(!aFrame->IsContainerForFontSizeInflation(), michael@0: "should not be container for font size inflation"); michael@0: michael@0: nsIFrame::InlineMinWidthData data; michael@0: DISPLAY_MIN_WIDTH(aFrame, data.prevLines); michael@0: aFrame->AddInlineMinWidth(aRenderingContext, &data); michael@0: data.ForceBreak(aRenderingContext); michael@0: return data.prevLines; michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::PrefWidthFromInline(nsIFrame* aFrame, michael@0: nsRenderingContext* aRenderingContext) michael@0: { michael@0: NS_ASSERTION(!aFrame->IsContainerForFontSizeInflation(), michael@0: "should not be container for font size inflation"); michael@0: michael@0: nsIFrame::InlinePrefWidthData data; michael@0: DISPLAY_PREF_WIDTH(aFrame, data.prevLines); michael@0: aFrame->AddInlinePrefWidth(aRenderingContext, &data); michael@0: data.ForceBreak(aRenderingContext); michael@0: return data.prevLines; michael@0: } michael@0: michael@0: static nscolor michael@0: DarkenColor(nscolor aColor) michael@0: { michael@0: uint16_t hue, sat, value; michael@0: uint8_t alpha; michael@0: michael@0: // convert the RBG to HSV so we can get the lightness (which is the v) michael@0: NS_RGB2HSV(aColor, hue, sat, value, alpha); michael@0: michael@0: // The goal here is to send white to black while letting colored michael@0: // stuff stay colored... So we adopt the following approach. michael@0: // Something with sat = 0 should end up with value = 0. Something michael@0: // with a high sat can end up with a high value and it's ok.... At michael@0: // the same time, we don't want to make things lighter. Do michael@0: // something simple, since it seems to work. michael@0: if (value > sat) { michael@0: value = sat; michael@0: // convert this color back into the RGB color space. michael@0: NS_HSV2RGB(aColor, hue, sat, value, alpha); michael@0: } michael@0: return aColor; michael@0: } michael@0: michael@0: // Check whether we should darken text/decoration colors. We need to do this if michael@0: // background images and colors are being suppressed, because that means michael@0: // light text will not be visible against the (presumed light-colored) background. michael@0: static bool michael@0: ShouldDarkenColors(nsPresContext* aPresContext) michael@0: { michael@0: return !aPresContext->GetBackgroundColorDraw() && michael@0: !aPresContext->GetBackgroundImageDraw(); michael@0: } michael@0: michael@0: nscolor michael@0: nsLayoutUtils::GetColor(nsIFrame* aFrame, nsCSSProperty aProperty) michael@0: { michael@0: if (aProperty == eCSSProperty_color) michael@0: { michael@0: nscolor nativeColor = NS_RGB(0, 0, 0); michael@0: if (GetNativeTextColor(aFrame, nativeColor)) michael@0: return nativeColor; michael@0: } michael@0: michael@0: nscolor color = aFrame->GetVisitedDependentColor(aProperty); michael@0: if (ShouldDarkenColors(aFrame->PresContext())) { michael@0: color = DarkenColor(color); michael@0: } michael@0: michael@0: return color; michael@0: } michael@0: michael@0: bool michael@0: nsLayoutUtils::GetNativeTextColor(nsIFrame* aFrame, nscolor& aColor) michael@0: { michael@0: nsPresContext *presContext = aFrame->PresContext(); michael@0: if (!presContext->IsChrome()) { michael@0: // If native appearance was used to draw the background of the containing michael@0: // frame, return a contrasting native foreground color instead of the michael@0: // color from the element's style. This avoids a problem where black michael@0: // text was displayed on a black background when a Windows theme such as michael@0: // "High Contrast Black" was used. The background is drawn inside michael@0: // nsNativeThemeWin::ClassicDrawWidgetBackground(). michael@0: // michael@0: // Because both the background color and this foreground color are used michael@0: // directly without exposing the colors via CSS computed styles, the michael@0: // native colors are not leaked to content. michael@0: nsIFrame* bgFrame = michael@0: nsCSSRendering::FindNonTransparentBackgroundFrame(aFrame); michael@0: if (bgFrame) { michael@0: const nsStyleDisplay* displayData = bgFrame->StyleDisplay(); michael@0: uint8_t widgetType = displayData->mAppearance; michael@0: nsITheme *theme = presContext->GetTheme(); michael@0: if (theme && widgetType && theme->ThemeSupportsWidget(presContext, michael@0: bgFrame, michael@0: widgetType)) { michael@0: bool isDisabled = false; michael@0: nsIContent* frameContent = bgFrame->GetContent(); michael@0: if (frameContent && frameContent->IsElement()) { michael@0: EventStates es = frameContent->AsElement()->State(); michael@0: isDisabled = es.HasState(NS_EVENT_STATE_DISABLED); michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(LookAndFeel::GetColorForNativeAppearance(widgetType, michael@0: isDisabled, &aColor))) { michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: gfxFloat michael@0: nsLayoutUtils::GetSnappedBaselineY(nsIFrame* aFrame, gfxContext* aContext, michael@0: nscoord aY, nscoord aAscent) michael@0: { michael@0: gfxFloat appUnitsPerDevUnit = aFrame->PresContext()->AppUnitsPerDevPixel(); michael@0: gfxFloat baseline = gfxFloat(aY) + aAscent; michael@0: gfxRect putativeRect(0, baseline/appUnitsPerDevUnit, 1, 1); michael@0: if (!aContext->UserToDevicePixelSnapped(putativeRect, true)) michael@0: return baseline; michael@0: return aContext->DeviceToUser(putativeRect.TopLeft()).y * appUnitsPerDevUnit; michael@0: } michael@0: michael@0: void michael@0: nsLayoutUtils::DrawString(const nsIFrame* aFrame, michael@0: nsRenderingContext* aContext, michael@0: const char16_t* aString, michael@0: int32_t aLength, michael@0: nsPoint aPoint, michael@0: nsStyleContext* aStyleContext) michael@0: { michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: nsPresContext* presContext = aFrame->PresContext(); michael@0: if (presContext->BidiEnabled()) { michael@0: nsBidiLevel level = michael@0: nsBidiPresUtils::BidiLevelFromStyle(aStyleContext ? michael@0: aStyleContext : aFrame->StyleContext()); michael@0: rv = nsBidiPresUtils::RenderText(aString, aLength, level, michael@0: presContext, *aContext, *aContext, michael@0: aPoint.x, aPoint.y); michael@0: } michael@0: if (NS_FAILED(rv)) michael@0: { michael@0: aContext->SetTextRunRTL(false); michael@0: aContext->DrawString(aString, aLength, aPoint.x, aPoint.y); michael@0: } michael@0: } michael@0: michael@0: nscoord michael@0: nsLayoutUtils::GetStringWidth(const nsIFrame* aFrame, michael@0: nsRenderingContext* aContext, michael@0: const char16_t* aString, michael@0: int32_t aLength) michael@0: { michael@0: nsPresContext* presContext = aFrame->PresContext(); michael@0: if (presContext->BidiEnabled()) { michael@0: nsBidiLevel level = michael@0: nsBidiPresUtils::BidiLevelFromStyle(aFrame->StyleContext()); michael@0: return nsBidiPresUtils::MeasureTextWidth(aString, aLength, michael@0: level, presContext, *aContext); michael@0: } michael@0: aContext->SetTextRunRTL(false); michael@0: return aContext->GetWidth(aString, aLength); michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::PaintTextShadow(const nsIFrame* aFrame, michael@0: nsRenderingContext* aContext, michael@0: const nsRect& aTextRect, michael@0: const nsRect& aDirtyRect, michael@0: const nscolor& aForegroundColor, michael@0: TextShadowCallback aCallback, michael@0: void* aCallbackData) michael@0: { michael@0: const nsStyleText* textStyle = aFrame->StyleText(); michael@0: if (!textStyle->HasTextShadow()) michael@0: return; michael@0: michael@0: // Text shadow happens with the last value being painted at the back, michael@0: // ie. it is painted first. michael@0: gfxContext* aDestCtx = aContext->ThebesContext(); michael@0: for (uint32_t i = textStyle->mTextShadow->Length(); i > 0; --i) { michael@0: nsCSSShadowItem* shadowDetails = textStyle->mTextShadow->ShadowAt(i - 1); michael@0: nsPoint shadowOffset(shadowDetails->mXOffset, michael@0: shadowDetails->mYOffset); michael@0: nscoord blurRadius = std::max(shadowDetails->mRadius, 0); michael@0: michael@0: nsRect shadowRect(aTextRect); michael@0: shadowRect.MoveBy(shadowOffset); michael@0: michael@0: nsPresContext* presCtx = aFrame->PresContext(); michael@0: nsContextBoxBlur contextBoxBlur; michael@0: gfxContext* shadowContext = contextBoxBlur.Init(shadowRect, 0, blurRadius, michael@0: presCtx->AppUnitsPerDevPixel(), michael@0: aDestCtx, aDirtyRect, nullptr); michael@0: if (!shadowContext) michael@0: continue; michael@0: michael@0: nscolor shadowColor; michael@0: if (shadowDetails->mHasColor) michael@0: shadowColor = shadowDetails->mColor; michael@0: else michael@0: shadowColor = aForegroundColor; michael@0: michael@0: // Conjure an nsRenderingContext from a gfxContext for drawing the text michael@0: // to blur. michael@0: nsRefPtr renderingContext = new nsRenderingContext(); michael@0: renderingContext->Init(presCtx->DeviceContext(), shadowContext); michael@0: michael@0: aDestCtx->Save(); michael@0: aDestCtx->NewPath(); michael@0: aDestCtx->SetColor(gfxRGBA(shadowColor)); michael@0: michael@0: // The callback will draw whatever we want to blur as a shadow. michael@0: aCallback(renderingContext, shadowOffset, shadowColor, aCallbackData); michael@0: michael@0: contextBoxBlur.DoPaint(); michael@0: aDestCtx->Restore(); michael@0: } michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::GetCenteredFontBaseline(nsFontMetrics* aFontMetrics, michael@0: nscoord aLineHeight) michael@0: { michael@0: nscoord fontAscent = aFontMetrics->MaxAscent(); michael@0: nscoord fontHeight = aFontMetrics->MaxHeight(); michael@0: michael@0: nscoord leading = aLineHeight - fontHeight; michael@0: return fontAscent + leading/2; michael@0: } michael@0: michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::GetFirstLineBaseline(const nsIFrame* aFrame, nscoord* aResult) michael@0: { michael@0: LinePosition position; michael@0: if (!GetFirstLinePosition(aFrame, &position)) michael@0: return false; michael@0: *aResult = position.mBaseline; michael@0: return true; michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::GetFirstLinePosition(const nsIFrame* aFrame, michael@0: LinePosition* aResult) michael@0: { michael@0: const nsBlockFrame* block = nsLayoutUtils::GetAsBlock(const_cast(aFrame)); michael@0: if (!block) { michael@0: // For the first-line baseline we also have to check for a table, and if michael@0: // so, use the baseline of its first row. michael@0: nsIAtom* fType = aFrame->GetType(); michael@0: if (fType == nsGkAtoms::tableOuterFrame) { michael@0: aResult->mTop = 0; michael@0: aResult->mBaseline = aFrame->GetBaseline(); michael@0: // This is what we want for the list bullet caller; not sure if michael@0: // other future callers will want the same. michael@0: aResult->mBottom = aFrame->GetSize().height; michael@0: return true; michael@0: } michael@0: michael@0: // For first-line baselines, we have to consider scroll frames. michael@0: if (fType == nsGkAtoms::scrollFrame) { michael@0: nsIScrollableFrame *sFrame = do_QueryFrame(const_cast(aFrame)); michael@0: if (!sFrame) { michael@0: NS_NOTREACHED("not scroll frame"); michael@0: } michael@0: LinePosition kidPosition; michael@0: if (GetFirstLinePosition(sFrame->GetScrolledFrame(), &kidPosition)) { michael@0: // Consider only the border and padding that contributes to the michael@0: // kid's position, not the scrolling, so we get the initial michael@0: // position. michael@0: *aResult = kidPosition + aFrame->GetUsedBorderAndPadding().top; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: if (fType == nsGkAtoms::fieldSetFrame) { michael@0: LinePosition kidPosition; michael@0: nsIFrame* kid = aFrame->GetFirstPrincipalChild(); michael@0: // kid might be a legend frame here, but that's ok. michael@0: if (GetFirstLinePosition(kid, &kidPosition)) { michael@0: *aResult = kidPosition + kid->GetNormalPosition().y; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // No baseline. michael@0: return false; michael@0: } michael@0: michael@0: for (nsBlockFrame::const_line_iterator line = block->begin_lines(), michael@0: line_end = block->end_lines(); michael@0: line != line_end; ++line) { michael@0: if (line->IsBlock()) { michael@0: nsIFrame *kid = line->mFirstChild; michael@0: LinePosition kidPosition; michael@0: if (GetFirstLinePosition(kid, &kidPosition)) { michael@0: *aResult = kidPosition + kid->GetNormalPosition().y; michael@0: return true; michael@0: } michael@0: } else { michael@0: // XXX Is this the right test? We have some bogus empty lines michael@0: // floating around, but IsEmpty is perhaps too weak. michael@0: if (line->BSize() != 0 || !line->IsEmpty()) { michael@0: nscoord top = line->BStart(); michael@0: aResult->mTop = top; michael@0: aResult->mBaseline = top + line->GetAscent(); michael@0: aResult->mBottom = top + line->BSize(); michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::GetLastLineBaseline(const nsIFrame* aFrame, nscoord* aResult) michael@0: { michael@0: const nsBlockFrame* block = nsLayoutUtils::GetAsBlock(const_cast(aFrame)); michael@0: if (!block) michael@0: // No baseline. (We intentionally don't descend into scroll frames.) michael@0: return false; michael@0: michael@0: for (nsBlockFrame::const_reverse_line_iterator line = block->rbegin_lines(), michael@0: line_end = block->rend_lines(); michael@0: line != line_end; ++line) { michael@0: if (line->IsBlock()) { michael@0: nsIFrame *kid = line->mFirstChild; michael@0: nscoord kidBaseline; michael@0: if (GetLastLineBaseline(kid, &kidBaseline)) { michael@0: // Ignore relative positioning for baseline calculations michael@0: *aResult = kidBaseline + kid->GetNormalPosition().y; michael@0: return true; michael@0: } else if (kid->GetType() == nsGkAtoms::scrollFrame) { michael@0: // Use the bottom of the scroll frame. michael@0: // XXX CSS2.1 really doesn't say what to do here. michael@0: *aResult = kid->GetNormalPosition().y + kid->GetRect().height; michael@0: return true; michael@0: } michael@0: } else { michael@0: // XXX Is this the right test? We have some bogus empty lines michael@0: // floating around, but IsEmpty is perhaps too weak. michael@0: if (line->BSize() != 0 || !line->IsEmpty()) { michael@0: *aResult = line->BStart() + line->GetAscent(); michael@0: return true; michael@0: } michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: static nscoord michael@0: CalculateBlockContentBottom(nsBlockFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "null ptr"); michael@0: michael@0: nscoord contentBottom = 0; michael@0: michael@0: for (nsBlockFrame::line_iterator line = aFrame->begin_lines(), michael@0: line_end = aFrame->end_lines(); michael@0: line != line_end; ++line) { michael@0: if (line->IsBlock()) { michael@0: nsIFrame* child = line->mFirstChild; michael@0: nscoord offset = child->GetNormalPosition().y; michael@0: contentBottom = std::max(contentBottom, michael@0: nsLayoutUtils::CalculateContentBottom(child) + offset); michael@0: } michael@0: else { michael@0: contentBottom = std::max(contentBottom, line->BEnd()); michael@0: } michael@0: } michael@0: return contentBottom; michael@0: } michael@0: michael@0: /* static */ nscoord michael@0: nsLayoutUtils::CalculateContentBottom(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame, "null ptr"); michael@0: michael@0: nscoord contentBottom = aFrame->GetRect().height; michael@0: michael@0: // We want scrollable overflow rather than visual because this michael@0: // calculation is intended to affect layout. michael@0: if (aFrame->GetScrollableOverflowRect().height > contentBottom) { michael@0: nsIFrame::ChildListIDs skip(nsIFrame::kOverflowList | michael@0: nsIFrame::kExcessOverflowContainersList | michael@0: nsIFrame::kOverflowOutOfFlowList); michael@0: nsBlockFrame* blockFrame = GetAsBlock(aFrame); michael@0: if (blockFrame) { michael@0: contentBottom = michael@0: std::max(contentBottom, CalculateBlockContentBottom(blockFrame)); michael@0: skip |= nsIFrame::kPrincipalList; michael@0: } michael@0: nsIFrame::ChildListIterator lists(aFrame); michael@0: for (; !lists.IsDone(); lists.Next()) { michael@0: if (!skip.Contains(lists.CurrentID())) { michael@0: nsFrameList::Enumerator childFrames(lists.CurrentList()); michael@0: for (; !childFrames.AtEnd(); childFrames.Next()) { michael@0: nsIFrame* child = childFrames.get(); michael@0: nscoord offset = child->GetNormalPosition().y; michael@0: contentBottom = std::max(contentBottom, michael@0: CalculateContentBottom(child) + offset); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: return contentBottom; michael@0: } michael@0: michael@0: /* static */ nsIFrame* michael@0: nsLayoutUtils::GetClosestLayer(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame* layer; michael@0: for (layer = aFrame; layer; layer = layer->GetParent()) { michael@0: if (layer->IsPositioned() || michael@0: (layer->GetParent() && michael@0: layer->GetParent()->GetType() == nsGkAtoms::scrollFrame)) michael@0: break; michael@0: } michael@0: if (layer) michael@0: return layer; michael@0: return aFrame->PresContext()->PresShell()->FrameManager()->GetRootFrame(); michael@0: } michael@0: michael@0: GraphicsFilter michael@0: nsLayoutUtils::GetGraphicsFilterForFrame(nsIFrame* aForFrame) michael@0: { michael@0: GraphicsFilter defaultFilter = GraphicsFilter::FILTER_GOOD; michael@0: nsStyleContext *sc; michael@0: if (nsCSSRendering::IsCanvasFrame(aForFrame)) { michael@0: nsCSSRendering::FindBackground(aForFrame, &sc); michael@0: } else { michael@0: sc = aForFrame->StyleContext(); michael@0: } michael@0: michael@0: switch (sc->StyleSVG()->mImageRendering) { michael@0: case NS_STYLE_IMAGE_RENDERING_OPTIMIZESPEED: michael@0: return GraphicsFilter::FILTER_FAST; michael@0: case NS_STYLE_IMAGE_RENDERING_OPTIMIZEQUALITY: michael@0: return GraphicsFilter::FILTER_BEST; michael@0: case NS_STYLE_IMAGE_RENDERING_CRISPEDGES: michael@0: return GraphicsFilter::FILTER_NEAREST; michael@0: default: michael@0: return defaultFilter; michael@0: } michael@0: } michael@0: michael@0: /** michael@0: * Given an image being drawn into an appunit coordinate system, and michael@0: * a point in that coordinate system, map the point back into image michael@0: * pixel space. michael@0: * @param aSize the size of the image, in pixels michael@0: * @param aDest the rectangle that the image is being mapped into michael@0: * @param aPt a point in the same coordinate system as the rectangle michael@0: */ michael@0: static gfxPoint michael@0: MapToFloatImagePixels(const gfxSize& aSize, michael@0: const gfxRect& aDest, const gfxPoint& aPt) michael@0: { michael@0: return gfxPoint(((aPt.x - aDest.X())*aSize.width)/aDest.Width(), michael@0: ((aPt.y - aDest.Y())*aSize.height)/aDest.Height()); michael@0: } michael@0: michael@0: /** michael@0: * Given an image being drawn into an pixel-based coordinate system, and michael@0: * a point in image space, map the point into the pixel-based coordinate michael@0: * system. michael@0: * @param aSize the size of the image, in pixels michael@0: * @param aDest the rectangle that the image is being mapped into michael@0: * @param aPt a point in image space michael@0: */ michael@0: static gfxPoint michael@0: MapToFloatUserPixels(const gfxSize& aSize, michael@0: const gfxRect& aDest, const gfxPoint& aPt) michael@0: { michael@0: return gfxPoint(aPt.x*aDest.Width()/aSize.width + aDest.X(), michael@0: aPt.y*aDest.Height()/aSize.height + aDest.Y()); michael@0: } michael@0: michael@0: /* static */ gfxRect michael@0: nsLayoutUtils::RectToGfxRect(const nsRect& aRect, int32_t aAppUnitsPerDevPixel) michael@0: { michael@0: return gfxRect(gfxFloat(aRect.x) / aAppUnitsPerDevPixel, michael@0: gfxFloat(aRect.y) / aAppUnitsPerDevPixel, michael@0: gfxFloat(aRect.width) / aAppUnitsPerDevPixel, michael@0: gfxFloat(aRect.height) / aAppUnitsPerDevPixel); michael@0: } michael@0: michael@0: struct SnappedImageDrawingParameters { michael@0: // A transform from either device space or user space (depending on mResetCTM) michael@0: // to image space michael@0: gfxMatrix mUserSpaceToImageSpace; michael@0: // A device-space, pixel-aligned rectangle to fill michael@0: gfxRect mFillRect; michael@0: // A pixel rectangle in tiled image space outside of which gfx should not michael@0: // sample (using EXTEND_PAD as necessary) michael@0: nsIntRect mSubimage; michael@0: // Whether there's anything to draw at all michael@0: bool mShouldDraw; michael@0: // true iff the CTM of the rendering context needs to be reset to the michael@0: // identity matrix before drawing michael@0: bool mResetCTM; michael@0: michael@0: SnappedImageDrawingParameters() michael@0: : mShouldDraw(false) michael@0: , mResetCTM(false) michael@0: {} michael@0: michael@0: SnappedImageDrawingParameters(const gfxMatrix& aUserSpaceToImageSpace, michael@0: const gfxRect& aFillRect, michael@0: const nsIntRect& aSubimage, michael@0: bool aResetCTM) michael@0: : mUserSpaceToImageSpace(aUserSpaceToImageSpace) michael@0: , mFillRect(aFillRect) michael@0: , mSubimage(aSubimage) michael@0: , mShouldDraw(true) michael@0: , mResetCTM(aResetCTM) michael@0: {} michael@0: }; michael@0: michael@0: /** michael@0: * Given a set of input parameters, compute certain output parameters michael@0: * for drawing an image with the image snapping algorithm. michael@0: * See https://wiki.mozilla.org/Gecko:Image_Snapping_and_Rendering michael@0: * michael@0: * @see nsLayoutUtils::DrawImage() for the descriptions of input parameters michael@0: */ michael@0: static SnappedImageDrawingParameters michael@0: ComputeSnappedImageDrawingParameters(gfxContext* aCtx, michael@0: int32_t aAppUnitsPerDevPixel, michael@0: const nsRect aDest, michael@0: const nsRect aFill, michael@0: const nsPoint aAnchor, michael@0: const nsRect aDirty, michael@0: const nsIntSize aImageSize) michael@0: michael@0: { michael@0: if (aDest.IsEmpty() || aFill.IsEmpty() || !aImageSize.width || !aImageSize.height) michael@0: return SnappedImageDrawingParameters(); michael@0: michael@0: gfxRect devPixelDest = michael@0: nsLayoutUtils::RectToGfxRect(aDest, aAppUnitsPerDevPixel); michael@0: gfxRect devPixelFill = michael@0: nsLayoutUtils::RectToGfxRect(aFill, aAppUnitsPerDevPixel); michael@0: gfxRect devPixelDirty = michael@0: nsLayoutUtils::RectToGfxRect(aDirty, aAppUnitsPerDevPixel); michael@0: michael@0: gfxMatrix currentMatrix = aCtx->CurrentMatrix(); michael@0: gfxRect fill = devPixelFill; michael@0: bool didSnap; michael@0: // Snap even if we have a scale in the context. But don't snap if michael@0: // we have something that's not translation+scale, or if the scale flips in michael@0: // the X or Y direction, because snapped image drawing can't handle that yet. michael@0: if (!currentMatrix.HasNonAxisAlignedTransform() && michael@0: currentMatrix.xx > 0.0 && currentMatrix.yy > 0.0 && michael@0: aCtx->UserToDevicePixelSnapped(fill, true)) { michael@0: didSnap = true; michael@0: if (fill.IsEmpty()) { michael@0: return SnappedImageDrawingParameters(); michael@0: } michael@0: } else { michael@0: didSnap = false; michael@0: fill = devPixelFill; michael@0: } michael@0: michael@0: gfxSize imageSize(aImageSize.width, aImageSize.height); michael@0: michael@0: // Compute the set of pixels that would be sampled by an ideal rendering michael@0: gfxPoint subimageTopLeft = michael@0: MapToFloatImagePixels(imageSize, devPixelDest, devPixelFill.TopLeft()); michael@0: gfxPoint subimageBottomRight = michael@0: MapToFloatImagePixels(imageSize, devPixelDest, devPixelFill.BottomRight()); michael@0: nsIntRect intSubimage; michael@0: intSubimage.MoveTo(NSToIntFloor(subimageTopLeft.x), michael@0: NSToIntFloor(subimageTopLeft.y)); michael@0: intSubimage.SizeTo(NSToIntCeil(subimageBottomRight.x) - intSubimage.x, michael@0: NSToIntCeil(subimageBottomRight.y) - intSubimage.y); michael@0: michael@0: // Compute the anchor point and compute final fill rect. michael@0: // This code assumes that pixel-based devices have one pixel per michael@0: // device unit! michael@0: gfxPoint anchorPoint(gfxFloat(aAnchor.x)/aAppUnitsPerDevPixel, michael@0: gfxFloat(aAnchor.y)/aAppUnitsPerDevPixel); michael@0: gfxPoint imageSpaceAnchorPoint = michael@0: MapToFloatImagePixels(imageSize, devPixelDest, anchorPoint); michael@0: michael@0: if (didSnap) { michael@0: imageSpaceAnchorPoint.Round(); michael@0: anchorPoint = imageSpaceAnchorPoint; michael@0: anchorPoint = MapToFloatUserPixels(imageSize, devPixelDest, anchorPoint); michael@0: anchorPoint = currentMatrix.Transform(anchorPoint); michael@0: anchorPoint.Round(); michael@0: michael@0: // This form of Transform is safe to call since non-axis-aligned michael@0: // transforms wouldn't be snapped. michael@0: devPixelDirty = currentMatrix.Transform(devPixelDirty); michael@0: } michael@0: michael@0: gfxFloat scaleX = imageSize.width*aAppUnitsPerDevPixel/aDest.width; michael@0: gfxFloat scaleY = imageSize.height*aAppUnitsPerDevPixel/aDest.height; michael@0: if (didSnap) { michael@0: // We'll reset aCTX to the identity matrix before drawing, so we need to michael@0: // adjust our scales to match. michael@0: scaleX /= currentMatrix.xx; michael@0: scaleY /= currentMatrix.yy; michael@0: } michael@0: gfxFloat translateX = imageSpaceAnchorPoint.x - anchorPoint.x*scaleX; michael@0: gfxFloat translateY = imageSpaceAnchorPoint.y - anchorPoint.y*scaleY; michael@0: gfxMatrix transform(scaleX, 0, 0, scaleY, translateX, translateY); michael@0: michael@0: gfxRect finalFillRect = fill; michael@0: // If the user-space-to-image-space transform is not a straight michael@0: // translation by integers, then filtering will occur, and michael@0: // restricting the fill rect to the dirty rect would change the values michael@0: // computed for edge pixels, which we can't allow. michael@0: // Also, if didSnap is false then rounding out 'devPixelDirty' might not michael@0: // produce pixel-aligned coordinates, which would also break the values michael@0: // computed for edge pixels. michael@0: if (didSnap && !transform.HasNonIntegerTranslation()) { michael@0: devPixelDirty.RoundOut(); michael@0: finalFillRect = fill.Intersect(devPixelDirty); michael@0: } michael@0: if (finalFillRect.IsEmpty()) michael@0: return SnappedImageDrawingParameters(); michael@0: michael@0: return SnappedImageDrawingParameters(transform, finalFillRect, intSubimage, michael@0: didSnap); michael@0: } michael@0: michael@0: michael@0: static nsresult michael@0: DrawImageInternal(nsRenderingContext* aRenderingContext, michael@0: imgIContainer* aImage, michael@0: GraphicsFilter aGraphicsFilter, michael@0: const nsRect& aDest, michael@0: const nsRect& aFill, michael@0: const nsPoint& aAnchor, michael@0: const nsRect& aDirty, michael@0: const nsIntSize& aImageSize, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aImageFlags) michael@0: { michael@0: if (aDest.Contains(aFill)) { michael@0: aImageFlags |= imgIContainer::FLAG_CLAMP; michael@0: } michael@0: int32_t appUnitsPerDevPixel = aRenderingContext->AppUnitsPerDevPixel(); michael@0: gfxContext* ctx = aRenderingContext->ThebesContext(); michael@0: michael@0: SnappedImageDrawingParameters drawingParams = michael@0: ComputeSnappedImageDrawingParameters(ctx, appUnitsPerDevPixel, aDest, aFill, michael@0: aAnchor, aDirty, aImageSize); michael@0: michael@0: if (!drawingParams.mShouldDraw) michael@0: return NS_OK; michael@0: michael@0: gfxContextMatrixAutoSaveRestore saveMatrix(ctx); michael@0: if (drawingParams.mResetCTM) { michael@0: ctx->IdentityMatrix(); michael@0: } michael@0: michael@0: aImage->Draw(ctx, aGraphicsFilter, drawingParams.mUserSpaceToImageSpace, michael@0: drawingParams.mFillRect, drawingParams.mSubimage, aImageSize, michael@0: aSVGContext, imgIContainer::FRAME_CURRENT, aImageFlags); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::DrawPixelSnapped(nsRenderingContext* aRenderingContext, michael@0: gfxDrawable* aDrawable, michael@0: GraphicsFilter aFilter, michael@0: const nsRect& aDest, michael@0: const nsRect& aFill, michael@0: const nsPoint& aAnchor, michael@0: const nsRect& aDirty) michael@0: { michael@0: int32_t appUnitsPerDevPixel = aRenderingContext->AppUnitsPerDevPixel(); michael@0: gfxContext* ctx = aRenderingContext->ThebesContext(); michael@0: gfxIntSize drawableSize = aDrawable->Size(); michael@0: nsIntSize imageSize(drawableSize.width, drawableSize.height); michael@0: michael@0: SnappedImageDrawingParameters drawingParams = michael@0: ComputeSnappedImageDrawingParameters(ctx, appUnitsPerDevPixel, aDest, aFill, michael@0: aAnchor, aDirty, imageSize); michael@0: michael@0: if (!drawingParams.mShouldDraw) michael@0: return; michael@0: michael@0: gfxContextMatrixAutoSaveRestore saveMatrix(ctx); michael@0: if (drawingParams.mResetCTM) { michael@0: ctx->IdentityMatrix(); michael@0: } michael@0: michael@0: gfxRect sourceRect = michael@0: drawingParams.mUserSpaceToImageSpace.Transform(drawingParams.mFillRect); michael@0: gfxRect imageRect(0, 0, imageSize.width, imageSize.height); michael@0: gfxRect subimage(drawingParams.mSubimage.x, drawingParams.mSubimage.y, michael@0: drawingParams.mSubimage.width, drawingParams.mSubimage.height); michael@0: michael@0: NS_ASSERTION(!sourceRect.Intersect(subimage).IsEmpty(), michael@0: "We must be allowed to sample *some* source pixels!"); michael@0: michael@0: gfxUtils::DrawPixelSnapped(ctx, aDrawable, michael@0: drawingParams.mUserSpaceToImageSpace, subimage, michael@0: sourceRect, imageRect, drawingParams.mFillRect, michael@0: gfxImageFormat::ARGB32, aFilter); michael@0: } michael@0: michael@0: /* static */ nsresult michael@0: nsLayoutUtils::DrawSingleUnscaledImage(nsRenderingContext* aRenderingContext, michael@0: imgIContainer* aImage, michael@0: GraphicsFilter aGraphicsFilter, michael@0: const nsPoint& aDest, michael@0: const nsRect* aDirty, michael@0: uint32_t aImageFlags, michael@0: const nsRect* aSourceArea) michael@0: { michael@0: nsIntSize imageSize; michael@0: aImage->GetWidth(&imageSize.width); michael@0: aImage->GetHeight(&imageSize.height); michael@0: NS_ENSURE_TRUE(imageSize.width > 0 && imageSize.height > 0, NS_ERROR_FAILURE); michael@0: michael@0: nscoord appUnitsPerCSSPixel = nsDeviceContext::AppUnitsPerCSSPixel(); michael@0: nsSize size(imageSize.width*appUnitsPerCSSPixel, michael@0: imageSize.height*appUnitsPerCSSPixel); michael@0: michael@0: nsRect source; michael@0: if (aSourceArea) { michael@0: source = *aSourceArea; michael@0: } else { michael@0: source.SizeTo(size); michael@0: } michael@0: michael@0: nsRect dest(aDest - source.TopLeft(), size); michael@0: nsRect fill(aDest, source.Size()); michael@0: // Ensure that only a single image tile is drawn. If aSourceArea extends michael@0: // outside the image bounds, we want to honor the aSourceArea-to-aDest michael@0: // translation but we don't want to actually tile the image. michael@0: fill.IntersectRect(fill, dest); michael@0: return DrawImageInternal(aRenderingContext, aImage, aGraphicsFilter, michael@0: dest, fill, aDest, aDirty ? *aDirty : dest, michael@0: imageSize, nullptr, aImageFlags); michael@0: } michael@0: michael@0: /* static */ nsresult michael@0: nsLayoutUtils::DrawSingleImage(nsRenderingContext* aRenderingContext, michael@0: imgIContainer* aImage, michael@0: GraphicsFilter aGraphicsFilter, michael@0: const nsRect& aDest, michael@0: const nsRect& aDirty, michael@0: const SVGImageContext* aSVGContext, michael@0: uint32_t aImageFlags, michael@0: const nsRect* aSourceArea) michael@0: { michael@0: nsIntSize imageSize; michael@0: if (aImage->GetType() == imgIContainer::TYPE_VECTOR) { michael@0: // We choose a size for vector images that emulates a raster image which michael@0: // is perfectly sized for the destination rect: each pixel in the image michael@0: // maps exactly to a single pixel on-screen. michael@0: nscoord appUnitsPerDevPx = aRenderingContext->AppUnitsPerDevPixel(); michael@0: imageSize.width = NSAppUnitsToIntPixels(aDest.width, appUnitsPerDevPx); michael@0: imageSize.height = NSAppUnitsToIntPixels(aDest.height, appUnitsPerDevPx); michael@0: } else { michael@0: // Raster images have an intrinsic size, so we just use that. michael@0: aImage->GetWidth(&imageSize.width); michael@0: aImage->GetHeight(&imageSize.height); michael@0: } michael@0: NS_ENSURE_TRUE(imageSize.width > 0 && imageSize.height > 0, NS_ERROR_FAILURE); michael@0: michael@0: nsRect source; michael@0: if (aSourceArea) { michael@0: source = *aSourceArea; michael@0: } else { michael@0: nscoord appUnitsPerCSSPixel = nsDeviceContext::AppUnitsPerCSSPixel(); michael@0: source.SizeTo(imageSize.width*appUnitsPerCSSPixel, michael@0: imageSize.height*appUnitsPerCSSPixel); michael@0: } michael@0: michael@0: nsRect dest = nsLayoutUtils::GetWholeImageDestination(imageSize, source, michael@0: aDest); michael@0: // Ensure that only a single image tile is drawn. If aSourceArea extends michael@0: // outside the image bounds, we want to honor the aSourceArea-to-aDest michael@0: // transform but we don't want to actually tile the image. michael@0: nsRect fill; michael@0: fill.IntersectRect(aDest, dest); michael@0: return DrawImageInternal(aRenderingContext, aImage, aGraphicsFilter, dest, fill, michael@0: fill.TopLeft(), aDirty, imageSize, aSVGContext, aImageFlags); michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::ComputeSizeForDrawing(imgIContainer *aImage, michael@0: nsIntSize& aImageSize, /*outparam*/ michael@0: nsSize& aIntrinsicRatio, /*outparam*/ michael@0: bool& aGotWidth, /*outparam*/ michael@0: bool& aGotHeight /*outparam*/) michael@0: { michael@0: aGotWidth = NS_SUCCEEDED(aImage->GetWidth(&aImageSize.width)); michael@0: aGotHeight = NS_SUCCEEDED(aImage->GetHeight(&aImageSize.height)); michael@0: bool gotRatio = NS_SUCCEEDED(aImage->GetIntrinsicRatio(&aIntrinsicRatio)); michael@0: michael@0: if (!(aGotWidth && aGotHeight) && !gotRatio) { michael@0: // We hit an error (say, because the image failed to load or couldn't be michael@0: // decoded) and should return zero size. michael@0: aGotWidth = aGotHeight = true; michael@0: aImageSize = nsIntSize(0, 0); michael@0: aIntrinsicRatio = nsSize(0, 0); michael@0: } michael@0: } michael@0: michael@0: michael@0: /* static */ nsresult michael@0: nsLayoutUtils::DrawBackgroundImage(nsRenderingContext* aRenderingContext, michael@0: imgIContainer* aImage, michael@0: const nsIntSize& aImageSize, michael@0: GraphicsFilter aGraphicsFilter, michael@0: const nsRect& aDest, michael@0: const nsRect& aFill, michael@0: const nsPoint& aAnchor, michael@0: const nsRect& aDirty, michael@0: uint32_t aImageFlags) michael@0: { michael@0: PROFILER_LABEL("layout", "nsLayoutUtils::DrawBackgroundImage"); michael@0: michael@0: if (UseBackgroundNearestFiltering()) { michael@0: aGraphicsFilter = GraphicsFilter::FILTER_NEAREST; michael@0: } michael@0: michael@0: return DrawImageInternal(aRenderingContext, aImage, aGraphicsFilter, michael@0: aDest, aFill, aAnchor, aDirty, michael@0: aImageSize, nullptr, aImageFlags); michael@0: } michael@0: michael@0: /* static */ nsresult michael@0: nsLayoutUtils::DrawImage(nsRenderingContext* aRenderingContext, michael@0: imgIContainer* aImage, michael@0: GraphicsFilter aGraphicsFilter, michael@0: const nsRect& aDest, michael@0: const nsRect& aFill, michael@0: const nsPoint& aAnchor, michael@0: const nsRect& aDirty, michael@0: uint32_t aImageFlags) michael@0: { michael@0: nsIntSize imageSize; michael@0: nsSize imageRatio; michael@0: bool gotHeight, gotWidth; michael@0: ComputeSizeForDrawing(aImage, imageSize, imageRatio, gotWidth, gotHeight); michael@0: michael@0: // XXX Dimensionless images shouldn't fall back to filled-area size -- the michael@0: // caller should provide the image size, a la DrawBackgroundImage. michael@0: if (gotWidth != gotHeight) { michael@0: if (!gotWidth) { michael@0: if (imageRatio.height != 0) { michael@0: imageSize.width = michael@0: NSCoordSaturatingNonnegativeMultiply(imageSize.height, michael@0: float(imageRatio.width) / michael@0: float(imageRatio.height)); michael@0: gotWidth = true; michael@0: } michael@0: } else { michael@0: if (imageRatio.width != 0) { michael@0: imageSize.height = michael@0: NSCoordSaturatingNonnegativeMultiply(imageSize.width, michael@0: float(imageRatio.height) / michael@0: float(imageRatio.width)); michael@0: gotHeight = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (!gotWidth) { michael@0: imageSize.width = nsPresContext::AppUnitsToIntCSSPixels(aFill.width); michael@0: } michael@0: if (!gotHeight) { michael@0: imageSize.height = nsPresContext::AppUnitsToIntCSSPixels(aFill.height); michael@0: } michael@0: michael@0: return DrawImageInternal(aRenderingContext, aImage, aGraphicsFilter, michael@0: aDest, aFill, aAnchor, aDirty, michael@0: imageSize, nullptr, aImageFlags); michael@0: } michael@0: michael@0: /* static */ nsRect michael@0: nsLayoutUtils::GetWholeImageDestination(const nsIntSize& aWholeImageSize, michael@0: const nsRect& aImageSourceArea, michael@0: const nsRect& aDestArea) michael@0: { michael@0: double scaleX = double(aDestArea.width)/aImageSourceArea.width; michael@0: double scaleY = double(aDestArea.height)/aImageSourceArea.height; michael@0: nscoord destOffsetX = NSToCoordRound(aImageSourceArea.x*scaleX); michael@0: nscoord destOffsetY = NSToCoordRound(aImageSourceArea.y*scaleY); michael@0: nscoord appUnitsPerCSSPixel = nsDeviceContext::AppUnitsPerCSSPixel(); michael@0: nscoord wholeSizeX = NSToCoordRound(aWholeImageSize.width*appUnitsPerCSSPixel*scaleX); michael@0: nscoord wholeSizeY = NSToCoordRound(aWholeImageSize.height*appUnitsPerCSSPixel*scaleY); michael@0: return nsRect(aDestArea.TopLeft() - nsPoint(destOffsetX, destOffsetY), michael@0: nsSize(wholeSizeX, wholeSizeY)); michael@0: } michael@0: michael@0: /* static */ already_AddRefed michael@0: nsLayoutUtils::OrientImage(imgIContainer* aContainer, michael@0: const nsStyleImageOrientation& aOrientation) michael@0: { michael@0: MOZ_ASSERT(aContainer, "Should have an image container"); michael@0: nsCOMPtr img(aContainer); michael@0: michael@0: if (aOrientation.IsFromImage()) { michael@0: img = ImageOps::Orient(img, img->GetOrientation()); michael@0: } else if (!aOrientation.IsDefault()) { michael@0: Angle angle = aOrientation.Angle(); michael@0: Flip flip = aOrientation.IsFlipped() ? Flip::Horizontal michael@0: : Flip::Unflipped; michael@0: img = ImageOps::Orient(img, Orientation(angle, flip)); michael@0: } michael@0: michael@0: return img.forget(); michael@0: } michael@0: michael@0: static bool NonZeroStyleCoord(const nsStyleCoord& aCoord) michael@0: { michael@0: if (aCoord.IsCoordPercentCalcUnit()) { michael@0: // Since negative results are clamped to 0, check > 0. michael@0: return nsRuleNode::ComputeCoordPercentCalc(aCoord, nscoord_MAX) > 0 || michael@0: nsRuleNode::ComputeCoordPercentCalc(aCoord, 0) > 0; michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::HasNonZeroCorner(const nsStyleCorners& aCorners) michael@0: { michael@0: NS_FOR_CSS_HALF_CORNERS(corner) { michael@0: if (NonZeroStyleCoord(aCorners.Get(corner))) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: // aCorner is a "full corner" value, i.e. NS_CORNER_TOP_LEFT etc michael@0: static bool IsCornerAdjacentToSide(uint8_t aCorner, css::Side aSide) michael@0: { michael@0: PR_STATIC_ASSERT((int)NS_SIDE_TOP == NS_CORNER_TOP_LEFT); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_RIGHT == NS_CORNER_TOP_RIGHT); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_BOTTOM == NS_CORNER_BOTTOM_RIGHT); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_LEFT == NS_CORNER_BOTTOM_LEFT); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_TOP == ((NS_CORNER_TOP_RIGHT - 1)&3)); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_RIGHT == ((NS_CORNER_BOTTOM_RIGHT - 1)&3)); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_BOTTOM == ((NS_CORNER_BOTTOM_LEFT - 1)&3)); michael@0: PR_STATIC_ASSERT((int)NS_SIDE_LEFT == ((NS_CORNER_TOP_LEFT - 1)&3)); michael@0: michael@0: return aSide == aCorner || aSide == ((aCorner - 1)&3); michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::HasNonZeroCornerOnSide(const nsStyleCorners& aCorners, michael@0: css::Side aSide) michael@0: { michael@0: PR_STATIC_ASSERT(NS_CORNER_TOP_LEFT_X/2 == NS_CORNER_TOP_LEFT); michael@0: PR_STATIC_ASSERT(NS_CORNER_TOP_LEFT_Y/2 == NS_CORNER_TOP_LEFT); michael@0: PR_STATIC_ASSERT(NS_CORNER_TOP_RIGHT_X/2 == NS_CORNER_TOP_RIGHT); michael@0: PR_STATIC_ASSERT(NS_CORNER_TOP_RIGHT_Y/2 == NS_CORNER_TOP_RIGHT); michael@0: PR_STATIC_ASSERT(NS_CORNER_BOTTOM_RIGHT_X/2 == NS_CORNER_BOTTOM_RIGHT); michael@0: PR_STATIC_ASSERT(NS_CORNER_BOTTOM_RIGHT_Y/2 == NS_CORNER_BOTTOM_RIGHT); michael@0: PR_STATIC_ASSERT(NS_CORNER_BOTTOM_LEFT_X/2 == NS_CORNER_BOTTOM_LEFT); michael@0: PR_STATIC_ASSERT(NS_CORNER_BOTTOM_LEFT_Y/2 == NS_CORNER_BOTTOM_LEFT); michael@0: michael@0: NS_FOR_CSS_HALF_CORNERS(corner) { michael@0: // corner is a "half corner" value, so dividing by two gives us a michael@0: // "full corner" value. michael@0: if (NonZeroStyleCoord(aCorners.Get(corner)) && michael@0: IsCornerAdjacentToSide(corner/2, aSide)) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: /* static */ nsTransparencyMode michael@0: nsLayoutUtils::GetFrameTransparency(nsIFrame* aBackgroundFrame, michael@0: nsIFrame* aCSSRootFrame) { michael@0: if (aCSSRootFrame->StyleDisplay()->mOpacity < 1.0f) michael@0: return eTransparencyTransparent; michael@0: michael@0: if (HasNonZeroCorner(aCSSRootFrame->StyleBorder()->mBorderRadius)) michael@0: return eTransparencyTransparent; michael@0: michael@0: if (aCSSRootFrame->StyleDisplay()->mAppearance == NS_THEME_WIN_GLASS) michael@0: return eTransparencyGlass; michael@0: michael@0: if (aCSSRootFrame->StyleDisplay()->mAppearance == NS_THEME_WIN_BORDERLESS_GLASS) michael@0: return eTransparencyBorderlessGlass; michael@0: michael@0: nsITheme::Transparency transparency; michael@0: if (aCSSRootFrame->IsThemed(&transparency)) michael@0: return transparency == nsITheme::eTransparent michael@0: ? eTransparencyTransparent michael@0: : eTransparencyOpaque; michael@0: michael@0: // We need an uninitialized window to be treated as opaque because michael@0: // doing otherwise breaks window display effects on some platforms, michael@0: // specifically Vista. (bug 450322) michael@0: if (aBackgroundFrame->GetType() == nsGkAtoms::viewportFrame && michael@0: !aBackgroundFrame->GetFirstPrincipalChild()) { michael@0: return eTransparencyOpaque; michael@0: } michael@0: michael@0: nsStyleContext* bgSC; michael@0: if (!nsCSSRendering::FindBackground(aBackgroundFrame, &bgSC)) { michael@0: return eTransparencyTransparent; michael@0: } michael@0: const nsStyleBackground* bg = bgSC->StyleBackground(); michael@0: if (NS_GET_A(bg->mBackgroundColor) < 255 || michael@0: // bottom layer's clip is used for the color michael@0: bg->BottomLayer().mClip != NS_STYLE_BG_CLIP_BORDER) michael@0: return eTransparencyTransparent; michael@0: return eTransparencyOpaque; michael@0: } michael@0: michael@0: static bool IsPopupFrame(nsIFrame* aFrame) michael@0: { michael@0: // aFrame is a popup it's the list control frame dropdown for a combobox. michael@0: nsIAtom* frameType = aFrame->GetType(); michael@0: if (frameType == nsGkAtoms::listControlFrame) { michael@0: nsListControlFrame* lcf = static_cast(aFrame); michael@0: return lcf->IsInDropDownMode(); michael@0: } michael@0: michael@0: // ... or if it's a XUL menupopup frame. michael@0: return frameType == nsGkAtoms::menuPopupFrame; michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::IsPopup(nsIFrame* aFrame) michael@0: { michael@0: // Optimization: the frame can't possibly be a popup if it has no view. michael@0: if (!aFrame->HasView()) { michael@0: NS_ASSERTION(!IsPopupFrame(aFrame), "popup frame must have a view"); michael@0: return false; michael@0: } michael@0: return IsPopupFrame(aFrame); michael@0: } michael@0: michael@0: /* static */ nsIFrame* michael@0: nsLayoutUtils::GetDisplayRootFrame(nsIFrame* aFrame) michael@0: { michael@0: // We could use GetRootPresContext() here if the michael@0: // NS_FRAME_IN_POPUP frame bit is set. michael@0: nsIFrame* f = aFrame; michael@0: for (;;) { michael@0: if (!f->HasAnyStateBits(NS_FRAME_IN_POPUP)) { michael@0: f = f->PresContext()->FrameManager()->GetRootFrame(); michael@0: } else if (IsPopup(f)) { michael@0: return f; michael@0: } michael@0: nsIFrame* parent = GetCrossDocParentFrame(f); michael@0: if (!parent) michael@0: return f; michael@0: f = parent; michael@0: } michael@0: } michael@0: michael@0: /* static */ nsIFrame* michael@0: nsLayoutUtils::GetReferenceFrame(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame *f = aFrame; michael@0: for (;;) { michael@0: if (f->IsTransformed() || IsPopup(f)) { michael@0: return f; michael@0: } michael@0: nsIFrame* parent = GetCrossDocParentFrame(f); michael@0: if (!parent) { michael@0: return f; michael@0: } michael@0: f = parent; michael@0: } michael@0: } michael@0: michael@0: /* static */ nsIFrame* michael@0: nsLayoutUtils::GetTransformRootFrame(nsIFrame* aFrame) michael@0: { michael@0: nsIFrame *parent = nsLayoutUtils::GetCrossDocParentFrame(aFrame); michael@0: while (parent && parent->Preserves3DChildren()) { michael@0: parent = nsLayoutUtils::GetCrossDocParentFrame(parent); michael@0: } michael@0: return parent; michael@0: } michael@0: michael@0: /* static */ uint32_t michael@0: nsLayoutUtils::GetTextRunFlagsForStyle(nsStyleContext* aStyleContext, michael@0: const nsStyleFont* aStyleFont, michael@0: const nsStyleText* aStyleText, michael@0: nscoord aLetterSpacing) michael@0: { michael@0: uint32_t result = 0; michael@0: if (aLetterSpacing != 0) { michael@0: result |= gfxTextRunFactory::TEXT_DISABLE_OPTIONAL_LIGATURES; michael@0: } michael@0: if (aStyleText->mControlCharacterVisibility == NS_STYLE_CONTROL_CHARACTER_VISIBILITY_HIDDEN) { michael@0: result |= gfxTextRunFactory::TEXT_HIDE_CONTROL_CHARACTERS; michael@0: } michael@0: switch (aStyleContext->StyleSVG()->mTextRendering) { michael@0: case NS_STYLE_TEXT_RENDERING_OPTIMIZESPEED: michael@0: result |= gfxTextRunFactory::TEXT_OPTIMIZE_SPEED; michael@0: break; michael@0: case NS_STYLE_TEXT_RENDERING_AUTO: michael@0: if (aStyleFont->mFont.size < michael@0: aStyleContext->PresContext()->GetAutoQualityMinFontSize()) { michael@0: result |= gfxTextRunFactory::TEXT_OPTIMIZE_SPEED; michael@0: } michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /* static */ void michael@0: nsLayoutUtils::GetRectDifferenceStrips(const nsRect& aR1, const nsRect& aR2, michael@0: nsRect* aHStrip, nsRect* aVStrip) { michael@0: NS_ASSERTION(aR1.TopLeft() == aR2.TopLeft(), michael@0: "expected rects at the same position"); michael@0: nsRect unionRect(aR1.x, aR1.y, std::max(aR1.width, aR2.width), michael@0: std::max(aR1.height, aR2.height)); michael@0: nscoord VStripStart = std::min(aR1.width, aR2.width); michael@0: nscoord HStripStart = std::min(aR1.height, aR2.height); michael@0: *aVStrip = unionRect; michael@0: aVStrip->x += VStripStart; michael@0: aVStrip->width -= VStripStart; michael@0: *aHStrip = unionRect; michael@0: aHStrip->y += HStripStart; michael@0: aHStrip->height -= HStripStart; michael@0: } michael@0: michael@0: nsDeviceContext* michael@0: nsLayoutUtils::GetDeviceContextForScreenInfo(nsPIDOMWindow* aWindow) michael@0: { michael@0: if (!aWindow) { michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCOMPtr docShell = aWindow->GetDocShell(); michael@0: while (docShell) { michael@0: // Now make sure our size is up to date. That will mean that the device michael@0: // context does the right thing on multi-monitor systems when we return it to michael@0: // the caller. It will also make sure that our prescontext has been created, michael@0: // if we're supposed to have one. michael@0: nsCOMPtr win = do_GetInterface(docShell); michael@0: if (!win) { michael@0: // No reason to go on michael@0: return nullptr; michael@0: } michael@0: michael@0: win->EnsureSizeUpToDate(); michael@0: michael@0: nsRefPtr presContext; michael@0: docShell->GetPresContext(getter_AddRefs(presContext)); michael@0: if (presContext) { michael@0: nsDeviceContext* context = presContext->DeviceContext(); michael@0: if (context) { michael@0: return context; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr parentItem; michael@0: docShell->GetParent(getter_AddRefs(parentItem)); michael@0: docShell = do_QueryInterface(parentItem); michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: /* static */ bool michael@0: nsLayoutUtils::IsReallyFixedPos(nsIFrame* aFrame) michael@0: { michael@0: NS_PRECONDITION(aFrame->GetParent(), michael@0: "IsReallyFixedPos called on frame not in tree"); michael@0: NS_PRECONDITION(aFrame->StyleDisplay()->mPosition == michael@0: NS_STYLE_POSITION_FIXED, michael@0: "IsReallyFixedPos called on non-'position:fixed' frame"); michael@0: michael@0: nsIAtom *parentType = aFrame->GetParent()->GetType(); michael@0: return parentType == nsGkAtoms::viewportFrame || michael@0: parentType == nsGkAtoms::pageContentFrame; michael@0: } michael@0: michael@0: nsLayoutUtils::SurfaceFromElementResult michael@0: nsLayoutUtils::SurfaceFromElement(nsIImageLoadingContent* aElement, michael@0: uint32_t aSurfaceFlags, michael@0: DrawTarget* aTarget) michael@0: { michael@0: SurfaceFromElementResult result; michael@0: nsresult rv; michael@0: michael@0: nsCOMPtr imgRequest; michael@0: rv = aElement->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, michael@0: getter_AddRefs(imgRequest)); michael@0: if (NS_FAILED(rv) || !imgRequest) michael@0: return result; michael@0: michael@0: uint32_t status; michael@0: imgRequest->GetImageStatus(&status); michael@0: if ((status & imgIRequest::STATUS_LOAD_COMPLETE) == 0) { michael@0: // Spec says to use GetComplete, but that only works on michael@0: // nsIDOMHTMLImageElement, and we support all sorts of other stuff michael@0: // here. Do this for now pending spec clarification. michael@0: result.mIsStillLoading = (status & imgIRequest::STATUS_ERROR) == 0; michael@0: return result; michael@0: } michael@0: michael@0: nsCOMPtr principal; michael@0: rv = imgRequest->GetImagePrincipal(getter_AddRefs(principal)); michael@0: if (NS_FAILED(rv)) michael@0: return result; michael@0: michael@0: nsCOMPtr imgContainer; michael@0: rv = imgRequest->GetImage(getter_AddRefs(imgContainer)); michael@0: if (NS_FAILED(rv)) michael@0: return result; michael@0: michael@0: uint32_t noRasterize = aSurfaceFlags & SFE_NO_RASTERIZING_VECTORS; michael@0: michael@0: uint32_t whichFrame = (aSurfaceFlags & SFE_WANT_FIRST_FRAME) michael@0: ? (uint32_t) imgIContainer::FRAME_FIRST michael@0: : (uint32_t) imgIContainer::FRAME_CURRENT; michael@0: uint32_t frameFlags = imgIContainer::FLAG_SYNC_DECODE; michael@0: if (aSurfaceFlags & SFE_NO_COLORSPACE_CONVERSION) michael@0: frameFlags |= imgIContainer::FLAG_DECODE_NO_COLORSPACE_CONVERSION; michael@0: if (aSurfaceFlags & SFE_PREFER_NO_PREMULTIPLY_ALPHA) { michael@0: frameFlags |= imgIContainer::FLAG_DECODE_NO_PREMULTIPLY_ALPHA; michael@0: result.mIsPremultiplied = false; michael@0: } michael@0: michael@0: int32_t imgWidth, imgHeight; michael@0: rv = imgContainer->GetWidth(&imgWidth); michael@0: nsresult rv2 = imgContainer->GetHeight(&imgHeight); michael@0: if (NS_FAILED(rv) || NS_FAILED(rv2)) michael@0: return result; michael@0: michael@0: if (!noRasterize || imgContainer->GetType() == imgIContainer::TYPE_RASTER) { michael@0: if (aSurfaceFlags & SFE_WANT_IMAGE_SURFACE) { michael@0: frameFlags |= imgIContainer::FLAG_WANT_DATA_SURFACE; michael@0: } michael@0: result.mSourceSurface = imgContainer->GetFrame(whichFrame, frameFlags); michael@0: if (!result.mSourceSurface) { michael@0: return result; michael@0: } michael@0: // The surface we return is likely to be cached. We don't want to have to michael@0: // convert to a surface that's compatible with aTarget each time it's used michael@0: // (that would result in terrible performance), so we convert once here michael@0: // upfront if aTarget is specified. michael@0: if (aTarget) { michael@0: RefPtr optSurface = michael@0: aTarget->OptimizeSourceSurface(result.mSourceSurface); michael@0: if (optSurface) { michael@0: result.mSourceSurface = optSurface; michael@0: } michael@0: } michael@0: } else { michael@0: result.mDrawInfo.mImgContainer = imgContainer; michael@0: result.mDrawInfo.mWhichFrame = whichFrame; michael@0: result.mDrawInfo.mDrawingFlags = frameFlags; michael@0: } michael@0: michael@0: int32_t corsmode; michael@0: if (NS_SUCCEEDED(imgRequest->GetCORSMode(&corsmode))) { michael@0: result.mCORSUsed = (corsmode != imgIRequest::CORS_NONE); michael@0: } michael@0: michael@0: result.mSize = gfxIntSize(imgWidth, imgHeight); michael@0: result.mPrincipal = principal.forget(); michael@0: // no images, including SVG images, can load content from another domain. michael@0: result.mIsWriteOnly = false; michael@0: result.mImageRequest = imgRequest.forget(); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsLayoutUtils::SurfaceFromElementResult michael@0: nsLayoutUtils::SurfaceFromElement(HTMLImageElement *aElement, michael@0: uint32_t aSurfaceFlags, michael@0: DrawTarget* aTarget) michael@0: { michael@0: return SurfaceFromElement(static_cast(aElement), michael@0: aSurfaceFlags, aTarget); michael@0: } michael@0: michael@0: nsLayoutUtils::SurfaceFromElementResult michael@0: nsLayoutUtils::SurfaceFromElement(HTMLCanvasElement* aElement, michael@0: uint32_t aSurfaceFlags, michael@0: DrawTarget* aTarget) michael@0: { michael@0: SurfaceFromElementResult result; michael@0: michael@0: bool* isPremultiplied = nullptr; michael@0: if (aSurfaceFlags & SFE_PREFER_NO_PREMULTIPLY_ALPHA) { michael@0: isPremultiplied = &result.mIsPremultiplied; michael@0: } michael@0: michael@0: gfxIntSize size = aElement->GetSize(); michael@0: michael@0: result.mSourceSurface = aElement->GetSurfaceSnapshot(isPremultiplied); michael@0: if (!result.mSourceSurface) { michael@0: // If the element doesn't have a context then we won't get a snapshot. The canvas spec wants us to not error and just michael@0: // draw nothing, so return an empty surface. michael@0: DrawTarget *ref = aTarget ? aTarget : gfxPlatform::GetPlatform()->ScreenReferenceDrawTarget(); michael@0: RefPtr dt = ref->CreateSimilarDrawTarget(IntSize(size.width, size.height), michael@0: SurfaceFormat::B8G8R8A8); michael@0: if (dt) { michael@0: result.mSourceSurface = dt->Snapshot(); michael@0: } michael@0: } else if (aTarget) { michael@0: RefPtr opt = aTarget->OptimizeSourceSurface(result.mSourceSurface); michael@0: if (opt) { michael@0: result.mSourceSurface = opt; michael@0: } michael@0: } michael@0: michael@0: // Ensure that any future changes to the canvas trigger proper invalidation, michael@0: // in case this is being used by -moz-element() michael@0: aElement->MarkContextClean(); michael@0: michael@0: result.mSize = size; michael@0: result.mPrincipal = aElement->NodePrincipal(); michael@0: result.mIsWriteOnly = aElement->IsWriteOnly(); michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsLayoutUtils::SurfaceFromElementResult michael@0: nsLayoutUtils::SurfaceFromElement(HTMLVideoElement* aElement, michael@0: uint32_t aSurfaceFlags, michael@0: DrawTarget* aTarget) michael@0: { michael@0: SurfaceFromElementResult result; michael@0: michael@0: NS_WARN_IF_FALSE((aSurfaceFlags & SFE_PREFER_NO_PREMULTIPLY_ALPHA) == 0, "We can't support non-premultiplied alpha for video!"); michael@0: michael@0: uint16_t readyState; michael@0: if (NS_SUCCEEDED(aElement->GetReadyState(&readyState)) && michael@0: (readyState == nsIDOMHTMLMediaElement::HAVE_NOTHING || michael@0: readyState == nsIDOMHTMLMediaElement::HAVE_METADATA)) { michael@0: result.mIsStillLoading = true; michael@0: return result; michael@0: } michael@0: michael@0: // If it doesn't have a principal, just bail michael@0: nsCOMPtr principal = aElement->GetCurrentPrincipal(); michael@0: if (!principal) michael@0: return result; michael@0: michael@0: ImageContainer *container = aElement->GetImageContainer(); michael@0: if (!container) michael@0: return result; michael@0: michael@0: mozilla::gfx::IntSize size; michael@0: result.mSourceSurface = container->GetCurrentAsSourceSurface(&size); michael@0: if (!result.mSourceSurface) michael@0: return result; michael@0: michael@0: if (aTarget) { michael@0: RefPtr opt = aTarget->OptimizeSourceSurface(result.mSourceSurface); michael@0: if (opt) { michael@0: result.mSourceSurface = opt; michael@0: } michael@0: } michael@0: michael@0: result.mCORSUsed = aElement->GetCORSMode() != CORS_NONE; michael@0: result.mSize = ThebesIntSize(size); michael@0: result.mPrincipal = principal.forget(); michael@0: result.mIsWriteOnly = false; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsLayoutUtils::SurfaceFromElementResult michael@0: nsLayoutUtils::SurfaceFromElement(dom::Element* aElement, michael@0: uint32_t aSurfaceFlags, michael@0: DrawTarget* aTarget) michael@0: { michael@0: // If it's a , we may be able to just grab its internal surface michael@0: if (HTMLCanvasElement* canvas = michael@0: HTMLCanvasElement::FromContentOrNull(aElement)) { michael@0: return SurfaceFromElement(canvas, aSurfaceFlags, aTarget); michael@0: } michael@0: michael@0: // Maybe it's