DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (d38398e5144e)

VCS Links

PullFrameState

RubyReflowInput

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
/* -*- 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-base-container" */

#include "nsRubyBaseContainerFrame.h"
#include "nsRubyTextContainerFrame.h"
#include "nsRubyBaseFrame.h"
#include "nsRubyTextFrame.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/Maybe.h"
#include "mozilla/WritingModes.h"
#include "nsLayoutUtils.h"
#include "nsLineLayout.h"
#include "nsPresContext.h"
#include "nsStyleContext.h"
#include "nsStyleStructInlines.h"
#include "nsTextFrame.h"
#include "RubyUtils.h"

using namespace mozilla;
using namespace mozilla::gfx;

//----------------------------------------------------------------------

// Frame class boilerplate
// =======================

NS_QUERYFRAME_HEAD(nsRubyBaseContainerFrame)
  NS_QUERYFRAME_ENTRY(nsRubyBaseContainerFrame)
NS_QUERYFRAME_TAIL_INHERITING(nsContainerFrame)

NS_IMPL_FRAMEARENA_HELPERS(nsRubyBaseContainerFrame)

nsContainerFrame*
NS_NewRubyBaseContainerFrame(nsIPresShell* aPresShell,
                             nsStyleContext* aContext)
{
  return new (aPresShell) nsRubyBaseContainerFrame(aContext);
}


//----------------------------------------------------------------------

// nsRubyBaseContainerFrame Method Implementations
// ===============================================

nsIAtom*
nsRubyBaseContainerFrame::GetType() const
{
  return nsGkAtoms::rubyBaseContainerFrame;
}

#ifdef DEBUG_FRAME_DUMP
nsresult
nsRubyBaseContainerFrame::GetFrameName(nsAString& aResult) const
{
  return MakeFrameName(NS_LITERAL_STRING("RubyBaseContainer"), aResult);
}
#endif

static gfxBreakPriority
LineBreakBefore(nsIFrame* aFrame,
                DrawTarget* aDrawTarget,
                nsIFrame* aLineContainerFrame,
                const nsLineList::iterator* aLine)
{
  for (nsIFrame* child = aFrame; child;
       child = child->PrincipalChildList().FirstChild()) {
    if (!child->CanContinueTextRun()) {
      // It is not an inline element. We can break before it.
      return gfxBreakPriority::eNormalBreak;
    }
    if (child->GetType() != nsGkAtoms::textFrame) {
      continue;
    }

    auto textFrame = static_cast<nsTextFrame*>(child);
    gfxSkipCharsIterator iter =
      textFrame->EnsureTextRun(nsTextFrame::eInflated, aDrawTarget,
                               aLineContainerFrame, aLine);
    iter.SetOriginalOffset(textFrame->GetContentOffset());
    uint32_t pos = iter.GetSkippedOffset();
    gfxTextRun* textRun = textFrame->GetTextRun(nsTextFrame::eInflated);
    if (pos >= textRun->GetLength()) {
      // The text frame contains no character at all.
      return gfxBreakPriority::eNoBreak;
    }
    // Return whether we can break before the first character.
    if (textRun->CanBreakLineBefore(pos)) {
      return gfxBreakPriority::eNormalBreak;
    }
    // Check whether we can wrap word here.
    const nsStyleText* textStyle = textFrame->StyleText();
    if (textStyle->WordCanWrap(textFrame) && textRun->IsClusterStart(pos)) {
      return gfxBreakPriority::eWordWrapBreak;
    }
    // We cannot break before.
    return gfxBreakPriority::eNoBreak;
  }
  // Neither block, nor text frame is found as a leaf. We won't break
  // before this base frame. It is the behavior of empty spans.
  return gfxBreakPriority::eNoBreak;
}

