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 IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either of the GNU General Public License Version 2 or later (the "GPL"),
25 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "nsSVGTextPathFrame.h"
38 #include "nsIDOMSVGTextPathElement.h"
39 #include "nsSVGLength2.h"
40 #include "nsIDOMSVGURIReference.h"
41 #include "nsSVGEffects.h"
42 #include "nsContentUtils.h"
43 #include "nsSVGPathElement.h"
44 #include "nsSVGTextPathElement.h"
46 //----------------------------------------------------------------------
50 NS_NewSVGTextPathFrame(nsIPresShell
* aPresShell
, nsIContent
* aContent
,
51 nsIFrame
* parentFrame
, nsStyleContext
* aContext
)
53 NS_ASSERTION(parentFrame
, "null parent");
54 if (parentFrame
->GetType() != nsGkAtoms::svgTextFrame
) {
55 NS_ERROR("trying to construct an SVGTextPathFrame for an invalid container");
59 nsCOMPtr
<nsIDOMSVGTextPathElement
> textPath
= do_QueryInterface(aContent
);
61 NS_ERROR("Can't create frame! Content is not an SVG textPath");
65 return new (aPresShell
) nsSVGTextPathFrame(aContext
);
69 nsSVGTextPathFrame::GetType() const
71 return nsGkAtoms::svgTextPathFrame
;
75 NS_IMETHODIMP_(already_AddRefed
<nsIDOMSVGLengthList
>)
76 nsSVGTextPathFrame::GetX()
81 NS_IMETHODIMP_(already_AddRefed
<nsIDOMSVGLengthList
>)
82 nsSVGTextPathFrame::GetY()
87 NS_IMETHODIMP_(already_AddRefed
<nsIDOMSVGLengthList
>)
88 nsSVGTextPathFrame::GetDx()
93 NS_IMETHODIMP_(already_AddRefed
<nsIDOMSVGLengthList
>)
94 nsSVGTextPathFrame::GetDy()
99 //----------------------------------------------------------------------
100 // nsSVGTextPathFrame methods:
103 nsSVGTextPathFrame::GetPathFrame()
105 nsSVGTextPathProperty
*property
=
106 static_cast<nsSVGTextPathProperty
*>(GetProperty(nsGkAtoms::href
));
109 nsSVGTextPathElement
*tp
= static_cast<nsSVGTextPathElement
*>(mContent
);
110 const nsString
&href
= tp
->mStringAttributes
[nsSVGTextPathElement::HREF
].GetAnimValue();
111 if (href
.IsEmpty()) {
112 return nsnull
; // no URL
115 nsCOMPtr
<nsIURI
> targetURI
;
116 nsCOMPtr
<nsIURI
> base
= mContent
->GetBaseURI();
117 nsContentUtils::NewURIWithDocumentCharset(getter_AddRefs(targetURI
), href
,
118 mContent
->GetCurrentDoc(), base
);
120 property
= nsSVGEffects::GetTextPathProperty(
121 targetURI
, this, nsGkAtoms::href
);
126 nsIFrame
*result
= property
->GetReferencedFrame();
127 if (!result
|| result
->GetType() != nsGkAtoms::svgPathGeometryFrame
)
133 already_AddRefed
<gfxFlattenedPath
>
134 nsSVGTextPathFrame::GetFlattenedPath()
136 nsIFrame
*path
= GetPathFrame();
137 return path
? GetFlattenedPath(path
) : nsnull
;
140 already_AddRefed
<gfxFlattenedPath
>
141 nsSVGTextPathFrame::GetFlattenedPath(nsIFrame
*path
)
143 NS_PRECONDITION(path
, "Unexpected null path");
144 nsSVGPathGeometryElement
*element
= static_cast<nsSVGPathGeometryElement
*>
145 (path
->GetContent());
147 nsCOMPtr
<nsIDOMSVGMatrix
> localTM
= element
->GetLocalTransformMatrix();
149 return element
->GetFlattenedPath(localTM
);
153 nsSVGTextPathFrame::GetStartOffset()
155 nsSVGTextPathElement
*tp
= static_cast<nsSVGTextPathElement
*>(mContent
);
156 nsSVGLength2
*length
= &tp
->mLengthAttributes
[nsSVGTextPathElement::STARTOFFSET
];
157 float val
= length
->GetAnimValInSpecifiedUnits();
162 if (length
->IsPercentage()) {
163 nsRefPtr
<gfxFlattenedPath
> data
= GetFlattenedPath();
164 return data
? (val
* data
->GetLength() / 100.0) : 0.0;
166 return val
* GetPathScale();
171 nsSVGTextPathFrame::GetPathScale()
173 nsIFrame
*pathFrame
= GetPathFrame();
177 nsSVGPathElement
*path
= static_cast<nsSVGPathElement
*>(pathFrame
->GetContent());
178 float pl
= path
->mPathLength
.GetAnimValue();
183 nsRefPtr
<gfxFlattenedPath
> data
= GetFlattenedPath(pathFrame
);
184 return data
? data
->GetLength() / pl
: 1.0;
187 //----------------------------------------------------------------------
191 nsSVGTextPathFrame::AttributeChanged(PRInt32 aNameSpaceID
,
195 if (aNameSpaceID
== kNameSpaceID_None
&&
196 aAttribute
== nsGkAtoms::startOffset
) {
197 NotifyGlyphMetricsChange();
198 } else if (aNameSpaceID
== kNameSpaceID_XLink
&&
199 aAttribute
== nsGkAtoms::href
) {
200 // Blow away our reference, if any
201 DeleteProperty(nsGkAtoms::href
);
202 NotifyGlyphMetricsChange();