~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/content/svg/content/src/nsSVGTextElement.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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.
 
14
 *
 
15
 * The Original Code is the Mozilla SVG project.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *    Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
 
24
 *
 
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.
 
36
 *
 
37
 * ----- END LICENSE BLOCK ----- */
 
38
 
 
39
#include "nsSVGGraphicElement.h"
 
40
#include "nsSVGAtoms.h"
 
41
#include "nsIDOMSVGTextElement.h"
 
42
#include "nsCOMPtr.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"
 
50
#include "nsIFrame.h"
 
51
#include "nsIDocument.h"
 
52
 
 
53
typedef nsSVGGraphicElement nsSVGTextElementBase;
 
54
 
 
55
class nsSVGTextElement : public nsSVGTextElementBase,
 
56
                         public nsIDOMSVGTextElement // : nsIDOMSVGTextPositioningElement
 
57
                                                     // : nsIDOMSVGTextContentElement
 
58
{
 
59
protected:
 
60
  friend nsresult NS_NewSVGTextElement(nsIContent **aResult,
 
61
                                       nsINodeInfo *aNodeInfo);
 
62
  nsSVGTextElement();
 
63
  virtual ~nsSVGTextElement();
 
64
  nsresult Init(nsINodeInfo* aNodeInfo);
 
65
  
 
66
public:
 
67
  // interfaces:
 
68
  
 
69
  NS_DECL_ISUPPORTS_INHERITED
 
70
  NS_DECL_NSIDOMSVGTEXTELEMENT
 
71
  NS_DECL_NSIDOMSVGTEXTPOSITIONINGELEMENT
 
72
  NS_DECL_NSIDOMSVGTEXTCONTENTELEMENT
 
73
 
 
74
  // xxx If xpcom allowed virtual inheritance we wouldn't need to
 
75
  // forward here :-(
 
76
  NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsSVGTextElementBase::)
 
77
  NS_FORWARD_NSIDOMELEMENT(nsSVGTextElementBase::)
 
78
  NS_FORWARD_NSIDOMSVGELEMENT(nsSVGTextElementBase::)
 
79
 
 
80
  // nsIStyledContent interface
 
81
  NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
 
82
  
 
83
  // nsISVGContent specializations:
 
84
  virtual void ParentChainChanged();
 
85
 
 
86
protected:
 
87
 
 
88
  already_AddRefed<nsISVGTextContentMetrics> GetTextContentMetrics();
 
89
  
 
90
  // nsIDOMSVGTextPositioning properties:
 
91
  nsCOMPtr<nsIDOMSVGAnimatedLengthList> mX;
 
92
  nsCOMPtr<nsIDOMSVGAnimatedLengthList> mY;
 
93
  nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdX;
 
94
  nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdY;
 
95
 
 
96
};
 
97
 
 
98
 
 
99
nsresult NS_NewSVGTextElement(nsIContent **aResult, nsINodeInfo *aNodeInfo)
 
100
{
 
101
  *aResult = nsnull;
 
102
  nsSVGTextElement* it = new nsSVGTextElement();
 
103
 
 
104
  if (!it) return NS_ERROR_OUT_OF_MEMORY;
 
105
  NS_ADDREF(it);
 
106
 
 
107
  nsresult rv = it->Init(aNodeInfo);
 
108
 
 
109
  if (NS_FAILED(rv)) {
 
110
    it->Release();
 
111
    return rv;
 
112
  }
 
113
  
 
114
  *aResult = it;
 
115
 
 
116
  return NS_OK;
 
117
}
 
118
 
 
119
//----------------------------------------------------------------------
 
120
// nsISupports methods
 
121
 
 
122
NS_IMPL_ADDREF_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
 
123
NS_IMPL_RELEASE_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
 
124
 
 
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)
 
134
 
 
135
//----------------------------------------------------------------------
 
136
// Implementation
 
137
 
 
138
nsSVGTextElement::nsSVGTextElement()
 
139
{
 
140
 
 
141
}
 
142
 
 
143
nsSVGTextElement::~nsSVGTextElement()
 
144
{
 
145
}
 
146
 
 
147
  
 
148
nsresult
 
149
nsSVGTextElement::Init(nsINodeInfo* aNodeInfo)
 