static void
GetIsLineBreakAllowed(nsIFrame* aFrame, bool aIsLineBreakable,
                      bool* aAllowInitialLineBreak, bool* aAllowLineBreak)
{
  nsIFrame* parent = aFrame->GetParent();
  bool lineBreakSuppressed = parent->StyleContext()->ShouldSuppressLineBreak();
  // Allow line break between ruby bases when white-space allows,
  // we are not inside a nested ruby, and there is no span.
  bool allowLineBreak = !lineBreakSuppressed &&
                        aFrame->StyleText()->WhiteSpaceCanWrap(aFrame);
  bool allowInitialLineBreak = allowLineBreak;
  if (!aFrame->GetPrevInFlow()) {
    allowInitialLineBreak = !lineBreakSuppressed &&
                            parent->StyleText()->WhiteSpaceCanWrap(parent);
  }
  if (!aIsLineBreakable) {
    allowInitialLineBreak = false;
  }
  *aAllowInitialLineBreak = allowInitialLineBreak;
  *aAllowLineBreak = allowLineBreak;
}

/**
 * @param aBaseISizeData is an in/out param. This method updates the
 * `skipWhitespace` and `trailingWhitespace` fields of the struct with
 * the base level frame. Note that we don't need to do the same thing
 * for ruby text frames, because they are text run container themselves
 * (see nsTextFrame.cpp:BuildTextRuns), and thus no whitespace collapse
 * happens across the boundary of those frames.
 */
static nscoord
CalculateColumnPrefISize(nsRenderingContext* aRenderingContext,
                         const RubyColumnEnumerator& aEnumerator,
                         nsIFrame::InlineIntrinsicISizeData* aBaseISizeData)
{
  nscoord max = 0;
  uint32_t levelCount = aEnumerator.GetLevelCount();
  for (uint32_t i = 0; i < levelCount; i++) {
    nsIFrame* frame = aEnumerator.GetFrameAtLevel(i);
    if (frame) {
      nsIFrame::InlinePrefISizeData data;
      if (i == 0) {
        data.SetLineContainer(aBaseISizeData->LineContainer());
        data.mSkipWhitespace = aBaseISizeData->mSkipWhitespace;
        data.mTrailingWhitespace = aBaseISizeData->mTrailingWhitespace;
      } else {
        // The line container of ruby text frames is their parent,
        // ruby text container frame.
        data.SetLineContainer(frame->GetParent());
      }
      frame->AddInlinePrefISize(aRenderingContext, &data);
      MOZ_ASSERT(data.mPrevLines == 0, "Shouldn't have prev lines");
      max = std::max(max, data.mCurrentLine);
      if (i == 0) {
        aBaseISizeData->mSkipWhitespace = data.mSkipWhitespace;
        aBaseISizeData->mTrailingWhitespace = data.mTrailingWhitespace;
      }
    }
  }
  return max;
}

// FIXME Currently we use pref isize of ruby content frames for
//       computing min isize of ruby frame, which may cause problem.
//       See bug 1134945.
/* virtual */ void
nsRubyBaseContainerFrame::AddInlineMinISize(
  nsRenderingContext *aRenderingContext, nsIFrame::InlineMinISizeData *aData)
{
  AutoRubyTextContainerArray textContainers(this);

  for (uint32_t i = 0, iend = textContainers.Length(); i < iend; i++) {
    if (textContainers[i]->IsSpanContainer()) {
      // Since spans are not breakable internally, use our pref isize
      // directly if there is any span.
      nsIFrame::InlinePrefISizeData data;
      data.SetLineContainer(aData->LineContainer());
      data.mSkipWhitespace = aData->mSkipWhitespace;
      data.mTrailingWhitespace = aData->mTrailingWhitespace;
      AddInlinePrefISize(aRenderingContext, &data);
      aData->mCurrentLine += data.mCurrentLine;
      if (data.mCurrentLine > 0) {
        aData->mAtStartOfLine = false;
      }
      aData->mSkipWhitespace = data.mSkipWhitespace;
      aData->mTrailingWhitespace = data.mTrailingWhitespace;
      return;
    }
  }

  bool firstFrame = true;
  bool allowInitialLineBreak, allowLineBreak;
  GetIsLineBreakAllowed(this, !aData->mAtStartOfLine,
                        &allowInitialLineBreak, &allowLineBreak);
  for (nsIFrame* frame = this; frame; frame = frame->GetNextInFlow()) {
    RubyColumnEnumerator enumerator(
      static_cast<nsRubyBaseContainerFrame*>(frame), textContainers);
    for (; !enumerator.AtEnd(); enumerator.Next()) {
      if (firstFrame ? allowInitialLineBreak : allowLineBreak) {
        nsIFrame* baseFrame = enumerator.GetFrameAtLevel(0);
        if (baseFrame) {
          gfxBreakPriority breakPriority =
            LineBreakBefore(baseFrame, aRenderingContext->GetDrawTarget(),
                            nullptr, nullptr);
          if (breakPriority != gfxBreakPriority::eNoBreak) {
            aData->OptionallyBreak();
          }
        }
      }
      firstFrame = false;
      nscoord isize = CalculateColumnPrefISize(aRenderingContext,
                                               enumerator, aData);
      aData->mCurrentLine += isize;
      if (isize > 0) {
        aData->mAtStartOfLine = false;
      }
    }
  }
}

