michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim:cindent:ts=2:et:sw=2: michael@0: * 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: * This Original Code has been modified by IBM Corporation. Modifications made by IBM michael@0: * described herein are Copyright (c) International Business Machines Corporation, 2000. michael@0: * Modifications to Mozilla code or documentation identified per MPL Section 3.3 michael@0: * michael@0: * Date Modified by Description of modification michael@0: * 04/20/2000 IBM Corp. OS/2 VisualAge build. michael@0: */ michael@0: michael@0: /* state and methods used while laying out a single line of a block frame */ michael@0: michael@0: #ifndef nsLineLayout_h___ michael@0: #define nsLineLayout_h___ michael@0: michael@0: #include "nsLineBox.h" michael@0: #include "nsBlockReflowState.h" michael@0: #include "plarena.h" michael@0: #include "gfxTypes.h" michael@0: #include "WritingModes.h" michael@0: michael@0: class nsFloatManager; michael@0: struct nsStyleText; michael@0: michael@0: class nsLineLayout { michael@0: public: michael@0: nsLineLayout(nsPresContext* aPresContext, michael@0: nsFloatManager* aFloatManager, michael@0: const nsHTMLReflowState* aOuterReflowState, michael@0: const nsLineList::iterator* aLine); michael@0: ~nsLineLayout(); michael@0: michael@0: void Init(nsBlockReflowState* aState, nscoord aMinLineBSize, michael@0: int32_t aLineNumber) { michael@0: mBlockRS = aState; michael@0: mMinLineBSize = aMinLineBSize; michael@0: mLineNumber = aLineNumber; michael@0: } michael@0: michael@0: int32_t GetLineNumber() const { michael@0: return mLineNumber; michael@0: } michael@0: michael@0: void BeginLineReflow(nscoord aICoord, nscoord aBCoord, michael@0: nscoord aISize, nscoord aBSize, michael@0: bool aImpactedByFloats, michael@0: bool aIsTopOfPage, michael@0: mozilla::WritingMode aWritingMode, michael@0: nscoord aContainerWidth); michael@0: michael@0: void EndLineReflow(); michael@0: michael@0: /** michael@0: * Called when a float has been placed. This method updates the michael@0: * inline frame and span data to account for any change in positions michael@0: * due to available space for the line boxes changing. michael@0: * @param aX/aY/aWidth/aHeight are the new available michael@0: * space rectangle, relative to the containing block. michael@0: * @param aFloatFrame the float frame that was placed. michael@0: */ michael@0: void UpdateBand(const nsRect& aNewAvailableSpace, michael@0: nsIFrame* aFloatFrame); michael@0: michael@0: void BeginSpan(nsIFrame* aFrame, const nsHTMLReflowState* aSpanReflowState, michael@0: nscoord aLeftEdge, nscoord aRightEdge, nscoord* aBaseline); michael@0: michael@0: // Returns the width of the span michael@0: nscoord EndSpan(nsIFrame* aFrame); michael@0: michael@0: int32_t GetCurrentSpanCount() const; michael@0: michael@0: void SplitLineTo(int32_t aNewCount); michael@0: michael@0: bool IsZeroBSize(); michael@0: michael@0: // Reflows the frame and returns the reflow status. aPushedFrame is true michael@0: // if the frame is pushed to the next line because it doesn't fit michael@0: nsresult ReflowFrame(nsIFrame* aFrame, michael@0: nsReflowStatus& aReflowStatus, michael@0: nsHTMLReflowMetrics* aMetrics, michael@0: bool& aPushedFrame); michael@0: michael@0: void AddBulletFrame(nsIFrame* aFrame, const nsHTMLReflowMetrics& aMetrics); michael@0: michael@0: void RemoveBulletFrame(nsIFrame* aFrame) { michael@0: PushFrame(aFrame); michael@0: } michael@0: michael@0: void BlockDirAlignLine(); michael@0: michael@0: bool TrimTrailingWhiteSpace(); michael@0: michael@0: void InlineDirAlignFrames(nsLineBox* aLine, bool aIsLastLine); michael@0: michael@0: /** michael@0: * Handle all the relative positioning in the line, compute the michael@0: * combined area (== overflow area) for the line, and handle view michael@0: * sizing/positioning and the setting of the overflow rect. michael@0: */ michael@0: void RelativePositionFrames(nsOverflowAreas& aOverflowAreas); michael@0: michael@0: // Support methods for word-wrapping during line reflow michael@0: michael@0: void SetTextJustificationWeights(int32_t aNumSpaces, int32_t aNumLetters) { michael@0: mTextJustificationNumSpaces = aNumSpaces; michael@0: mTextJustificationNumLetters = aNumLetters; michael@0: } michael@0: michael@0: /** michael@0: * @return true if so far during reflow no non-empty content has been michael@0: * placed in the line (according to nsIFrame::IsEmpty()) michael@0: */ michael@0: bool LineIsEmpty() const michael@0: { michael@0: return mLineIsEmpty; michael@0: } michael@0: michael@0: /** michael@0: * @return true if so far during reflow no non-empty leaf content michael@0: * (non-collapsed whitespace, replaced element, inline-block, etc) has been michael@0: * placed in the line michael@0: */ michael@0: bool LineAtStart() const michael@0: { michael@0: return mLineAtStart; michael@0: } michael@0: michael@0: bool LineIsBreakable() const; michael@0: michael@0: bool GetLineEndsInBR() const michael@0: { michael@0: return mLineEndsInBR; michael@0: } michael@0: michael@0: void SetLineEndsInBR(bool aOn) michael@0: { michael@0: mLineEndsInBR = aOn; michael@0: } michael@0: michael@0: //---------------------------------------- michael@0: // Inform the line-layout about the presence of a floating frame michael@0: // XXX get rid of this: use get-frame-type? michael@0: bool AddFloat(nsIFrame* aFloat, nscoord aAvailableWidth) michael@0: { michael@0: return mBlockRS->AddFloat(this, aFloat, aAvailableWidth); michael@0: } michael@0: michael@0: void SetTrimmableWidth(nscoord aTrimmableWidth) { michael@0: mTrimmableWidth = aTrimmableWidth; michael@0: } michael@0: michael@0: //---------------------------------------- michael@0: michael@0: bool GetFirstLetterStyleOK() const { michael@0: return mFirstLetterStyleOK; michael@0: } michael@0: michael@0: void SetFirstLetterStyleOK(bool aSetting) { michael@0: mFirstLetterStyleOK = aSetting; michael@0: } michael@0: michael@0: bool GetInFirstLetter() const { michael@0: return mInFirstLetter; michael@0: } michael@0: michael@0: void SetInFirstLetter(bool aSetting) { michael@0: mInFirstLetter = aSetting; michael@0: } michael@0: michael@0: bool GetInFirstLine() const { michael@0: return mInFirstLine; michael@0: } michael@0: michael@0: void SetInFirstLine(bool aSetting) { michael@0: mInFirstLine = aSetting; michael@0: } michael@0: michael@0: // Calling this during block reflow ensures that the next line of inlines michael@0: // will be marked dirty, if there is one. michael@0: void SetDirtyNextLine() { michael@0: mDirtyNextLine = true; michael@0: } michael@0: bool GetDirtyNextLine() { michael@0: return mDirtyNextLine; michael@0: } michael@0: michael@0: //---------------------------------------- michael@0: michael@0: nsPresContext* mPresContext; michael@0: michael@0: /** michael@0: * Record where an optional break could have been placed. During line reflow, michael@0: * frames containing optional break points (e.g., whitespace in text frames) michael@0: * can call SetLastOptionalBreakPosition to record where a break could michael@0: * have been made, but wasn't because we decided to place more content on michael@0: * the line. For non-text frames, offset 0 means michael@0: * before the content, offset INT32_MAX means after the content. michael@0: * michael@0: * Currently this is used to handle cases where a single word comprises michael@0: * multiple frames, and the first frame fits on the line but the whole word michael@0: * doesn't. We look back to the last optional break position and michael@0: * reflow the whole line again, forcing a break at that position. The last michael@0: * optional break position could be in a text frame or else after a frame michael@0: * that cannot be part of a text run, so those are the positions we record. michael@0: * michael@0: * @param aFits set to true if the break position is within the available width. michael@0: * michael@0: * @param aPriority the priority of the break opportunity. If we are michael@0: * prioritizing break opportunities, we will not set a break if we have michael@0: * already set a break with a higher priority. @see gfxBreakPriority. michael@0: * michael@0: * @return true if we are actually reflowing with forced break position and we michael@0: * should break here michael@0: */ michael@0: bool NotifyOptionalBreakPosition(nsIContent* aContent, int32_t aOffset, michael@0: bool aFits, gfxBreakPriority aPriority) { michael@0: NS_ASSERTION(!aFits || !mNeedBackup, michael@0: "Shouldn't be updating the break position with a break that fits after we've already flagged an overrun"); michael@0: // Remember the last break position that fits; if there was no break that fit, michael@0: // just remember the first break michael@0: if ((aFits && aPriority >= mLastOptionalBreakPriority) || michael@0: !mLastOptionalBreakContent) { michael@0: mLastOptionalBreakContent = aContent; michael@0: mLastOptionalBreakContentOffset = aOffset; michael@0: mLastOptionalBreakPriority = aPriority; michael@0: } michael@0: return aContent && mForceBreakContent == aContent && michael@0: mForceBreakContentOffset == aOffset; michael@0: } michael@0: /** michael@0: * Like NotifyOptionalBreakPosition, but here it's OK for mNeedBackup michael@0: * to be set, because the caller is merely pruning some saved break position(s) michael@0: * that are actually not feasible. michael@0: */ michael@0: void RestoreSavedBreakPosition(nsIContent* aContent, int32_t aOffset, michael@0: gfxBreakPriority aPriority) { michael@0: mLastOptionalBreakContent = aContent; michael@0: mLastOptionalBreakContentOffset = aOffset; michael@0: mLastOptionalBreakPriority = aPriority; michael@0: } michael@0: /** michael@0: * Signal that no backing up will be required after all. michael@0: */ michael@0: void ClearOptionalBreakPosition() { michael@0: mNeedBackup = false; michael@0: mLastOptionalBreakContent = nullptr; michael@0: mLastOptionalBreakContentOffset = -1; michael@0: mLastOptionalBreakPriority = gfxBreakPriority::eNoBreak; michael@0: } michael@0: // Retrieve last set optional break position. When this returns null, no michael@0: // optional break has been recorded (which means that the line can't break yet). michael@0: nsIContent* GetLastOptionalBreakPosition(int32_t* aOffset, michael@0: gfxBreakPriority* aPriority) { michael@0: *aOffset = mLastOptionalBreakContentOffset; michael@0: *aPriority = mLastOptionalBreakPriority; michael@0: return mLastOptionalBreakContent; michael@0: } michael@0: michael@0: /** michael@0: * Check whether frames overflowed the available width and CanPlaceFrame michael@0: * requested backing up to a saved break position. michael@0: */ michael@0: bool NeedsBackup() { return mNeedBackup; } michael@0: michael@0: // Line layout may place too much content on a line, overflowing its available michael@0: // width. When that happens, if SetLastOptionalBreakPosition has been michael@0: // used to record an optional break that wasn't taken, we can reflow the line michael@0: // again and force the break to happen at that point (i.e., backtracking michael@0: // to the last choice point). michael@0: michael@0: // Record that we want to break at the given content+offset (which michael@0: // should have been previously returned by GetLastOptionalBreakPosition michael@0: // from another nsLineLayout). michael@0: void ForceBreakAtPosition(nsIContent* aContent, int32_t aOffset) { michael@0: mForceBreakContent = aContent; michael@0: mForceBreakContentOffset = aOffset; michael@0: } michael@0: bool HaveForcedBreakPosition() { return mForceBreakContent != nullptr; } michael@0: int32_t GetForcedBreakPosition(nsIContent* aContent) { michael@0: return mForceBreakContent == aContent ? mForceBreakContentOffset : -1; michael@0: } michael@0: michael@0: /** michael@0: * This can't be null. It usually returns a block frame but may return michael@0: * some other kind of frame when inline frames are reflowed in a non-block michael@0: * context (e.g. MathML or floating first-letter). michael@0: */ michael@0: nsIFrame* LineContainerFrame() const { return mBlockReflowState->frame; } michael@0: const nsHTMLReflowState* LineContainerRS() const { return mBlockReflowState; } michael@0: const nsLineList::iterator* GetLine() const { michael@0: return mGotLineBox ? &mLineBox : nullptr; michael@0: } michael@0: nsLineList::iterator* GetLine() { michael@0: return mGotLineBox ? &mLineBox : nullptr; michael@0: } michael@0: michael@0: /** michael@0: * Returns the accumulated advance width of frames before the current frame michael@0: * on the line, plus the line container's left border+padding. michael@0: * This is always positive, the advance width is measured from michael@0: * the right edge for RTL blocks and from the left edge for LTR blocks. michael@0: * In other words, the current frame's distance from the line container's michael@0: * start content edge is: michael@0: * GetCurrentFrameInlineDistanceFromBlock() - lineContainer->GetUsedBorderAndPadding().left michael@0: * Note the use of .left for both LTR and RTL line containers. michael@0: */ michael@0: nscoord GetCurrentFrameInlineDistanceFromBlock(); michael@0: michael@0: protected: michael@0: // This state is constant for a given block frame doing line layout michael@0: nsFloatManager* mFloatManager; michael@0: const nsStyleText* mStyleText; // for the block michael@0: const nsHTMLReflowState* mBlockReflowState; michael@0: michael@0: nsIContent* mLastOptionalBreakContent; michael@0: nsIContent* mForceBreakContent; michael@0: michael@0: // XXX remove this when landing bug 154892 (splitting absolute positioned frames) michael@0: friend class nsInlineFrame; michael@0: michael@0: nsBlockReflowState* mBlockRS;/* XXX hack! */ michael@0: michael@0: nsLineList::iterator mLineBox; michael@0: michael@0: // Per-frame data recorded by the line-layout reflow logic. This michael@0: // state is the state needed to post-process the line after reflow michael@0: // has completed (block-direction alignment, inline-direction alignment, michael@0: // justification and relative positioning). michael@0: michael@0: struct PerSpanData; michael@0: struct PerFrameData; michael@0: friend struct PerSpanData; michael@0: friend struct PerFrameData; michael@0: struct PerFrameData michael@0: { michael@0: PerFrameData(mozilla::WritingMode aWritingMode) michael@0: : mBounds(aWritingMode) michael@0: , mMargin(aWritingMode) michael@0: , mBorderPadding(aWritingMode) michael@0: , mOffsets(aWritingMode) michael@0: {} michael@0: michael@0: // link to next/prev frame in same span michael@0: PerFrameData* mNext; michael@0: PerFrameData* mPrev; michael@0: michael@0: // pointer to child span data if this is an inline container frame michael@0: PerSpanData* mSpan; michael@0: michael@0: // The frame michael@0: nsIFrame* mFrame; michael@0: michael@0: // From metrics michael@0: nscoord mAscent; michael@0: // note that mBounds is a logical rect in the *line*'s writing mode. michael@0: // When setting frame coordinates, we have to convert to the frame's michael@0: // writing mode michael@0: mozilla::LogicalRect mBounds; michael@0: nsOverflowAreas mOverflowAreas; michael@0: michael@0: // From reflow-state michael@0: mozilla::LogicalMargin mMargin; michael@0: mozilla::LogicalMargin mBorderPadding; michael@0: mozilla::LogicalMargin mOffsets; michael@0: michael@0: // state for text justification michael@0: int32_t mJustificationNumSpaces; michael@0: int32_t mJustificationNumLetters; michael@0: michael@0: // Other state we use michael@0: uint8_t mBlockDirAlign; michael@0: michael@0: // PerFrameData flags michael@0: #define PFD_RELATIVEPOS 0x00000001 michael@0: #define PFD_ISTEXTFRAME 0x00000002 michael@0: #define PFD_ISNONEMPTYTEXTFRAME 0x00000004 michael@0: #define PFD_ISNONWHITESPACETEXTFRAME 0x00000008 michael@0: #define PFD_ISLETTERFRAME 0x00000010 michael@0: #define PFD_RECOMPUTEOVERFLOW 0x00000020 michael@0: #define PFD_ISBULLET 0x00000040 michael@0: #define PFD_SKIPWHENTRIMMINGWHITESPACE 0x00000080 michael@0: #define PFD_LASTFLAG PFD_SKIPWHENTRIMMINGWHITESPACE michael@0: michael@0: uint8_t mFlags; michael@0: michael@0: void SetFlag(uint32_t aFlag, bool aValue) michael@0: { michael@0: NS_ASSERTION(aFlag<=PFD_LASTFLAG, "bad flag"); michael@0: NS_ASSERTION(aFlag<=UINT8_MAX, "bad flag"); michael@0: if (aValue) { // set flag michael@0: mFlags |= aFlag; michael@0: } michael@0: else { // unset flag michael@0: mFlags &= ~aFlag; michael@0: } michael@0: } michael@0: michael@0: bool GetFlag(uint32_t aFlag) const michael@0: { michael@0: NS_ASSERTION(aFlag<=PFD_LASTFLAG, "bad flag"); michael@0: return !!(mFlags & aFlag); michael@0: } michael@0: michael@0: michael@0: PerFrameData* Last() { michael@0: PerFrameData* pfd = this; michael@0: while (pfd->mNext) { michael@0: pfd = pfd->mNext; michael@0: } michael@0: return pfd; michael@0: } michael@0: }; michael@0: PerFrameData* mFrameFreeList; michael@0: michael@0: struct PerSpanData { michael@0: union { michael@0: PerSpanData* mParent; michael@0: PerSpanData* mNextFreeSpan; michael@0: }; michael@0: PerFrameData* mFrame; michael@0: PerFrameData* mFirstFrame; michael@0: PerFrameData* mLastFrame; michael@0: michael@0: const nsHTMLReflowState* mReflowState; michael@0: bool mNoWrap; michael@0: mozilla::WritingMode mWritingMode; michael@0: bool mZeroEffectiveSpanBox; michael@0: bool mContainsFloat; michael@0: bool mHasNonemptyContent; michael@0: michael@0: nscoord mIStart; michael@0: nscoord mICoord; michael@0: nscoord mIEnd; michael@0: michael@0: nscoord mBStartLeading, mBEndLeading; michael@0: nscoord mLogicalBSize; michael@0: nscoord mMinBCoord, mMaxBCoord; michael@0: nscoord* mBaseline; michael@0: michael@0: void AppendFrame(PerFrameData* pfd) { michael@0: if (nullptr == mLastFrame) { michael@0: mFirstFrame = pfd; michael@0: } michael@0: else { michael@0: mLastFrame->mNext = pfd; michael@0: pfd->mPrev = mLastFrame; michael@0: } michael@0: mLastFrame = pfd; michael@0: } michael@0: }; michael@0: PerSpanData* mSpanFreeList; michael@0: PerSpanData* mRootSpan; michael@0: PerSpanData* mCurrentSpan; michael@0: michael@0: gfxBreakPriority mLastOptionalBreakPriority; michael@0: int32_t mLastOptionalBreakContentOffset; michael@0: int32_t mForceBreakContentOffset; michael@0: michael@0: nscoord mMinLineBSize; michael@0: michael@0: // The amount of text indent that we applied to this line, needed for michael@0: // max-element-size calculation. michael@0: nscoord mTextIndent; michael@0: michael@0: // This state varies during the reflow of a line but is line michael@0: // "global" state not span "local" state. michael@0: int32_t mLineNumber; michael@0: int32_t mTextJustificationNumSpaces; michael@0: int32_t mTextJustificationNumLetters; michael@0: michael@0: int32_t mTotalPlacedFrames; michael@0: michael@0: nscoord mBStartEdge; michael@0: nscoord mMaxStartBoxBSize; michael@0: nscoord mMaxEndBoxBSize; michael@0: michael@0: nscoord mInflationMinFontSize; michael@0: michael@0: // Final computed line-bSize value after BlockDirAlignFrames for michael@0: // the block has been called. michael@0: nscoord mFinalLineBSize; michael@0: michael@0: // Amount of trimmable whitespace width for the trailing text frame, if any michael@0: nscoord mTrimmableWidth; michael@0: michael@0: nscoord mContainerWidth; michael@0: michael@0: bool mFirstLetterStyleOK : 1; michael@0: bool mIsTopOfPage : 1; michael@0: bool mImpactedByFloats : 1; michael@0: bool mLastFloatWasLetterFrame : 1; michael@0: bool mLineIsEmpty : 1; michael@0: bool mLineEndsInBR : 1; michael@0: bool mNeedBackup : 1; michael@0: bool mInFirstLine : 1; michael@0: bool mGotLineBox : 1; michael@0: bool mInFirstLetter : 1; michael@0: bool mHasBullet : 1; michael@0: bool mDirtyNextLine : 1; michael@0: bool mLineAtStart : 1; michael@0: michael@0: int32_t mSpanDepth; michael@0: #ifdef DEBUG michael@0: int32_t mSpansAllocated, mSpansFreed; michael@0: int32_t mFramesAllocated, mFramesFreed; michael@0: #endif michael@0: PLArenaPool mArena; // Per span and per frame data, 4 byte aligned michael@0: michael@0: /** michael@0: * Allocate a PerFrameData from the mArena pool. The allocation is infallible. michael@0: */ michael@0: PerFrameData* NewPerFrameData(nsIFrame* aFrame); michael@0: michael@0: /** michael@0: * Allocate a PerSpanData from the mArena pool. The allocation is infallible. michael@0: */ michael@0: PerSpanData* NewPerSpanData(); michael@0: michael@0: void FreeSpan(PerSpanData* psd); michael@0: michael@0: bool InBlockContext() const { michael@0: return mSpanDepth == 0; michael@0: } michael@0: michael@0: void PushFrame(nsIFrame* aFrame); michael@0: michael@0: void AllowForStartMargin(PerFrameData* pfd, michael@0: nsHTMLReflowState& aReflowState); michael@0: michael@0: bool CanPlaceFrame(PerFrameData* pfd, michael@0: bool aNotSafeToBreak, michael@0: bool aFrameCanContinueTextRun, michael@0: bool aCanRollBackBeforeFrame, michael@0: nsHTMLReflowMetrics& aMetrics, michael@0: nsReflowStatus& aStatus, michael@0: bool* aOptionalBreakAfterFits); michael@0: michael@0: void PlaceFrame(PerFrameData* pfd, michael@0: nsHTMLReflowMetrics& aMetrics); michael@0: michael@0: void BlockDirAlignFrames(PerSpanData* psd); michael@0: michael@0: void PlaceStartEndFrames(PerSpanData* psd, michael@0: nscoord aDistanceFromStart, michael@0: nscoord aLineBSize); michael@0: michael@0: void RelativePositionFrames(PerSpanData* psd, nsOverflowAreas& aOverflowAreas); michael@0: michael@0: bool TrimTrailingWhiteSpaceIn(PerSpanData* psd, nscoord* aDeltaISize); michael@0: michael@0: void ComputeJustificationWeights(PerSpanData* psd, int32_t* numSpaces, int32_t* numLetters); michael@0: michael@0: struct FrameJustificationState { michael@0: int32_t mTotalNumSpaces; michael@0: int32_t mTotalNumLetters; michael@0: nscoord mTotalWidthForSpaces; michael@0: nscoord mTotalWidthForLetters; michael@0: int32_t mNumSpacesProcessed; michael@0: int32_t mNumLettersProcessed; michael@0: nscoord mWidthForSpacesProcessed; michael@0: nscoord mWidthForLettersProcessed; michael@0: }; michael@0: michael@0: // Apply justification. The return value is the amount by which the width of michael@0: // the span corresponding to aPSD got increased due to justification. michael@0: nscoord ApplyFrameJustification(PerSpanData* aPSD, michael@0: FrameJustificationState* aState); michael@0: michael@0: michael@0: #ifdef DEBUG michael@0: void DumpPerSpanData(PerSpanData* psd, int32_t aIndent); michael@0: #endif michael@0: }; michael@0: michael@0: #endif /* nsLineLayout_h___ */