summaryrefslogtreecommitdiffstats
path: root/layout/mathml/nsMathMLmfracFrame.cpp
blob: fc43d2dff179c0f05f07deca2a06b03c78187d3b (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 "nsMathMLmfracFrame.h"

#include "gfxUtils.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/PresShell.h"
#include "mozilla/RefPtr.h"
#include "mozilla/StaticPrefs_mathml.h"
#include "nsLayoutUtils.h"
#include "nsPresContext.h"
#include "nsDisplayList.h"
#include "gfxContext.h"
#include "mozilla/dom/Document.h"
#include "mozilla/dom/MathMLElement.h"
#include <algorithm>
#include "gfxMathTable.h"
#include "gfxTextRun.h"

using namespace mozilla;
using namespace mozilla::gfx;

//
// <mfrac> -- form a fraction from two subexpressions - implementation
//

nsIFrame* NS_NewMathMLmfracFrame(PresShell* aPresShell, ComputedStyle* aStyle) {
  return new (aPresShell)
      nsMathMLmfracFrame(aStyle, aPresShell->GetPresContext());
}

NS_IMPL_FRAMEARENA_HELPERS(nsMathMLmfracFrame)

nsMathMLmfracFrame::~nsMathMLmfracFrame() = default;

eMathMLFrameType nsMathMLmfracFrame::GetMathMLFrameType() {
  // frac is "inner" in TeXBook, Appendix G, rule 15e. See also page 170.
  return eMathMLFrameType_Inner;
}

uint8_t nsMathMLmfracFrame::ScriptIncrement(nsIFrame* aFrame) {
  if (StyleFont()->mMathStyle == StyleMathStyle::Compact && aFrame &&
      (mFrames.FirstChild() == aFrame || mFrames.LastChild() == aFrame)) {
    return 1;
  }
  return 0;
}

NS_IMETHODIMP
nsMathMLmfracFrame::TransmitAutomaticData() {
  // The TeXbook (Ch 17. p.141) says the numerator inherits the compression
  //  while the denominator is compressed
  UpdatePresentationDataFromChildAt(1, 1, NS_MATHML_COMPRESSED,
                                    NS_MATHML_COMPRESSED);

  // If displaystyle is false, then scriptlevel is incremented, so notify the
  // children of this.
  if (StyleFont()->mMathStyle == StyleMathStyle::Compact) {
    PropagateFrameFlagFor(mFrames.FirstChild(),
                          NS_FRAME_MATHML_SCRIPT_DESCENDANT);
    PropagateFrameFlagFor(mFrames.LastChild(),
                          NS_FRAME_MATHML_SCRIPT_DESCENDANT);
  }

  // if our numerator is an embellished operator, let its state bubble to us
  GetEmbellishDataFrom(mFrames.FirstChild(), mEmbellishData);
  if (NS_MATHML_IS_EMBELLISH_OPERATOR(mEmbellishData.flags)) {
    // even when embellished, we need to record that <mfrac> won't fire
    // Stretch() on its embellished child
    mEmbellishData.direction = NS_STRETCH_DIRECTION_UNSUPPORTED;
  }

  return NS_OK;
}

nscoord nsMathMLmfracFrame::CalcLineThickness(nsPresContext* aPresContext,
                                              ComputedStyle* aComputedStyle,
                                              nsString& aThicknessAttribute,
                                              nscoord onePixel,
                                              nscoord aDefaultRuleThickness,
                                              float aFontSizeInflation) {
  nscoord defaultThickness = aDefaultRuleThickness;
  nscoord lineThickness = aDefaultRuleThickness;
  nscoord minimumThickness = onePixel;

  // linethickness
  // https://w3c.github.io/mathml-core/#dfn-linethickness
  if (!aThicknessAttribute.IsEmpty()) {
    lineThickness = defaultThickness;
    ParseNumericValue(aThicknessAttribute, &lineThickness,
                      dom::MathMLElement::PARSE_ALLOW_NEGATIVE, aPresContext,
                      aComputedStyle, aFontSizeInflation);
    // MathML Core says a negative value is interpreted as 0.
    if (lineThickness < 0) {
      lineThickness = 0;
    }
  }
  // use minimum if the lineThickness is a non-zero value less than minimun
  if (lineThickness && lineThickness < minimumThickness)
    lineThickness = minimumThickness;

  return lineThickness;
}

void nsMathMLmfracFrame::BuildDisplayList(nsDisplayListBuilder* aBuilder,
                                          const nsDisplayListSet& aLists) {
  /////////////
  // paint the numerator and denominator
  nsMathMLContainerFrame::BuildDisplayList(aBuilder, aLists);

  /////////////
  // paint the fraction line
  DisplayBar(aBuilder, this, mLineRect, aLists);
}

nsresult nsMathMLmfracFrame::AttributeChanged(int32_t aNameSpaceID,
                                              nsAtom* aAttribute,
                                              int32_t aModType) {
  if (nsGkAtoms::linethickness_ == aAttribute) {
    InvalidateFrame();
  }
  return nsMathMLContainerFrame::AttributeChanged(aNameSpaceID, aAttribute,
                                                  aModType);
}

/* virtual */
nsresult nsMathMLmfracFrame::MeasureForWidth(DrawTarget* aDrawTarget,
                                             ReflowOutput& aDesiredSize) {
  return PlaceInternal(aDrawTarget, false, aDesiredSize, true);
}

nscoord nsMathMLmfracFrame::FixInterFrameSpacing(ReflowOutput& aDesiredSize) {
  nscoord gap = nsMathMLContainerFrame::FixInterFrameSpacing(aDesiredSize);
  if (!gap) return 0;

  mLineRect.MoveBy(gap, 0);
  return gap;
}

/* virtual */
nsresult nsMathMLmfracFrame::Place(DrawTarget* aDrawTarget, bool aPlaceOrigin,
                                   ReflowOutput& aDesiredSize) {
  return PlaceInternal(aDrawTarget, aPlaceOrigin, aDesiredSize, false);
}

nsresult nsMathMLmfracFrame::PlaceInternal(DrawTarget* aDrawTarget,
                                           bool aPlaceOrigin,
                                           ReflowOutput& aDesiredSize,
                                           bool aWidthOnly) {
  ////////////////////////////////////
  // Get the children's desired sizes
  nsBoundingMetrics bmNum, bmDen;
  ReflowOutput sizeNum(aDesiredSize.GetWritingMode());
  ReflowOutput sizeDen(aDesiredSize.GetWritingMode());
  nsIFrame* frameDen = nullptr;
  nsIFrame* frameNum = mFrames.FirstChild();
  if (frameNum) frameDen = frameNum->GetNextSibling();
  if (!frameNum || !frameDen || frameDen->GetNextSibling()) {
    // report an error, encourage people to get their markups in order
    if (aPlaceOrigin) {
      ReportChildCountError();
    }
    return PlaceAsMrow(aDrawTarget, aPlaceOrigin, aDesiredSize);
  }
  GetReflowAndBoundingMetricsFor(frameNum, sizeNum, bmNum);
  GetReflowAndBoundingMetricsFor(frameDen, sizeDen, bmDen);

  nsPresContext* presContext = PresContext();
  nscoord onePixel = nsPresContext::CSSPixelsToAppUnits(1);

  float fontSizeInflation = nsLayoutUtils::FontSizeInflationFor(this);
  RefPtr<nsFontMetrics> fm =
      nsLayoutUtils::GetFontMetricsForFrame(this, fontSizeInflation);

  nscoord defaultRuleThickness, axisHeight;
  nscoord oneDevPixel = fm->AppUnitsPerDevPixel();
  RefPtr<gfxFont> mathFont = fm->GetThebesFontGroup()->GetFirstMathFont();
  if (mathFont) {
    defaultRuleThickness = mathFont->MathTable()->Constant(
        gfxMathTable::FractionRuleThickness, oneDevPixel);
  } else {
    GetRuleThickness(aDrawTarget, fm, defaultRuleThickness);
  }
  GetAxisHeight(aDrawTarget, fm, axisHeight);

  bool outermostEmbellished = false;
  if (mEmbellishData.coreFrame) {
    nsEmbellishData parentData;
    GetEmbellishDataFrom(GetParent(), parentData);
    outermostEmbellished = parentData.coreFrame != mEmbellishData.coreFrame;
  }

  // see if the linethickness attribute is there
  nsAutoString value;
  mContent->AsElement()->GetAttr(nsGkAtoms::linethickness_, value);
  mLineThickness =
      CalcLineThickness(presContext, mComputedStyle, value, onePixel,
                        defaultRuleThickness, fontSizeInflation);

  bool displayStyle = StyleFont()->mMathStyle == StyleMathStyle::Normal;

  mLineRect.height = mLineThickness;

  // by default, leave at least one-pixel padding at either end, and add
  // lspace & rspace that may come from <mo> if we are an outermost
  // embellished container (we fetch values from the core since they may use
  // units that depend on style data, and style changes could have occurred
  // in the core since our last visit there)
  nscoord leftSpace = onePixel;
  nscoord rightSpace = onePixel;
  if (outermostEmbellished) {
    const bool isRTL = StyleVisibility()->mDirection == StyleDirection::Rtl;
    nsEmbellishData coreData;
    GetEmbellishDataFrom(mEmbellishData.coreFrame, coreData);
    leftSpace += isRTL ? coreData.trailingSpace : coreData.leadingSpace;
    rightSpace += isRTL ? coreData.leadingSpace : coreData.trailingSpace;
  }

  nscoord actualRuleThickness = mLineThickness;

  //////////////////
  // Get shifts
  nscoord numShift = 0;
  nscoord denShift = 0;

  // Rule 15b, App. G, TeXbook
  nscoord numShift1, numShift2, numShift3;
  nscoord denShift1, denShift2;

  GetNumeratorShifts(fm, numShift1, numShift2, numShift3);
  GetDenominatorShifts(fm, denShift1, denShift2);

  if (0 == actualRuleThickness) {
    numShift = displayStyle ? numShift1 : numShift3;
    denShift = displayStyle ? denShift1 : denShift2;
    if (mathFont) {
      numShift = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::StackTopDisplayStyleShiftUp
                       : gfxMathTable::StackTopShiftUp,
          oneDevPixel);
      denShift = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::StackBottomDisplayStyleShiftDown
                       : gfxMathTable::StackBottomShiftDown,
          oneDevPixel);
    }
  } else {
    numShift = displayStyle ? numShift1 : numShift2;
    denShift = displayStyle ? denShift1 : denShift2;
    if (mathFont) {
      numShift = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::FractionNumeratorDisplayStyleShiftUp
                       : gfxMathTable::FractionNumeratorShiftUp,
          oneDevPixel);
      denShift = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::FractionDenominatorDisplayStyleShiftDown
                       : gfxMathTable::FractionDenominatorShiftDown,
          oneDevPixel);
    }
  }

  if (0 == actualRuleThickness) {
    // Rule 15c, App. G, TeXbook

    // min clearance between numerator and denominator
    nscoord minClearance =
        displayStyle ? 7 * defaultRuleThickness : 3 * defaultRuleThickness;
    if (mathFont) {
      minClearance = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::StackDisplayStyleGapMin
                       : gfxMathTable::StackGapMin,
          oneDevPixel);
    }

    nscoord actualClearance =
        (numShift - bmNum.descent) - (bmDen.ascent - denShift);
    // actualClearance should be >= minClearance
    if (actualClearance < minClearance) {
      nscoord halfGap = (minClearance - actualClearance) / 2;
      numShift += halfGap;
      denShift += halfGap;
    }
  } else {
    // Rule 15d, App. G, TeXbook

    // min clearance between numerator or denominator and middle of bar

    // TeX has a different interpretation of the thickness.
    // Try $a \above10pt b$ to see. Here is what TeX does:
    // minClearance = displayStyle ?
    //   3 * actualRuleThickness : actualRuleThickness;

    // we slightly depart from TeX here. We use the defaultRuleThickness
    // instead of the value coming from the linethickness attribute, i.e., we
    // recover what TeX does if the user hasn't set linethickness. But when
    // the linethickness is set, we avoid the wide gap problem.
    nscoord minClearanceNum = displayStyle ? 3 * defaultRuleThickness
                                           : defaultRuleThickness + onePixel;
    nscoord minClearanceDen = minClearanceNum;
    if (mathFont) {
      minClearanceNum = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::FractionNumDisplayStyleGapMin
                       : gfxMathTable::FractionNumeratorGapMin,
          oneDevPixel);
      minClearanceDen = mathFont->MathTable()->Constant(
          displayStyle ? gfxMathTable::FractionDenomDisplayStyleGapMin
                       : gfxMathTable::FractionDenominatorGapMin,
          oneDevPixel);
    }

    // adjust numShift to maintain minClearanceNum if needed
    nscoord actualClearanceNum =
        (numShift - bmNum.descent) - (axisHeight + actualRuleThickness / 2);
    if (actualClearanceNum < minClearanceNum) {
      numShift += (minClearanceNum - actualClearanceNum);
    }
    // adjust denShift to maintain minClearanceDen if needed
    nscoord actualClearanceDen =
        (axisHeight - actualRuleThickness / 2) - (bmDen.ascent - denShift);
    if (actualClearanceDen < minClearanceDen) {
      denShift += (minClearanceDen - actualClearanceDen);
    }
  }

  //////////////////
  // Place Children

  // XXX Need revisiting the width. TeX uses the exact width
  // e.g. in $$\huge\frac{\displaystyle\int}{i}$$
  nscoord width = std::max(bmNum.width, bmDen.width);
  nscoord dxNum = leftSpace + (width - sizeNum.Width()) / 2;
  nscoord dxDen = leftSpace + (width - sizeDen.Width()) / 2;
  width += leftSpace + rightSpace;

  mBoundingMetrics.rightBearing =
      std::max(dxNum + bmNum.rightBearing, dxDen + bmDen.rightBearing);
  if (mBoundingMetrics.rightBearing < width - rightSpace)
    mBoundingMetrics.rightBearing = width - rightSpace;
  mBoundingMetrics.leftBearing =
      std::min(dxNum + bmNum.leftBearing, dxDen + bmDen.leftBearing);
  if (mBoundingMetrics.leftBearing > leftSpace)
    mBoundingMetrics.leftBearing = leftSpace;
  mBoundingMetrics.ascent = bmNum.ascent + numShift;
  mBoundingMetrics.descent = bmDen.descent + denShift;
  mBoundingMetrics.width = width;

  aDesiredSize.SetBlockStartAscent(sizeNum.BlockStartAscent() + numShift);
  aDesiredSize.Height() = aDesiredSize.BlockStartAscent() + sizeDen.Height() -
                          sizeDen.BlockStartAscent() + denShift;
  aDesiredSize.Width() = mBoundingMetrics.width;
  aDesiredSize.mBoundingMetrics = mBoundingMetrics;

  mReference.x = 0;
  mReference.y = aDesiredSize.BlockStartAscent();

  if (aPlaceOrigin) {
    nscoord dy;
    // place numerator
    dy = 0;
    FinishReflowChild(frameNum, presContext, sizeNum, nullptr, dxNum, dy,
                      ReflowChildFlags::Default);
    // place denominator
    dy = aDesiredSize.Height() - sizeDen.Height();
    FinishReflowChild(frameDen, presContext, sizeDen, nullptr, dxDen, dy,
                      ReflowChildFlags::Default);
    // place the fraction bar - dy is top of bar
    dy = aDesiredSize.BlockStartAscent() -
         (axisHeight + actualRuleThickness / 2);
    mLineRect.SetRect(leftSpace, dy, width - (leftSpace + rightSpace),
                      actualRuleThickness);
  }

  return NS_OK;
}