/* virtual */ void
nsRubyBaseContainerFrame::AddInlinePrefISize(
  nsRenderingContext *aRenderingContext, nsIFrame::InlinePrefISizeData *aData)
{
  AutoRubyTextContainerArray textContainers(this);

  nscoord sum = 0;
  for (nsIFrame* frame = this; frame; frame = frame->GetNextInFlow()) {
    RubyColumnEnumerator enumerator(
      static_cast<nsRubyBaseContainerFrame*>(frame), textContainers);
    for (; !enumerator.AtEnd(); enumerator.Next()) {
      sum += CalculateColumnPrefISize(aRenderingContext, enumerator, aData);
    }
  }
  for (uint32_t i = 0, iend = textContainers.Length(); i < iend; i++) {
    if (textContainers[i]->IsSpanContainer()) {
      nsIFrame* frame = textContainers[i]->PrincipalChildList().FirstChild();
      nsIFrame::InlinePrefISizeData data;
      frame->AddInlinePrefISize(aRenderingContext, &data);
      MOZ_ASSERT(data.mPrevLines == 0, "Shouldn't have prev lines");
      sum = std::max(sum, data.mCurrentLine);
    }
  }
  aData->mCurrentLine += sum;
}

/* virtual */ bool 
nsRubyBaseContainerFrame::IsFrameOfType(uint32_t aFlags) const 
{
  if (aFlags & eSupportsCSSTransforms) {
    return false;
  }
  return nsContainerFrame::IsFrameOfType(aFlags &
         ~(nsIFrame::eLineParticipant));
}

/* virtual */ bool
nsRubyBaseContainerFrame::CanContinueTextRun() const
{
  return true;
}

/* virtual */ LogicalSize
nsRubyBaseContainerFrame::ComputeSize(nsRenderingContext *aRenderingContext,
                                      WritingMode aWM,
                                      const LogicalSize& aCBSize,
                                      nscoord aAvailableISize,
                                      const LogicalSize& aMargin,
                                      const LogicalSize& aBorder,
                                      const LogicalSize& aPadding,
                                      ComputeSizeFlags aFlags)
{
  // Ruby base container frame is inline,
  // hence don't compute size before reflow.
  return LogicalSize(aWM, NS_UNCONSTRAINEDSIZE, NS_UNCONSTRAINEDSIZE);
}

/* virtual */ nscoord
nsRubyBaseContainerFrame::GetLogicalBaseline(WritingMode aWritingMode) const
{
  return mBaseline;
}

struct nsRubyBaseContainerFrame::RubyReflowInput
{
  bool mAllowInitialLineBreak;
  bool mAllowLineBreak;
  const AutoRubyTextContainerArray& mTextContainers;
  const ReflowInput& mBaseReflowInput;
  const nsTArray<UniquePtr<ReflowInput>>& mTextReflowInputs;
};

