Back to index

lightning-sunbird  0.9+nobinonly
nsSVGMarkerFrame.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 IBM Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2004
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either of the GNU General Public License Version 2 or later (the "GPL"),
00025  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "nsIDOMSVGFitToViewBox.h"
00038 #include "nsIDOMSVGAnimatedLength.h"
00039 #include "nsIDOMSVGAnimatedEnum.h"
00040 #include "nsIDOMSVGAnimatedAngle.h"
00041 #include "nsIDOMSVGAnimatedRect.h"
00042 #include "nsIDOMSVGAngle.h"
00043 #include "nsIDOMSVGLength.h"
00044 #include "nsIDOMSVGPoint.h"
00045 #include "nsIDOMSVGRect.h"
00046 #include "nsIDOMSVGRectElement.h"
00047 #include "nsIDOMSVGElement.h"
00048 #include "nsIDOMSVGSVGElement.h"
00049 #include "nsISVGRendererPathBuilder.h"
00050 #include "nsSVGDefsFrame.h"
00051 #include "nsISVGValue.h"
00052 #include "nsIDOMSVGMarkerElement.h"
00053 #include "nsIDOMDocument.h"
00054 #include "nsIDocument.h"
00055 #include "nsSVGMarkerFrame.h"
00056 #include "nsSVGPathGeometryFrame.h"
00057 #include "nsISVGRendererCanvas.h"
00058 #include "nsSVGUtils.h"
00059 #include "nsSVGMatrix.h"
00060 
00061 NS_IMETHODIMP_(nsrefcnt)
00062   nsSVGMarkerFrame::AddRef()
00063 {
00064   return NS_OK;
00065 }
00066 
00067 NS_IMETHODIMP_(nsrefcnt)
00068   nsSVGMarkerFrame::Release()
00069 {
00070   return NS_OK;
00071 }
00072 
00073 // Trying to implement a QueryInterfacable class without a nsIFoo class.
00074 // Couldn't find a macro incantation for this situation, so roll by hand.
00075 NS_IMETHODIMP
00076 nsSVGMarkerFrame::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00077 {
00078   if (nsnull == aInstancePtr) {
00079     return NS_ERROR_NULL_POINTER;
00080   }
00081   if (aIID.Equals(nsSVGMarkerFrame::GetCID())) {
00082     *aInstancePtr = (void*)(nsSVGMarkerFrame*)this;
00083     NS_ADDREF_THIS();
00084     return NS_OK;
00085   }
00086   return (nsSVGDefsFrame::QueryInterface(aIID, aInstancePtr));
00087 }
00088 
00089 nsresult
00090 NS_NewSVGMarkerFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame)
00091 {
00092   *aNewFrame = nsnull;
00093 
00094   nsSVGMarkerFrame* it = new (aPresShell) nsSVGMarkerFrame;
00095   if (nsnull == it)
00096     return NS_ERROR_OUT_OF_MEMORY;
00097 
00098   *aNewFrame = it;
00099 
00100   return NS_OK;
00101 }
00102 
00103 nsresult
00104 NS_GetSVGMarkerFrame(nsSVGMarkerFrame **aResult, nsIURI *aURI, nsIContent *aContent)
00105 {
00106   *aResult = nsnull;
00107 
00108   // Get the PresShell
00109   nsIDocument *myDoc = aContent->GetCurrentDoc();
00110   if (!myDoc) {
00111     NS_WARNING("No document for this content!");
00112     return NS_ERROR_FAILURE;
00113   }
00114   nsIPresShell *aPresShell = myDoc->GetShellAt(0);
00115 
00116   // Get the URI Spec
00117   nsCAutoString uriSpec;
00118   aURI->GetSpec(uriSpec);
00119 
00120   // Find the referenced frame
00121   nsIFrame *marker;
00122   if (!NS_SUCCEEDED(nsSVGUtils::GetReferencedFrame(&marker, 
00123                                                    uriSpec, aContent, aPresShell)))
00124     return NS_ERROR_FAILURE;
00125 
00126   nsIAtom* frameType = marker->GetType();
00127   if (frameType != nsLayoutAtoms::svgMarkerFrame)
00128     return NS_ERROR_FAILURE;
00129 
00130   *aResult = (nsSVGMarkerFrame *)marker;
00131   return NS_OK;
00132 }
00133 
00134 nsSVGMarkerFrame::~nsSVGMarkerFrame()
00135 {
00136   nsCOMPtr<nsISVGValue> value;
00137   if (mRefX && (value = do_QueryInterface(mRefX)))
00138     value->RemoveObserver(this);
00139   if (mRefY && (value = do_QueryInterface(mRefY)))
00140     value->RemoveObserver(this);
00141   if (mMarkerWidth && (value = do_QueryInterface(mMarkerWidth)))
00142     value->RemoveObserver(this);
00143   if (mMarkerHeight && (value = do_QueryInterface(mMarkerHeight)))
00144     value->RemoveObserver(this);
00145   if (mOrientAngle && (value = do_QueryInterface(mOrientAngle)))
00146     value->RemoveObserver(this);
00147   if (mViewBox && (value = do_QueryInterface(mViewBox)))
00148     value->RemoveObserver(this);
00149 }
00150 
00151 NS_IMETHODIMP
00152 nsSVGMarkerFrame::InitSVG()
00153 {
00154   nsresult rv = nsSVGDefsFrame::InitSVG();
00155   if (NS_FAILED(rv))
00156     return rv;
00157 
00158   nsCOMPtr<nsIDOMSVGMarkerElement> marker = do_QueryInterface(mContent);
00159   NS_ASSERTION(marker, "wrong content element");
00160 
00161   {
00162     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00163     marker->GetRefX(getter_AddRefs(length));
00164     length->GetAnimVal(getter_AddRefs(mRefX));
00165     NS_ASSERTION(mRefX, "no RefX");
00166     if (!mRefX) return NS_ERROR_FAILURE;
00167     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mRefX);
00168     if (value)
00169       value->AddObserver(this);
00170   }
00171 
00172   {
00173     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00174     marker->GetRefY(getter_AddRefs(length));
00175     length->GetAnimVal(getter_AddRefs(mRefY));
00176     NS_ASSERTION(mRefY, "no RefY");
00177     if (!mRefY) return NS_ERROR_FAILURE;
00178     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mRefY);
00179     if (value)
00180       value->AddObserver(this);
00181   }
00182 
00183   {
00184     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00185     marker->GetMarkerWidth(getter_AddRefs(length));
00186     length->GetAnimVal(getter_AddRefs(mMarkerWidth));
00187     NS_ASSERTION(mMarkerWidth, "no markerWidth");
00188     if (!mMarkerWidth) return NS_ERROR_FAILURE;
00189     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mMarkerWidth);
00190     if (value)
00191       value->AddObserver(this);
00192   }
00193 
00194   {
00195     nsCOMPtr<nsIDOMSVGAnimatedLength> length;
00196     marker->GetMarkerHeight(getter_AddRefs(length));
00197     length->GetAnimVal(getter_AddRefs(mMarkerHeight));
00198     NS_ASSERTION(mMarkerHeight, "no markerHeight");
00199     if (!mMarkerHeight) return NS_ERROR_FAILURE;
00200     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mMarkerHeight);
00201     if (value)
00202       value->AddObserver(this);
00203   }
00204 
00205   {
00206     nsCOMPtr<nsIDOMSVGAnimatedAngle> angle;
00207     marker->GetOrientAngle(getter_AddRefs(angle));
00208     angle->GetAnimVal(getter_AddRefs(mOrientAngle));
00209     NS_ASSERTION(mOrientAngle, "no orientAngle");
00210     if (!mOrientAngle) return NS_ERROR_FAILURE;
00211     nsCOMPtr<nsISVGValue> value = do_QueryInterface(mOrientAngle);
00212     if (value)
00213       value->AddObserver(this);
00214   }
00215 
00216   {
00217     nsCOMPtr<nsIDOMSVGAnimatedRect> rect;
00218     nsCOMPtr<nsIDOMSVGFitToViewBox> box = do_QueryInterface(marker);
00219     box->GetViewBox(getter_AddRefs(rect));
00220 
00221     if (rect) {
00222       rect->GetAnimVal(getter_AddRefs(mViewBox));
00223       NS_ASSERTION(mRefY, "no viewBox");
00224       if (!mRefY) return NS_ERROR_FAILURE;
00225       nsCOMPtr<nsISVGValue> value = do_QueryInterface(mRefY);
00226       if (value)
00227         value->AddObserver(this);
00228     }
00229   }
00230 
00231   marker->GetMarkerUnits(getter_AddRefs(mMarkerUnits));
00232   marker->GetOrientType(getter_AddRefs(mOrientType));
00233 
00234   mMarkerParent = nsnull;
00235   mInUse = mInUse2 = PR_FALSE;
00236 
00237   return NS_OK;
00238 }
00239 
00240 //----------------------------------------------------------------------
00241 // nsISVGValueObserver methods:
00242 
00243 NS_IMETHODIMP
00244 nsSVGMarkerFrame::DidModifySVGObservable(nsISVGValue* observable,
00245                                          nsISVGValue::modificationType aModType)
00246 {
00247   return nsSVGDefsFrame::DidModifySVGObservable(observable, aModType);
00248 }
00249 
00250 //----------------------------------------------------------------------
00251 // nsISVGContainerFrame methods:
00252 already_AddRefed<nsIDOMSVGMatrix>
00253 nsSVGMarkerFrame::GetCanvasTM()
00254 {
00255   if (mInUse2) {
00256     // really we should return null, but the rest of the SVG code
00257     // isn't set up for that.  We're going to be bailing drawing the
00258     // marker anyway, so return an identity.
00259     nsCOMPtr<nsIDOMSVGMatrix> ident;
00260     NS_NewSVGMatrix(getter_AddRefs(ident));
00261 
00262     nsIDOMSVGMatrix *retval = ident.get();
00263     NS_IF_ADDREF(retval);
00264     return retval;
00265   }
00266 
00267   mInUse2 = PR_TRUE;
00268 
00269   // get our parent's tm and append local transform
00270   nsCOMPtr<nsIDOMSVGMatrix> parentTM;
00271   if (mMarkerParent) {
00272     nsISVGGeometrySource *geometrySource;
00273     mMarkerParent->QueryInterface(NS_GET_IID(nsISVGGeometrySource),
00274                                   (void**)&geometrySource);
00275     if (!geometrySource) {
00276       NS_ERROR("invalid parent");
00277       mInUse2 = PR_FALSE;
00278       return nsnull;
00279     }
00280     geometrySource->GetCanvasTM(getter_AddRefs(parentTM));
00281   } else {
00282     // <svg:defs> 
00283     nsISVGContainerFrame *containerFrame;
00284     mParent->QueryInterface(NS_GET_IID(nsISVGContainerFrame),
00285                             (void**)&containerFrame);
00286     if (!containerFrame) {
00287       NS_ERROR("invalid parent");
00288       mInUse2 = PR_FALSE;
00289       return nsnull;
00290     }
00291     parentTM = containerFrame->GetCanvasTM();
00292   }
00293   NS_ASSERTION(parentTM, "null TM");
00294 
00295   // get element
00296   nsCOMPtr<nsIDOMSVGMarkerElement> element = do_QueryInterface(mContent);
00297 
00298   // scale/move marker
00299   nsCOMPtr<nsIDOMSVGMatrix> markerTM;
00300   element->GetMarkerTransform(mStrokeWidth, mX, mY, mAngle, getter_AddRefs(markerTM));
00301 
00302   // viewport marker
00303   nsCOMPtr<nsIDOMSVGMatrix> viewTM;
00304   element->GetViewboxToViewportTransform(getter_AddRefs(viewTM));
00305 
00306   nsCOMPtr<nsIDOMSVGMatrix> tmpTM;
00307   nsCOMPtr<nsIDOMSVGMatrix> resultTM;
00308 
00309   parentTM->Multiply(markerTM, getter_AddRefs(tmpTM));
00310   tmpTM->Multiply(viewTM, getter_AddRefs(resultTM));
00311 
00312   nsIDOMSVGMatrix *retval = resultTM.get();
00313   NS_IF_ADDREF(retval);
00314 
00315   mInUse2 = PR_FALSE;
00316 
00317   return retval;
00318 }
00319 
00320 
00321 void
00322 nsSVGMarkerFrame::PaintMark(nsISVGRendererCanvas *aCanvas,
00323                             nsSVGPathGeometryFrame *aParent,
00324                             nsSVGMark *aMark, float aStrokeWidth)
00325 {
00326   // If the flag is set when we get here, it means this marker frame
00327   // has already been used painting the current mark, and the document
00328   // has a marker reference loop.
00329   if (mInUse)
00330     return;
00331 
00332   mInUse = PR_TRUE;
00333   mStrokeWidth = aStrokeWidth;
00334   mX = aMark->x;
00335   mY = aMark->y;
00336   mAngle = aMark->angle;
00337   mMarkerParent = aParent;
00338 
00339   if (GetStyleDisplay()->IsScrollableOverflow()) {
00340     aCanvas->PushClip();
00341 
00342     nsCOMPtr<nsIDOMSVGMatrix> parentTransform, markerTransform, clipTransform;
00343     nsCOMPtr<nsIDOMSVGMatrix> viewTransform;
00344 
00345     nsISVGGeometrySource *parent;
00346     CallQueryInterface(mMarkerParent, &parent);
00347     if (parent)
00348       parent->GetCanvasTM(getter_AddRefs(parentTransform));
00349 
00350     nsCOMPtr<nsIDOMSVGMarkerElement> element = do_QueryInterface(mContent);
00351     element->GetMarkerTransform(mStrokeWidth, mX, mY, mAngle,
00352                                 getter_AddRefs(markerTransform));
00353 
00354     element->GetViewboxToViewportTransform(getter_AddRefs(viewTransform));
00355 
00356     if (parentTransform && markerTransform)
00357       parentTransform->Multiply(markerTransform,
00358                                 getter_AddRefs(clipTransform));
00359 
00360     if (clipTransform && viewTransform) {
00361       float x, y, width, height;
00362 
00363       viewTransform->GetE(&x);
00364       viewTransform->GetF(&y);
00365       mMarkerWidth->GetValue(&width);
00366       mMarkerHeight->GetValue(&height);
00367       aCanvas->SetClipRect(clipTransform, x, y, width, height);
00368     }
00369   }
00370 
00371   nsRect dirtyRectTwips;
00372   for (nsIFrame* kid = mFrames.FirstChild(); kid;
00373        kid = kid->GetNextSibling()) {
00374     nsISVGChildFrame* SVGFrame=nsnull;
00375     kid->QueryInterface(NS_GET_IID(nsISVGChildFrame),(void**)&SVGFrame);
00376     if (SVGFrame) {
00377       SVGFrame->NotifyCanvasTMChanged();
00378       SVGFrame->PaintSVG(aCanvas, dirtyRectTwips);
00379     }
00380   }
00381 
00382   if (GetStyleDisplay()->IsScrollableOverflow())
00383     aCanvas->PopClip();
00384 
00385   mMarkerParent = nsnull;
00386   mInUse = PR_FALSE;
00387 }
00388 
00389 
00390 NS_IMETHODIMP_(already_AddRefed<nsISVGRendererRegion>)
00391   nsSVGMarkerFrame::RegionMark(nsSVGPathGeometryFrame *aParent,
00392                                nsSVGMark *aMark, float aStrokeWidth)
00393 {
00394   // If the flag is set when we get here, it means this marker frame
00395   // has already been used in calculating the current mark region, and
00396   // the document has a marker reference loop.
00397   if (mInUse)
00398     return nsnull;
00399 
00400   mInUse = PR_TRUE;
00401 
00402   mStrokeWidth = aStrokeWidth;
00403   mX = aMark->x;
00404   mY = aMark->y;
00405   mAngle = aMark->angle;
00406   mMarkerParent = aParent;
00407 
00408   nsISVGRendererRegion *accu_region=nsnull;
00409   
00410   nsIFrame* kid = mFrames.FirstChild();
00411   while (kid) {
00412     nsISVGChildFrame* SVGFrame=0;
00413     kid->QueryInterface(NS_GET_IID(nsISVGChildFrame),(void**)&SVGFrame);
00414     if (SVGFrame) {
00415       SVGFrame->NotifyCanvasTMChanged();
00416 
00417       nsCOMPtr<nsISVGRendererRegion> dirty_region = SVGFrame->GetCoveredRegion();
00418       if (dirty_region) {
00419         if (accu_region) {
00420           nsCOMPtr<nsISVGRendererRegion> temp = dont_AddRef(accu_region);
00421           dirty_region->Combine(temp, &accu_region);
00422         }
00423         else {
00424           accu_region = dirty_region;
00425           NS_IF_ADDREF(accu_region);
00426         }
00427       }
00428     }
00429     kid = kid->GetNextSibling();
00430   }
00431   mMarkerParent = nsnull;
00432 
00433   mInUse = PR_FALSE;
00434 
00435   return accu_region;
00436 }
00437 
00438 
00439 float
00440 nsSVGMarkerFrame::bisect(float a1, float a2)
00441 {
00442   if (a2 - a1 < M_PI)
00443     return (a1+a2)/2;
00444   else
00445     return M_PI + (a1+a2)/2;
00446 }
00447 
00448 nsIAtom *
00449 nsSVGMarkerFrame::GetType() const
00450 {
00451   return nsLayoutAtoms::svgMarkerFrame;
00452 }