2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2014-12-12 13:30:43 +03:00
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "RubyUtils.h"
|
2015-04-08 02:22:34 +03:00
|
|
|
#include "nsRubyFrame.h"
|
|
|
|
#include "nsRubyBaseFrame.h"
|
|
|
|
#include "nsRubyTextFrame.h"
|
2015-04-08 02:22:34 +03:00
|
|
|
#include "nsRubyBaseContainerFrame.h"
|
2015-04-08 02:22:34 +03:00
|
|
|
#include "nsRubyTextContainerFrame.h"
|
2014-12-12 13:30:43 +03:00
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2016-01-28 06:23:59 +03:00
|
|
|
NS_DECLARE_FRAME_PROPERTY_SMALL_VALUE(ReservedISize, nscoord)
|
2014-12-12 13:30:43 +03:00
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
void RubyUtils::SetReservedISize(nsIFrame* aFrame, nscoord aISize) {
|
2014-12-12 13:30:43 +03:00
|
|
|
MOZ_ASSERT(IsExpandableRubyBox(aFrame));
|
2017-05-27 14:36:00 +03:00
|
|
|
aFrame->SetProperty(ReservedISize(), aISize);
|
2014-12-12 13:30:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
void RubyUtils::ClearReservedISize(nsIFrame* aFrame) {
|
2014-12-12 13:30:43 +03:00
|
|
|
MOZ_ASSERT(IsExpandableRubyBox(aFrame));
|
2017-05-27 14:36:00 +03:00
|
|
|
aFrame->RemoveProperty(ReservedISize());
|
2014-12-12 13:30:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:24 +03:00
|
|
|
/* static */
|
|
|
|
nscoord RubyUtils::GetReservedISize(nsIFrame* aFrame) {
|
2014-12-12 13:30:43 +03:00
|
|
|
MOZ_ASSERT(IsExpandableRubyBox(aFrame));
|
2017-05-27 14:36:00 +03:00
|
|
|
return aFrame->GetProperty(ReservedISize());
|
2014-12-12 13:30:43 +03:00
|
|
|
}
|
2014-12-29 03:14:53 +03:00
|
|
|
|
2015-04-08 02:22:34 +03:00
|
|
|
AutoRubyTextContainerArray::AutoRubyTextContainerArray(
|
2014-12-29 03:14:53 +03:00
|
|
|
nsRubyBaseContainerFrame* aBaseContainer) {
|
2015-04-08 02:22:34 +03:00
|
|
|
for (nsIFrame* frame = aBaseContainer->GetNextSibling();
|
2017-04-30 18:30:08 +03:00
|
|
|
frame && frame->IsRubyTextContainerFrame();
|
2015-04-08 02:22:34 +03:00
|
|
|
frame = frame->GetNextSibling()) {
|
|
|
|
AppendElement(static_cast<nsRubyTextContainerFrame*>(frame));
|
2015-04-07 23:40:31 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-08 02:22:34 +03:00
|
|
|
|
2016-01-27 08:58:53 +03:00
|
|
|
nsIFrame* RubyColumn::Iterator::operator*() const {
|
|
|
|
nsIFrame* frame;
|
|
|
|
if (mIndex == -1) {
|
|
|
|
frame = mColumn.mBaseFrame;
|
|
|
|
} else {
|
|
|
|
frame = mColumn.mTextFrames[mIndex];
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(frame, "Frame here cannot be null");
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RubyColumn::Iterator::SkipUntilExistingFrame() {
|
|
|
|
if (mIndex == -1) {
|
2016-01-27 11:04:57 +03:00
|
|
|
if (mColumn.mBaseFrame) {
|
|
|
|
return;
|
2016-01-27 08:58:53 +03:00
|
|
|
}
|
2016-01-27 11:04:57 +03:00
|
|
|
++mIndex;
|
2016-01-27 08:58:53 +03:00
|
|
|
}
|
2016-01-27 09:15:49 +03:00
|
|
|
int32_t numTextFrames = mColumn.mTextFrames.Length();
|
2016-01-27 08:58:53 +03:00
|
|
|
for (; mIndex < numTextFrames; ++mIndex) {
|
|
|
|
if (mColumn.mTextFrames[mIndex]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:22:34 +03:00
|
|
|
RubySegmentEnumerator::RubySegmentEnumerator(nsRubyFrame* aRubyFrame) {
|
2016-01-29 17:42:14 +03:00
|
|
|
nsIFrame* frame = aRubyFrame->PrincipalChildList().FirstChild();
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!frame || frame->IsRubyBaseContainerFrame());
|
2015-04-08 02:22:34 +03:00
|
|
|
mBaseContainer = static_cast<nsRubyBaseContainerFrame*>(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RubySegmentEnumerator::Next() {
|
|
|
|
MOZ_ASSERT(mBaseContainer);
|
|
|
|
nsIFrame* frame = mBaseContainer->GetNextSibling();
|
2017-04-30 18:30:08 +03:00
|
|
|
while (frame && !frame->IsRubyBaseContainerFrame()) {
|
2015-04-08 02:22:34 +03:00
|
|
|
frame = frame->GetNextSibling();
|
|
|
|
}
|
|
|
|
mBaseContainer = static_cast<nsRubyBaseContainerFrame*>(frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
RubyColumnEnumerator::RubyColumnEnumerator(
|
|
|
|
nsRubyBaseContainerFrame* aBaseContainer,
|
2015-04-08 02:22:34 +03:00
|
|
|
const AutoRubyTextContainerArray& aTextContainers)
|
2015-04-08 02:22:34 +03:00
|
|
|
: mAtIntraLevelWhitespace(false) {
|
|
|
|
const uint32_t rtcCount = aTextContainers.Length();
|
|
|
|
mFrames.SetCapacity(rtcCount + 1);
|
|
|
|
|
2016-01-29 17:42:14 +03:00
|
|
|
nsIFrame* rbFrame = aBaseContainer->PrincipalChildList().FirstChild();
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!rbFrame || rbFrame->IsRubyBaseFrame());
|
2015-04-08 02:22:34 +03:00
|
|
|
mFrames.AppendElement(static_cast<nsRubyContentFrame*>(rbFrame));
|
|
|
|
for (uint32_t i = 0; i < rtcCount; i++) {
|
|
|
|
nsRubyTextContainerFrame* container = aTextContainers[i];
|
|
|
|
// If the container is for span, leave a nullptr here.
|
|
|
|
// Spans do not take part in pairing.
|
|
|
|
nsIFrame* rtFrame = !container->IsSpanContainer()
|
2016-01-29 17:42:14 +03:00
|
|
|
? container->PrincipalChildList().FirstChild()
|
|
|
|
: nullptr;
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!rtFrame || rtFrame->IsRubyTextFrame());
|
2015-04-08 02:22:34 +03:00
|
|
|
mFrames.AppendElement(static_cast<nsRubyContentFrame*>(rtFrame));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to init mAtIntraLevelWhitespace to be correct for the
|
|
|
|
// first column. There are two ways we could end up with intra-level
|
|
|
|
// whitespace in our first colum:
|
|
|
|
// 1. The current segment itself is an inter-segment whitespace;
|
|
|
|
// 2. If our ruby segment is split across multiple lines, and some
|
|
|
|
// intra-level whitespace happens to fall right after a line-break.
|
|
|
|
// Each line will get its own nsRubyBaseContainerFrame, and the
|
|
|
|
// container right after the line-break will end up with its first
|
|
|
|
// column containing that intra-level whitespace.
|
|
|
|
for (uint32_t i = 0, iend = mFrames.Length(); i < iend; i++) {
|
|
|
|
nsRubyContentFrame* frame = mFrames[i];
|
|
|
|
if (frame && frame->IsIntraLevelWhitespace()) {
|
|
|
|
mAtIntraLevelWhitespace = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RubyColumnEnumerator::Next() {
|
|
|
|
bool advancingToIntraLevelWhitespace = false;
|
|
|
|
for (uint32_t i = 0, iend = mFrames.Length(); i < iend; i++) {
|
|
|
|
nsRubyContentFrame* frame = mFrames[i];
|
|
|
|
// If we've got intra-level whitespace frames at some levels in the
|
|
|
|
// current ruby column, we "faked" an anonymous box for all other
|
|
|
|
// levels for this column. So when we advance off this column, we
|
|
|
|
// don't advance any of the frames in those levels, because we're
|
|
|
|
// just advancing across the "fake" frames.
|
|
|
|
if (frame &&
|
|
|
|
(!mAtIntraLevelWhitespace || frame->IsIntraLevelWhitespace())) {
|
|
|
|
nsIFrame* nextSibling = frame->GetNextSibling();
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!nextSibling || nextSibling->Type() == frame->Type(),
|
2015-04-08 02:22:34 +03:00
|
|
|
"Frame type should be identical among a level");
|
|
|
|
mFrames[i] = frame = static_cast<nsRubyContentFrame*>(nextSibling);
|
|
|
|
if (!advancingToIntraLevelWhitespace && frame &&
|
|
|
|
frame->IsIntraLevelWhitespace()) {
|
|
|
|
advancingToIntraLevelWhitespace = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(!advancingToIntraLevelWhitespace || !mAtIntraLevelWhitespace,
|
|
|
|
"Should never have adjacent intra-level whitespace columns");
|
|
|
|
mAtIntraLevelWhitespace = advancingToIntraLevelWhitespace;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RubyColumnEnumerator::AtEnd() const {
|
|
|
|
for (uint32_t i = 0, iend = mFrames.Length(); i < iend; i++) {
|
|
|
|
if (mFrames[i]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRubyContentFrame* RubyColumnEnumerator::GetFrameAtLevel(
|
|
|
|
uint32_t aIndex) const {
|
|
|
|
// If the current ruby column is for intra-level whitespaces, we
|
|
|
|
// return nullptr for any levels that do not have an actual intra-
|
|
|
|
// level whitespace frame in this column. This nullptr represents
|
|
|
|
// an anonymous empty intra-level whitespace box. (In this case,
|
|
|
|
// it's important that we NOT return mFrames[aIndex], because it's
|
|
|
|
// really part of the next column, not the current one.)
|
|
|
|
nsRubyContentFrame* frame = mFrames[aIndex];
|
|
|
|
return !mAtIntraLevelWhitespace || (frame && frame->IsIntraLevelWhitespace())
|
|
|
|
? frame
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RubyColumnEnumerator::GetColumn(RubyColumn& aColumn) const {
|
|
|
|
nsRubyContentFrame* rbFrame = GetFrameAtLevel(0);
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!rbFrame || rbFrame->IsRubyBaseFrame());
|
2015-04-08 02:22:34 +03:00
|
|
|
aColumn.mBaseFrame = static_cast<nsRubyBaseFrame*>(rbFrame);
|
|
|
|
aColumn.mTextFrames.ClearAndRetainStorage();
|
|
|
|
for (uint32_t i = 1, iend = mFrames.Length(); i < iend; i++) {
|
|
|
|
nsRubyContentFrame* rtFrame = GetFrameAtLevel(i);
|
2017-04-30 18:30:08 +03:00
|
|
|
MOZ_ASSERT(!rtFrame || rtFrame->IsRubyTextFrame());
|
2015-04-08 02:22:34 +03:00
|
|
|
aColumn.mTextFrames.AppendElement(static_cast<nsRubyTextFrame*>(rtFrame));
|
|
|
|
}
|
|
|
|
aColumn.mIsIntraLevelWhitespace = mAtIntraLevelWhitespace;
|
|
|
|
}
|