/* virtual */ void
nsRubyBaseContainerFrame::Reflow(nsPresContext* aPresContext,
                                 ReflowOutput& aDesiredSize,
                                 const ReflowInput& aReflowInput,
                                 nsReflowStatus& aStatus)
{
  MarkInReflow();
  DO_GLOBAL_REFLOW_COUNT("nsRubyBaseContainerFrame");
  DISPLAY_REFLOW(aPresContext, this, aReflowInput, aDesiredSize, aStatus);
  aStatus.Reset();

  if (!aReflowInput.mLineLayout) {
    NS_ASSERTION(
      aReflowInput.mLineLayout,
      "No line layout provided to RubyBaseContainerFrame reflow method.");
    return;
  }

  mDescendantLeadings.Reset();

  MoveOverflowToChildList();
  // Ask text containers to drain overflows
  AutoRubyTextContainerArray textContainers(this);
  const uint32_t rtcCount = textContainers.Length();
  for (uint32_t i = 0; i < rtcCount; i++) {
    textContainers[i]->MoveOverflowToChildList();
  }

  WritingMode lineWM = aReflowInput.mLineLayout->GetWritingMode();
  LogicalSize availSize(lineWM, aReflowInput.AvailableISize(),
                        aReflowInput.AvailableBSize());

  // We have a reflow state and a line layout for each RTC.
  // They are conceptually the state of the RTCs, but we don't actually
  // reflow those RTCs in this code. These two arrays are holders of
  // the reflow states and line layouts.
  // Since there are pointers refer to reflow states and line layouts,
  // it is necessary to guarantee that they won't be moved. For this
  // reason, they are wrapped in UniquePtr here.
  AutoTArray<UniquePtr<ReflowInput>, RTC_ARRAY_SIZE> reflowInputs;
  AutoTArray<UniquePtr<nsLineLayout>, RTC_ARRAY_SIZE> lineLayouts;
  reflowInputs.SetCapacity(rtcCount);
  lineLayouts.SetCapacity(rtcCount);

  // Begin the line layout for each ruby text container in advance.
  bool hasSpan = false;
  for (uint32_t i = 0; i < rtcCount; i++) {
    nsRubyTextContainerFrame* textContainer = textContainers[i];
    if (textContainer->IsSpanContainer()) {
      hasSpan = true;
    }

    ReflowInput* reflowInput = new ReflowInput(
      aPresContext, *aReflowInput.mParentReflowInput, textContainer,
      availSize.ConvertTo(textContainer->GetWritingMode(), lineWM));
    reflowInputs.AppendElement(reflowInput);
    nsLineLayout* lineLayout = new nsLineLayout(aPresContext,
                                                reflowInput->mFloatManager,
                                                reflowInput, nullptr,
                                                aReflowInput.mLineLayout);
    lineLayout->SetSuppressLineWrap(true);
    lineLayouts.AppendElement(lineLayout);

    // Line number is useless for ruby text
    // XXX nullptr here may cause problem, see comments for
    //     nsLineLayout::mBlockRI and nsLineLayout::AddFloat
    lineLayout->Init(nullptr, reflowInput->CalcLineHeight(), -1);
    reflowInput->mLineLayout = lineLayout;

    // Border and padding are suppressed on ruby text containers.
    // If the writing mode is vertical-rl, the horizontal position of
    // rt frames will be updated when reflowing this text container,
    // hence leave container size 0 here for now.
    lineLayout->BeginLineReflow(0, 0, reflowInput->ComputedISize(),
                                NS_UNCONSTRAINEDSIZE,
                                false, false, lineWM, nsSize(0, 0));
    lineLayout->AttachRootFrameToBaseLineLayout();
  }

  aReflowInput.mLineLayout->BeginSpan(this, &aReflowInput,
                                      0, aReflowInput.AvailableISize(),
                                      &mBaseline);

  bool allowInitialLineBreak, allowLineBreak;
  GetIsLineBreakAllowed(this, aReflowInput.mLineLayout->LineIsBreakable(),
                        &allowInitialLineBreak, &allowLineBreak);

  nscoord isize = 0;
  // Reflow columns excluding any span
  RubyReflowInput reflowInput = {
    allowInitialLineBreak, allowLineBreak && !hasSpan,
    textContainers, aReflowInput, reflowInputs
  };
  isize = ReflowColumns(reflowInput, aStatus);
  DebugOnly<nscoord> lineSpanSize = aReflowInput.mLineLayout->EndSpan(this);
  aDesiredSize.ISize(lineWM) = isize;
  // When there are no frames inside the ruby base container, EndSpan
  // will return 0. However, in this case, the actual width of the
  // container could be non-zero because of non-empty ruby annotations.
  // XXX When bug 765861 gets fixed, this warning should be upgraded.
  NS_WARNING_ASSERTION(
    aStatus.IsInlineBreak() || isize == lineSpanSize || mFrames.IsEmpty(),
    "bad isize");

  // If there exists any span, the columns must either be completely
  // reflowed, or be not reflowed at all.
  MOZ_ASSERT(aStatus.IsInlineBreakBefore() ||
             aStatus.IsComplete() || !hasSpan);
  if (!aStatus.IsInlineBreakBefore() &&
      aStatus.IsComplete() && hasSpan) {
    // Reflow spans
    RubyReflowInput reflowInput = {
      false, false, textContainers, aReflowInput, reflowInputs
    };
    nscoord spanISize = ReflowSpans(reflowInput);
    isize = std::max(isize, spanISize);
  }

  for (uint32_t i = 0; i < rtcCount; i++) {
    // It happens before the ruby text container is reflowed, and that
    // when it is reflowed, it will just use this size.
    nsRubyTextContainerFrame* textContainer = textContainers[i];
    nsLineLayout* lineLayout = lineLayouts[i].get();

    RubyUtils::ClearReservedISize(textContainer);
    nscoord rtcISize = lineLayout->GetCurrentICoord();
    // Only span containers and containers with collapsed annotations
    // need reserving isize. For normal ruby text containers, their
    // children will be expanded properly. We only need to expand their
    // own size.
    if (!textContainer->IsSpanContainer()) {
      rtcISize = isize;
    } else if (isize > rtcISize) {
      RubyUtils::SetReservedISize(textContainer, isize - rtcISize);
    }

    lineLayout->VerticalAlignLine();
    textContainer->SetISize(rtcISize);
    lineLayout->EndLineReflow();
  }

  // Border and padding are suppressed on ruby base container,
  // create a fake borderPadding for setting BSize.
  WritingMode frameWM = aReflowInput.GetWritingMode();
  LogicalMargin borderPadding(frameWM);
  nsLayoutUtils::SetBSizeFromFontMetrics(this, aDesiredSize,
                                         borderPadding, lineWM, frameWM);
}

