Back to index

lightning-sunbird  0.9+nobinonly
nsSVGTextElement.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla SVG project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Crocodile Clips Ltd..
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsSVGGraphicElement.h"
00040 #include "nsSVGAtoms.h"
00041 #include "nsIDOMSVGTextElement.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsSVGAnimatedLengthList.h"
00044 #include "nsSVGLengthList.h"
00045 #include "nsISVGSVGElement.h"
00046 #include "nsSVGCoordCtxProvider.h"
00047 #include "nsISVGTextContentMetrics.h"
00048 #include "nsIPresShell.h"
00049 #include "nsIFrame.h"
00050 #include "nsIDocument.h"
00051 
00052 typedef nsSVGGraphicElement nsSVGTextElementBase;
00053 
00054 class nsSVGTextElement : public nsSVGTextElementBase,
00055                          public nsIDOMSVGTextElement // : nsIDOMSVGTextPositioningElement
00056                                                      // : nsIDOMSVGTextContentElement
00057 {
00058 protected:
00059   friend nsresult NS_NewSVGTextElement(nsIContent **aResult,
00060                                        nsINodeInfo *aNodeInfo);
00061   nsSVGTextElement(nsINodeInfo* aNodeInfo);
00062   virtual ~nsSVGTextElement();
00063   nsresult Init();
00064   
00065 public:
00066   // interfaces:
00067   
00068   NS_DECL_ISUPPORTS_INHERITED
00069   NS_DECL_NSIDOMSVGTEXTELEMENT
00070   NS_DECL_NSIDOMSVGTEXTPOSITIONINGELEMENT
00071   NS_DECL_NSIDOMSVGTEXTCONTENTELEMENT
00072 
00073   // xxx If xpcom allowed virtual inheritance we wouldn't need to
00074   // forward here :-(
00075   NS_FORWARD_NSIDOMNODE_NO_CLONENODE(nsSVGTextElementBase::)
00076   NS_FORWARD_NSIDOMELEMENT(nsSVGTextElementBase::)
00077   NS_FORWARD_NSIDOMSVGELEMENT(nsSVGTextElementBase::)
00078 
00079   // nsIStyledContent interface
00080   NS_IMETHOD_(PRBool) IsAttributeMapped(const nsIAtom* aAttribute) const;
00081   
00082   // nsISVGContent specializations:
00083   virtual void ParentChainChanged();
00084 
00085 protected:
00086 
00087   already_AddRefed<nsISVGTextContentMetrics> GetTextContentMetrics();
00088   
00089   // nsIDOMSVGTextPositioning properties:
00090   nsCOMPtr<nsIDOMSVGAnimatedLengthList> mX;
00091   nsCOMPtr<nsIDOMSVGAnimatedLengthList> mY;
00092   nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdX;
00093   nsCOMPtr<nsIDOMSVGAnimatedLengthList> mdY;
00094 
00095 };
00096 
00097 
00098 NS_IMPL_NS_NEW_SVG_ELEMENT(Text)
00099 
00100 
00101 //----------------------------------------------------------------------
00102 // nsISupports methods
00103 
00104 NS_IMPL_ADDREF_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
00105 NS_IMPL_RELEASE_INHERITED(nsSVGTextElement,nsSVGTextElementBase)
00106 
00107 NS_INTERFACE_MAP_BEGIN(nsSVGTextElement)
00108   NS_INTERFACE_MAP_ENTRY(nsIDOMNode)
00109   NS_INTERFACE_MAP_ENTRY(nsIDOMElement)
00110   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGElement)
00111   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextElement)
00112   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextPositioningElement)
00113   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTextContentElement)
00114   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGTextElement)
00115 NS_INTERFACE_MAP_END_INHERITING(nsSVGTextElementBase)
00116 
00117 //----------------------------------------------------------------------
00118 // Implementation
00119 
00120 nsSVGTextElement::nsSVGTextElement(nsINodeInfo* aNodeInfo)
00121   : nsSVGTextElementBase(aNodeInfo)
00122 {
00123 
00124 }
00125 
00126 nsSVGTextElement::~nsSVGTextElement()
00127 {
00128 }
00129 
00130   
00131 nsresult
00132 nsSVGTextElement::Init()
00133 {
00134   nsresult rv = nsSVGTextElementBase::Init();
00135   NS_ENSURE_SUCCESS(rv,rv);
00136 
00137   // Create mapped properties:
00138 
00139   // DOM property: nsIDOMSVGTextPositioningElement::x, #IMPLIED attrib: x
00140   {
00141     nsCOMPtr<nsISVGLengthList> lengthList;
00142     rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
00143     NS_ENSURE_SUCCESS(rv,rv);
00144     rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mX),
00145                                      lengthList);
00146     NS_ENSURE_SUCCESS(rv,rv);
00147     rv = AddMappedSVGValue(nsSVGAtoms::x, mX);
00148     NS_ENSURE_SUCCESS(rv,rv);
00149   }
00150   
00151   // DOM property: nsIDOMSVGTextPositioningElement::y, #IMPLIED attrib: y
00152   {
00153     nsCOMPtr<nsISVGLengthList> lengthList;
00154     rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
00155     NS_ENSURE_SUCCESS(rv,rv);
00156     rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mY),
00157                                      lengthList);
00158     NS_ENSURE_SUCCESS(rv,rv);
00159     rv = AddMappedSVGValue(nsSVGAtoms::y, mY);
00160     NS_ENSURE_SUCCESS(rv,rv);
00161   }
00162 
00163   // DOM property: nsIDOMSVGTextPositioningElement::dx, #IMPLIED attrib: dx
00164   {
00165     nsCOMPtr<nsISVGLengthList> lengthList;
00166     rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
00167     NS_ENSURE_SUCCESS(rv,rv);
00168     rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mdX),
00169                                      lengthList);
00170     NS_ENSURE_SUCCESS(rv,rv);
00171     rv = AddMappedSVGValue(nsSVGAtoms::dx, mdX);
00172     NS_ENSURE_SUCCESS(rv,rv);
00173   }
00174   
00175   // DOM property: nsIDOMSVGTextPositioningElement::dy, #IMPLIED attrib: dy
00176   {
00177     nsCOMPtr<nsISVGLengthList> lengthList;
00178     rv = NS_NewSVGLengthList(getter_AddRefs(lengthList));
00179     NS_ENSURE_SUCCESS(rv,rv);
00180     rv = NS_NewSVGAnimatedLengthList(getter_AddRefs(mdY),
00181                                      lengthList);
00182     NS_ENSURE_SUCCESS(rv,rv);
00183     rv = AddMappedSVGValue(nsSVGAtoms::dy, mdY);
00184     NS_ENSURE_SUCCESS(rv,rv);
00185   }
00186 
00187   return rv;
00188 }
00189 
00190 //----------------------------------------------------------------------
00191 // nsIDOMNode methods
00192 
00193 
00194 NS_IMPL_DOM_CLONENODE_WITH_INIT(nsSVGTextElement)
00195 
00196 
00197 //----------------------------------------------------------------------
00198 // nsIDOMSVGTextElement methods
00199 
00200 // - no methods -
00201 
00202 //----------------------------------------------------------------------
00203 // nsIDOMSVGTextPositioningElement methods
00204 
00205 /* readonly attribute nsIDOMSVGAnimatedLengthList x; */
00206 NS_IMETHODIMP nsSVGTextElement::GetX(nsIDOMSVGAnimatedLengthList * *aX)
00207 {
00208   *aX = mX;
00209   NS_IF_ADDREF(*aX);
00210   return NS_OK;
00211 }
00212 
00213 /* readonly attribute nsIDOMSVGAnimatedLengthList y; */
00214 NS_IMETHODIMP nsSVGTextElement::GetY(nsIDOMSVGAnimatedLengthList * *aY)
00215 {
00216   *aY = mY;
00217   NS_IF_ADDREF(*aY);
00218   return NS_OK;
00219 }
00220 
00221 /* readonly attribute nsIDOMSVGAnimatedLengthList dx; */
00222 NS_IMETHODIMP nsSVGTextElement::GetDx(nsIDOMSVGAnimatedLengthList * *aDx)
00223 {
00224   *aDx = mdX;
00225   NS_IF_ADDREF(*aDx);
00226   return NS_OK;
00227 }
00228 
00229 /* readonly attribute nsIDOMSVGAnimatedLengthList dy; */
00230 NS_IMETHODIMP nsSVGTextElement::GetDy(nsIDOMSVGAnimatedLengthList * *aDy)
00231 {
00232   *aDy = mdY;
00233   NS_IF_ADDREF(*aDy);
00234   return NS_OK;
00235 }
00236 
00237 /* readonly attribute nsIDOMSVGAnimatedNumberList rotate; */
00238 NS_IMETHODIMP nsSVGTextElement::GetRotate(nsIDOMSVGAnimatedNumberList * *aRotate)
00239 {
00240   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetRotate");
00241   return NS_ERROR_NOT_IMPLEMENTED;
00242 }
00243 
00244 //----------------------------------------------------------------------
00245 // nsIDOMSVGTextContentElement methods
00246 
00247 /* readonly attribute nsIDOMSVGAnimatedLength textLength; */
00248 NS_IMETHODIMP nsSVGTextElement::GetTextLength(nsIDOMSVGAnimatedLength * *aTextLength)
00249 {
00250   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetTextLength");
00251   return NS_ERROR_NOT_IMPLEMENTED;
00252 }
00253 
00254 /* readonly attribute nsIDOMSVGAnimatedEnumeration lengthAdjust; */
00255 NS_IMETHODIMP nsSVGTextElement::GetLengthAdjust(nsIDOMSVGAnimatedEnumeration * *aLengthAdjust)
00256 {
00257   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetLengthAdjust");
00258   return NS_ERROR_NOT_IMPLEMENTED;
00259 }
00260 
00261 /* long getNumberOfChars (); */
00262 NS_IMETHODIMP nsSVGTextElement::GetNumberOfChars(PRInt32 *_retval)
00263 {
00264   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetNumberOfChars");
00265   return NS_ERROR_NOT_IMPLEMENTED;
00266 }
00267 
00268 /* float getComputedTextLength (); */
00269 NS_IMETHODIMP nsSVGTextElement::GetComputedTextLength(float *_retval)
00270 {
00271   nsCOMPtr<nsIDOMSVGRect> bbox;
00272 
00273   GetBBox(getter_AddRefs(bbox));
00274   if (bbox)
00275     bbox->GetWidth(_retval);
00276   else
00277     *_retval = 0;
00278   
00279   return NS_OK;
00280 }
00281 
00282 /* float getSubStringLength (in unsigned long charnum, in unsigned long nchars); */
00283 NS_IMETHODIMP nsSVGTextElement::GetSubStringLength(PRUint32 charnum, PRUint32 nchars, float *_retval)
00284 {
00285   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetSubStringLength");
00286   return NS_ERROR_NOT_IMPLEMENTED;
00287 }
00288 
00289 /* nsIDOMSVGPoint getStartPositionOfChar (in unsigned long charnum); */
00290 NS_IMETHODIMP nsSVGTextElement::GetStartPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
00291 {
00292   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetStartPositionOfChar");
00293   return NS_ERROR_NOT_IMPLEMENTED;
00294 }
00295 
00296 /* nsIDOMSVGPoint getEndPositionOfChar (in unsigned long charnum); */
00297 NS_IMETHODIMP nsSVGTextElement::GetEndPositionOfChar(PRUint32 charnum, nsIDOMSVGPoint **_retval)
00298 {
00299   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetEndPositionOfChar");
00300   return NS_ERROR_NOT_IMPLEMENTED;
00301 }
00302 
00303 /* nsIDOMSVGRect getExtentOfChar (in unsigned long charnum); */
00304 NS_IMETHODIMP nsSVGTextElement::GetExtentOfChar(PRUint32 charnum, nsIDOMSVGRect **_retval)
00305 {
00306   *_retval = nsnull;
00307   nsCOMPtr<nsISVGTextContentMetrics> metrics = GetTextContentMetrics();
00308 
00309   if (!metrics) return NS_ERROR_FAILURE;
00310 
00311   return metrics->GetExtentOfChar(charnum, _retval);
00312 }
00313 
00314 /* float getRotationOfChar (in unsigned long charnum); */
00315 NS_IMETHODIMP nsSVGTextElement::GetRotationOfChar(PRUint32 charnum, float *_retval)
00316 {
00317   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetRotationOfChar");
00318   return NS_ERROR_NOT_IMPLEMENTED;
00319 }
00320 
00321 /* long getCharNumAtPosition (in nsIDOMSVGPoint point); */
00322 NS_IMETHODIMP nsSVGTextElement::GetCharNumAtPosition(nsIDOMSVGPoint *point, PRInt32 *_retval)
00323 {
00324   // null check when implementing - this method can be used by scripts!
00325   // if (!element)
00326   //   return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00327 
00328   NS_NOTYETIMPLEMENTED("nsSVGTextElement::GetCharNumAtPosition");
00329   return NS_ERROR_NOT_IMPLEMENTED;
00330 }
00331 
00332 /* void selectSubString (in unsigned long charnum, in unsigned long nchars); */
00333 NS_IMETHODIMP nsSVGTextElement::SelectSubString(PRUint32 charnum, PRUint32 nchars)
00334 {
00335   NS_NOTYETIMPLEMENTED("nsSVGTextElement::SelectSubString");
00336   return NS_ERROR_NOT_IMPLEMENTED;
00337 }
00338 
00339 //----------------------------------------------------------------------
00340 // nsIStyledContent methods
00341 
00342 NS_IMETHODIMP_(PRBool)
00343 nsSVGTextElement::IsAttributeMapped(const nsIAtom* name) const
00344 {
00345   static const MappedAttributeEntry* const map[] = {
00346     sTextContentElementsMap,
00347     sFontSpecificationMap
00348   };
00349 
00350   return FindAttributeDependence(name, map, NS_ARRAY_LENGTH(map)) ||
00351     nsSVGTextElementBase::IsAttributeMapped(name);
00352 }
00353 
00354 //----------------------------------------------------------------------
00355 // nsISVGContent methods
00356 
00357 void nsSVGTextElement::ParentChainChanged()
00358 {
00359   // set new context information on our length-properties:
00360   
00361   nsCOMPtr<nsIDOMSVGSVGElement> dom_elem;
00362   GetOwnerSVGElement(getter_AddRefs(dom_elem));
00363   if (!dom_elem) return;
00364 
00365   nsCOMPtr<nsSVGCoordCtxProvider> ctx = do_QueryInterface(dom_elem);
00366   NS_ASSERTION(ctx, "<svg> element missing interface");
00367 
00368   // x:
00369   {
00370     nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
00371     mX->GetAnimVal(getter_AddRefs(dom_lengthlist));
00372     nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
00373     NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
00374     
00375     lengthlist->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextX()));
00376   }
00377 
00378   // y:
00379   {
00380     nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
00381     mY->GetAnimVal(getter_AddRefs(dom_lengthlist));
00382     nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
00383     NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
00384     
00385     lengthlist->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextY()));
00386   }
00387 
00388   // dx:
00389   {
00390     nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
00391     mdX->GetAnimVal(getter_AddRefs(dom_lengthlist));
00392     nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
00393     NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
00394     
00395     lengthlist->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextX()));
00396   }
00397 
00398   // dy:
00399   {
00400     nsCOMPtr<nsIDOMSVGLengthList> dom_lengthlist;
00401     mdY->GetAnimVal(getter_AddRefs(dom_lengthlist));
00402     nsCOMPtr<nsISVGLengthList> lengthlist = do_QueryInterface(dom_lengthlist);
00403     NS_ASSERTION(lengthlist, "svg lengthlist missing interface");
00404     
00405     lengthlist->SetContext(nsRefPtr<nsSVGCoordCtx>(ctx->GetContextY()));
00406   }
00407 
00408   // recurse into child content:
00409   nsSVGTextElementBase::ParentChainChanged();
00410 }  
00411 
00412 //----------------------------------------------------------------------
00413 // implementation helpers:
00414 
00415 already_AddRefed<nsISVGTextContentMetrics>
00416 nsSVGTextElement::GetTextContentMetrics()
00417 {
00418   nsIDocument* doc = GetCurrentDoc();
00419   if (!doc) {
00420     NS_ERROR("no document");
00421     return nsnull;
00422   }
00423   
00424   nsIPresShell* presShell = doc->GetShellAt(0);
00425   if (!presShell) {
00426     NS_ERROR("no presshell");
00427     return nsnull;
00428   }
00429 
00430   nsIFrame* frame;
00431   presShell->GetPrimaryFrameFor(NS_STATIC_CAST(nsIStyledContent*, this), &frame);
00432 
00433   if (!frame) {
00434     NS_ERROR("no frame");
00435     return nsnull;
00436   }
00437   
00438   nsISVGTextContentMetrics* metrics;
00439   frame->QueryInterface(NS_GET_IID(nsISVGTextContentMetrics),(void**)&metrics);
00440   NS_ASSERTION(metrics, "wrong frame type");
00441   return metrics;
00442 }