gecko-dev/layout/generic/nsRubyTextContainerFrame.cpp
Susanna Bowen 779f8cef59 Bug 1030993 - Basic reflow implementation for ruby frame classes. r=dbaron
To account for spacing between bases or text boxes during reflow, the line
layout which manages the bases updates its inline direction coordinate based on
the preferred inline size for the corresponding text boxes. Next, the base is
reflowed at the correct inline coordinate. Each paired text box is then also
reflowed at the proper inline position determined by (1) the current position of
its corresponding base and (2) its own preferred width.

In computing intrinsic widths, accounting for spacing is less complicated. The
minimum intrinsic width is the width of the widest ruby column, and the
preferred intrinsic width is the sum of all the ruby column widths. Each ruby
column width is the maximum width of its base box and text boxes. These
individual widths are determined using GetPrefISize on the base and text boxes.

Ruby base container frames store a list of pointers to the ruby text container
frames in the segment they denote. This list of pointers is created in the ruby
frame reflow method before calling the reflow method for the ruby base
container. The list exists and is used only during reflow of the main ruby frame
and is cleared before returning from reflow.
2014-08-15 10:34:20 -07:00

180 lines
6.3 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code is subject to the terms of the Mozilla Public License
* version 2.0 (the "License"). You can obtain a copy of the License at
* http://mozilla.org/MPL/2.0/. */
/* rendering object for CSS "display: ruby-text-container" */
#include "nsRubyTextContainerFrame.h"
#include "nsPresContext.h"
#include "nsStyleContext.h"
#include "WritingModes.h"
#include "mozilla/UniquePtr.h"
//----------------------------------------------------------------------
// Frame class boilerplate
// =======================
NS_QUERYFRAME_HEAD(nsRubyTextContainerFrame)
NS_QUERYFRAME_ENTRY(nsRubyTextContainerFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsBlockFrame)
NS_IMPL_FRAMEARENA_HELPERS(nsRubyTextContainerFrame)
nsContainerFrame*
NS_NewRubyTextContainerFrame(nsIPresShell* aPresShell,
nsStyleContext* aContext)
{
return new (aPresShell) nsRubyTextContainerFrame(aContext);
}
//----------------------------------------------------------------------
// nsRubyTextContainerFrame Method Implementations
// ===============================================
nsIAtom*
nsRubyTextContainerFrame::GetType() const
{
return nsGkAtoms::rubyTextContainerFrame;
}
#ifdef DEBUG_FRAME_DUMP
nsresult
nsRubyTextContainerFrame::GetFrameName(nsAString& aResult) const
{
return MakeFrameName(NS_LITERAL_STRING("RubyTextContainer"), aResult);
}
#endif
void
nsRubyTextContainerFrame::BeginRTCLineLayout(nsPresContext* aPresContext,
const nsHTMLReflowState& aReflowState)
{
// Construct block reflow state and line layout
nscoord consumedBSize = GetConsumedBSize();
ClearLineCursor();
mISize = 0;
nsBlockReflowState state(aReflowState, aPresContext, this, true, true,
false, consumedBSize);
NS_ASSERTION(!mLines.empty(),
"There should be at least one line in the ruby text container");
line_iterator firstLine = begin_lines();
mLineLayout = mozilla::MakeUnique<nsLineLayout>(
state.mPresContext,
state.mReflowState.mFloatManager,
&state.mReflowState, &firstLine);
mLineLayout->Init(&state, state.mMinLineHeight, state.mLineNumber);
mozilla::WritingMode lineWM = aReflowState.mLineLayout->GetWritingMode();
mozilla::LogicalRect lineRect(state.mContentArea);
nscoord iStart = lineRect.IStart(lineWM);
nscoord availISize = lineRect.ISize(lineWM);
nscoord availBSize = NS_UNCONSTRAINEDSIZE;
mLineLayout->BeginLineReflow(iStart, state.mBCoord,
availISize, availBSize,
false,
false,
lineWM, state.mContainerWidth);
}
void
nsRubyTextContainerFrame::ReflowRubyTextFrame(
nsRubyTextFrame* rtFrame,
nsIFrame* rbFrame,
nscoord baseStart,
nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState)
{
nsReflowStatus frameReflowStatus;
nsHTMLReflowMetrics metrics(aReflowState, aDesiredSize.mFlags);
mozilla::WritingMode lineWM = mLineLayout->GetWritingMode();
mozilla::LogicalSize availSize(lineWM, aReflowState.AvailableWidth(),
aReflowState.AvailableHeight());
nsHTMLReflowState childReflowState(aPresContext, aReflowState, rtFrame, availSize);
// Determine the inline coordinate for the text frame by centering over
// the corresponding base frame
int baseWidth;
if (rbFrame) {
baseWidth = rbFrame->ISize();
// If this is the last ruby annotation, it gets paired with ALL remaining
// ruby bases
if (!rtFrame->GetNextSibling()) {
rbFrame = rbFrame->GetNextSibling();
while (rbFrame) {
baseWidth += rbFrame->ISize();
rbFrame = rbFrame->GetNextSibling();
}
}
} else {
baseWidth = 0;
}
int baseCenter = baseStart + baseWidth / 2;
// FIXME: Find a way to avoid using GetPrefISize here, potentially by moving
// the frame after it has reflowed.
nscoord ICoord = baseCenter - rtFrame->GetPrefISize(aReflowState.rendContext) / 2;
if (ICoord > mLineLayout->GetCurrentICoord()) {
mLineLayout->AdvanceICoord(ICoord - mLineLayout->GetCurrentICoord());
}
bool pushedFrame;
mLineLayout->ReflowFrame(rtFrame, frameReflowStatus,
&metrics, pushedFrame);
NS_ASSERTION(!pushedFrame, "Ruby line breaking is not yet implemented");
mISize += metrics.ISize(lineWM);
rtFrame->SetSize(nsSize(metrics.ISize(lineWM), metrics.BSize(lineWM)));
FinishReflowChild(rtFrame, aPresContext, metrics, &childReflowState, 0, 0,
NS_FRAME_NO_MOVE_FRAME | NS_FRAME_NO_MOVE_VIEW);
}
/* virtual */ void
nsRubyTextContainerFrame::Reflow(nsPresContext* aPresContext,
nsHTMLReflowMetrics& aDesiredSize,
const nsHTMLReflowState& aReflowState,
nsReflowStatus& aStatus)
{
DO_GLOBAL_REFLOW_COUNT("nsRubyTextContainerFrame");
DISPLAY_REFLOW(aPresContext, this, aReflowState, aDesiredSize, aStatus);
// All rt children have already been reflowed. All we need to do is clean up
// the line layout.
aStatus = NS_FRAME_COMPLETE;
mozilla::WritingMode lineWM = aReflowState.mLineLayout->GetWritingMode();
mozilla::WritingMode frameWM = aReflowState.GetWritingMode();
mozilla::LogicalMargin borderPadding =
aReflowState.ComputedLogicalBorderPadding();
aDesiredSize.ISize(lineWM) = mISize;
nsLayoutUtils::SetBSizeFromFontMetrics(this, aDesiredSize, aReflowState,
borderPadding, lineWM, frameWM);
nscoord bsize = aDesiredSize.BSize(lineWM);
if (!mLines.empty()) {
// Okay to use BlockStartAscent because it has just been correctly set by
// nsLayoutUtils::SetBSizeFromFontMetrics.
mLines.begin()->SetLogicalAscent(aDesiredSize.BlockStartAscent());
mLines.begin()->SetBounds(aReflowState.GetWritingMode(), 0, 0, mISize,
bsize, mISize);
}
if (mLineLayout) {
mLineLayout->EndLineReflow();
mLineLayout = nullptr;
}
}