This commit is contained in:
kipp%netscape.com 1998-09-23 02:19:05 +00:00
parent 31dd9c5d3a
commit 230b467895
4 changed files with 1159 additions and 0 deletions

View File

@ -0,0 +1,71 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#include "nsFrameReflowState.h"
#include "nsIStyleContext.h"
#include "nsStyleConsts.h"
nsFrameReflowState::nsFrameReflowState(nsIPresContext& aPresContext,
const nsReflowState& aReflowState,
const nsReflowMetrics& aMetrics)
: nsReflowState(aReflowState),
mPresContext(aPresContext)
{
// While we skip around the reflow state that our parent gave us so
// that the parentReflowState is linked properly, we don't want to
// skip over it's reason.
reason = aReflowState.reason;
mNextRCFrame = nsnull;
// Initialize max-element-size
mComputeMaxElementSize = nsnull != aMetrics.maxElementSize;
mMaxElementSize.width = 0;
mMaxElementSize.height = 0;
// Get style data that we need
frame->GetStyleData(eStyleStruct_Text,
(const nsStyleStruct*&) mStyleText);
frame->GetStyleData(eStyleStruct_Display,
(const nsStyleStruct*&) mStyleDisplay);
frame->GetStyleData(eStyleStruct_Spacing,
(const nsStyleStruct*&) mStyleSpacing);
// Calculate our border and padding value
mStyleSpacing->CalcBorderPaddingFor(frame, mBorderPadding);
// Set mNoWrap flag
switch (mStyleText->mWhiteSpace) {
case NS_STYLE_WHITESPACE_PRE:
case NS_STYLE_WHITESPACE_NOWRAP:
mNoWrap = PR_TRUE;
break;
default:
mNoWrap = PR_FALSE;
break;
}
// Set mDirection value
mDirection = mStyleDisplay->mDirection;
// Initialize running margin value
mRunningMargin = aMetrics.mCarriedInTopMargin;
}
nsFrameReflowState::~nsFrameReflowState()
{
}

View File

@ -0,0 +1,82 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#ifndef nsFrameReflowState_h___
#define nsFrameReflowState_h___
#include "nsIFrame.h"
struct nsStyleDisplay;
struct nsStyleSpacing;
struct nsStyleText;
class nsFrameReflowState : public nsReflowState {
public:
nsFrameReflowState(nsIPresContext& aPresContext,
const nsReflowState& aReflowState,
const nsReflowMetrics& aMetrics);
~nsFrameReflowState();
nsIPresContext& mPresContext;
nsIFrame* mNextRCFrame;
PRBool mComputeMaxElementSize;
nsSize mMaxElementSize;
// Style data pointers for frame
const nsStyleText* mStyleText;
const nsStyleDisplay* mStyleDisplay;
const nsStyleSpacing* mStyleSpacing;
// This is true if no-wrap is set for frame
PRBool mNoWrap;
// This direction of reflow (left to right, right to left)
PRUint8 mDirection;
// The area to reflow within that includes the frames border and
// padding. Because it contains everything, the x/y values are known
// to be zero. The width/height may be NS_UNCONSTRAINEDSIZE if the
// container reflowing this frame has given the frame an
// unconstrained area.
//
// Note: This is not initialized by the constructor; derived classes
// must initialize this field.
nsSize mBorderArea;
// The content area to reflow child frames within. The x/y
// coordinates are known to be mBorderPadding.left and
// mBorderPadding.top. The width/height may be NS_UNCONSTRAINEDSIZE
// if the container reflowing this frame has given the frame an
// unconstrained area.
//
// Note: This is not initialized by the constructor; derived classes
// must initialize this field.
nsSize mContentArea;
// The frames border+padding value
nsMargin mBorderPadding;
// The running margin value. This value is a copy of
// nsReflowMetrics.mCarriedInTopMargin before the first frame is
// reflowed, and after a frame is reflowed and placed it will be the
// collapsed bottom margin value from the frame.
nscoord mRunningMargin;
};
#endif /* nsFrameReflowState_h___ */

View File

