Back to index

lightning-sunbird  0.9+nobinonly
nsSVGGFrame.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  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * 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 "nsIDOMSVGTransformable.h"
00040 #include "nsSVGGFrame.h"
00041 #include "nsISVGRenderer.h"
00042 #include "nsISVGRendererSurface.h"
00043 #include "nsISVGOuterSVGFrame.h"
00044 #include "nsISVGRendererCanvas.h"
00045 #include "nsIFrame.h"
00046 #include "nsSVGMatrix.h"
00047 #include "nsSVGClipPathFrame.h"
00048 #include "nsISVGRendererCanvas.h"
00049 #include "nsLayoutAtoms.h"
00050 #include "nsSVGUtils.h"
00051 #include <math.h>
00052 
00053 //----------------------------------------------------------------------
00054 // Implementation
00055 
00056 nsresult
00057 NS_NewSVGGFrame(nsIPresShell* aPresShell, nsIContent* aContent, nsIFrame** aNewFrame)
00058 {
00059   *aNewFrame = nsnull;
00060   
00061   nsCOMPtr<nsIDOMSVGTransformable> transformable = do_QueryInterface(aContent);
00062   if (!transformable) {
00063 #ifdef DEBUG
00064     printf("warning: trying to construct an SVGGFrame for a content element that doesn't support the right interfaces\n");
00065 #endif
00066     return NS_ERROR_FAILURE;
00067   }
00068   
00069   nsSVGGFrame* it = new (aPresShell) nsSVGGFrame;
00070   if (nsnull == it)
00071     return NS_ERROR_OUT_OF_MEMORY;
00072 
00073   *aNewFrame = it;
00074 
00075   return NS_OK;
00076 }
00077 
00078 nsIAtom *
00079 nsSVGGFrame::GetType() const
00080 {
00081   return nsLayoutAtoms::svgGFrame;
00082 }
00083 
00084 //----------------------------------------------------------------------
00085 // nsISVGChildFrame methods
00086 
00087 NS_IMETHODIMP
00088 nsSVGGFrame::PaintSVG(nsISVGRendererCanvas* canvas, const nsRect& dirtyRectTwips)
00089 {
00090   nsCOMPtr<nsISVGRendererSurface> surface;
00091 
00092   const nsStyleDisplay *display = mStyleContext->GetStyleDisplay();
00093   if (display->mOpacity == 0.0)
00094     return NS_OK;
00095 
00096   nsIURI *aURI;
00097   nsSVGClipPathFrame *clip = NULL;
00098   aURI = GetStyleSVGReset()->mClipPath;
00099   if (aURI) {
00100     NS_GetSVGClipPathFrame(&clip, aURI, mContent);
00101 
00102     if (clip) {
00103       nsCOMPtr<nsIDOMSVGMatrix> matrix = GetCanvasTM();
00104       canvas->PushClip();
00105       clip->ClipPaint(canvas, this, matrix);
00106     }
00107   }
00108 
00109   if (display->mOpacity != 1.0) {
00110     nsISVGOuterSVGFrame* outerSVGFrame = GetOuterSVGFrame();
00111     if (outerSVGFrame) {
00112       nsIFrame *frame = nsnull;
00113       CallQueryInterface(outerSVGFrame, &frame);
00114 
00115       if (frame) {
00116         nsSize size = frame->GetSize();
00117         float p2t = GetPresContext()->ScaledPixelsToTwips();
00118         PRUint32 width = (PRUint32)ceil(size.width/p2t);
00119         PRUint32 height = (PRUint32)ceil(size.height/p2t);
00120         
00121         nsCOMPtr<nsISVGRenderer> renderer;
00122         outerSVGFrame->GetRenderer(getter_AddRefs(renderer));
00123         if (renderer)
00124           renderer->CreateSurface(width, height, getter_AddRefs(surface));
00125         if (surface) {
00126           if (NS_FAILED(canvas->PushSurface(surface)))
00127             surface = nsnull;
00128         }
00129       }
00130     }
00131   }
00132 
00133   for (nsIFrame* kid = mFrames.FirstChild(); kid;
00134        kid = kid->GetNextSibling()) {
00135     nsISVGChildFrame* SVGFrame=nsnull;
00136     kid->QueryInterface(NS_GET_IID(nsISVGChildFrame),(void**)&SVGFrame);
00137     if (SVGFrame)
00138       SVGFrame->PaintSVG(canvas, dirtyRectTwips);
00139   }
00140 
00141   if (surface) {
00142     canvas->PopSurface();
00143     canvas->CompositeSurface(surface, 0, 0, display->mOpacity);
00144   }
00145 
00146   if (clip)
00147     canvas->PopClip();
00148 
00149   return NS_OK;
00150 }
00151 
00152 NS_IMETHODIMP
00153 nsSVGGFrame::GetFrameForPointSVG(float x, float y, nsIFrame** hit)
00154 {
00155   *hit = nsnull;
00156   for (nsIFrame* kid = mFrames.FirstChild(); kid;
00157        kid = kid->GetNextSibling()) {
00158     nsISVGChildFrame* SVGFrame=nsnull;
00159     kid->QueryInterface(NS_GET_IID(nsISVGChildFrame),(void**)&SVGFrame);
00160     if (SVGFrame) {
00161       nsIFrame* temp=nsnull;
00162       nsresult rv = SVGFrame->GetFrameForPointSVG(x, y, &temp);
00163       if (NS_SUCCEEDED(rv) && temp) {
00164         *hit = temp;
00165         // return NS_OK; can't return. we need reverse order but only
00166         // have a singly linked list...
00167       }
00168     }
00169   }
00170 
00171   if (*hit) {
00172     PRBool clipHit = PR_TRUE;;
00173 
00174     nsIURI *aURI;
00175     nsSVGClipPathFrame *clip = NULL;
00176     aURI = GetStyleSVGReset()->mClipPath;
00177     if (aURI)
00178       NS_GetSVGClipPathFrame(&clip, aURI, mContent);
00179 
00180     if (clip) {
00181       nsCOMPtr<nsIDOMSVGMatrix> matrix = GetCanvasTM();
00182       clip->ClipHitTest(this, matrix, x, y, &clipHit);
00183     }
00184 
00185     if (!clipHit)
00186       *hit = nsnull;
00187   }
00188   
00189   return *hit ? NS_OK : NS_ERROR_FAILURE;
00190 }
00191 
00192 NS_IMETHODIMP_(already_AddRefed<nsISVGRendererRegion>)
00193 nsSVGGFrame::GetCoveredRegion()
00194 {
00195   nsISVGRendererRegion *accu_region=nsnull;
00196   
00197   nsIFrame* kid = mFrames.FirstChild();
00198   while (kid) {
00199     nsISVGChildFrame* SVGFrame=0;
00200     kid->QueryInterface(NS_GET_IID(nsISVGChildFrame),(void**)&SVGFrame);
00201     if (SVGFrame) {
00202       nsCOMPtr<nsISVGRendererRegion> dirty_region = SVGFrame->GetCoveredRegion();
00203       if (dirty_region) {
00204         if (accu_region) {
00205           nsCOMPtr<nsISVGRendererRegion> temp = dont_AddRef(accu_region);
00206           dirty_region->Combine(temp, &accu_region);
00207         }
00208         else {
00209           accu_region = dirty_region;
00210           NS_IF_ADDREF(accu_region);
00211         }
00212       }
00213     }
00214     kid = kid->GetNextSibling();
00215   }
00216   
00217   return accu_region;
00218 }
00219 
00220 NS_IMETHODIMP
00221 nsSVGGFrame::GetBBox(nsIDOMSVGRect **_retval)
00222 {
00223   return nsSVGUtils::GetBBox(&mFrames, _retval);
00224 }