Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / layout / svg / base / src / nsSVGTextPathFrame.cpp
blobbd73a753d8dab1f3c375a645be6f7d654a5a697d
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 IBM Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2005
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
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 //----------------------------------------------------------------------
47 // Implementation
49 nsIFrame*
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");
56 return nsnull;
59 nsCOMPtr<nsIDOMSVGTextPathElement> textPath = do_QueryInterface(aContent);
60 if (!textPath) {
61 NS_ERROR("Can't create frame! Content is not an SVG textPath");
62 return nsnull;
65 return new (aPresShell) nsSVGTextPathFrame(aContext);
68 nsIAtom *
69 nsSVGTextPathFrame::GetType() const
71 return nsGkAtoms::svgTextPathFrame;
75 NS_IMETHODIMP_(already_AddRefed<nsIDOMSVGLengthList>)
76 nsSVGTextPathFrame::GetX()
78 return nsnull;
81 NS_IMETHODIMP_(already_AddRefed<nsIDOMSVGLengthList>)
82 nsSVGTextPathFrame::GetY()
84 return nsnull;
87 NS_IMETHODIMP_(already_AddRefed<nsIDOMSVGLengthList>)
88 nsSVGTextPathFrame::GetDx()
90 return nsnull;
93 NS_IMETHODIMP_(already_AddRefed<nsIDOMSVGLengthList>)
94 nsSVGTextPathFrame::GetDy()
96 return nsnull;
99 //----------------------------------------------------------------------
100 // nsSVGTextPathFrame methods:
102 nsIFrame *
103 nsSVGTextPathFrame::GetPathFrame()
105 nsSVGTextPathProperty *property =
106 static_cast<nsSVGTextPathProperty*>(GetProperty(nsGkAtoms::href));
108 if (!property) {
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);
122 if (!property)
123 return nsnull;
126 nsIFrame *result = property->GetReferencedFrame();
127 if (!result || result->GetType() != nsGkAtoms::svgPathGeometryFrame)
128 return nsnull;
130 return result;
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);
152 gfxFloat
153 nsSVGTextPathFrame::GetStartOffset()
155 nsSVGTextPathElement *tp = static_cast<nsSVGTextPathElement*>(mContent);
156 nsSVGLength2 *length = &tp->mLengthAttributes[nsSVGTextPathElement::STARTOFFSET];
157 float val = length->GetAnimValInSpecifiedUnits();
159 if (val == 0.0f)
160 return 0.0;
162 if (length->IsPercentage()) {
163 nsRefPtr<gfxFlattenedPath> data = GetFlattenedPath();
164 return data ? (val * data->GetLength() / 100.0) : 0.0;
165 } else {
166 return val * GetPathScale();
170 gfxFloat
171 nsSVGTextPathFrame::GetPathScale()
173 nsIFrame *pathFrame = GetPathFrame();
174 if (!pathFrame)
175 return 1.0;
177 nsSVGPathElement *path = static_cast<nsSVGPathElement*>(pathFrame->GetContent());
178 float pl = path->mPathLength.GetAnimValue();
180 if (pl == 0.0f)
181 return 1.0;
183 nsRefPtr<gfxFlattenedPath> data = GetFlattenedPath(pathFrame);
184 return data ? data->GetLength() / pl : 1.0;
187 //----------------------------------------------------------------------
188 // nsIFrame methods
190 NS_IMETHODIMP
191 nsSVGTextPathFrame::AttributeChanged(PRInt32 aNameSpaceID,
192 nsIAtom* aAttribute,
193 PRInt32 aModType)
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();
205 return NS_OK;