~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/layout/mathml/base/src/nsMathMLmrootFrame.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is Mozilla MathML Project.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is The University Of 
 
15
 * Queensland.  Portions created by The University Of Queensland are
 
16
 * Copyright (C) 1999 The University Of Queensland.  All Rights Reserved.
 
17
 * 
 
18
 * Contributor(s): 
 
19
 *   Roger B. Sidje <rbs@maths.uq.edu.au>
 
20
 *   David J. Fiddes <D.J.Fiddes@hw.ac.uk>
 
21
 *   Vilya Harvey <vilya@nag.co.uk>
 
22
 *   Shyjan Mahamud <mahamud@cs.cmu.edu>
 
23
 */
 
24
 
 
25
 
 
26
#include "nsCOMPtr.h"
 
27
#include "nsFrame.h"
 
28
#include "nsIPresContext.h"
 
29
#include "nsUnitConversion.h"
 
30
#include "nsStyleContext.h"
 
31
#include "nsStyleConsts.h"
 
32
#include "nsIRenderingContext.h"
 
33
#include "nsIFontMetrics.h"
 
34
 
 
35
#include "nsMathMLmrootFrame.h"
 
36
 
 
37
//
 
38
// <msqrt> and <mroot> -- form a radical - implementation
 
39
//
 
40
 
 
41
//NOTE:
 
42
//  The code assumes that TeX fonts are picked.
 
43
//  There is no fall-back to draw the branches of the sqrt explicitly
 
44
//  in the case where TeX fonts are not there. In general, there are no
 
45
//  fall-back(s) in MathML when some (freely-downloadable) fonts are missing.
 
46
//  Otherwise, this will add much work and unnecessary complexity to the core
 
47
//  MathML  engine. Assuming that authors have the free fonts is part of the
 
48
//  deal. We are not responsible for cases of misconfigurations out there.
 
49
 
 
50
// additional style context to be used by our MathMLChar.
 
51
#define NS_SQR_CHAR_STYLE_CONTEXT_INDEX   0
 
52
 
 
53
static const PRUnichar kSqrChar = PRUnichar(0x221A);
 
54
 
 
55
nsresult
 
56
NS_NewMathMLmrootFrame(nsIPresShell* aPresShell, nsIFrame** aNewFrame)
 
57
{
 
58
  NS_PRECONDITION(aNewFrame, "null OUT ptr");
 
59
  if (nsnull == aNewFrame) {
 
60
    return NS_ERROR_NULL_POINTER;
 
61
  }
 
62
  nsMathMLmrootFrame* it = new (aPresShell) nsMathMLmrootFrame;
 
63
  if (nsnull == it) {
 
64
    return NS_ERROR_OUT_OF_MEMORY;
 
65
  }
 
66
  *aNewFrame = it;
 
67
  return NS_OK;
 
68
}
 
69
 
 
70
nsMathMLmrootFrame::nsMathMLmrootFrame() :
 
71
  mSqrChar(),
 
72
  mBarRect()
 
73
{
 
74
}
 
75
 
 
76
nsMathMLmrootFrame::~nsMathMLmrootFrame()
 
77
{
 
78
}
 
79
 
 
80
NS_IMETHODIMP
 
81
nsMathMLmrootFrame::Init(nsIPresContext*  aPresContext,
 
82
                         nsIContent*      aContent,
 
83
                         nsIFrame*        aParent,
 
84
                         nsStyleContext*  aContext,
 
85
                         nsIFrame*        aPrevInFlow)
 
86
{
 
87
  nsresult rv = nsMathMLContainerFrame::Init(aPresContext, aContent, aParent,
 
88
                                             aContext, aPrevInFlow);
 
89
 
 
90
  // No need to tract the style context given to our MathML char. 
 
91
  // The Style System will use Get/SetAdditionalStyleContext() to keep it
 
92
  // up-to-date if dynamic changes arise.
 
93
  nsAutoString sqrChar; sqrChar.Assign(kSqrChar);
 
94
  mSqrChar.SetData(aPresContext, sqrChar);
 
95
  ResolveMathMLCharStyle(aPresContext, mContent, mStyleContext, &mSqrChar, PR_TRUE);
 
96
 
 
97
  return rv;
 
98
}
 