150
{
 
151
  nsresult rv = nsSVGTextElementBase::Init(aNodeInfo);
 
152
  NS_ENSURE_SUCCESS(rv,rv);
 
153
 
 
154
  // Create mapped properties:
 
155
 
 
156
  // DOM property: nsIDOMSVGTextPositioningElement::x, #IMPLIED attrib: x
 
157
  {
 
158
    nsCOMPtr<nsISVGLengthList> lengthList;
 
159
    rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
 
160
    NS_ENSURE_SUCCESS(rv,rv);
 
161
    rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mX),
 
162
                                     lengthList);
 
163
    NS_ENSURE_SUCCESS(rv,rv);
 
164
    rv = AddMappedSVGValue(nsSVGAtoms::x, mX);
 
165
    NS_ENSURE_SUCCESS(rv,rv);
 
166
  }
 
167
  
 
168
  // DOM property: nsIDOMSVGTextPositioningElement::y, #IMPLIED attrib: y
 
169
  {
 
170
    nsCOMPtr<nsISVGLengthList> lengthList;
 
171
    rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
 
172
    NS_ENSURE_SUCCESS(rv,rv);
 
173
    rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mY),
 
174
                                     lengthList);
 
175
    NS_ENSURE_SUCCESS(rv,rv);
 
176
    rv = AddMappedSVGValue(nsSVGAtoms::y, mY);
 
177
    NS_ENSURE_SUCCESS(rv,rv);
 
178
  }
 
179
  return NS_OK;
 
180
}
 
181
 
 
182
//----------------------------------------------------------------------
 
183
// nsIDOMNode methods
 
184
 
 
185
NS_IMETHODIMP
 
186
nsSVGTextElement::CloneNode(PRBool aDeep, nsIDOMNode** aReturn)
 
187
{
 
188
  *aReturn = nsnull;
 
189
  nsSVGTextElement* it = new nsSVGTextElement();
 
190
 
 
191
  if (!it) return NS_ERROR_OUT_OF_MEMORY;
 
192
  NS_ADDREF(it);
 
193
 
 
194
  nsresult rv = it->Init(mNodeInfo);
 
195
 
 
196
  if (NS_FAILED(rv)) {
 
197
    it->Release();
 
198
    return rv;
 
199
  }
 
200
 
 
201
  rv = CopyNode(it, aDeep);
 
202
 
 
203
  if (NS_FAILED(rv)) {
 
204
    it->Release();
 
205
    return rv;
 
206
  }
 
207
 
 
208
  *aReturn = it;
 
209
 
 
210
  return NS_OK; 
 
211
}
 
212
 
 
213
//----------------------------------------------------------------------
 
214
// nsIDOMSVGTextElement methods
 
215
 
 
216
// - no methods -
 
217
 
 
218
//----------------------------------------------------------------------
 
219
// nsIDOMSVGTextPositioningElement methods
 
220
 
 
221
/* readonly attribute nsIDOMSVGAnimatedLengthList x; */
 
222
NS_IMETHODIMP nsSVGTextElement::GetX(nsIDOMSVGAnimatedLengthList * *aX)
 
223
{
 
224
  *aX = mX;
 
225
  NS_IF_ADDREF(*aX);
 
226
  return NS_OK;
 
227
}
 
228
 
 
229
/* readonly attribute nsIDOMSVGAnimatedLengthList y; */
 
230
NS_IMETHODIMP nsSVGTextElement::GetY(nsIDOMSVGAnimatedLengthList * *aY)
 
231
{
 
232
  *aY = mY;
 
233
  NS_IF_ADDREF(*aY);
 
234
  return NS_OK;
 
235
}
 
236
 
 
237
/* readonly attribute nsIDOMSVGAnimatedLengthList dx; */
 
238
NS_IMETHODIMP nsSVGTextElement::GetDx(nsIDOMSVGAnimatedLengthList * *aDx)
 
239
{
 
240
  NS_NOTYETIMPLEMENTED("write me!");
 
241
  return NS_ERROR_UNEXPECTED;
 
242
}
 
243
 
 
244
/* readonly attribute nsIDOMSVGAnimatedLengthList dy; */
 
