tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Baseline.cpp (4786B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "Baseline.h"
      6 
      7 #include "nsIFrame.h"
      8 
      9 namespace mozilla {
     10 
     11 nscoord Baseline::SynthesizeBOffsetFromMarginBox(const nsIFrame* aFrame,
     12                                                 WritingMode aWM,
     13                                                 BaselineSharingGroup aGroup) {
     14  MOZ_ASSERT(!aWM.IsOrthogonalTo(aFrame->GetWritingMode()));
     15  auto margin = aFrame->GetLogicalUsedMargin(aWM);
     16  if (aGroup == BaselineSharingGroup::First) {
     17    if (aWM.IsAlphabeticalBaseline()) {
     18      // First baseline for inverted-line content is the block-start margin
     19      // edge, as the frame is in effect "flipped" for alignment purposes.
     20      return MOZ_UNLIKELY(aWM.IsLineInverted())
     21                 ? -margin.BStart(aWM)
     22                 : aFrame->BSize(aWM) + margin.BEnd(aWM);
     23    }
     24    nscoord marginBoxCenter = (aFrame->BSize(aWM) + margin.BStartEnd(aWM)) / 2;
     25    return marginBoxCenter - margin.BStart(aWM);
     26  }
     27  MOZ_ASSERT(aGroup == BaselineSharingGroup::Last);
     28  if (aWM.IsAlphabeticalBaseline()) {
     29    // Last baseline for inverted-line content is the block-start margin edge,
     30    // as the frame is in effect "flipped" for alignment purposes.
     31    return MOZ_UNLIKELY(aWM.IsLineInverted())
     32               ? aFrame->BSize(aWM) + margin.BStart(aWM)
     33               : -margin.BEnd(aWM);
     34  }
     35  // Round up for central baseline offset, to be consistent with ::First.
     36  nscoord marginBoxSize = aFrame->BSize(aWM) + margin.BStartEnd(aWM);
     37  nscoord marginBoxCenter = (marginBoxSize / 2) + (marginBoxSize % 2);
     38  return marginBoxCenter - margin.BEnd(aWM);
     39 }
     40 
     41 enum class BoxType { Border, Padding, Content };
     42 
     43 template <BoxType aType>
     44 static nscoord SynthesizeBOffsetFromInnerBox(const nsIFrame* aFrame,
     45                                             WritingMode aWM,
     46                                             BaselineSharingGroup aGroup) {
     47  WritingMode wm = aFrame->GetWritingMode();
     48  MOZ_ASSERT_IF(aType != BoxType::Border, !aWM.IsOrthogonalTo(wm));
     49  const nscoord borderBoxSize = MOZ_UNLIKELY(aWM.IsOrthogonalTo(wm))
     50                                    ? aFrame->ISize(aWM)
     51                                    : aFrame->BSize(aWM);
     52  const LogicalMargin bp = ([&] {
     53    switch (aType) {
     54      case BoxType::Border:
     55        return LogicalMargin(aWM);
     56      case BoxType::Padding:
     57        return aFrame->GetLogicalUsedBorder(wm)
     58            .ApplySkipSides(aFrame->GetLogicalSkipSides())
     59            .ConvertTo(aWM, wm);
     60      case BoxType::Content:
     61        return aFrame->GetLogicalUsedBorderAndPadding(wm)
     62            .ApplySkipSides(aFrame->GetLogicalSkipSides())
     63            .ConvertTo(aWM, wm);
     64    }
     65    MOZ_CRASH();
     66  })();
     67  if (MOZ_UNLIKELY(aWM.IsCentralBaseline())) {
     68    nscoord boxBSize = borderBoxSize - bp.BStartEnd(aWM);
     69    if (aGroup == BaselineSharingGroup::First) {
     70      return boxBSize / 2 + bp.BStart(aWM);
     71    }
     72    // Return the same center position as for ::First, but as offset from end:
     73    nscoord halfBoxBSize = (boxBSize / 2) + (boxBSize % 2);
     74    return halfBoxBSize + bp.BEnd(aWM);
     75  }
     76  if (aGroup == BaselineSharingGroup::First) {
     77    // First baseline for inverted-line content is the block-start content
     78    // edge, as the frame is in effect "flipped" for alignment purposes.
     79    return MOZ_UNLIKELY(aWM.IsLineInverted()) ? bp.BStart(aWM)
     80                                              : borderBoxSize - bp.BEnd(aWM);
     81  }
     82  // Last baseline for inverted-line content is the block-start content edge,
     83  // as the frame is in effect "flipped" for alignment purposes.
     84  return MOZ_UNLIKELY(aWM.IsLineInverted()) ? borderBoxSize - bp.BStart(aWM)
     85                                            : bp.BEnd(aWM);
     86 }
     87 
     88 nscoord Baseline::SynthesizeBOffsetFromContentBox(const nsIFrame* aFrame,
     89                                                  WritingMode aWM,
     90                                                  BaselineSharingGroup aGroup) {
     91  return SynthesizeBOffsetFromInnerBox<BoxType::Content>(aFrame, aWM, aGroup);
     92 }
     93 
     94 nscoord Baseline::SynthesizeBOffsetFromPaddingBox(const nsIFrame* aFrame,
     95                                                  WritingMode aWM,
     96                                                  BaselineSharingGroup aGroup) {
     97  return SynthesizeBOffsetFromInnerBox<BoxType::Padding>(aFrame, aWM, aGroup);
     98 }
     99 
    100 nscoord Baseline::SynthesizeBOffsetFromBorderBox(const nsIFrame* aFrame,
    101                                                 WritingMode aWM,
    102                                                 BaselineSharingGroup aGroup) {
    103  return SynthesizeBOffsetFromInnerBox<BoxType::Border>(aFrame, aWM, aGroup);
    104 }
    105 
    106 }  // namespace mozilla