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
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with 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 the GNU General Public License Version 2 or later (the "GPL"), or
27
* 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 NPL, 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 "nsSVGGraphicElement.h"
40
#include "nsSVGAtoms.h"
41
#include "nsIDOMSVGTextElement.h"
43
#include "nsSVGAnimatedLengthList.h"
44
#include "nsSVGLengthList.h"
45
#include "nsISVGSVGElement.h"
46
#include "nsISVGViewportAxis.h"
47
#include "nsISVGViewportRect.h"
48
#include "nsISVGTextContentMetrics.h"
49
#include "nsIPresShell.h"
51
#include "nsIDocument.h"
53
typedef nsSVGGraphicElement nsSVGTextElementBase;
55
class nsSVGTextElement : public nsSVGTextElementBase,
56
public nsIDOMSVGTextElement // : nsIDOMSVGTextPositioningElement
57
// : nsIDOMSVGTextContentElement
60
friend nsresult NS_NewSVGTextElement(nsIContent **aResult,
61
nsINodeInfo *aNodeInfo);
63
virtual ~nsSVGTextElement();
64
nsresult Init(nsINodeInfo* aNodeInfo);
69
NS_DECL_ISUPPORTS_INHERITED
70
NS_DECL_NSIDOMSVGTEXTELEMENT
71
NS_DECL_NSIDOMSVGTEXTPOSITIONINGELEMENT
72
NS_DECL_NSIDOMSVGTEXTCONTENTELEMENT
74
// xxx If xpcom allowed virtual inheritance we wouldn't need to
76
NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsSVGTextElementBase::)
77
NS_FORWARD_NSIDOMELEMENT(nsSVGTextElementBase::)
78
NS_FORWARD_NSIDOMSVGELEMENT(nsSVGTextElementBase::)
80
// nsIStyledContent interface
81
NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
83
// nsISVGContent specializations:
84
virtual void ParentChainChanged();
88
already_AddRefed<nsISVGTextContentMetrics> GetTextContentMetrics();
90
// nsIDOMSVGTextPositioning properties:
91
nsCOMPtr<nsIDOMSVGAnimatedLengthList> mX;
92
nsCOMPtr<nsIDOMSVGAnimatedLengthList> mY;
93
nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdX;
94
nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdY;
99
nsresult NS_NewSVGTextElement(nsIContent **aResult, nsINodeInfo *aNodeInfo)
102
nsSVGTextElement* it = new nsSVGTextElement();
104
if (!it) return NS_ERROR_OUT_OF_MEMORY;
107
nsresult rv = it->Init(aNodeInfo);
119
//----------------------------------------------------------------------
120
// nsISupports methods
122
NS_IMPL_ADDREF_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
123
NS_IMPL_RELEASE_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
125
NS_INTERFACE_MAP_BEGIN(nsSVGTextElement)
126
NS_INTERFACE_MAP_ENTRY(nsIDOMNode)
127
NS_INTERFACE_MAP_ENTRY(nsIDOMElement)
128
NS_INTERFACE_MAP_ENTRY(nsIDOMSVGElement)
129
NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextElement)
130
NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextPositioningElement)
131
NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextContentElement)
132
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGTextElement)
133
NS_INTERFACE_MAP_END_INHERITING(nsSVGTextElementBase)
135
//----------------------------------------------------------------------
138
nsSVGTextElement::nsSVGTextElement()
143
nsSVGTextElement::~nsSVGTextElement()
149
nsSVGTextElement::Init(nsINodeInfo* aNodeInfo)
151
nsresult rv = nsSVGTextElementBase::Init(aNodeInfo);
152
NS_ENSURE_SUCCESS(rv,rv);
154
// Create mapped properties:
156
// DOM property: nsIDOMSVGTextPositioningElement::x, #IMPLIED attrib: x
158
nsCOMPtr<nsISVGLengthList> lengthList;
159
rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
160
NS_ENSURE_SUCCESS(rv,rv);
161
rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mX),
163
NS_ENSURE_SUCCESS(rv,rv);
164
rv = AddMappedSVGValue(nsSVGAtoms::x, mX);
165
NS_ENSURE_SUCCESS(rv,rv);
168
// DOM property: nsIDOMSVGTextPositioningElement::y, #IMPLIED attrib: y
170
nsCOMPtr<nsISVGLengthList> lengthList;
171
rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
172
NS_ENSURE_SUCCESS(rv,rv);
173
rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mY),
175
NS_ENSURE_SUCCESS(rv,rv);
176
rv = AddMappedSVGValue(nsSVGAtoms::y, mY);
177
NS_ENSURE_SUCCESS(rv,rv);
182
//----------------------------------------------------------------------
183
// nsIDOMNode methods
186
nsSVGTextElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
189
nsSVGTextElement* it = new nsSVGTextElement();
191
if (!it) return NS_ERROR_OUT_OF_MEMORY;
194
nsresult rv = it->Init(mNodeInfo);
201
rv = CopyNode(it, aDeep);
213
//----------------------------------------------------------------------
214
// nsIDOMSVGTextElement methods
218
//----------------------------------------------------------------------
219
// nsIDOMSVGTextPositioningElement methods
221
/* readonly attribute nsIDOMSVGAnimatedLengthList x; */
222
NS_IMETHODIMP nsSVGTextElement::GetX(nsIDOMSVGAnimatedLengthList * *aX)
229
/* readonly attribute nsIDOMSVGAnimatedLengthList y; */
230
NS_IMETHODIMP nsSVGTextElement::GetY(nsIDOMSVGAnimatedLengthList * *aY)
237
/* readonly attribute nsIDOMSVGAnimatedLengthList dx; */
238
NS_IMETHODIMP nsSVGTextElement::GetDx(nsIDOMSVGAnimatedLengthList * *aDx)
240
NS_NOTYETIMPLEMENTED("write me!");
241
return NS_ERROR_UNEXPECTED;
244
/* readonly attribute nsIDOMSVGAnimatedLengthList dy; */
245
NS_IMETHODIMP nsSVGTextElement::GetDy(nsIDOMSVGAnimatedLengthList * *aDy)
247
NS_NOTYETIMPLEMENTED("write me!");
248
return NS_ERROR_UNEXPECTED;
251
/* readonly attribute nsIDOMSVGAnimatedNumberList rotate; */
252
NS_IMETHODIMP nsSVGTextElement::GetRotate(nsIDOMSVGAnimatedNumberList * *aRotate)
254
NS_NOTYETIMPLEMENTED("write me!");
255
return NS_ERROR_UNEXPECTED;
258
//----------------------------------------------------------------------
259
// nsIDOMSVGTextContentElement methods
261
/* readonly attribute nsIDOMSVGAnimatedLength textLength; */
262
NS_IMETHODIMP nsSVGTextElement::GetTextLength(nsIDOMSVGAnimatedLength * *aTextLength)
264
NS_NOTYETIMPLEMENTED("write me!");
265
return NS_ERROR_UNEXPECTED;
268
/* readonly attribute nsIDOMSVGAnimatedEnumeration lengthAdjust; */
269
NS_IMETHODIMP nsSVGTextElement::GetLengthAdjust(nsIDOMSVGAnimatedEnumeration * *aLengthAdjust)
271
NS_NOTYETIMPLEMENTED("write me!");
272
return NS_ERROR_UNEXPECTED;
275
/* long getNumberOfChars (); */
276
NS_IMETHODIMP nsSVGTextElement::GetNumberOfChars(PRInt32 *_retval)
278
NS_NOTYETIMPLEMENTED("write me!");
279
return NS_ERROR_UNEXPECTED;
282
/* float getComputedTextLength (); */
283
NS_IMETHODIMP nsSVGTextElement::GetComputedTextLength(float *_retval)
285
NS_NOTYETIMPLEMENTED("write me!");
286
return NS_ERROR_UNEXPECTED;
289
/* float getSubStringLength (in unsigned long charnum, in unsigned long nchars); */
290
NS_IMETHODIMP nsSVGTextElement::GetSubStringLength(PRUint32 charnum, PRUint32 nchars, float *_retval)
292
NS_NOTYETIMPLEMENTED("write me!");
293
return NS_ERROR_UNEXPECTED;
296
/* nsIDOMSVGPoint getStartPositionOfChar (in unsigned long charnum); */
297
NS_IMETHODIMP nsSVGTextElement::GetStartPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
299
NS_NOTYETIMPLEMENTED("write me!");
300
return NS_ERROR_UNEXPECTED;
303
/* nsIDOMSVGPoint getEndPositionOfChar (in unsigned long charnum); */
304
NS_IMETHODIMP nsSVGTextElement::GetEndPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
306
NS_NOTYETIMPLEMENTED("write me!");
307
return NS_ERROR_UNEXPECTED;
310
/* nsIDOMSVGRect getExtentOfChar (in unsigned long charnum); */
311
NS_IMETHODIMP nsSVGTextElement::GetExtentOfChar(PRUint32 charnum, nsIDOMSVGRect **_retval)
314
nsCOMPtr<nsISVGTextContentMetrics> metrics = GetTextContentMetrics();
316
if (!metrics) return NS_ERROR_FAILURE;
318
return metrics->GetExtentOfChar(charnum, _retval);
321
/* float getRotationOfChar (in unsigned long charnum); */
322
NS_IMETHODIMP nsSVGTextElement::GetRotationOfChar(PRUint32 charnum, float *_retval)
324
NS_NOTYETIMPLEMENTED("write me!");
325
return NS_ERROR_UNEXPECTED;
328
/* long getCharNumAtPosition (in nsIDOMSVGPoint point); */
329
NS_IMETHODIMP nsSVGTextElement::GetCharNumAtPosition(nsIDOMSVGPoint *point, PRInt32 *_retval)
331
NS_NOTYETIMPLEMENTED("write me!");
332
return NS_ERROR_UNEXPECTED;
335
/* void selectSubString (in unsigned long charnum, in unsigned long nchars); */
336
NS_IMETHODIMP nsSVGTextElement::SelectSubString(PRUint32 charnum, PRUint32 nchars)
338
NS_NOTYETIMPLEMENTED("write me!");
339
return NS_ERROR_UNEXPECTED;
342
//----------------------------------------------------------------------
343
// nsIStyledContent methods
345
NS_IMETHODIMP_(PRBool)
346
nsSVGTextElement::IsAttributeMapped(const nsIAtom* name) const
348
static const MappedAttributeEntry* const map[] = {
349
sTextContentElementsMap,
350
sFontSpecificationMap
353
return FindAttributeDependence(name, map, NS_ARRAY_LENGTH(map)) ||
354
nsSVGTextElementBase::IsAttributeMapped(name);
357
//----------------------------------------------------------------------
358
// nsISVGContent methods
360
void nsSVGTextElement::ParentChainChanged()
362
// set new context information on our length-properties:
364
nsCOMPtr<nsIDOMSVGSVGElement> dom_elem;
365
GetOwnerSVGElement(getter_AddRefs(dom_elem));
366
if (!dom_elem) return;
368
nsCOMPtr<nsISVGSVGElement> svg_elem = do_QueryInterface(dom_elem);
369
NS_ASSERTION(svg_elem, "<svg> element missing interface");
373
nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
374
mX->GetBaseVal(getter_AddRefs(dom_lengthlist));
375
nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
376
NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
378
nsCOMPtr<nsIDOMSVGRect> vp_dom;
379
svg_elem->GetViewport(getter_AddRefs(vp_dom));
380
nsCOMPtr<nsISVGViewportRect> vp = do_QueryInterface(vp_dom);
381
nsCOMPtr<nsISVGViewportAxis> ctx;
382
vp->GetXAxis(getter_AddRefs(ctx));
384
lengthlist->SetContext(ctx);
389
nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
390
mY->GetBaseVal(getter_AddRefs(dom_lengthlist));
391
nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
392
NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
394
nsCOMPtr<nsIDOMSVGRect> vp_dom;
395
svg_elem->GetViewport(getter_AddRefs(vp_dom));
396
nsCOMPtr<nsISVGViewportRect> vp = do_QueryInterface(vp_dom);
397
nsCOMPtr<nsISVGViewportAxis> ctx;
398
vp->GetYAxis(getter_AddRefs(ctx));
400
lengthlist->SetContext(ctx);
403
// recurse into child content:
404
nsSVGTextElementBase::ParentChainChanged();
407
//----------------------------------------------------------------------
408
// implementation helpers:
410
already_AddRefed<nsISVGTextContentMetrics>
411
nsSVGTextElement::GetTextContentMetrics()
414
NS_ERROR("no document");
418
nsIPresShell* presShell = mDocument->GetShellAt(0);
420
NS_ERROR("no presshell");
425
presShell->GetPrimaryFrameFor(NS_STATIC_CAST(nsIStyledContent*, this), &frame);
428
NS_ERROR("no frame");
432
nsISVGTextContentMetrics* metrics;
433
frame->QueryInterface(NS_GET_IID(nsISVGTextContentMetrics),(void**)&metrics);
434
NS_ASSERTION(metrics, "wrong frame type");