Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / svg / base / src / nsSVGTextFrame.cpp
blobc632453165d80535acbdae8924d2989de4be7be8
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is the Mozilla SVG project.
17 * The Initial Developer of the Original Code is
18 * Crocodile Clips Ltd..
19 * Portions created by the Initial Developer are Copyright (C) 2002
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
23 * Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsIDOMSVGTextElement.h"
40 #include "nsSVGTextFrame.h"
41 #include "nsWeakReference.h"
42 #include "nsIDOMSVGLengthList.h"
43 #include "nsIDOMSVGLength.h"
44 #include "nsIDOMSVGAnimatedNumber.h"
45 #include "nsISVGGlyphFragmentNode.h"
46 #include "nsISVGGlyphFragmentLeaf.h"
47 #include "nsSVGOuterSVGFrame.h"
48 #include "nsIDOMSVGRect.h"
49 #include "nsISVGTextContentMetrics.h"
50 #include "nsSVGRect.h"
51 #include "nsSVGMatrix.h"
52 #include "nsGkAtoms.h"
53 #include "nsSVGTextPathFrame.h"
54 #include "nsSVGPathElement.h"
55 #include "nsSVGUtils.h"
56 #include "nsSVGGraphicElement.h"
58 //----------------------------------------------------------------------
59 // Implementation
61 nsIFrame*
62 NS_NewSVGTextFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsStyleContext* aContext)
64 nsCOMPtr<nsIDOMSVGTextElement> text = do_QueryInterface(aContent);
65 if (!text) {
66 NS_ERROR("Can't create frame! Content is not an SVG text");
67 return nsnull;
70 return new (aPresShell) nsSVGTextFrame(aContext);
73 //----------------------------------------------------------------------
74 // nsIFrame methods
76 NS_IMETHODIMP
77 nsSVGTextFrame::AttributeChanged(PRInt32 aNameSpaceID,
78 nsIAtom* aAttribute,
79 PRInt32 aModType)
81 if (aNameSpaceID != kNameSpaceID_None)
82 return NS_OK;
84 if (aAttribute == nsGkAtoms::transform) {
85 // transform has changed
87 // make sure our cached transform matrix gets (lazily) updated
88 mCanvasTM = nsnull;
90 nsSVGUtils::NotifyChildrenOfSVGChange(this, TRANSFORM_CHANGED);
92 } else if (aAttribute == nsGkAtoms::x ||
93 aAttribute == nsGkAtoms::y ||
94 aAttribute == nsGkAtoms::dx ||
95 aAttribute == nsGkAtoms::dy) {
96 NotifyGlyphMetricsChange();
99 return NS_OK;
102 nsIAtom *
103 nsSVGTextFrame::GetType() const
105 return nsGkAtoms::svgTextFrame;
108 //----------------------------------------------------------------------
109 // nsISVGTextContentMetrics
110 NS_IMETHODIMP
111 nsSVGTextFrame::GetNumberOfChars(PRInt32 *_retval)
113 UpdateGlyphPositioning(PR_FALSE);
115 return nsSVGTextFrameBase::GetNumberOfChars(_retval);
118 NS_IMETHODIMP
119 nsSVGTextFrame::GetComputedTextLength(float *_retval)
121 UpdateGlyphPositioning(PR_FALSE);
123 return nsSVGTextFrameBase::GetComputedTextLength(_retval);
126 NS_IMETHODIMP
127 nsSVGTextFrame::GetSubStringLength(PRUint32 charnum, PRUint32 nchars, float *_retval)
129 UpdateGlyphPositioning(PR_FALSE);
131 return nsSVGTextFrameBase::GetSubStringLength(charnum, nchars, _retval);
134 NS_IMETHODIMP
135 nsSVGTextFrame::GetStartPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
137 UpdateGlyphPositioning(PR_FALSE);
139 return nsSVGTextFrameBase::GetStartPositionOfChar(charnum, _retval);
142 NS_IMETHODIMP
143 nsSVGTextFrame::GetEndPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
145 UpdateGlyphPositioning(PR_FALSE);
147 return nsSVGTextFrameBase::GetEndPositionOfChar(charnum, _retval);
150 NS_IMETHODIMP
151 nsSVGTextFrame::GetExtentOfChar(PRUint32 charnum, nsIDOMSVGRect **_retval)
153 UpdateGlyphPositioning(PR_FALSE);
155 return nsSVGTextFrameBase::GetExtentOfChar(charnum, _retval);
158 NS_IMETHODIMP
159 nsSVGTextFrame::GetRotationOfChar(PRUint32 charnum, float *_retval)
161 UpdateGlyphPositioning(PR_FALSE);
163 return nsSVGTextFrameBase::GetRotationOfChar(charnum, _retval);
166 NS_IMETHODIMP
167 nsSVGTextFrame::GetCharNumAtPosition(nsIDOMSVGPoint *point, PRInt32 *_retval)
169 UpdateGlyphPositioning(PR_FALSE);
171 return nsSVGTextFrameBase::GetCharNumAtPosition(point, _retval);
175 //----------------------------------------------------------------------
176 // nsISVGChildFrame methods
178 void
179 nsSVGTextFrame::NotifySVGChanged(PRUint32 aFlags)
181 if (aFlags & TRANSFORM_CHANGED) {
182 // make sure our cached transform matrix gets (lazily) updated
183 mCanvasTM = nsnull;
186 if (aFlags & COORD_CONTEXT_CHANGED) {
187 // If we are positioned using percentage values we need to update our
188 // position whenever our viewport's dimensions change.
190 // XXX We could check here whether the text frame or any of its children
191 // have any percentage co-ordinates and only update if they don't. This
192 // may not be worth it as we might need to check each glyph
193 NotifyGlyphMetricsChange();
196 nsSVGTextFrameBase::NotifySVGChanged(aFlags);
199 NS_IMETHODIMP
200 nsSVGTextFrame::NotifyRedrawSuspended()
202 mMetricsState = suspended;
204 return nsSVGTextFrameBase::NotifyRedrawSuspended();
207 NS_IMETHODIMP
208 nsSVGTextFrame::NotifyRedrawUnsuspended()
210 mMetricsState = unsuspended;
211 UpdateGlyphPositioning(PR_FALSE);
212 return nsSVGTextFrameBase::NotifyRedrawUnsuspended();
215 NS_IMETHODIMP
216 nsSVGTextFrame::PaintSVG(nsSVGRenderState* aContext,
217 const nsIntRect *aDirtyRect)
219 UpdateGlyphPositioning(PR_TRUE);
221 return nsSVGTextFrameBase::PaintSVG(aContext, aDirtyRect);
224 NS_IMETHODIMP_(nsIFrame*)
225 nsSVGTextFrame::GetFrameForPoint(const nsPoint &aPoint)
227 UpdateGlyphPositioning(PR_TRUE);
229 return nsSVGTextFrameBase::GetFrameForPoint(aPoint);
232 NS_IMETHODIMP
233 nsSVGTextFrame::UpdateCoveredRegion()
235 UpdateGlyphPositioning(PR_TRUE);
237 return nsSVGTextFrameBase::UpdateCoveredRegion();
240 NS_IMETHODIMP
241 nsSVGTextFrame::InitialUpdate()
243 nsresult rv = nsSVGTextFrameBase::InitialUpdate();
245 UpdateGlyphPositioning(PR_FALSE);
247 return rv;
250 NS_IMETHODIMP
251 nsSVGTextFrame::GetBBox(nsIDOMSVGRect **_retval)
253 UpdateGlyphPositioning(PR_TRUE);
255 return nsSVGTextFrameBase::GetBBox(_retval);
258 //----------------------------------------------------------------------
259 // nsSVGContainerFrame methods:
261 already_AddRefed<nsIDOMSVGMatrix>
262 nsSVGTextFrame::GetCanvasTM()
264 if (!GetMatrixPropagation()) {
265 nsIDOMSVGMatrix *retval;
266 NS_NewSVGMatrix(&retval);
267 return retval;
270 if (!mCanvasTM) {
271 // get our parent's tm and append local transforms (if any):
272 NS_ASSERTION(mParent, "null parent");
273 nsSVGContainerFrame *containerFrame = static_cast<nsSVGContainerFrame*>
274 (mParent);
275 nsCOMPtr<nsIDOMSVGMatrix> parentTM = containerFrame->GetCanvasTM();
276 NS_ASSERTION(parentTM, "null TM");
278 // got the parent tm, now check for local tm:
279 nsSVGGraphicElement *element =
280 static_cast<nsSVGGraphicElement*>(mContent);
281 nsCOMPtr<nsIDOMSVGMatrix> localTM = element->GetLocalTransformMatrix();
283 if (localTM)
284 parentTM->Multiply(localTM, getter_AddRefs(mCanvasTM));
285 else
286 mCanvasTM = parentTM;
289 nsIDOMSVGMatrix* retval = mCanvasTM.get();
290 NS_IF_ADDREF(retval);
291 return retval;
294 //----------------------------------------------------------------------
297 void
298 nsSVGTextFrame::NotifyGlyphMetricsChange()
300 mPositioningDirty = PR_TRUE;
301 UpdateGlyphPositioning(PR_FALSE);
304 static void
305 GetSingleValue(nsISVGGlyphFragmentLeaf *fragment,
306 nsIDOMSVGLengthList *list, float *val)
308 if (!list)
309 return;
311 PRUint32 count = 0;
312 list->GetNumberOfItems(&count);
313 #ifdef DEBUG
314 if (count > 1)
315 NS_WARNING("multiple lengths for x/y attributes on <text> elements not implemented yet!");
316 #endif
317 if (count) {
318 nsCOMPtr<nsIDOMSVGLength> length;
319 list->GetItem(0, getter_AddRefs(length));
320 length->GetValue(val);
324 void
325 nsSVGTextFrame::UpdateGlyphPositioning(PRBool aForceGlobalTransform)
327 if (mMetricsState == suspended || !mPositioningDirty)
328 return;
330 SetWhitespaceHandling();
332 nsISVGGlyphFragmentNode* node = GetFirstGlyphFragmentChildNode();
333 if (!node) return;
335 mPositioningDirty = PR_FALSE;
337 // we'll align every fragment in this chunk on the dominant-baseline:
338 // XXX should actually inspect 'alignment-baseline' for each fragment
340 PRUint8 baseline;
341 switch(GetStyleSVGReset()->mDominantBaseline) {
342 case NS_STYLE_DOMINANT_BASELINE_TEXT_BEFORE_EDGE:
343 baseline = nsISVGGlyphFragmentLeaf::BASELINE_TEXT_BEFORE_EDGE;
344 break;
345 case NS_STYLE_DOMINANT_BASELINE_TEXT_AFTER_EDGE:
346 baseline = nsISVGGlyphFragmentLeaf::BASELINE_TEXT_AFTER_EDGE;
347 break;
348 case NS_STYLE_DOMINANT_BASELINE_MIDDLE:
349 baseline = nsISVGGlyphFragmentLeaf::BASELINE_MIDDLE;
350 break;
351 case NS_STYLE_DOMINANT_BASELINE_CENTRAL:
352 baseline = nsISVGGlyphFragmentLeaf::BASELINE_CENTRAL;
353 break;
354 case NS_STYLE_DOMINANT_BASELINE_MATHEMATICAL:
355 baseline = nsISVGGlyphFragmentLeaf::BASELINE_MATHEMATICAL;
356 break;
357 case NS_STYLE_DOMINANT_BASELINE_IDEOGRAPHIC:
358 baseline = nsISVGGlyphFragmentLeaf::BASELINE_IDEOGRAPHC;
359 break;
360 case NS_STYLE_DOMINANT_BASELINE_HANGING:
361 baseline = nsISVGGlyphFragmentLeaf::BASELINE_HANGING;
362 break;
363 case NS_STYLE_DOMINANT_BASELINE_AUTO:
364 case NS_STYLE_DOMINANT_BASELINE_USE_SCRIPT:
365 case NS_STYLE_DOMINANT_BASELINE_ALPHABETIC:
366 default:
367 baseline = nsISVGGlyphFragmentLeaf::BASELINE_ALPHABETIC;
368 break;
371 nsISVGGlyphFragmentLeaf *fragment, *firstFragment;
373 firstFragment = node->GetFirstGlyphFragment();
374 if (!firstFragment) {
375 return;
378 float x = 0, y = 0;
381 nsCOMPtr<nsIDOMSVGLengthList> list = GetX();
382 GetSingleValue(firstFragment, list, &x);
385 nsCOMPtr<nsIDOMSVGLengthList> list = GetY();
386 GetSingleValue(firstFragment, list, &y);
389 // loop over chunks
390 while (firstFragment) {
392 nsCOMPtr<nsIDOMSVGLengthList> list = firstFragment->GetX();
393 GetSingleValue(firstFragment, list, &x);
396 nsCOMPtr<nsIDOMSVGLengthList> list = firstFragment->GetY();
397 GetSingleValue(firstFragment, list, &y);
400 // check for startOffset on textPath
401 nsSVGTextPathFrame *textPath = firstFragment->FindTextPathParent();
402 if (textPath) {
403 x = textPath->GetStartOffset();
406 // determine x offset based on text_anchor:
408 PRUint8 anchor = firstFragment->GetTextAnchor();
410 float chunkLength = 0.0f;
411 if (anchor != NS_STYLE_TEXT_ANCHOR_START) {
412 // need to get the total chunk length
414 fragment = firstFragment;
415 while (fragment) {
416 float dx = 0.0f;
417 nsCOMPtr<nsIDOMSVGLengthList> list = fragment->GetDx();
418 GetSingleValue(fragment, list, &dx);
419 chunkLength += dx + fragment->GetAdvance(aForceGlobalTransform);
420 fragment = fragment->GetNextGlyphFragment();
421 if (fragment && fragment->IsAbsolutelyPositioned())
422 break;
426 if (anchor == NS_STYLE_TEXT_ANCHOR_MIDDLE)
427 x -= chunkLength/2.0f;
428 else if (anchor == NS_STYLE_TEXT_ANCHOR_END)
429 x -= chunkLength;
431 // set position of each fragment in this chunk:
433 fragment = firstFragment;
434 while (fragment) {
436 float dx = 0.0f, dy = 0.0f;
438 nsCOMPtr<nsIDOMSVGLengthList> list = fragment->GetDx();
439 GetSingleValue(fragment, list, &dx);
442 nsCOMPtr<nsIDOMSVGLengthList> list = fragment->GetDy();
443 GetSingleValue(fragment, list, &dy);
446 float baseline_offset =
447 fragment->GetBaselineOffset(baseline, aForceGlobalTransform);
448 fragment->SetGlyphPosition(x + dx, y + dy - baseline_offset);
450 x += dx + fragment->GetAdvance(aForceGlobalTransform);
451 y += dy;
452 fragment = fragment->GetNextGlyphFragment();
453 if (fragment && fragment->IsAbsolutelyPositioned())
454 break;
456 firstFragment = fragment;