Bug 983971 - Do not use gralloc for small size on ICS gonk. r=nical, a=1.4+
[gecko.git] / layout / generic / nsLineLayout.h
blob7fbe921678396c57ef649e27e8132cdb351b1e75
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim:cindent:ts=2:et:sw=2:
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 * This Original Code has been modified by IBM Corporation. Modifications made by IBM
8 * described herein are Copyright (c) International Business Machines Corporation, 2000.
9 * Modifications to Mozilla code or documentation identified per MPL Section 3.3
11 * Date Modified by Description of modification
12 * 04/20/2000 IBM Corp. OS/2 VisualAge build.
15 /* state and methods used while laying out a single line of a block frame */
17 #ifndef nsLineLayout_h___
18 #define nsLineLayout_h___
20 #include "nsLineBox.h"
21 #include "nsBlockReflowState.h"
22 #include "plarena.h"
23 #include "gfxTypes.h"
24 #include "WritingModes.h"
26 class nsFloatManager;
27 struct nsStyleText;
29 class nsLineLayout {
30 public:
31 nsLineLayout(nsPresContext* aPresContext,
32 nsFloatManager* aFloatManager,
33 const nsHTMLReflowState* aOuterReflowState,
34 const nsLineList::iterator* aLine);
35 ~nsLineLayout();
37 void Init(nsBlockReflowState* aState, nscoord aMinLineBSize,
38 int32_t aLineNumber) {
39 mBlockRS = aState;
40 mMinLineBSize = aMinLineBSize;
41 mLineNumber = aLineNumber;
44 int32_t GetLineNumber() const {
45 return mLineNumber;
48 void BeginLineReflow(nscoord aICoord, nscoord aBCoord,
49 nscoord aISize, nscoord aBSize,
50 bool aImpactedByFloats,
51 bool aIsTopOfPage,
52 mozilla::WritingMode aWritingMode,
53 nscoord aContainerWidth);
55 void EndLineReflow();
57 /**
58 * Called when a float has been placed. This method updates the
59 * inline frame and span data to account for any change in positions
60 * due to available space for the line boxes changing.
61 * @param aX/aY/aWidth/aHeight are the new available
62 * space rectangle, relative to the containing block.
63 * @param aFloatFrame the float frame that was placed.
65 void UpdateBand(const nsRect& aNewAvailableSpace,
66 nsIFrame* aFloatFrame);
68 void BeginSpan(nsIFrame* aFrame, const nsHTMLReflowState* aSpanReflowState,
69 nscoord aLeftEdge, nscoord aRightEdge, nscoord* aBaseline);
71 // Returns the width of the span
72 nscoord EndSpan(nsIFrame* aFrame);
74 int32_t GetCurrentSpanCount() const;
76 void SplitLineTo(int32_t aNewCount);
78 bool IsZeroBSize();
80 // Reflows the frame and returns the reflow status. aPushedFrame is true
81 // if the frame is pushed to the next line because it doesn't fit
82 nsresult ReflowFrame(nsIFrame* aFrame,
83 nsReflowStatus& aReflowStatus,
84 nsHTMLReflowMetrics* aMetrics,
85 bool& aPushedFrame);
87 void AddBulletFrame(nsIFrame* aFrame, const nsHTMLReflowMetrics& aMetrics);
89 void RemoveBulletFrame(nsIFrame* aFrame) {
90 PushFrame(aFrame);
93 void BlockDirAlignLine();
95 bool TrimTrailingWhiteSpace();
97 void InlineDirAlignFrames(nsRect& aLineBounds, bool aIsLastLine,
98 int32_t aFrameCount);
101 * Handle all the relative positioning in the line, compute the
102 * combined area (== overflow area) for the line, and handle view
103 * sizing/positioning and the setting of the overflow rect.
105 void RelativePositionFrames(nsOverflowAreas& aOverflowAreas);
107 // Support methods for word-wrapping during line reflow
109 void SetTextJustificationWeights(int32_t aNumSpaces, int32_t aNumLetters) {
110 mTextJustificationNumSpaces = aNumSpaces;
111 mTextJustificationNumLetters = aNumLetters;
115 * @return true if so far during reflow no non-empty content has been
116 * placed in the line (according to nsIFrame::IsEmpty())
118 bool LineIsEmpty() const
120 return mLineIsEmpty;
124 * @return true if so far during reflow no non-empty leaf content
125 * (non-collapsed whitespace, replaced element, inline-block, etc) has been
126 * placed in the line
128 bool LineAtStart() const
130 return mLineAtStart;
133 bool LineIsBreakable() const;
135 bool GetLineEndsInBR() const
137 return mLineEndsInBR;
140 void SetLineEndsInBR(bool aOn)
142 mLineEndsInBR = aOn;
145 //----------------------------------------
146 // Inform the line-layout about the presence of a floating frame
147 // XXX get rid of this: use get-frame-type?
148 bool AddFloat(nsIFrame* aFloat, nscoord aAvailableWidth)
150 return mBlockRS->AddFloat(this, aFloat, aAvailableWidth);
153 void SetTrimmableWidth(nscoord aTrimmableWidth) {
154 mTrimmableWidth = aTrimmableWidth;
157 //----------------------------------------
159 bool GetFirstLetterStyleOK() const {
160 return mFirstLetterStyleOK;
163 void SetFirstLetterStyleOK(bool aSetting) {
164 mFirstLetterStyleOK = aSetting;
167 bool GetInFirstLetter() const {
168 return mInFirstLetter;
171 void SetInFirstLetter(bool aSetting) {
172 mInFirstLetter = aSetting;
175 bool GetInFirstLine() const {
176 return mInFirstLine;
179 void SetInFirstLine(bool aSetting) {
180 mInFirstLine = aSetting;
183 // Calling this during block reflow ensures that the next line of inlines
184 // will be marked dirty, if there is one.
185 void SetDirtyNextLine() {
186 mDirtyNextLine = true;
188 bool GetDirtyNextLine() {
189 return mDirtyNextLine;
192 //----------------------------------------
194 nsPresContext* mPresContext;
197 * Record where an optional break could have been placed. During line reflow,
198 * frames containing optional break points (e.g., whitespace in text frames)
199 * can call SetLastOptionalBreakPosition to record where a break could
200 * have been made, but wasn't because we decided to place more content on
201 * the line. For non-text frames, offset 0 means
202 * before the content, offset INT32_MAX means after the content.
204 * Currently this is used to handle cases where a single word comprises
205 * multiple frames, and the first frame fits on the line but the whole word
206 * doesn't. We look back to the last optional break position and
207 * reflow the whole line again, forcing a break at that position. The last
208 * optional break position could be in a text frame or else after a frame
209 * that cannot be part of a text run, so those are the positions we record.
211 * @param aFits set to true if the break position is within the available width.
213 * @param aPriority the priority of the break opportunity. If we are
214 * prioritizing break opportunities, we will not set a break if we have
215 * already set a break with a higher priority. @see gfxBreakPriority.
217 * @return true if we are actually reflowing with forced break position and we
218 * should break here
220 bool NotifyOptionalBreakPosition(nsIContent* aContent, int32_t aOffset,
221 bool aFits, gfxBreakPriority aPriority) {
222 NS_ASSERTION(!aFits || !mNeedBackup,
223 "Shouldn't be updating the break position with a break that fits after we've already flagged an overrun");
224 // Remember the last break position that fits; if there was no break that fit,
225 // just remember the first break
226 if ((aFits && aPriority >= mLastOptionalBreakPriority) ||
227 !mLastOptionalBreakContent) {
228 mLastOptionalBreakContent = aContent;
229 mLastOptionalBreakContentOffset = aOffset;
230 mLastOptionalBreakPriority = aPriority;
232 return aContent && mForceBreakContent == aContent &&
233 mForceBreakContentOffset == aOffset;
236 * Like NotifyOptionalBreakPosition, but here it's OK for mNeedBackup
237 * to be set, because the caller is merely pruning some saved break position(s)
238 * that are actually not feasible.
240 void RestoreSavedBreakPosition(nsIContent* aContent, int32_t aOffset,
241 gfxBreakPriority aPriority) {
242 mLastOptionalBreakContent = aContent;
243 mLastOptionalBreakContentOffset = aOffset;
244 mLastOptionalBreakPriority = aPriority;
247 * Signal that no backing up will be required after all.
249 void ClearOptionalBreakPosition() {
250 mNeedBackup = false;
251 mLastOptionalBreakContent = nullptr;
252 mLastOptionalBreakContentOffset = -1;
253 mLastOptionalBreakPriority = gfxBreakPriority::eNoBreak;
255 // Retrieve last set optional break position. When this returns null, no
256 // optional break has been recorded (which means that the line can't break yet).
257 nsIContent* GetLastOptionalBreakPosition(int32_t* aOffset,
258 gfxBreakPriority* aPriority) {
259 *aOffset = mLastOptionalBreakContentOffset;
260 *aPriority = mLastOptionalBreakPriority;
261 return mLastOptionalBreakContent;
265 * Check whether frames overflowed the available width and CanPlaceFrame
266 * requested backing up to a saved break position.
268 bool NeedsBackup() { return mNeedBackup; }
270 // Line layout may place too much content on a line, overflowing its available
271 // width. When that happens, if SetLastOptionalBreakPosition has been
272 // used to record an optional break that wasn't taken, we can reflow the line
273 // again and force the break to happen at that point (i.e., backtracking
274 // to the last choice point).
276 // Record that we want to break at the given content+offset (which
277 // should have been previously returned by GetLastOptionalBreakPosition
278 // from another nsLineLayout).
279 void ForceBreakAtPosition(nsIContent* aContent, int32_t aOffset) {
280 mForceBreakContent = aContent;
281 mForceBreakContentOffset = aOffset;
283 bool HaveForcedBreakPosition() { return mForceBreakContent != nullptr; }
284 int32_t GetForcedBreakPosition(nsIContent* aContent) {
285 return mForceBreakContent == aContent ? mForceBreakContentOffset : -1;
289 * This can't be null. It usually returns a block frame but may return
290 * some other kind of frame when inline frames are reflowed in a non-block
291 * context (e.g. MathML or floating first-letter).
293 nsIFrame* LineContainerFrame() const { return mBlockReflowState->frame; }
294 const nsHTMLReflowState* LineContainerRS() const { return mBlockReflowState; }
295 const nsLineList::iterator* GetLine() const {
296 return mGotLineBox ? &mLineBox : nullptr;
298 nsLineList::iterator* GetLine() {
299 return mGotLineBox ? &mLineBox : nullptr;
303 * Returns the accumulated advance width of frames before the current frame
304 * on the line, plus the line container's left border+padding.
305 * This is always positive, the advance width is measured from
306 * the right edge for RTL blocks and from the left edge for LTR blocks.
307 * In other words, the current frame's distance from the line container's
308 * start content edge is:
309 * <code>GetCurrentFrameInlineDistanceFromBlock() - lineContainer->GetUsedBorderAndPadding().left</code>
310 * Note the use of <code>.left</code> for both LTR and RTL line containers.
312 nscoord GetCurrentFrameInlineDistanceFromBlock();
314 protected:
315 // This state is constant for a given block frame doing line layout
316 nsFloatManager* mFloatManager;
317 const nsStyleText* mStyleText; // for the block
318 const nsHTMLReflowState* mBlockReflowState;
320 nsIContent* mLastOptionalBreakContent;
321 nsIContent* mForceBreakContent;
323 // XXX remove this when landing bug 154892 (splitting absolute positioned frames)
324 friend class nsInlineFrame;
326 nsBlockReflowState* mBlockRS;/* XXX hack! */
328 nsLineList::iterator mLineBox;
330 // Per-frame data recorded by the line-layout reflow logic. This
331 // state is the state needed to post-process the line after reflow
332 // has completed (block-direction alignment, inline-direction alignment,
333 // justification and relative positioning).
335 struct PerSpanData;
336 struct PerFrameData;
337 friend struct PerSpanData;
338 friend struct PerFrameData;
339 struct PerFrameData
341 PerFrameData(mozilla::WritingMode aWritingMode)
342 : mBounds(aWritingMode)
343 , mMargin(aWritingMode)
344 , mBorderPadding(aWritingMode)
345 , mOffsets(aWritingMode)
348 // link to next/prev frame in same span
349 PerFrameData* mNext;
350 PerFrameData* mPrev;
352 // pointer to child span data if this is an inline container frame
353 PerSpanData* mSpan;
355 // The frame
356 nsIFrame* mFrame;
358 // From metrics
359 nscoord mAscent;
360 // note that mBounds is a logical rect in the *line*'s writing mode.
361 // When setting frame coordinates, we have to convert to the frame's
362 // writing mode
363 mozilla::LogicalRect mBounds;
364 nsOverflowAreas mOverflowAreas;
366 // From reflow-state
367 mozilla::LogicalMargin mMargin;
368 mozilla::LogicalMargin mBorderPadding;
369 mozilla::LogicalMargin mOffsets;
371 // state for text justification
372 int32_t mJustificationNumSpaces;
373 int32_t mJustificationNumLetters;
375 // Other state we use
376 uint8_t mBlockDirAlign;
378 // PerFrameData flags
379 #define PFD_RELATIVEPOS 0x00000001
380 #define PFD_ISTEXTFRAME 0x00000002
381 #define PFD_ISNONEMPTYTEXTFRAME 0x00000004
382 #define PFD_ISNONWHITESPACETEXTFRAME 0x00000008
383 #define PFD_ISLETTERFRAME 0x00000010
384 #define PFD_RECOMPUTEOVERFLOW 0x00000020
385 #define PFD_ISBULLET 0x00000040
386 #define PFD_SKIPWHENTRIMMINGWHITESPACE 0x00000080
387 #define PFD_LASTFLAG PFD_SKIPWHENTRIMMINGWHITESPACE
389 uint8_t mFlags;
391 void SetFlag(uint32_t aFlag, bool aValue)
393 NS_ASSERTION(aFlag<=PFD_LASTFLAG, "bad flag");
394 NS_ASSERTION(aFlag<=UINT8_MAX, "bad flag");
395 if (aValue) { // set flag
396 mFlags |= aFlag;
398 else { // unset flag
399 mFlags &= ~aFlag;
403 bool GetFlag(uint32_t aFlag) const
405 NS_ASSERTION(aFlag<=PFD_LASTFLAG, "bad flag");
406 return !!(mFlags & aFlag);
410 PerFrameData* Last() {
411 PerFrameData* pfd = this;
412 while (pfd->mNext) {
413 pfd = pfd->mNext;
415 return pfd;
418 PerFrameData* mFrameFreeList;
420 struct PerSpanData {
421 union {
422 PerSpanData* mParent;
423 PerSpanData* mNextFreeSpan;
425 PerFrameData* mFrame;
426 PerFrameData* mFirstFrame;
427 PerFrameData* mLastFrame;
429 const nsHTMLReflowState* mReflowState;
430 bool mNoWrap;
431 mozilla::WritingMode mWritingMode;
432 bool mZeroEffectiveSpanBox;
433 bool mContainsFloat;
434 bool mHasNonemptyContent;
436 nscoord mIStart;
437 nscoord mICoord;
438 nscoord mIEnd;
440 nscoord mBStartLeading, mBEndLeading;
441 nscoord mLogicalBSize;
442 nscoord mMinBCoord, mMaxBCoord;
443 nscoord* mBaseline;
445 void AppendFrame(PerFrameData* pfd) {
446 if (nullptr == mLastFrame) {
447 mFirstFrame = pfd;
449 else {
450 mLastFrame->mNext = pfd;
451 pfd->mPrev = mLastFrame;
453 mLastFrame = pfd;
456 PerSpanData* mSpanFreeList;
457 PerSpanData* mRootSpan;
458 PerSpanData* mCurrentSpan;
460 gfxBreakPriority mLastOptionalBreakPriority;
461 int32_t mLastOptionalBreakContentOffset;
462 int32_t mForceBreakContentOffset;
464 nscoord mMinLineBSize;
466 // The amount of text indent that we applied to this line, needed for
467 // max-element-size calculation.
468 nscoord mTextIndent;
470 // This state varies during the reflow of a line but is line
471 // "global" state not span "local" state.
472 int32_t mLineNumber;
473 int32_t mTextJustificationNumSpaces;
474 int32_t mTextJustificationNumLetters;
476 int32_t mTotalPlacedFrames;
478 nscoord mBStartEdge;
479 nscoord mMaxStartBoxBSize;
480 nscoord mMaxEndBoxBSize;
482 nscoord mInflationMinFontSize;
484 // Final computed line-bSize value after BlockDirAlignFrames for
485 // the block has been called.
486 nscoord mFinalLineBSize;
488 // Amount of trimmable whitespace width for the trailing text frame, if any
489 nscoord mTrimmableWidth;
491 nscoord mContainerWidth;
493 bool mFirstLetterStyleOK : 1;
494 bool mIsTopOfPage : 1;
495 bool mImpactedByFloats : 1;
496 bool mLastFloatWasLetterFrame : 1;
497 bool mLineIsEmpty : 1;
498 bool mLineEndsInBR : 1;
499 bool mNeedBackup : 1;
500 bool mInFirstLine : 1;
501 bool mGotLineBox : 1;
502 bool mInFirstLetter : 1;
503 bool mHasBullet : 1;
504 bool mDirtyNextLine : 1;
505 bool mLineAtStart : 1;
507 int32_t mSpanDepth;
508 #ifdef DEBUG
509 int32_t mSpansAllocated, mSpansFreed;
510 int32_t mFramesAllocated, mFramesFreed;
511 #endif
512 PLArenaPool mArena; // Per span and per frame data, 4 byte aligned
515 * Allocate a PerFrameData from the mArena pool. The allocation is infallible.
517 PerFrameData* NewPerFrameData(nsIFrame* aFrame);
520 * Allocate a PerSpanData from the mArena pool. The allocation is infallible.
522 PerSpanData* NewPerSpanData();
524 void FreeSpan(PerSpanData* psd);
526 bool InBlockContext() const {
527 return mSpanDepth == 0;
530 void PushFrame(nsIFrame* aFrame);
532 void ApplyStartMargin(PerFrameData* pfd,
533 nsHTMLReflowState& aReflowState);
535 bool CanPlaceFrame(PerFrameData* pfd,
536 bool aNotSafeToBreak,
537 bool aFrameCanContinueTextRun,
538 bool aCanRollBackBeforeFrame,
539 nsHTMLReflowMetrics& aMetrics,
540 nsReflowStatus& aStatus,
541 bool* aOptionalBreakAfterFits);
543 void PlaceFrame(PerFrameData* pfd,
544 nsHTMLReflowMetrics& aMetrics);
546 void BlockDirAlignFrames(PerSpanData* psd);
548 void PlaceStartEndFrames(PerSpanData* psd,
549 nscoord aDistanceFromStart,
550 nscoord aLineBSize);
552 void RelativePositionFrames(PerSpanData* psd, nsOverflowAreas& aOverflowAreas);
554 bool TrimTrailingWhiteSpaceIn(PerSpanData* psd, nscoord* aDeltaISize);
556 void ComputeJustificationWeights(PerSpanData* psd, int32_t* numSpaces, int32_t* numLetters);
558 struct FrameJustificationState {
559 int32_t mTotalNumSpaces;
560 int32_t mTotalNumLetters;
561 nscoord mTotalWidthForSpaces;
562 nscoord mTotalWidthForLetters;
563 int32_t mNumSpacesProcessed;
564 int32_t mNumLettersProcessed;
565 nscoord mWidthForSpacesProcessed;
566 nscoord mWidthForLettersProcessed;
569 // Apply justification. The return value is the amount by which the width of
570 // the span corresponding to aPSD got increased due to justification.
571 nscoord ApplyFrameJustification(PerSpanData* aPSD,
572 FrameJustificationState* aState);
575 #ifdef DEBUG
576 void DumpPerSpanData(PerSpanData* psd, int32_t aIndent);
577 #endif
580 #endif /* nsLineLayout_h___ */