@ -0,0 +1,830 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#include "nsInlineReflow.h"
#include "nsLineLayout.h"
#include "nsIFontMetrics.h"
#include "nsIPresContext.h"
#include "nsIRunaround.h"
#include "nsISpaceManager.h"
#include "nsIStyleContext.h"
#include "nsCSSLayout.h"
#include "nsFrameReflowState.h"
#include "nsHTMLContainerFrame.h"
#include "nsHTMLIIDs.h"
#include "nsStyleConsts.h"
// XXX We could support "arbitrary" negative margins if we detected
// frames falling outside the parent frame and wrap them in a view
// when it happens.
// XXX move the nsCSSLayout alignment code here? Will body frame be
// using it?
nsInlineReflow::nsInlineReflow(nsLineLayout& aLineLayout,
nsFrameReflowState& aOuterReflowState,
nsHTMLContainerFrame* aOuterFrame)
: mLineLayout(aLineLayout),
mPresContext(aOuterReflowState.mPresContext),
mOuterReflowState(aOuterReflowState),
mComputeMaxElementSize(aOuterReflowState.mComputeMaxElementSize)
{
mSpaceManager = aLineLayout.mSpaceManager;
NS_ASSERTION(nsnull != mSpaceManager, "caller must have space manager");
mOuterFrame = aOuterFrame;
mFrameData = mFrameDataBuf;
mNumFrameData = sizeof(mFrameDataBuf) / sizeof(mFrameDataBuf[0]);
}
nsInlineReflow::~nsInlineReflow()
{
}
void
nsInlineReflow::Init(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight)
{
mLeftEdge = aX;
mX = aX;
if (NS_UNCONSTRAINEDSIZE == aWidth) {
mRightEdge = NS_UNCONSTRAINEDSIZE;
}
else {
mRightEdge = aX + aWidth;
}
mTopEdge = aY;
mY = aY;
if (NS_UNCONSTRAINEDSIZE == aHeight) {
mBottomEdge = NS_UNCONSTRAINEDSIZE;
}
else {
mBottomEdge = aY + aHeight;
}
mIsBlock = PR_FALSE;
mIsFirstChild = PR_FALSE;
mMaxAscent = 0;
mMaxDescent = 0;
mFirstFrame = nsnull;
mFrameNum = 0;
mMaxElementSize.width = 0;
mMaxElementSize.height = 0;
}
void
nsInlineReflow::UpdateBand(nscoord aX, nscoord aY,
nscoord aWidth, nscoord aHeight)
{
NS_PRECONDITION(mX == mLeftEdge, "update-band called after place-frame");
mLeftEdge = aX;
mX = aX;
if (NS_UNCONSTRAINEDSIZE == aWidth) {
mRightEdge = NS_UNCONSTRAINEDSIZE;
}
else {
mRightEdge = aX + aWidth;
}
mTopEdge = aY;
mY = aY;
if (NS_UNCONSTRAINEDSIZE == aHeight) {
mBottomEdge = NS_UNCONSTRAINEDSIZE;
}
else {
mBottomEdge = aY + aHeight;
}
}
const nsStyleDisplay*
nsInlineReflow::GetDisplay()
{
if (nsnull != mDisplay) {
return mDisplay;
}
mFrame->GetStyleData(eStyleStruct_Display,
(const nsStyleStruct*&)mDisplay);
return mDisplay;
}
const nsStylePosition*
nsInlineReflow::GetPosition()
{
if (nsnull != mPosition) {
return mPosition;
}
mFrame->GetStyleData(eStyleStruct_Position,
(const nsStyleStruct*&)mPosition);
return mPosition;
}
const nsStyleSpacing*
nsInlineReflow::GetSpacing()
{
if (nsnull != mSpacing) {
return mSpacing;
}
mFrame->GetStyleData(eStyleStruct_Spacing,
(const nsStyleStruct*&)mSpacing);
return mSpacing;
}
PRBool
nsInlineReflow::TreatFrameAsBlockFrame()
{
const nsStyleDisplay* display = GetDisplay();
const nsStylePosition* position = GetPosition();
if (NS_STYLE_POSITION_ABSOLUTE == position->mPosition) {
return PR_FALSE;
}
if (NS_STYLE_FLOAT_NONE != display->mFloats) {
return PR_FALSE;
}
switch (display->mDisplay) {
case NS_STYLE_DISPLAY_BLOCK:
case NS_STYLE_DISPLAY_LIST_ITEM:
case NS_STYLE_DISPLAY_COMPACT:
case NS_STYLE_DISPLAY_RUN_IN:
case NS_STYLE_DISPLAY_TABLE:
return PR_TRUE;
default:
break;
}
return PR_FALSE;
}
void
nsInlineReflow::SetFrame(nsIFrame* aFrame)
{
if (nsnull == mFirstFrame) {
mFirstFrame = aFrame;
}
// We can break before the frame if we placed at least one frame on
// the line.
mCanBreakBeforeFrame = mLineLayout.GetPlacedFrames() > 0;
mFrame = aFrame;
mDisplay = nsnull;
mSpacing = nsnull;
mPosition = nsnull;
mTreatFrameAsBlock = TreatFrameAsBlockFrame();
mIsInlineAware = PR_FALSE;
}
nsInlineReflowStatus
nsInlineReflow::ReflowFrame(nsIFrame* aFrame)
{
nsReflowStatus result;
nsSize innerMaxElementSize;
nsReflowMetrics metrics(mComputeMaxElementSize
? &innerMaxElementSize
: nsnull);
// Prepare for reflowing the frame
SetFrame(aFrame);
// Do a quick check and see if we are trying to place a block on a
// line that already has a placed frame on it.
PRInt32 placedFrames = mLineLayout.GetPlacedFrames();
if (mTreatFrameAsBlock && (placedFrames > 0)) {
return NS_INLINE_LINE_BREAK_BEFORE();
}
// Next figure out how much available space there is for the frame.
// Calculate raw margin values.
CalculateMargins();
// Apply top+left margins (as appropriate) to the frame computing
// the new starting x,y coordinates for the frame.
ApplyTopLeftMargins();
// Compute the available area to reflow the frame into.
if (!ComputeAvailableSize()) {
return NS_INLINE_LINE_BREAK_BEFORE();
}
// Reflow the frame. If the frame must be placed somewhere else
// then we return immediately.
nsRect bounds;
if (ReflowFrame(metrics, bounds, result)) {
// See if we can place the frame. If we can't fit it, then we
// return now.
if (CanPlaceFrame(metrics, bounds, result)) {
// Place the frame, updating aBounds with the final size and
// location. Then apply the bottom+right margins (as
// appropriate) to the frame.
PlaceFrame(metrics, bounds);
}
}
return result;
}
void
nsInlineReflow::CalculateMargins()
{
const nsStyleSpacing* spacing = GetSpacing();
// Get the margins from the style system
spacing->CalcMarginFor(mFrame, mMargin);
if (mTreatFrameAsBlock) {
// Get font height if we will be doing an auto margin. We use the
// default font height for the auto margin value.
nsStyleUnit topUnit = spacing->mMargin.GetTopUnit();
nsStyleUnit bottomUnit = spacing->mMargin.GetBottomUnit();
nscoord fontHeight = 0;
if ((eStyleUnit_Auto == topUnit) || (eStyleUnit_Auto == bottomUnit)) {
const nsFont& defaultFont = mPresContext.GetDefaultFont();
nsIFontMetrics* fm = mPresContext.GetMetricsFor(defaultFont);
fm->GetHeight(fontHeight);
NS_RELEASE(fm);
}
// For auto margins use the font height computed above
if (eStyleUnit_Auto == topUnit) {
mMargin.top = fontHeight;
}
if (eStyleUnit_Auto == bottomUnit) {
mMargin.bottom = fontHeight;
}
}
}
void
nsInlineReflow::ApplyTopLeftMargins()
{
mFrameX = mX;
mFrameY = mY;
// Compute left margin
nscoord leftMargin = 0;
const nsStyleDisplay* display = GetDisplay();
switch (display->mFloats) {
default:
NS_NOTYETIMPLEMENTED("Unsupported floater type");
// FALL THROUGH
case NS_STYLE_FLOAT_LEFT:
case NS_STYLE_FLOAT_RIGHT:
// When something is floated, its margins are applied there
// not here.
break;
case NS_STYLE_FLOAT_NONE:
leftMargin = mMargin.left;
break;
}
mFrameX += leftMargin;
// Apply top margin only if the child frame is to be treated as a
// block frame. Compute collapsed value to apply.
if (mTreatFrameAsBlock) {
// Collapse childs margin with previous running margin value
nscoord inTopMargin = mOuterReflowState.mRunningMargin;
nscoord deltaMargin = mMargin.top - inTopMargin;
if (deltaMargin < 0) {
deltaMargin = 0;
}
mDeltaTopMargin = deltaMargin;
mTotalTopMargin = mMargin.top + deltaMargin;
// Apply change in margin to this frame's Y coordinate
mFrameY += deltaMargin;
}
else {
mTotalTopMargin = 0;
mDeltaTopMargin = 0;
}
}
PRBool
nsInlineReflow::ComputeAvailableSize()
{
// Compute the available size from the outer's perspective
if (NS_UNCONSTRAINEDSIZE == mRightEdge) {
mFrameAvailSize.width = NS_UNCONSTRAINEDSIZE;
}
else {
mFrameAvailSize.width = mRightEdge - mFrameX - mMargin.right;
}
if (NS_UNCONSTRAINEDSIZE == mBottomEdge) {
mFrameAvailSize.height = NS_UNCONSTRAINEDSIZE;
}
else {
mFrameAvailSize.height = mBottomEdge - mFrameY - mMargin.bottom;
}
// Give up now if there is no chance. Note that we allow a reflow if
// the available space is zero because that way things that end up
// zero sized won't trigger a new line to be created. We also allow
// a reflow if we can't break before this frame.
if (mCanBreakBeforeFrame &&
((mFrameAvailSize.width < 0) || (mFrameAvailSize.height < 0))) {
return PR_FALSE;
}
return PR_TRUE;
}
/**
* Reflow the frame, choosing the appropriate reflow method.
*/
PRBool
nsInlineReflow::ReflowFrame(nsReflowMetrics& aMetrics,
nsRect& aBounds,
nsInlineReflowStatus& aStatus)
{
// Get reflow reason set correctly. It's possible that a child was
// created and then it was decided that it could not be reflowed
// (for example, a block frame that isn't at the start of a
// line). In this case the reason will be wrong so we need to check
// the frame state.
nsReflowReason reason = eReflowReason_Resize;
nsFrameState state;
mFrame->GetFrameState(state);
if (NS_FRAME_FIRST_REFLOW & state) {
reason = eReflowReason_Initial;
}
else if (mOuterReflowState.mNextRCFrame == mFrame) {
reason = eReflowReason_Incremental;
// Make sure we only incrementally reflow once
mOuterReflowState.mNextRCFrame = nsnull;
}
// Setup reflow state for reflowing the frame
nsReflowState reflowState(mFrame, mOuterReflowState, mFrameAvailSize);
reflowState.reason = reason;
// Let frame know that are reflowing it
nscoord x = mFrameX;
nscoord y = mFrameY;
mFrame->WillReflow(mPresContext);
mFrame->MoveTo(x, y);
// There are 3 ways to reflow the child frame: using the
// nsIRunaround interface, using the nsIInlineReflow interface or
// using the default Reflow method in nsIFrame. The order of
// precedence is nsIRunaround, nsIInlineReflow, nsIFrame. For all
// three API's we map the reflow status into an
// nsInlineReflowStatus.
nsIRunaround* runAround;
nsIInlineReflow* inlineReflow;
aBounds.x = x;
aBounds.y = y;
mSpaceManager->Translate(x, y);
if ((nsnull != mSpaceManager) &&
(NS_OK == mFrame->QueryInterface(kIRunaroundIID, (void**)&runAround))) {
nsRect r;
runAround->ReflowAround(mPresContext, mSpaceManager,
aMetrics, reflowState, r, aStatus);
aBounds.width = r.width;
aBounds.height = r.height;
aMetrics.width = r.width;
aMetrics.height = r.height;
aMetrics.ascent = r.height;
aMetrics.descent = 0;
}
else if (NS_OK == mFrame->QueryInterface(kIInlineReflowIID,
(void**)&inlineReflow)) {
aStatus = inlineReflow->InlineReflow(mLineLayout, aMetrics, reflowState);
mIsInlineAware = PR_TRUE;
aBounds.width = aMetrics.width;
aBounds.height = aMetrics.height;
}
else {
mFrame->Reflow(mPresContext, aMetrics, reflowState, aStatus);
aBounds.width = aMetrics.width;
aBounds.height = aMetrics.height;
}
mSpaceManager->Translate(-x, -y);
// Now that frame has been reflowed at least one time make sure that
// the NS_FRAME_FIRST_REFLOW bit is cleared so that never give it an
// initial reflow reason again.
if (eReflowReason_Initial == reason) {
mFrame->GetFrameState(state);
mFrame->SetFrameState(state & ~NS_FRAME_FIRST_REFLOW);
}
// If frame is complete and has a next-in-flow, we need to delete
// them now. Do not do this when a break-before is signaled because
// the frame is going to get reflowed again (and may end up wanting
// a next-in-flow where it ends up).
if (!NS_INLINE_IS_BREAK_BEFORE(aStatus) &&
NS_FRAME_IS_COMPLETE(aStatus)) {
nsIFrame* kidNextInFlow;
mFrame->GetNextInFlow(kidNextInFlow);
if (nsnull != kidNextInFlow) {
// Remove all of the childs next-in-flows. Make sure that we ask
// the right parent to do the removal (it's possible that the
// parent is not this because we are executing pullup code)
nsHTMLContainerFrame* parent;
mFrame->GetGeometricParent((nsIFrame*&) parent);
parent->DeleteChildsNextInFlow(mPresContext, mFrame);
}
}
NS_FRAME_LOG(NS_FRAME_TRACE_CHILD_REFLOW,
("nsInlineReflow::ReflowFrame: frame=%p reflowStatus=%x %saware",
mFrame, aStatus, mIsInlineAware ? "" :"not "));
return !NS_INLINE_IS_BREAK_BEFORE(aStatus);
}
/**
* See if the frame can be placed now that we know it's desired size.
* We can always place the frame if the line is empty. Note that we
* know that the reflow-status is not a break-before because if it was
* ReflowFrame above would have returned false, preventing this method
* from being called. The logic in this method assumes that.
*
* Note that there is no check against the Y coordinate because we
* assume that the caller will take care of that.
*/
PRBool
nsInlineReflow::CanPlaceFrame(nsReflowMetrics& aMetrics,
nsRect& aBounds,
nsInlineReflowStatus& aStatus)
{
// Compute right margin to use
mRightMargin = 0;
if (0 != aBounds.width) {
const nsStyleDisplay* display = GetDisplay();
switch (display->mFloats) {
default:
NS_NOTYETIMPLEMENTED("Unsupported floater type");
// FALL THROUGH
case NS_STYLE_FLOAT_LEFT:
case NS_STYLE_FLOAT_RIGHT:
// When something is floated, its margins are applied there
// not here.
break;
case NS_STYLE_FLOAT_NONE:
mRightMargin = mMargin.right;
break;
}
}
// If this is the first frame going into this inline reflow or it's
// the first placed frame in the line or wrapping is disabled then
// the frame fits regardless of who much room there is. This
// guarantees that we always make progress regardless of the
// limitations of the reflow area. If the container reflowing this
// frame ends up too big then the container may be pushable to a new
// location.
if ((0 == mFrameNum) ||
(0 == mLineLayout.GetPlacedFrames()) ||
mOuterReflowState.mNoWrap) {
return PR_TRUE;
}
// See if the frame fits. If it doesn't then we fabricate up a
// break-before status.
if (aBounds.XMost() + mRightMargin > mRightEdge) {
// Retain the completion status of the frame that was reflowed in
// case the caller cares.
aStatus = NS_INLINE_LINE_BREAK_BEFORE();
return PR_FALSE;
}
return PR_TRUE;
}
/**
* Place the frame. Update running counters.
*/
void
nsInlineReflow::PlaceFrame(nsReflowMetrics& aMetrics, nsRect& aBounds)
{
// Remember this for later...
if (mTreatFrameAsBlock) {
mIsBlock = PR_TRUE;
}
// If frame is zero width then do not apply its left and right margins.
PRBool emptyFrame = PR_FALSE;
if ((0 == aBounds.width) || (0 == aBounds.height)) {
aBounds.x = mX;
aBounds.y = 0;
emptyFrame = PR_TRUE;
}
mFrame->SetRect(aBounds);
// Record ascent and update max-ascent and max-descent values
SetFrameData(aMetrics);
mFrameNum++;
if (aMetrics.ascent > mMaxAscent) {
mMaxAscent = aMetrics.ascent;
}
if (aMetrics.descent > mMaxDescent) {
mMaxDescent = aMetrics.descent;
}
// Advance to next X coordinate
mX = aBounds.XMost() + mRightMargin;
// If the frame is a not inline aware and it takes up some area
// disable leading white-space compression for the next frame to
// be reflowed.
if (!mIsInlineAware && !emptyFrame) {
mLineLayout.SetSkipLeadingWhiteSpace(PR_FALSE);
}
// Compute the bottom margin to apply. Note that the margin only
// applies if the frame ends up with a non-zero height.
mBottomMargin = 0;
mInnerBottomMargin = aMetrics.mCarriedOutBottomMargin;
if (!emptyFrame) {
// Compute the effective amount of bottom margin by collapsing
// it with the inner margin returned from reflowing the frame.
nscoord inBottomMargin = aMetrics.mCarriedOutBottomMargin;
nscoord deltaMargin = mMargin.bottom - inBottomMargin;
if (deltaMargin < 0) {
deltaMargin = 0;
}
mBottomMargin = deltaMargin;/* XXX rename mBottomMargin? */
mInnerBottomMargin = inBottomMargin;/* XXX rename? */
mOuterReflowState.mRunningMargin = inBottomMargin + deltaMargin;
// Inform line layout that we have placed a non-empty frame
#ifdef NS_DEBUG
mLineLayout.AddPlacedFrame(mFrame);
#else
mLineLayout.AddPlacedFrame();
#endif
// Update max-element-size
if (mComputeMaxElementSize) {
nscoord mw = aMetrics.maxElementSize->width;
if (mw > mMaxElementSize.width) {
mMaxElementSize.width = mw;
}
nscoord mh = aMetrics.maxElementSize->height;
if (mh > mMaxElementSize.height) {
mMaxElementSize.height = mh;
}
}
}
}
/**
* Store away the ascent value associated with the current frame
*/
nsresult
nsInlineReflow::SetFrameData(const nsReflowMetrics& aMetrics)
{
PRInt32 frameNum = mFrameNum;
if (frameNum == mNumFrameData) {
mNumFrameData *= 2;
PerFrameData* newData = new PerFrameData[mNumFrameData];
if (nsnull == newData) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsCRT::memcpy(newData, mFrameData, sizeof(PerFrameData) * frameNum);
if (mFrameData != mFrameDataBuf) {
delete [] mFrameData;
}
mFrameData = newData;
}
PerFrameData* pfd = &mFrameData[frameNum];
pfd->mAscent = aMetrics.ascent;
pfd->mDescent = aMetrics.descent;
pfd->mMargin = mMargin;
return NS_OK;
}
// XXX what about ebina's center vs. ncsa-center?
void
nsInlineReflow::VerticalAlignFrames(nsRect& aLineBox)
{
nscoord x = mLeftEdge;
nscoord y0 = mTopEdge + mDeltaTopMargin;
nscoord width = mX - mLeftEdge;
nscoord height = mMaxAscent + mMaxDescent;
if ((mFrameNum > 1) && !mIsBlock) {
// Only when we have more than one frame should we do vertical
// alignment. Sometimes we will have 2 frames with the second one
// being a block; we don't vertically align then either. This
// happens when the first frame is nothing but compressed
// whitespace.
const nsStyleFont* font;
mOuterFrame->GetStyleData(eStyleStruct_Font,
(const nsStyleStruct*&)font);
// Determine minimum and maximum y values for the line and
// perform alignment of all children except those requesting bottom
// alignment. The second pass will align bottom children (if any)
nsIFontMetrics* fm = mPresContext.GetMetricsFor(font->mFont);
nsIFrame* kid = mFirstFrame;
nsRect kidRect;
nscoord minY = y0;
nscoord maxY = y0;
PRIntn pass2Kids = 0;
PRIntn kidCount = mFrameNum;
PerFrameData* pfd = mFrameData;
for (; --kidCount >= 0; pfd++) {
nscoord kidAscent = pfd->mAscent;
const nsStyleText* textStyle;
kid->GetStyleData(eStyleStruct_Text, (const nsStyleStruct*&)textStyle);
nsStyleUnit verticalAlignUnit = textStyle->mVerticalAlign.GetUnit();
PRUint8 verticalAlignEnum = NS_STYLE_VERTICAL_ALIGN_BASELINE;
kid->GetRect(kidRect);
// Vertically align the child
nscoord kidYTop = 0;
PRBool isPass2Kid = PR_FALSE;
nscoord fontParam;
switch (verticalAlignUnit) {
case eStyleUnit_Coord:
// According to the spec, a positive value "raises" the box by
// the given distance while a negative value "lowers" the box
// by the given distance. Since Y coordinates increase towards
// the bottom of the screen we reverse the sign. All of the
// raising and lowering is done relative to the baseline, so
// we start our adjustments there.
kidYTop = mMaxAscent - kidAscent; // get baseline first
kidYTop -= textStyle->mVerticalAlign.GetCoordValue();
break;
case eStyleUnit_Percent:
pass2Kids++;
isPass2Kid = PR_TRUE;
break;
case eStyleUnit_Enumerated:
verticalAlignEnum = textStyle->mVerticalAlign.GetIntValue();
switch (verticalAlignEnum) {
default:
case NS_STYLE_VERTICAL_ALIGN_BASELINE:
// Align the kid's baseline at the max baseline
kidYTop = mMaxAscent - kidAscent;
break;
case NS_STYLE_VERTICAL_ALIGN_TOP:
// Align the top of the kid with the top of the line box
break;
case NS_STYLE_VERTICAL_ALIGN_SUB:
// Align the child's baseline on the superscript baseline
fm->GetSubscriptOffset(fontParam);
kidYTop = mMaxAscent + fontParam - kidAscent;
break;
case NS_STYLE_VERTICAL_ALIGN_SUPER:
// Align the child's baseline on the subscript baseline
fm->GetSuperscriptOffset(fontParam);
kidYTop = mMaxAscent - fontParam - kidAscent;
break;
case NS_STYLE_VERTICAL_ALIGN_BOTTOM:
pass2Kids++;
isPass2Kid = PR_TRUE;
break;
case NS_STYLE_VERTICAL_ALIGN_MIDDLE:
// Align the midpoint of the box with 1/2 the parent's x-height
fm->GetXHeight(fontParam);
kidYTop = mMaxAscent - (fontParam / 2) - (kidRect.height/2);
break;
case NS_STYLE_VERTICAL_ALIGN_TEXT_BOTTOM:
fm->GetMaxDescent(fontParam);
kidYTop = mMaxAscent + fontParam - kidRect.height;
break;
case NS_STYLE_VERTICAL_ALIGN_TEXT_TOP:
fm->GetMaxAscent(fontParam);
kidYTop = mMaxAscent - fontParam;
break;
}
break;
default:
// Align the kid's baseline at the max baseline
kidYTop = mMaxAscent - kidAscent;
break;
}
/* XXX or grow the box - which is it? */
if (kidYTop < 0) {
kidYTop = 0;
}
// Place kid and update min and max Y values
if (!isPass2Kid) {
nscoord y = y0 + kidYTop;
if (y < minY) minY = y;
kid->MoveTo(kidRect.x, y);
y += kidRect.height;
if (y > maxY) maxY = y;
}
else {
nscoord y = y0 + kidRect.height;
if (y > maxY) maxY = y;
}
kid->GetNextSibling(kid);
}
// Now compute the final line-height
nscoord lineHeight = maxY - minY;
if (0 != pass2Kids) {
// Position all of the bottom aligned children
kidCount = mFrameNum;
kid = mFirstFrame;
pfd = mFrameData;
for (; --kidCount >= 0; pfd++) {
nscoord kidAscent = pfd->mAscent;
// Get kid's vertical align style data
const nsStyleText* textStyle;
kid->GetStyleData(eStyleStruct_Text, (const nsStyleStruct*&)textStyle);
nsStyleUnit verticalAlignUnit = textStyle->mVerticalAlign.GetUnit();
if (eStyleUnit_Percent == verticalAlignUnit) {
// According to the spec, a positive value "raises" the box by
// the given distance while a negative value "lowers" the box
// by the given distance. Since Y coordinates increase towards
// the bottom of the screen we reverse the sign. All of the
// raising and lowering is done relative to the baseline, so
// we start our adjustments there.
nscoord kidYTop = mMaxAscent - kidAscent; // get baseline first
kidYTop -=
nscoord(textStyle->mVerticalAlign.GetPercentValue() * lineHeight);
kid->GetRect(kidRect);
kid->MoveTo(kidRect.x, y0 + kidYTop);
if (--pass2Kids == 0) {
// Stop on last pass2 kid
break;
}
}
else if (verticalAlignUnit == eStyleUnit_Enumerated) {
PRUint8 verticalAlignEnum = textStyle->mVerticalAlign.GetIntValue();
// Vertically align the child
if (NS_STYLE_VERTICAL_ALIGN_BOTTOM == verticalAlignEnum) {
// Place kid along the bottom
kid->GetRect(kidRect);
kid->MoveTo(kidRect.x, y0 + lineHeight - kidRect.height);
if (--pass2Kids == 0) {
// Stop on last pass2 kid
break;
}
}
}
kid->GetNextSibling(kid);
}
}
NS_RELEASE(fm);
}
aLineBox.x = x;
aLineBox.y = y0;
aLineBox.width = width;
aLineBox.height = height;
}
void
nsInlineReflow::HorizontalAlignFrames(const nsRect& aLineBox)
{
const nsStyleText* styleText = mOuterReflowState.mStyleText;
nsCSSLayout::HorizontallyPlaceChildren(&mPresContext,
mOuterFrame,
styleText->mTextAlign,
mOuterReflowState.mDirection,
mFirstFrame, mFrameNum,
aLineBox.width,
mRightEdge - mLeftEdge);
}
void
nsInlineReflow::RelativePositionFrames()
{
nsCSSLayout::RelativePositionChildren(&mPresContext,
mOuterFrame,
mFirstFrame, mFrameNum);
}

