Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / generic / nsRubyTextContainerFrame.cpp
blob6f657b6356bce1539b6aa011e13537fcfdadcae7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* rendering object for CSS "display: ruby-text-container" */
9 #include "nsRubyTextContainerFrame.h"
11 #include "mozilla/ComputedStyle.h"
12 #include "mozilla/PresShell.h"
13 #include "mozilla/UniquePtr.h"
14 #include "mozilla/WritingModes.h"
15 #include "nsLayoutUtils.h"
16 #include "nsLineLayout.h"
17 #include "nsPresContext.h"
19 using namespace mozilla;
21 //----------------------------------------------------------------------
23 // Frame class boilerplate
24 // =======================
26 NS_QUERYFRAME_HEAD(nsRubyTextContainerFrame)
27 NS_QUERYFRAME_ENTRY(nsRubyTextContainerFrame)
28 NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)
30 NS_IMPL_FRAMEARENA_HELPERS(nsRubyTextContainerFrame)
32 nsContainerFrame* NS_NewRubyTextContainerFrame(PresShell* aPresShell,
33 ComputedStyle* aStyle) {
34 return new (aPresShell)
35 nsRubyTextContainerFrame(aStyle, aPresShell->GetPresContext());
38 //----------------------------------------------------------------------
40 // nsRubyTextContainerFrame Method Implementations
41 // ===============================================
43 #ifdef DEBUG_FRAME_DUMP
44 nsresult nsRubyTextContainerFrame::GetFrameName(nsAString& aResult) const {
45 return MakeFrameName(u"RubyTextContainer"_ns, aResult);
47 #endif
49 /* virtual */
50 bool nsRubyTextContainerFrame::IsFrameOfType(uint32_t aFlags) const {
51 if (aFlags & (eSupportsCSSTransforms | eSupportsContainLayoutAndPaint |
52 eSupportsAspectRatio)) {
53 return false;
55 return nsContainerFrame::IsFrameOfType(aFlags);
58 /* virtual */
59 void nsRubyTextContainerFrame::SetInitialChildList(ChildListID aListID,
60 nsFrameList&& aChildList) {
61 nsContainerFrame::SetInitialChildList(aListID, std::move(aChildList));
62 if (aListID == FrameChildListID::Principal) {
63 UpdateSpanFlag();
67 /* virtual */
68 void nsRubyTextContainerFrame::AppendFrames(ChildListID aListID,
69 nsFrameList&& aFrameList) {
70 nsContainerFrame::AppendFrames(aListID, std::move(aFrameList));
71 UpdateSpanFlag();
74 /* virtual */
75 void nsRubyTextContainerFrame::InsertFrames(
76 ChildListID aListID, nsIFrame* aPrevFrame,
77 const nsLineList::iterator* aPrevFrameLine, nsFrameList&& aFrameList) {
78 nsContainerFrame::InsertFrames(aListID, aPrevFrame, aPrevFrameLine,
79 std::move(aFrameList));
80 UpdateSpanFlag();
83 /* virtual */
84 void nsRubyTextContainerFrame::RemoveFrame(DestroyContext& aContext,
85 ChildListID aListID,
86 nsIFrame* aOldFrame) {
87 nsContainerFrame::RemoveFrame(aContext, aListID, aOldFrame);
88 UpdateSpanFlag();
91 void nsRubyTextContainerFrame::UpdateSpanFlag() {
92 bool isSpan = false;
93 // The continuation checks are safe here because spans never break.
94 if (!GetPrevContinuation() && !GetNextContinuation()) {
95 nsIFrame* onlyChild = mFrames.OnlyChild();
96 if (onlyChild && onlyChild->IsPseudoFrame(GetContent())) {
97 // Per CSS Ruby spec, if the only child of an rtc frame is
98 // a pseudo rt frame, it spans all bases in the segment.
99 isSpan = true;
103 if (isSpan) {
104 AddStateBits(NS_RUBY_TEXT_CONTAINER_IS_SPAN);
105 } else {
106 RemoveStateBits(NS_RUBY_TEXT_CONTAINER_IS_SPAN);
110 /* virtual */
111 void nsRubyTextContainerFrame::Reflow(nsPresContext* aPresContext,
112 ReflowOutput& aDesiredSize,
113 const ReflowInput& aReflowInput,
114 nsReflowStatus& aStatus) {
115 MarkInReflow();
116 DO_GLOBAL_REFLOW_COUNT("nsRubyTextContainerFrame");
117 DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
118 MOZ_ASSERT(aStatus.IsEmpty(), "Caller should pass a fresh reflow status!");
120 // Although a ruby text container may have continuations, returning
121 // complete reflow status is still safe, since its parent, ruby frame,
122 // ignores the status, and continuations of the ruby base container
123 // will take care of our continuations.
124 WritingMode rtcWM = GetWritingMode();
126 nscoord minBCoord = nscoord_MAX;
127 nscoord maxBCoord = nscoord_MIN;
128 // The container size is not yet known, so we use a dummy (0, 0) size.
129 // The block-dir position will be corrected below after containerSize
130 // is finalized.
131 const nsSize dummyContainerSize;
132 for (nsIFrame* child : mFrames) {
133 MOZ_ASSERT(child->IsRubyTextFrame());
134 LogicalRect rect = child->GetLogicalRect(rtcWM, dummyContainerSize);
135 LogicalMargin margin = child->GetLogicalUsedMargin(rtcWM);
136 nscoord blockStart = rect.BStart(rtcWM) - margin.BStart(rtcWM);
137 minBCoord = std::min(minBCoord, blockStart);
138 nscoord blockEnd = rect.BEnd(rtcWM) + margin.BEnd(rtcWM);
139 maxBCoord = std::max(maxBCoord, blockEnd);
142 if (!mFrames.IsEmpty()) {
143 if (MOZ_UNLIKELY(minBCoord > maxBCoord)) {
144 // XXX When bug 765861 gets fixed, this warning should be upgraded.
145 NS_WARNING("bad block coord");
146 minBCoord = maxBCoord = 0;
148 LogicalSize size(rtcWM, mISize, maxBCoord - minBCoord);
149 nsSize containerSize = size.GetPhysicalSize(rtcWM);
150 for (nsIFrame* child : mFrames) {
151 // We reflowed the child with a dummy container size, as the true size
152 // was not yet known at that time.
153 LogicalPoint pos = child->GetLogicalPosition(rtcWM, dummyContainerSize);
154 // Adjust block position to account for minBCoord,
155 // then reposition child based on the true container width.
156 pos.B(rtcWM) -= minBCoord;
157 // Relative positioning hasn't happened yet.
158 // So MovePositionBy should not be used here.
159 child->SetPosition(rtcWM, pos, containerSize);
160 nsContainerFrame::PlaceFrameView(child);
162 aDesiredSize.SetSize(rtcWM, size);
163 } else {
164 // If this ruby text container is empty, size it as if there were
165 // an empty inline child inside.
166 // Border and padding are suppressed on ruby text container, so we
167 // create a dummy zero-sized borderPadding for setting BSize.
168 aDesiredSize.ISize(rtcWM) = mISize;
169 LogicalMargin borderPadding(rtcWM);
170 nsLayoutUtils::SetBSizeFromFontMetrics(this, aDesiredSize, borderPadding,
171 rtcWM, rtcWM);