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
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.
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 //----------------------------------------------------------------------
62 NS_NewSVGTextFrame(nsIPresShell
* aPresShell
, nsIContent
* aContent
, nsStyleContext
* aContext
)
64 nsCOMPtr
<nsIDOMSVGTextElement
> text
= do_QueryInterface(aContent
);
66 NS_ERROR("Can't create frame! Content is not an SVG text");
70 return new (aPresShell
) nsSVGTextFrame(aContext
);
73 //----------------------------------------------------------------------
77 nsSVGTextFrame::AttributeChanged(PRInt32 aNameSpaceID
,
81 if (aNameSpaceID
!= kNameSpaceID_None
)
84 if (aAttribute
== nsGkAtoms::transform
) {
85 // transform has changed
87 // make sure our cached transform matrix gets (lazily) updated
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();
103 nsSVGTextFrame::GetType() const
105 return nsGkAtoms::svgTextFrame
;
108 //----------------------------------------------------------------------
109 // nsISVGTextContentMetrics
111 nsSVGTextFrame::GetNumberOfChars(PRInt32
*_retval
)
113 UpdateGlyphPositioning(PR_FALSE
);
115 return nsSVGTextFrameBase::GetNumberOfChars(_retval
);
119 nsSVGTextFrame::GetComputedTextLength(float *_retval
)
121 UpdateGlyphPositioning(PR_FALSE
);
123 return nsSVGTextFrameBase::GetComputedTextLength(_retval
);
127 nsSVGTextFrame::GetSubStringLength(PRUint32 charnum
, PRUint32 nchars
, float *_retval
)
129 UpdateGlyphPositioning(PR_FALSE
);
131 return nsSVGTextFrameBase::GetSubStringLength(charnum
, nchars
, _retval
);
135 nsSVGTextFrame::GetStartPositionOfChar(PRUint32 charnum
, nsIDOMSVGPoint
**_retval
)
137 UpdateGlyphPositioning(PR_FALSE
);
139 return nsSVGTextFrameBase::GetStartPositionOfChar(charnum
, _retval
);
143 nsSVGTextFrame::GetEndPositionOfChar(PRUint32 charnum
, nsIDOMSVGPoint
**_retval
)
145 UpdateGlyphPositioning(PR_FALSE
);
147 return nsSVGTextFrameBase::GetEndPositionOfChar(charnum
, _retval
);
151 nsSVGTextFrame::GetExtentOfChar(PRUint32 charnum
, nsIDOMSVGRect
**_retval
)
153 UpdateGlyphPositioning(PR_FALSE
);
155 return nsSVGTextFrameBase::GetExtentOfChar(charnum
, _retval
);
159 nsSVGTextFrame::GetRotationOfChar(PRUint32 charnum
, float *_retval
)
161 UpdateGlyphPositioning(PR_FALSE
);
163 return nsSVGTextFrameBase::GetRotationOfChar(charnum
, _retval
);
167 nsSVGTextFrame::GetCharNumAtPosition(nsIDOMSVGPoint
*point
, PRInt32
*_retval
)
169 UpdateGlyphPositioning(PR_FALSE
);
171 return nsSVGTextFrameBase::GetCharNumAtPosition(point
, _retval
);
175 //----------------------------------------------------------------------
176 // nsISVGChildFrame methods
179 nsSVGTextFrame::NotifySVGChanged(PRUint32 aFlags
)
181 if (aFlags
& TRANSFORM_CHANGED
) {
182 // make sure our cached transform matrix gets (lazily) updated
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
);
200 nsSVGTextFrame::NotifyRedrawSuspended()
202 mMetricsState
= suspended
;
204 return nsSVGTextFrameBase::NotifyRedrawSuspended();
208 nsSVGTextFrame::NotifyRedrawUnsuspended()
210 mMetricsState
= unsuspended
;
211 UpdateGlyphPositioning(PR_FALSE
);
212 return nsSVGTextFrameBase::NotifyRedrawUnsuspended();
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
);
233 nsSVGTextFrame::UpdateCoveredRegion()
235 UpdateGlyphPositioning(PR_TRUE
);
237 return nsSVGTextFrameBase::UpdateCoveredRegion();
241 nsSVGTextFrame::InitialUpdate()
243 nsresult rv
= nsSVGTextFrameBase::InitialUpdate();
245 UpdateGlyphPositioning(PR_FALSE
);
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
);
271 // get our parent's tm and append local transforms (if any):
272 NS_ASSERTION(mParent
, "null parent");
273 nsSVGContainerFrame
*containerFrame
= static_cast<nsSVGContainerFrame
*>
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();
284 parentTM
->Multiply(localTM
, getter_AddRefs(mCanvasTM
));
286 mCanvasTM
= parentTM
;
289 nsIDOMSVGMatrix
* retval
= mCanvasTM
.get();
290 NS_IF_ADDREF(retval
);
294 //----------------------------------------------------------------------
298 nsSVGTextFrame::NotifyGlyphMetricsChange()
300 mPositioningDirty
= PR_TRUE
;
301 UpdateGlyphPositioning(PR_FALSE
);
305 GetSingleValue(nsISVGGlyphFragmentLeaf
*fragment
,
306 nsIDOMSVGLengthList
*list
, float *val
)
312 list
->GetNumberOfItems(&count
);
315 NS_WARNING("multiple lengths for x/y attributes on <text> elements not implemented yet!");
318 nsCOMPtr
<nsIDOMSVGLength
> length
;
319 list
->GetItem(0, getter_AddRefs(length
));
320 length
->GetValue(val
);
325 nsSVGTextFrame::UpdateGlyphPositioning(PRBool aForceGlobalTransform
)
327 if (mMetricsState
== suspended
|| !mPositioningDirty
)
330 SetWhitespaceHandling();
332 nsISVGGlyphFragmentNode
* node
= GetFirstGlyphFragmentChildNode();
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
341 switch(GetStyleSVGReset()->mDominantBaseline
) {
342 case NS_STYLE_DOMINANT_BASELINE_TEXT_BEFORE_EDGE
:
343 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_TEXT_BEFORE_EDGE
;
345 case NS_STYLE_DOMINANT_BASELINE_TEXT_AFTER_EDGE
:
346 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_TEXT_AFTER_EDGE
;
348 case NS_STYLE_DOMINANT_BASELINE_MIDDLE
:
349 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_MIDDLE
;
351 case NS_STYLE_DOMINANT_BASELINE_CENTRAL
:
352 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_CENTRAL
;
354 case NS_STYLE_DOMINANT_BASELINE_MATHEMATICAL
:
355 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_MATHEMATICAL
;
357 case NS_STYLE_DOMINANT_BASELINE_IDEOGRAPHIC
:
358 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_IDEOGRAPHC
;
360 case NS_STYLE_DOMINANT_BASELINE_HANGING
:
361 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_HANGING
;
363 case NS_STYLE_DOMINANT_BASELINE_AUTO
:
364 case NS_STYLE_DOMINANT_BASELINE_USE_SCRIPT
:
365 case NS_STYLE_DOMINANT_BASELINE_ALPHABETIC
:
367 baseline
= nsISVGGlyphFragmentLeaf::BASELINE_ALPHABETIC
;
371 nsISVGGlyphFragmentLeaf
*fragment
, *firstFragment
;
373 firstFragment
= node
->GetFirstGlyphFragment();
374 if (!firstFragment
) {
381 nsCOMPtr
<nsIDOMSVGLengthList
> list
= GetX();
382 GetSingleValue(firstFragment
, list
, &x
);
385 nsCOMPtr
<nsIDOMSVGLengthList
> list
= GetY();
386 GetSingleValue(firstFragment
, list
, &y
);
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();
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
;
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())
426 if (anchor
== NS_STYLE_TEXT_ANCHOR_MIDDLE
)
427 x
-= chunkLength
/2.0f
;
428 else if (anchor
== NS_STYLE_TEXT_ANCHOR_END
)
431 // set position of each fragment in this chunk:
433 fragment
= firstFragment
;
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
);
452 fragment
= fragment
->GetNextGlyphFragment();
453 if (fragment
&& fragment
->IsAbsolutelyPositioned())
456 firstFragment
= fragment
;