/**
 * This struct stores the continuations after this frame and
 * corresponding text containers. It is used to speed up looking
 * ahead for nonempty continuations.
 */
struct MOZ_STACK_CLASS nsRubyBaseContainerFrame::PullFrameState
{
  ContinuationTraversingState mBase;
  AutoTArray<ContinuationTraversingState, RTC_ARRAY_SIZE> mTexts;
  const AutoRubyTextContainerArray& mTextContainers;

  PullFrameState(nsRubyBaseContainerFrame* aBaseContainer,
                 const AutoRubyTextContainerArray& aTextContainers);
};

nscoord
nsRubyBaseContainerFrame::ReflowColumns(const RubyReflowInput& aReflowInput,
                                        nsReflowStatus& aStatus)
{
  nsLineLayout* lineLayout = aReflowInput.mBaseReflowInput.mLineLayout;
  const uint32_t rtcCount = aReflowInput.mTextContainers.Length();
  nscoord icoord = lineLayout->GetCurrentICoord();
  MOZ_ASSERT(icoord == 0, "border/padding of rbc should have been suppressed");
  nsReflowStatus reflowStatus;
  aStatus.Reset();

  uint32_t columnIndex = 0;
  RubyColumn column;
  column.mTextFrames.SetCapacity(rtcCount);
  RubyColumnEnumerator e(this, aReflowInput.mTextContainers);
  for (; !e.AtEnd(); e.Next()) {
    e.GetColumn(column);
    icoord += ReflowOneColumn(aReflowInput, columnIndex, column, reflowStatus);
    if (!reflowStatus.IsInlineBreakBefore()) {
      columnIndex++;
    }
    if (reflowStatus.IsInlineBreak()) {
      break;
    }
    // We are not handling overflow here.
    MOZ_ASSERT(reflowStatus.IsEmpty());
  }

  bool isComplete = false;
  PullFrameState pullFrameState(this,