245
NS_IMETHODIMP nsSVGTextElement::GetDy(nsIDOMSVGAnimatedLengthList * *aDy)
 
246
{
 
247
  NS_NOTYETIMPLEMENTED("write me!");
 
248
  return NS_ERROR_UNEXPECTED;
 
249
}
 
250
 
 
251
/* readonly attribute nsIDOMSVGAnimatedNumberList rotate; */
 
252
NS_IMETHODIMP nsSVGTextElement::GetRotate(nsIDOMSVGAnimatedNumberList * *aRotate)
 
253
{
 
254
  NS_NOTYETIMPLEMENTED("write me!");
 
255
  return NS_ERROR_UNEXPECTED;
 
256
}
 
257
 
 
258
//----------------------------------------------------------------------
 
259
// nsIDOMSVGTextContentElement methods
 
260
 
 
261
/* readonly attribute nsIDOMSVGAnimatedLength textLength; */
 
262
NS_IMETHODIMP nsSVGTextElement::GetTextLength(nsIDOMSVGAnimatedLength * *aTextLength)
 
263
{
 
264
  NS_NOTYETIMPLEMENTED("write me!");
 
265
  return NS_ERROR_UNEXPECTED;
 
266
}
 
267
 
 
268
/* readonly attribute nsIDOMSVGAnimatedEnumeration lengthAdjust; */
 
269
NS_IMETHODIMP nsSVGTextElement::GetLengthAdjust(nsIDOMSVGAnimatedEnumeration * *aLengthAdjust)
 
270
{
 
271
  NS_NOTYETIMPLEMENTED("write me!");
 
272
  return NS_ERROR_UNEXPECTED;
 
273
}
 
274
 
 
275
/* long getNumberOfChars (); */
 
276
NS_IMETHODIMP nsSVGTextElement::GetNumberOfChars(PRInt32 *_retval)
 
277
{
 
278
  NS_NOTYETIMPLEMENTED("write me!");
 
279
  return NS_ERROR_UNEXPECTED;
 
280
}
 
281
 
 
282
/* float getComputedTextLength (); */
 
283
NS_IMETHODIMP nsSVGTextElement::GetComputedTextLength(float *_retval)
 
284
{
 
285
  NS_NOTYETIMPLEMENTED("write me!");
 
286
  return NS_ERROR_UNEXPECTED;
 
287
}
 
288
 
 
289
/* float getSubStringLength (in unsigned long charnum, in unsigned long nchars); */
 
290
NS_IMETHODIMP nsSVGTextElement::GetSubStringLength(PRUint32 charnum, PRUint32 nchars, float *_retval)
 
291
{
 
292
  NS_NOTYETIMPLEMENTED("write me!");
 
293
  return NS_ERROR_UNEXPECTED;
 
294
}
 
295
 
 
296
/* nsIDOMSVGPoint getStartPositionOfChar (in unsigned long charnum); */
 
297
NS_IMETHODIMP nsSVGTextElement::GetStartPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
 
298
{
 
299
  NS_NOTYETIMPLEMENTED("write me!");
 
300
  return NS_ERROR_UNEXPECTED;
 
301
}
 
302
 
 
303
/* nsIDOMSVGPoint getEndPositionOfChar (in unsigned long charnum); */
 
304
NS_IMETHODIMP nsSVGTextElement::GetEndPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
 
305
{
 
306
  NS_NOTYETIMPLEMENTED("write me!");
 
307
  return NS_ERROR_UNEXPECTED;
 
308
}
 
309
 
 
310
/* nsIDOMSVGRect getExtentOfChar (in unsigned long charnum); */
 
311
NS_IMETHODIMP nsSVGTextElement::GetExtentOfChar(PRUint32 charnum, nsIDOMSVGRect **_retval)
 
312
{
 
313
  *_retval = nsnull;
 
314
  nsCOMPtr<nsISVGTextContentMetrics> metrics = GetTextContentMetrics();
 
315
 
 
316
  if (!metrics) return NS_ERROR_FAILURE;
 
317
 
 
318
  return metrics->GetExtentOfChar(charnum, _retval);
 
319
}
 
320
 
 
321
/* float getRotationOfChar (in unsigned long charnum); */
 
