Bug 1709347 - Add CanvasRenderingContext2D.reset(). r=lsalzman,webidl,smaug
[gecko.git] / layout / base / Baseline.cpp
blob00166be9ef0155c492f9eebc759e797d1acdef5f
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/. */
5 #include "Baseline.h"
6 #include "nsIFrame.h"
8 namespace mozilla {
10 nscoord Baseline::SynthesizeBOffsetFromMarginBox(const nsIFrame* aFrame,
11 WritingMode aWM,
12 BaselineSharingGroup aGroup) {
13 MOZ_ASSERT(!aWM.IsOrthogonalTo(aFrame->GetWritingMode()));
14 auto margin = aFrame->GetLogicalUsedMargin(aWM);
15 if (aGroup == BaselineSharingGroup::First) {
16 if (aWM.IsAlphabeticalBaseline()) {
17 // First baseline for inverted-line content is the block-start margin
18 // edge, as the frame is in effect "flipped" for alignment purposes.
19 return MOZ_UNLIKELY(aWM.IsLineInverted())
20 ? -margin.BStart(aWM)
21 : aFrame->BSize(aWM) + margin.BEnd(aWM);
23 nscoord marginBoxCenter = (aFrame->BSize(aWM) + margin.BStartEnd(aWM)) / 2;
24 return marginBoxCenter - margin.BStart(aWM);
26 MOZ_ASSERT(aGroup == BaselineSharingGroup::Last);
27 if (aWM.IsAlphabeticalBaseline()) {
28 // Last baseline for inverted-line content is the block-start margin edge,
29 // as the frame is in effect "flipped" for alignment purposes.
30 return MOZ_UNLIKELY(aWM.IsLineInverted())
31 ? aFrame->BSize(aWM) + margin.BStart(aWM)
32 : -margin.BEnd(aWM);
34 // Round up for central baseline offset, to be consistent with ::First.
35 nscoord marginBoxSize = aFrame->BSize(aWM) + margin.BStartEnd(aWM);
36 nscoord marginBoxCenter = (marginBoxSize / 2) + (marginBoxSize % 2);
37 return marginBoxCenter - margin.BEnd(aWM);
40 nscoord Baseline::SynthesizeBOffsetFromBorderBox(const nsIFrame* aFrame,
41 WritingMode aWM,
42 BaselineSharingGroup aGroup) {
43 nscoord borderBoxSize =
44 MOZ_UNLIKELY(aWM.IsOrthogonalTo(aFrame->GetWritingMode()))
45 ? aFrame->ISize(aWM)
46 : aFrame->BSize(aWM);
47 if (aGroup == BaselineSharingGroup::First) {
48 return MOZ_LIKELY(aWM.IsAlphabeticalBaseline()) ? borderBoxSize
49 : borderBoxSize / 2;
51 MOZ_ASSERT(aGroup == BaselineSharingGroup::Last);
52 // Round up for central baseline offset, to be consistent with ::First.
53 auto borderBoxCenter = (borderBoxSize / 2) + (borderBoxSize % 2);
54 return MOZ_LIKELY(aWM.IsAlphabeticalBaseline()) ? 0 : borderBoxCenter;
57 nscoord Baseline::SynthesizeBOffsetFromContentBox(const nsIFrame* aFrame,
58 WritingMode aWM,
59 BaselineSharingGroup aGroup) {
60 WritingMode wm = aFrame->GetWritingMode();
61 MOZ_ASSERT(!aWM.IsOrthogonalTo(wm));
62 const auto bp = aFrame->GetLogicalUsedBorderAndPadding(wm)
63 .ApplySkipSides(aFrame->GetLogicalSkipSides())
64 .ConvertTo(aWM, wm);
66 if (MOZ_UNLIKELY(aWM.IsCentralBaseline())) {
67 nscoord contentBoxBSize = aFrame->BSize(aWM) - bp.BStartEnd(aWM);
68 if (aGroup == BaselineSharingGroup::First) {
69 return contentBoxBSize / 2 + bp.BStart(aWM);
71 // Return the same center position as for ::First, but as offset from end:
72 nscoord halfContentBoxBSize = (contentBoxBSize / 2) + (contentBoxBSize % 2);
73 return halfContentBoxBSize + bp.BEnd(aWM);
75 if (aGroup == BaselineSharingGroup::First) {
76 // First baseline for inverted-line content is the block-start content
77 // edge, as the frame is in effect "flipped" for alignment purposes.
78 return MOZ_UNLIKELY(aWM.IsLineInverted())
79 ? bp.BStart(aWM)
80 : aFrame->BSize(aWM) - bp.BEnd(aWM);
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())
85 ? aFrame->BSize(aWM) - bp.BStart(aWM)
86 : bp.BEnd(aWM);
89 } // namespace mozilla