Back to index

lightning-sunbird  0.9+nobinonly
nsSVGTransformList.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) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex.fritze@crocodile-clips.com> (original author)
00024  *   Jonathan Watt <jonathan.watt@strath.ac.uk>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsSVGTransformList.h"
00041 #include "nsSVGTransform.h"
00042 #include "nsSVGMatrix.h"
00043 #include "nsDOMError.h"
00044 #include "prdtoa.h"
00045 #include "nsSVGAtoms.h"
00046 #include "nsReadableUtils.h"
00047 #include "nsCRT.h"
00048 #include "nsCOMArray.h"
00049 #include "nsContentUtils.h"
00050 
00051 nsresult
00052 nsSVGTransformList::Create(nsIDOMSVGTransformList** aResult)
00053 {
00054   *aResult = new nsSVGTransformList();
00055   if (!*aResult) return NS_ERROR_OUT_OF_MEMORY;
00056   
00057   NS_ADDREF(*aResult);
00058   return NS_OK;
00059 }
00060 
00061 nsSVGTransformList::nsSVGTransformList()
00062 {
00063 }
00064 
00065 nsSVGTransformList::~nsSVGTransformList()
00066 {
00067   ReleaseTransforms();
00068 }
00069 
00070 void
00071 nsSVGTransformList::ReleaseTransforms()
00072 {
00073   PRInt32 count = mTransforms.Count();
00074   for (PRInt32 i = 0; i < count; ++i) {
00075     nsIDOMSVGTransform* transform = ElementAt(i);
00076     nsCOMPtr<nsISVGValue> val = do_QueryInterface(transform);
00077     if (val)
00078       val->RemoveObserver(this);
00079     NS_RELEASE(transform);
00080   }
00081   mTransforms.Clear();
00082 }
00083 
00084 nsIDOMSVGTransform*
00085 nsSVGTransformList::ElementAt(PRInt32 index)
00086 {
00087   return (nsIDOMSVGTransform*)mTransforms.ElementAt(index);
00088 }
00089 
00090 PRBool
00091 nsSVGTransformList::AppendElement(nsIDOMSVGTransform* aElement)
00092 {
00093   PRBool rv = mTransforms.AppendElement((void*)aElement);
00094   if (rv) {
00095     NS_ADDREF(aElement);
00096     nsCOMPtr<nsISVGValue> val = do_QueryInterface(aElement);
00097     if (val)
00098       val->AddObserver(this);
00099   }
00100 
00101   return rv;
00102 }
00103 
00104 //----------------------------------------------------------------------
00105 // XPConnect interface list
00106 NS_CLASSINFO_MAP_BEGIN(SVGTransformList)
00107   NS_CLASSINFO_MAP_ENTRY(nsIDOMSVGTransformList)
00108 NS_CLASSINFO_MAP_END
00109 
00110 //----------------------------------------------------------------------
00111 // nsISupports methods:
00112 
00113 NS_IMPL_ADDREF(nsSVGTransformList)
00114 NS_IMPL_RELEASE(nsSVGTransformList)
00115 
00116 NS_INTERFACE_MAP_BEGIN(nsSVGTransformList)
00117   NS_INTERFACE_MAP_ENTRY(nsISVGValue)
00118   NS_INTERFACE_MAP_ENTRY(nsIDOMSVGTransformList)
00119   NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
00120   NS_INTERFACE_MAP_ENTRY(nsISVGValueObserver)
00121   NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(SVGTransformList)
00122   NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISVGValue)
00123 NS_INTERFACE_MAP_END
00124 
00125 //----------------------------------------------------------------------
00126 // nsISVGValue methods:
00127 
00128 NS_IMETHODIMP
00129 nsSVGTransformList::SetValueString(const nsAString& aValue)
00130 {
00131   // XXX: we don't implement the _exact_ BNF given in the
00132   // specs. 
00133   
00134   nsresult rv = NS_OK;
00135 
00136   char *str = ToNewCString(aValue);
00137   
00138   char* rest = str;
00139   char* keyword;
00140   char* args;
00141   const char* delimiters1 = "\x20\x9\xD\xA,(";
00142   const char* delimiters2 = "()";
00143   nsCOMArray<nsIDOMSVGTransform> xforms;
00144     
00145   while ((keyword = nsCRT::strtok(rest, delimiters1, &rest))) {
00146 
00147     while (rest && isspace(*rest))
00148       ++rest;
00149 
00150     if (!(args = nsCRT::strtok(rest, delimiters2, &rest))) {
00151       rv = NS_ERROR_FAILURE;
00152       break; // parse error
00153     }
00154     nsCOMPtr<nsIDOMSVGTransform> transform;
00155     NS_NewSVGTransform(getter_AddRefs(transform));
00156     if (!transform) {
00157       rv = NS_ERROR_OUT_OF_MEMORY;
00158       break;
00159     }
00160     
00161     nsCOMPtr<nsIAtom> keyatom = do_GetAtom(keyword);
00162     
00163     if (keyatom == nsSVGAtoms::translate) {
00164       // tx [ty=0]
00165       float t[2] = { 0.f };
00166       PRInt32 num_parsed = ParseParameterList(args, t, 2);
00167       if (num_parsed != 1 && num_parsed != 2) {
00168         rv = NS_ERROR_FAILURE;
00169         break; // parse error
00170       }
00171 
00172       transform->SetTranslate(t[0], t[1]);
00173     }
00174     else if (keyatom == nsSVGAtoms::scale) { 
00175       // sx [sy=sx]
00176       float s[2] = { 0.f };
00177       PRInt32 num_parsed = ParseParameterList(args, s, 2);
00178       if (num_parsed != 1 && num_parsed != 2) {
00179         rv = NS_ERROR_FAILURE;
00180         break; // parse error
00181       }
00182 
00183       if (num_parsed == 1)
00184         s[1] = s[0];
00185 
00186       transform->SetScale(s[0], s[1]);
00187     }
00188     else if (keyatom == nsSVGAtoms::rotate) {
00189       // r [x0=0 y0=0]
00190       float r[3] = { 0.f };
00191       PRInt32 num_parsed = ParseParameterList(args, r, 3);
00192       if (num_parsed != 1 && num_parsed != 3) {
00193         rv = NS_ERROR_FAILURE;
00194         break; // parse error
00195       }
00196 
00197       transform->SetRotate(r[0], r[1], r[2]);
00198     }
00199     else if (keyatom == nsSVGAtoms::skewX) {
00200       // x-angle
00201       float angle;
00202       PRInt32 num_parsed = ParseParameterList(args, &angle, 1);
00203       if (num_parsed != 1) {
00204         rv = NS_ERROR_FAILURE;
00205         break; // parse error
00206       }
00207 
00208       transform->SetSkewX(angle);
00209     }
00210     else if (keyatom == nsSVGAtoms::skewY) {
00211       // y-angle
00212       float angle;
00213       PRInt32 num_parsed = ParseParameterList(args, &angle, 1);
00214       if (num_parsed != 1) {
00215         rv = NS_ERROR_FAILURE;
00216         break; // parse error
00217       }
00218 
00219       transform->SetSkewY(angle);
00220     }
00221     else if (keyatom == nsSVGAtoms::matrix) {
00222       // a b c d e f
00223       float m[6];
00224       PRInt32 num_parsed = ParseParameterList(args, m, 6);
00225       if (num_parsed != 6) {
00226         rv = NS_ERROR_FAILURE;
00227         break; // parse error
00228       }
00229 
00230       nsCOMPtr<nsIDOMSVGMatrix> matrix;
00231       NS_NewSVGMatrix(getter_AddRefs(matrix),
00232                       m[0], m[1], m[2], m[3], m[4], m[5]);
00233       NS_ASSERTION(matrix, "couldn't create matrix");
00234       transform->SetMatrix(matrix);
00235     }
00236     else { // parse error
00237       rv = NS_ERROR_FAILURE;
00238       break;
00239     }
00240     xforms.AppendObject(transform);
00241   }
00242 
00243   if (keyword || NS_FAILED(rv)) { 
00244     // there was a parse error. 
00245     rv = NS_ERROR_FAILURE;
00246     NS_ERROR("transform-attribute parse error");
00247   }
00248   else {
00249     WillModify();
00250     ReleaseTransforms();
00251     PRInt32 count = xforms.Count();
00252     for (PRInt32 i=0; i<count; ++i) {
00253       AppendElement(xforms.ObjectAt(i));
00254     }
00255     DidModify();
00256   }
00257 
00258   nsMemory::Free(str);
00259   
00260   return rv;
00261 }
00262 
00263 // helper for SetValueString
00264 // parse up to nvars comma-separated parameters into vars, returning
00265 // the number of variables actually provided.
00266 //
00267 // -1 will be returned if any of the arguments can't be converted to a
00268 // float.  The return value may be higher than nvars, if more
00269 // arguments were provided; no attempt is made to actually parse any
00270 // more arguments than nvars.
00271 //
00272 // note that this would accept ",,," and will just return 0
00273 // arguments found, instead of -1.  This is because the numeric
00274 // values can have spaces surrounding them, but the spaces can also
00275 // be used as delimiters.  strtok doesn't tell us what the
00276 // delimiter is, so we have no way to distinguish
00277 // ' 20,20 ' from ',,20,20,,' (or from ',,20 20,,', for that matter).
00278 //
00279 // XXX If someone knows for sure that it's not ok to mix commas and
00280 // spaces as delimiters, then we can scan the string for a comma,
00281 // and if found use a delimiter of just ",", otherwise use a set of
00282 // spaces (without a comma).
00283 PRInt32
00284 nsSVGTransformList::ParseParameterList(char *paramstr, float *vars, PRInt32 nvars)
00285 {
00286   if (!paramstr)
00287     return 0;
00288 
00289   char *arg, *argend, *argrest = paramstr;
00290   int num_args_found = 0;
00291   float f;
00292 
00293   const char arg_delimiters[] = "\x20\x09\x0d\x0a,";
00294 
00295   while ((arg = nsCRT::strtok(argrest, arg_delimiters, &argrest))) {
00296     if (num_args_found < nvars) {
00297       f = (float) PR_strtod(arg, &argend);
00298       if (arg == argend || *argend != '\0')
00299         return -1;
00300 
00301       vars[num_args_found] = f;
00302     }
00303 
00304     arg = argrest;
00305     num_args_found++;
00306   }
00307 
00308   return num_args_found;
00309 }
00310 
00311 NS_IMETHODIMP
00312 nsSVGTransformList::GetValueString(nsAString& aValue)
00313 {
00314   aValue.Truncate();
00315 
00316   PRInt32 count = mTransforms.Count();
00317 
00318   if (count<=0) return NS_OK;
00319 
00320   PRInt32 i = 0;
00321   
00322   while (1) {
00323     nsIDOMSVGTransform* transform = ElementAt(i);
00324 
00325     nsCOMPtr<nsISVGValue> val = do_QueryInterface(transform);
00326     nsAutoString str;
00327     val->GetValueString(str);
00328     aValue.Append(str);
00329 
00330     if (++i >= count) break;
00331 
00332     aValue.AppendLiteral(" ");
00333   }
00334   
00335   return NS_OK;
00336 
00337 }
00338 
00339 //----------------------------------------------------------------------
00340 // nsIDOMSVGTransformList methods:
00341 
00342 /* readonly attribute unsigned long numberOfItems; */
00343 NS_IMETHODIMP nsSVGTransformList::GetNumberOfItems(PRUint32 *aNumberOfItems)
00344 {
00345   *aNumberOfItems = mTransforms.Count();
00346   return NS_OK;
00347 }
00348 
00349 /* void clear (); */
00350 NS_IMETHODIMP nsSVGTransformList::Clear()
00351 {
00352   WillModify();
00353   ReleaseTransforms();
00354   DidModify();
00355   return NS_OK;
00356 }
00357 
00358 /* nsIDOMSVGTransform initialize (in nsIDOMSVGTransform newItem); */
00359 NS_IMETHODIMP nsSVGTransformList::Initialize(nsIDOMSVGTransform *newItem,
00360                                              nsIDOMSVGTransform **_retval)
00361 {
00362   *_retval = newItem;
00363   if (!newItem)
00364     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00365 
00366   nsSVGValueAutoNotifier autonotifier(this);
00367 
00368   ReleaseTransforms();
00369   if (!AppendElement(newItem)) {
00370     *_retval = nsnull;
00371     return NS_ERROR_OUT_OF_MEMORY;
00372   }
00373 
00374   NS_ADDREF(*_retval);
00375   return NS_OK;
00376 }
00377 
00378 /* nsIDOMSVGTransform getItem (in unsigned long index); */
00379 NS_IMETHODIMP nsSVGTransformList::GetItem(PRUint32 index, nsIDOMSVGTransform **_retval)
00380 {
00381   if (index >= NS_STATIC_CAST(PRUint32, mTransforms.Count())) {
00382     *_retval = nsnull;
00383     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00384   }
00385 
00386   *_retval  = ElementAt(index);
00387   NS_ADDREF(*_retval);
00388   return NS_OK;
00389 }
00390 
00391 /* nsIDOMSVGTransform insertItemBefore (in nsIDOMSVGTransform newItem, in unsigned long index); */
00392 NS_IMETHODIMP nsSVGTransformList::InsertItemBefore(nsIDOMSVGTransform *newItem,
00393                                                    PRUint32 index,
00394                                                    nsIDOMSVGTransform **_retval)
00395 {
00396   *_retval = newItem;
00397   if (!newItem)
00398     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00399 
00400   nsSVGValueAutoNotifier autonotifier(this);
00401 
00402   PRUint32 count = mTransforms.Count();
00403 
00404   if (!mTransforms.InsertElementAt((void*)newItem, (index < count)? index: count)) {
00405     *_retval = nsnull;
00406     return NS_ERROR_OUT_OF_MEMORY;
00407   }
00408 
00409   NS_ADDREF(newItem);
00410   nsCOMPtr<nsISVGValue> val = do_QueryInterface(newItem);
00411   if (val)
00412     val->AddObserver(this);
00413 
00414   NS_ADDREF(*_retval);
00415   return NS_OK;
00416 }
00417 
00418 /* nsIDOMSVGTransform replaceItem (in nsIDOMSVGTransform newItem, in unsigned long index); */
00419 NS_IMETHODIMP nsSVGTransformList::ReplaceItem(nsIDOMSVGTransform *newItem,
00420                                               PRUint32 index,
00421                                               nsIDOMSVGTransform **_retval)
00422 {
00423   if (!newItem)
00424     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00425 
00426   *_retval = nsnull;
00427 
00428   nsSVGValueAutoNotifier autonotifier(this);
00429 
00430   if (index >= PRUint32(mTransforms.Count()))
00431     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00432 
00433   nsIDOMSVGTransform* oldItem = ElementAt(index);
00434 
00435   if (!mTransforms.ReplaceElementAt((void*)newItem, index)) {
00436     NS_NOTREACHED("removal of element failed");
00437     return NS_ERROR_UNEXPECTED;
00438   }
00439 
00440   nsCOMPtr<nsISVGValue> val = do_QueryInterface(oldItem);
00441   if (val)
00442     val->RemoveObserver(this);
00443   NS_RELEASE(oldItem);
00444   val = do_QueryInterface(newItem);
00445   if (val)
00446     val->AddObserver(this);
00447   NS_ADDREF(newItem);
00448 
00449   *_retval = newItem;
00450   NS_ADDREF(*_retval);
00451   return NS_OK;
00452 }
00453 
00454 /* nsIDOMSVGTransform removeItem (in unsigned long index); */
00455 NS_IMETHODIMP nsSVGTransformList::RemoveItem(PRUint32 index, nsIDOMSVGTransform **_retval)
00456 {
00457   nsSVGValueAutoNotifier autonotifier(this);
00458 
00459   if (index >= NS_STATIC_CAST(PRUint32, mTransforms.Count())) {
00460     *_retval = nsnull;
00461     return NS_ERROR_DOM_INDEX_SIZE_ERR;
00462   }
00463 
00464   *_retval = ElementAt(index);
00465 
00466   if (!mTransforms.RemoveElementAt(index)) {
00467     NS_NOTREACHED("removal of element failed");
00468     *_retval = nsnull;
00469     return NS_ERROR_UNEXPECTED;
00470   }
00471 
00472   nsCOMPtr<nsISVGValue> val = do_QueryInterface(*_retval);
00473   if (val)
00474     val->RemoveObserver(this);
00475 
00476   // don't NS_ADDREF(*_retval)
00477   return NS_OK;
00478 }
00479 
00480 /* nsIDOMSVGTransform appendItem (in nsIDOMSVGTransform newItem); */
00481 NS_IMETHODIMP nsSVGTransformList::AppendItem(nsIDOMSVGTransform *newItem,
00482                                              nsIDOMSVGTransform **_retval)
00483 {
00484   *_retval = newItem;
00485   if (!newItem)
00486     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00487 
00488   nsSVGValueAutoNotifier autonotifier(this);
00489 
00490   if (!AppendElement(newItem)) {
00491     *_retval = nsnull;
00492     return NS_ERROR_OUT_OF_MEMORY;
00493   }
00494 
00495   NS_ADDREF(*_retval);
00496   return NS_OK;
00497 }
00498 
00499 /* nsIDOMSVGTransform createSVGTransformFromMatrix (in nsIDOMSVGMatrix matrix); */
00500 NS_IMETHODIMP
00501 nsSVGTransformList::CreateSVGTransformFromMatrix(nsIDOMSVGMatrix *matrix,
00502                                                  nsIDOMSVGTransform **_retval)
00503 {
00504   if (!matrix)
00505     return NS_ERROR_DOM_SVG_WRONG_TYPE_ERR;
00506 
00507   nsresult rv = NS_NewSVGTransform(_retval);
00508   if (NS_FAILED(rv))
00509     return rv;
00510 
00511   (*_retval)->SetMatrix(matrix);
00512   return NS_OK;
00513 }
00514 
00515 /* nsIDOMSVGTransform consolidate (); */
00516 NS_IMETHODIMP nsSVGTransformList::Consolidate(nsIDOMSVGTransform **_retval)
00517 {
00518   // Note we don't want WillModify/DidModify since nothing really changes
00519 
00520   *_retval = nsnull;
00521 
00522   PRInt32 count = mTransforms.Count();
00523   if (count==0) return NS_OK;
00524   if (count==1) {
00525     *_retval = ElementAt(0);
00526     NS_ADDREF(*_retval);
00527     return NS_OK;
00528   }
00529 
00530   nsCOMPtr<nsIDOMSVGMatrix> conmatrix;
00531   nsresult rv = GetConsolidationMatrix(getter_AddRefs(conmatrix));
00532   if (NS_FAILED(rv))
00533     return rv;
00534 
00535   nsCOMPtr<nsIDOMSVGTransform> consolidation;
00536   rv = CreateSVGTransformFromMatrix(conmatrix, getter_AddRefs(consolidation));
00537   if (NS_FAILED(rv))
00538     return rv;
00539 
00540   ReleaseTransforms();
00541   if (!AppendElement(consolidation))
00542     return NS_ERROR_OUT_OF_MEMORY;
00543 
00544   *_retval = consolidation;
00545   NS_ADDREF(*_retval);
00546   return rv;
00547 }
00548 
00549 /* nsIDOMSVGMatrix getConsolidation (); */
00550 NS_IMETHODIMP nsSVGTransformList::GetConsolidationMatrix(nsIDOMSVGMatrix **_retval)
00551 {
00552   *_retval = nsnull;
00553   PRInt32 count = mTransforms.Count();
00554 
00555   nsCOMPtr<nsIDOMSVGMatrix> conmatrix;
00556   nsresult rv = NS_NewSVGMatrix(getter_AddRefs(conmatrix));
00557   if (NS_FAILED(rv))
00558     return rv;
00559   
00560   nsCOMPtr<nsIDOMSVGMatrix> temp1, temp2;
00561 
00562   for (PRInt32 i = 0; i < count; ++i) {
00563     nsIDOMSVGTransform* transform = ElementAt(i);
00564     transform->GetMatrix(getter_AddRefs(temp1));
00565     conmatrix->Multiply(temp1, getter_AddRefs(temp2));
00566     if (!temp2)
00567       return NS_ERROR_OUT_OF_MEMORY;
00568     conmatrix = temp2;
00569   }
00570 
00571   *_retval = conmatrix;
00572   NS_ADDREF(*_retval);
00573   return NS_OK;
00574 }
00575 
00576 
00577 //----------------------------------------------------------------------
00578 // nsISVGValueObserver methods
00579 
00580 NS_IMETHODIMP
00581 nsSVGTransformList::WillModifySVGObservable(nsISVGValue* observable,
00582                                             modificationType aModType)
00583 {
00584   WillModify(aModType);
00585   return NS_OK;
00586 }
00587 
00588 NS_IMETHODIMP
00589 nsSVGTransformList::DidModifySVGObservable (nsISVGValue* observable,
00590                                             modificationType aModType)
00591 {
00592   DidModify(aModType);
00593   return NS_OK;
00594 }