322
NS_IMETHODIMP nsSVGTextElement::GetRotationOfChar(PRUint32 charnum, float *_retval)
 
323
{
 
324
  NS_NOTYETIMPLEMENTED("write me!");
 
325
  return NS_ERROR_UNEXPECTED;
 
326
}
 
327
 
 
328
/* long getCharNumAtPosition (in nsIDOMSVGPoint point); */
 
329
NS_IMETHODIMP nsSVGTextElement::GetCharNumAtPosition(nsIDOMSVGPoint *point, PRInt32 *_retval)
 
330
{
 
331
  NS_NOTYETIMPLEMENTED("write me!");
 
332
  return NS_ERROR_UNEXPECTED;
 
333
}
 
334
 
 
335
/* void selectSubString (in unsigned long charnum, in unsigned long nchars); */
 
336
NS_IMETHODIMP nsSVGTextElement::SelectSubString(PRUint32 charnum, PRUint32 nchars)
 
337
{
 
338
  NS_NOTYETIMPLEMENTED("write me!");
 
339
  return NS_ERROR_UNEXPECTED;
 
340
}
 
341
 
 
342
//----------------------------------------------------------------------
 
343
// nsIStyledContent methods
 
344
 
 
345
NS_IMETHODIMP_(PRBool)
 
346
nsSVGTextElement::IsAttributeMapped(const nsIAtom* name) const
 
347
{
 
348
  static const MappedAttributeEntry* const map[] = {
 
349
    sTextContentElementsMap,
 
350
    sFontSpecificationMap
 
351
  };
 
352
 
 
353
  return FindAttributeDependence(name, map, NS_ARRAY_LENGTH(map)) ||
 
354
    nsSVGTextElementBase::IsAttributeMapped(name);
 
355
}
 
356
 
 
357
//----------------------------------------------------------------------
 
358
// nsISVGContent methods
 
359
 
 
360
void nsSVGTextElement::ParentChainChanged()
 
361
{
 
362
  // set new context information on our length-properties:
 
363
  
 
364
  nsCOMPtr<nsIDOMSVGSVGElement> dom_elem;
 
365
  GetOwnerSVGElement(getter_AddRefs(dom_elem));
 
366
  if (!dom_elem) return;
 
367
 
 
368
  nsCOMPtr<nsISVGSVGElement> svg_elem = do_QueryInterface(dom_elem);
 
369
  NS_ASSERTION(svg_elem, "<svg> element missing interface");
 
370
 
 
371
  // x:
 
372
  {
 
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");
 
377
 
 
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));
 
383
    
 
384
    lengthlist->SetContext(ctx);
 
385
  }
 
386
 
 
387
  // y:
 
388
  {
 
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");
 
393
 
 
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));
 
399
    
 
400
    lengthlist->SetContext(ctx);
 
401
  }
 
402
 
 
403
  // recurse into child content:
 
404
  nsSVGTextElementBase::ParentChainChanged();
 
405
}  
 
406
 
 
407
//----------------------------------------------------------------------
 
408
// implementation helpers:
 
409
 
 
410
already_AddRefed<nsISVGTextContentMetrics>
 
411
nsSVGTextElement::GetTextContentMetrics()
 
412
{
 
413
  if (!mDocument) {
 
414
    NS_ERROR("no document");
 
415
    return nsnull;
 
416
  }
 
417
  
 
418
  nsIPresShell* presShell = mDocument->GetShellAt(0);
 
419
  if (!presShell) {
 
420
    NS_ERROR("no presshell");
 
421
    return nsnull;
 
422
  }
 
423
 
 
424
  nsIFrame* frame;
 
425
  presShell->GetPrimaryFrameFor(NS_STATIC_CAST(nsIStyledContent*, this), &frame);
 
426
 
 
427
  if (!frame) {
 
428
    NS_ERROR("no frame");
 
429
    return nsnull;
 
430
  }
 
431
  
 
432
  nsISVGTextContentMetrics* metrics;
 
433
  frame->QueryInterface(NS_GET_IID(nsISVGTextContentMetrics),(void**)&metrics);
 
434
  NS_ASSERTION(metrics, "wrong frame type");
 
435
  return metrics;
 
436
}