99
 
 
100
NS_IMETHODIMP
 
101
nsMathMLmrootFrame::TransmitAutomaticData(nsIPresContext* aPresContext)
 
102
{
 
103
  // 1. The REC says:
 
104
  //    The <mroot> element increments scriptlevel by 2, and sets displaystyle to
 
105
  //    "false", within index, but leaves both attributes unchanged within base.
 
106
  // 2. The TeXbook (Ch 17. p.141) says \sqrt is compressed
 
107
  UpdatePresentationDataFromChildAt(aPresContext, 1, 1, 2,
 
108
    ~NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED,
 
109
     NS_MATHML_DISPLAYSTYLE | NS_MATHML_COMPRESSED);
 
110
  UpdatePresentationDataFromChildAt(aPresContext, 0, 0, 0,
 
111
     NS_MATHML_COMPRESSED, NS_MATHML_COMPRESSED);
 
112
 
 
113
  return NS_OK;
 
114
}
 
115
 
 
116
NS_IMETHODIMP
 
117
nsMathMLmrootFrame::Paint(nsIPresContext*      aPresContext,
 
118
                          nsIRenderingContext& aRenderingContext,
 
119
                          const nsRect&        aDirtyRect,
 
120
                          nsFramePaintLayer    aWhichLayer,
 
121
                          PRUint32             aFlags)
 
122
{
 
123
  /////////////
 
124
  // paint the content we are square-rooting
 
125
  nsresult rv = nsMathMLContainerFrame::Paint(aPresContext, aRenderingContext, 
 
126
                                              aDirtyRect, aWhichLayer);
 
127
  /////////////
 
128
  // paint the sqrt symbol
 
129
  if (!NS_MATHML_HAS_ERROR(mPresentationData.flags)) {
 
130
    mSqrChar.Paint(aPresContext, aRenderingContext,
 
131
                   aDirtyRect, aWhichLayer, this);
 
132
 
 
133
    if (NS_FRAME_PAINT_LAYER_FOREGROUND == aWhichLayer && !mBarRect.IsEmpty()) {
 
134
      // paint the overline bar
 
135
      const nsStyleColor* color = GetStyleColor();
 
136
      aRenderingContext.SetColor(color->mColor);
 
137
      aRenderingContext.FillRect(mBarRect);
 
138
    }
 
139
 
 
140
#if defined(NS_DEBUG) && defined(SHOW_BOUNDING_BOX)
 
141
    // for visual debug
 
142
    if (NS_MATHML_PAINT_BOUNDING_METRICS(mPresentationData.flags)) {
 
143
      nsRect rect;
 
144
      mSqrChar.GetRect(rect);
 
145
 
 
146
      nsBoundingMetrics bm;
 
147
      mSqrChar.GetBoundingMetrics(bm);
 
148
 
 
149
      aRenderingContext.SetColor(NS_RGB(255,0,0));
 
150
      nscoord x = rect.x + bm.leftBearing;
 
151
      nscoord y = rect.y;
 
152
      nscoord w = bm.rightBearing - bm.leftBearing;
 
153
      nscoord h = bm.ascent + bm.descent;
 
154
      aRenderingContext.DrawRect(x,y,w,h);
 
155
    }
 
156
#endif
 
157
  }
 
158
 
 
159
  return rv;
 
160
}
 
161
 
 
162
NS_IMETHODIMP
 
163
nsMathMLmrootFrame::Reflow(nsIPresContext*          aPresContext,
 
164
                           nsHTMLReflowMetrics&     aDesiredSize,
 
165
                           const nsHTMLReflowState& aReflowState,
 
166
                           nsReflowStatus&          aStatus)
 