View File

@ -0,0 +1,176 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* The contents of this file are subject to the Netscape Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/NPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Mozilla Communicator client code.
*
* The Initial Developer of the Original Code is Netscape Communications
* Corporation. Portions created by Netscape are Copyright (C) 1998
* Netscape Communications Corporation. All Rights Reserved.
*/
#ifndef nsInlineReflow_h___
#define nsInlineReflow_h___
#include "nsIFrame.h"
#include "nsIInlineReflow.h"
class nsFrameReflowState;
class nsHTMLContainerFrame;
class nsIRunaround;
class nsLineLayout;
struct nsStyleDisplay;
struct nsStylePosition;
struct nsStyleSpacing;
/** A class that implements a css compatible horizontal reflow
* algorithm. Frames are stacked left to right (or right to left)
* until the horizontal space runs out. Then the 1 or more frames are
* vertically aligned, horizontally positioned and relatively
* positioned. */
class nsInlineReflow {
public:
nsInlineReflow(nsLineLayout& aLineLayout,
nsFrameReflowState& aOuterReflowState,
nsHTMLContainerFrame* aOuter);
~nsInlineReflow();
void Init(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
void SetIsFirstChild(PRBool aValue) {
mIsFirstChild = aValue;
}
void UpdateBand(nscoord aX, nscoord aY, nscoord aWidth, nscoord aHeight);
nsInlineReflowStatus ReflowFrame(nsIFrame* aFrame);
void VerticalAlignFrames(nsRect& aLineBox);
void HorizontalAlignFrames(const nsRect& aLineBox);
void RelativePositionFrames();
nscoord GetMaxAscent() const { return mMaxAscent; }
nscoord GetMaxDescent() const { return mMaxDescent; }
PRInt32 GetCurrentFrameNum() const { return mFrameNum; }
nscoord GetInnerBottomMargin() const { return mInnerBottomMargin; }
nscoord GetBottomMargin() const { return mBottomMargin; }
PRBool GetIsBlock() const { return mIsBlock; }
const nsSize& GetMaxElementSize() const { return mMaxElementSize; }
protected:
void SetFrame(nsIFrame* aFrame);
PRBool TreatFrameAsBlockFrame();
const nsStyleDisplay* GetDisplay();
const nsStylePosition* GetPosition();
const nsStyleSpacing* GetSpacing();
void CalculateMargins();
void ApplyTopLeftMargins();
PRBool ComputeAvailableSize();
PRBool ReflowFrame(nsReflowMetrics& aMetrics,
nsRect& aBounds,
nsInlineReflowStatus& aStatus);
PRBool CanPlaceFrame(nsReflowMetrics& aMetrics,
nsRect& aBounds,
nsInlineReflowStatus& aStatus);
void PlaceFrame(nsReflowMetrics& aMetrics, nsRect& aBounds);
nsresult SetFrameData(const nsReflowMetrics& aMetrics);
// The outer frame that contains the frames that we reflow.
nsHTMLContainerFrame* mOuterFrame;
nsISpaceManager* mSpaceManager;
nsLineLayout& mLineLayout;
nsFrameReflowState& mOuterReflowState;
nsIPresContext& mPresContext;
nsIFrame* mFirstFrame;
PRIntn mFrameNum;
struct PerFrameData {
nscoord mAscent; // computed ascent value
nscoord mDescent; // computed descent value
nsMargin mMargin; // computed margin value
};
PerFrameData* mFrameData;
PerFrameData mFrameDataBuf[20];
PRIntn mNumFrameData;
nscoord mMaxAscent;
nscoord mMaxDescent;
// Current frame state
nsIFrame* mFrame;
const nsStyleSpacing* mSpacing;
const nsStylePosition* mPosition;
const nsStyleDisplay* mDisplay;
PRBool mTreatFrameAsBlock; // treat the frame as a block frame
// Entire reflow should act like a block; this means that somewhere
// during reflow we encountered a block frame. There can be more one
// than one frame in the reflow group (because of empty frames), so
// this flag keeps us honest regarding the state of this reflow.
PRBool mIsBlock;
PRBool mCanBreakBeforeFrame; // we can break before the frame
PRBool mIsInlineAware;
PRBool mIsFirstChild;
PRBool mComputeMaxElementSize;
nsSize mMaxElementSize;
// The frame's computed margin values (includes auto value
// computation)
nsMargin mMargin;
// The computed delta top margin that has been applied for the
// frame. This is the collapsed margin value.
nscoord mDeltaTopMargin;
// The sum ofthe previous top margin plus additional margin
// contributed by the frame being reflowed.
nscoord mTotalTopMargin;
nscoord mRightMargin;/* XXX why? */
nscoord mInnerBottomMargin;
nscoord mBottomMargin;
// The computed available size and location for the frame
nscoord mFrameX, mFrameY;
nsSize mFrameAvailSize;
nscoord mLeftEdge;
nscoord mX;
nscoord mRightEdge;
nscoord mTopEdge;
nscoord mY;
nscoord mBottomEdge;
};
#endif /* nsInlineReflow_h___ */