167
{
 
168
  nsresult rv = NS_OK;
 
169
  // ask our children to compute their bounding metrics 
 
170
  nsHTMLReflowMetrics childDesiredSize(aDesiredSize.mComputeMEW,
 
171
                      aDesiredSize.mFlags | NS_REFLOW_CALC_BOUNDING_METRICS);
 
172
  nsSize availSize(aReflowState.mComputedWidth, aReflowState.mComputedHeight);
 
173
  nsReflowStatus childStatus;
 
174
 
 
175
  aDesiredSize.width = aDesiredSize.height = 0;
 
176
  aDesiredSize.ascent = aDesiredSize.descent = 0;
 
177
 
 
178
  nsBoundingMetrics bmSqr, bmBase, bmIndex;
 
179
  nsIRenderingContext& renderingContext = *aReflowState.rendContext;
 
180
 
 
181
  //////////////////
 
182
  // Reflow Children
 
183
 
 
184
  PRInt32 count = 0;
 
185
  nsIFrame* baseFrame = nsnull;
 
186
  nsIFrame* indexFrame = nsnull;
 
187
  nsHTMLReflowMetrics baseSize(nsnull);
 
188
  nsHTMLReflowMetrics indexSize(nsnull);
 
189
  nsIFrame* childFrame = mFrames.FirstChild();
 
190
  while (childFrame) {
 
191
    nsHTMLReflowState childReflowState(aPresContext, aReflowState,
 
192
                                       childFrame, availSize);
 
193
    rv = ReflowChild(childFrame, aPresContext,
 
194
                     childDesiredSize, childReflowState, childStatus);
 
195
    //NS_ASSERTION(NS_FRAME_IS_COMPLETE(childStatus), "bad status");
 
196
    if (NS_FAILED(rv)) return rv;
 
197
    if (0 == count) {
 
198
      // base 
 
199
      baseFrame = childFrame;
 
200
      baseSize = childDesiredSize;
 
201
      bmBase = childDesiredSize.mBoundingMetrics;
 
202
    }
 
203
    else if (1 == count) {
 
204
      // index
 
205
      indexFrame = childFrame;
 
206
      indexSize = childDesiredSize;
 
207
      bmIndex = childDesiredSize.mBoundingMetrics;
 
208
    }
 
209
    count++;
 
210
    childFrame = childFrame->GetNextSibling();
 
211
  }
 
212
  if (aDesiredSize.mComputeMEW) {
 
213
    aDesiredSize.mMaxElementWidth = childDesiredSize.mMaxElementWidth;
 
214
  }
 
215
  if (2 != count) {
 
216
    // report an error, encourage people to get their markups in order
 
217
    NS_WARNING("invalid markup");
 
218
    return ReflowError(aPresContext, renderingContext, aDesiredSize);
 
219
  }
 
220
 
 
221
  ////////////
 
222
  // Prepare the radical symbol and the overline bar
 
223
 
 
224
  renderingContext.SetFont(GetStyleFont()->mFont, nsnull);
 
225
  nsCOMPtr<nsIFontMetrics> fm;
 
226
  renderingContext.GetFontMetrics(*getter_AddRefs(fm));
 
227
 
 
228
  nscoord ruleThickness, leading, em;
 
229
  GetRuleThickness(renderingContext, fm, ruleThickness);
 
230
 
 
231
  nsBoundingMetrics bmOne;
 
232
  renderingContext.GetBoundingMetrics(NS_LITERAL_STRING("1").get(), 1, bmOne);
 
233
 
 
234
  // get the leading to be left at the top of the resulting frame
 
235
  // this seems more reliable than using fm->GetLeading() on suspicious fonts               
 
236
  GetEmHeight(fm, em);
 
237
  leading = nscoord(0.2f * em); 
 
238
 
 
239
  // Rule 11, App. G, TeXbook
 
240
  // psi = clearance between rule and content
 
241
  nscoord phi = 0, psi = 0;
 
242
  if (NS_MATHML_IS_DISPLAYSTYLE(mPresentationData.flags))
 
243
    fm->GetXHeight(phi);
 
244
  else
 
245
    phi = ruleThickness;
 
246
  psi = ruleThickness + phi/4;
 
247
 
 
248
  // built-in: adjust clearance psi to emulate \mathstrut using '1' (TexBook, p.131)
 
249
  if (bmOne.ascent > bmBase.ascent)
 
250
    psi += bmOne.ascent - bmBase.ascent;
 
251
 
 
252
  // Stretch the radical symbol to the appropriate height if it is not big enough.
 
253
  nsBoundingMetrics contSize = bmBase;
 
254
  contSize.descent = bmBase.ascent + bmBase.descent + psi;
 
255
  contSize.ascent = ruleThickness;
 
256
 
 
257
  // height(radical) should be >= height(base) + psi + ruleThickness
 
258
  nsBoundingMetrics radicalSize;
 
259
  mSqrChar.Stretch(aPresContext, renderingContext,
 
260
                   NS_STRETCH_DIRECTION_VERTICAL, 
 
261
                   contSize, radicalSize,
 
262
                   NS_STRETCH_LARGER);
 
263
  // radicalSize have changed at this point, and should match with
 
264
  // the bounding metrics of the char
 
265
  mSqrChar.GetBoundingMetrics(bmSqr);
 
266
 
 
267
  // According to TeX, the ascent of the returned radical should be
 
268
  // the thickness of the overline
 
269
  ruleThickness = bmSqr.ascent;
 
270
  // make sure that the rule appears on on screen
 
271
  float p2t;
 
272
  aPresContext->GetScaledPixelsToTwips(&p2t);
 
273
  nscoord onePixel = NSIntPixelsToTwips(1, p2t);
 
274
  if (ruleThickness < onePixel) {
 
275
    ruleThickness = onePixel;
 
276
  }
 
277
 
 
278
  // adjust clearance psi to get an exact number of pixels -- this
 
279
  // gives a nicer & uniform look on stacked radicals (bug 130282)
 
280
  nscoord delta = psi % onePixel;
 
281
  if (delta)
 
282
    psi += onePixel - delta; // round up
 
283
 
 
284
  // Update the desired size for the container (like msqrt, index is not yet included)
 
285
  // the baseline will be that of the base.
 
286
  mBoundingMetrics.ascent = bmBase.ascent + psi + ruleThickness;
 
287
  mBoundingMetrics.descent = 
 
288
    PR_MAX(bmBase.descent, (bmSqr.descent - (bmBase.ascent + psi)));
 
289
  mBoundingMetrics.width = bmSqr.width + bmBase.width;
 
290
  mBoundingMetrics.leftBearing = bmSqr.leftBearing;
 
291
  mBoundingMetrics.rightBearing = bmSqr.width + 
 
292
    PR_MAX(bmBase.width, bmBase.rightBearing); // take also care of the rule
 
293
 
 
294
  aDesiredSize.ascent = mBoundingMetrics.ascent + leading;
 
295
  aDesiredSize.descent =
 
296
    PR_MAX(baseSize.descent, (mBoundingMetrics.descent + ruleThickness));
 
297
  aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
 
298
  aDesiredSize.width = mBoundingMetrics.width;
 
299
 
 
300
  /////////////
 
301
  // Re-adjust the desired size to include the index.
 
302
  
 
303
  // the index is raised by some fraction of the height
 
304
  // of the radical, see \mroot macro in App. B, TexBook
 
305
  nscoord raiseIndexDelta = NSToCoordRound(0.6f * (bmSqr.ascent + bmSqr.descent));
 
306
  nscoord indexRaisedAscent = mBoundingMetrics.ascent // top of radical 
 
307
    - (bmSqr.ascent + bmSqr.descent) // to bottom of radical
 
308
    + raiseIndexDelta + bmIndex.ascent + bmIndex.descent; // to top of raised index
 
309
 
 
310
  nscoord indexClearance = 0;
 
311
  if (mBoundingMetrics.ascent < indexRaisedAscent) {
 
312
    indexClearance = 
 
313
      indexRaisedAscent - mBoundingMetrics.ascent; // excess gap introduced by a tall index 
 
314
    mBoundingMetrics.ascent = indexRaisedAscent;
 
315
    aDesiredSize.ascent = mBoundingMetrics.ascent + leading;
 
316
    aDesiredSize.height = aDesiredSize.ascent + aDesiredSize.descent;
 
317
  }
 
318
 
 
319
  // the index is tucked in closer to the radical while making sure
 
320
  // that the kern does not make the index and radical collide
 
321
  nscoord dxIndex, dxSqr, dx, dy;
 
322
  nscoord xHeight = 0;
 
323
  fm->GetXHeight(xHeight);
 
324
  nscoord indexRadicalKern = NSToCoordRound(1.35f * xHeight);
 
325
  if (indexRadicalKern > bmIndex.width) {
 
326
    dxIndex = indexRadicalKern - bmIndex.width;
 
327
    dxSqr = 0;
 
328
  }
 
329
  else {
 
330
    dxIndex = 0;
 
331
    dxSqr = bmIndex.width - indexRadicalKern;
 
332
  }
 
333
  // avoid collision by leaving a minimun space between index and radical
 
334
  nscoord minimumClearance = bmSqr.width/2;
 
335
  if (dxIndex + bmIndex.width + minimumClearance > dxSqr + bmSqr.width) {
 
336
    if (bmIndex.width + minimumClearance < bmSqr.width) {
 
337
      dxIndex = bmSqr.width - (bmIndex.width + minimumClearance);
 
338
      dxSqr = 0;
 
339
    }
 
340
    else {
 
341
      dxIndex = 0;
 
342
      dxSqr = (bmIndex.width + minimumClearance) - bmSqr.width;
 
343
    }
 
344
  }
 
345
 
 
346
  // place the index
 
347
  dx = dxIndex;
 
348
  dy = aDesiredSize.ascent - (indexRaisedAscent + indexSize.ascent - bmIndex.ascent);
 
349
  FinishReflowChild(indexFrame, aPresContext, nsnull, indexSize, dx, dy, 0);
 
350
 
 
351
  // place the radical symbol and the radical bar
 
352
  dx = dxSqr;
 
353
  dy = indexClearance + leading; // leave a leading at the top
 
354
  mSqrChar.SetRect(nsRect(dx, dy, bmSqr.width, bmSqr.ascent + bmSqr.descent));
 
355
  dx += bmSqr.width;
 
356
  mBarRect.SetRect(dx, dy, bmBase.width, ruleThickness);
 
357
 
 
358
  // place the base
 
359
  dy = aDesiredSize.ascent - baseSize.ascent;
 
360
  FinishReflowChild(baseFrame, aPresContext, nsnull, baseSize, dx, dy, 0);
 
361
 
 
362
  mReference.x = 0;
 
363
  mReference.y = aDesiredSize.ascent;
 
364
 
 
365
  mBoundingMetrics.width = dx + bmBase.width;
 
366
  mBoundingMetrics.leftBearing = 
 
367
    PR_MIN(dxIndex + bmIndex.leftBearing, dxSqr + bmSqr.leftBearing);
 
368
  mBoundingMetrics.rightBearing = dx +
 
369
    PR_MAX(bmBase.width, bmBase.rightBearing);
 
370
 
 
371
  aDesiredSize.width = mBoundingMetrics.width;
 
372
  aDesiredSize.mBoundingMetrics = mBoundingMetrics;
 
373
 
 
374
  if (aDesiredSize.mComputeMEW) {
 
375
    aDesiredSize.mMaxElementWidth = aDesiredSize.width;
 
376
  }
 
377
  aStatus = NS_FRAME_COMPLETE;
 
378
  NS_FRAME_SET_TRUNCATION(aStatus, aReflowState, aDesiredSize);
 
379
  return NS_OK;
 
380
}
 
381
 
 
382
 
 
383
// ----------------------
 
384
// the Style System will use these to pass the proper style context to our MathMLChar
 
385
nsStyleContext*
 
386
nsMathMLmrootFrame::GetAdditionalStyleContext(PRInt32 aIndex) const
 
387
{
 
388
  switch (aIndex) {
 
389
  case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
 
390
    return mSqrChar.GetStyleContext();
 
391
    break;
 
392
  default:
 
393
    return nsnull;
 
394
  }
 
395
}
 
396
 
 
397
void
 
398
nsMathMLmrootFrame::SetAdditionalStyleContext(PRInt32          aIndex, 
 
399
                                              nsStyleContext*  aStyleContext)
 
400
{
 
401
  switch (aIndex) {
 
402
  case NS_SQR_CHAR_STYLE_CONTEXT_INDEX:
 
403
    mSqrChar.SetStyleContext(aStyleContext);
 
404
    break;
 
405
  }
 
406
}