Back to index

lightning-sunbird  0.9+nobinonly
nsSVGCairoSurface.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 Cairo Renderer 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 "nsISupports.h"
00038 #include "nsCOMPtr.h"
00039 #include "nsSVGCairoSurface.h"
00040 #include "nsISVGCairoSurface.h"
00041 #include "prmem.h"
00042 #include "cairo.h"
00043 
00048 
00049 
00052 class nsSVGCairoSurface : public nsISVGCairoSurface
00053 {
00054 public:
00055   nsSVGCairoSurface();
00056   ~nsSVGCairoSurface();
00057   nsresult Init(PRUint32 width, PRUint32 height);
00058     
00059   // nsISupports interface:
00060   NS_DECL_ISUPPORTS
00061     
00062   // nsISVGRendererSurface interface:
00063   NS_DECL_NSISVGRENDERERSURFACE
00064 
00065   // nsISVGCairoSurface interface:
00066   NS_IMETHOD_(cairo_surface_t*) GetSurface() { return mSurface; }
00067     
00068 private:
00069   PRUint8 *mData;
00070   cairo_surface_t *mSurface;
00071   PRUint32 mWidth, mHeight;
00072 };
00073 
00074 
00077 //----------------------------------------------------------------------
00078 // implementation:
00079 
00080 nsSVGCairoSurface::nsSVGCairoSurface() : mData(nsnull), mSurface(nsnull)
00081 {
00082 }
00083 
00084 nsSVGCairoSurface::~nsSVGCairoSurface()
00085 {
00086   if (mSurface) {
00087     cairo_surface_destroy(mSurface);
00088     mSurface = nsnull;
00089   }
00090   if (mData) {
00091     PR_Free(mData);
00092     mData = nsnull;
00093   }
00094 }
00095 
00096 static PRBool
00097 CheckSaneImageSize(PRUint32 width, PRUint32 height)
00098 {
00099     if (width <= 0 || height <= 0)
00100         return PR_FALSE;
00101 
00102     /* check to make sure we don't overflow a 32-bit */
00103     PRUint32 tmp = width * height;
00104     if (tmp / height != width)
00105         return PR_FALSE;
00106 
00107     tmp = tmp * 4;
00108     if (tmp / 4 != width * height)
00109         return PR_FALSE;
00110 
00111     /* reject over-wide or over-tall images */
00112     const PRUint32 k64KLimit = 0x0000FFFF;
00113     if (width > k64KLimit || height > k64KLimit)
00114         return PR_FALSE;
00115 
00116     return PR_TRUE;
00117 }
00118 
00119 nsresult
00120 nsSVGCairoSurface::Init(PRUint32 width, PRUint32 height)
00121 {
00122   mWidth = width;
00123   mHeight = height;
00124 
00125   if (!CheckSaneImageSize(width, height))
00126     return NS_ERROR_FAILURE;
00127 
00128   mData = (PRUint8 *)PR_Malloc(4*width*height);
00129 
00130   if (!mData)
00131     return NS_ERROR_OUT_OF_MEMORY;
00132 
00133   memset(mData, 0, 4*width*height);
00134   mSurface = cairo_image_surface_create_for_data(mData, CAIRO_FORMAT_ARGB32,
00135                                                  mWidth, mHeight, 4*mWidth);
00136   if (!mSurface)
00137     return NS_ERROR_FAILURE;
00138 
00139   return NS_OK;
00140 }
00141 
00142 nsresult
00143 NS_NewSVGCairoSurface(nsISVGRendererSurface **result,
00144                       PRUint32 width, PRUint32 height)
00145 {
00146   nsSVGCairoSurface* surf = new nsSVGCairoSurface();
00147   if (!surf)
00148     return NS_ERROR_OUT_OF_MEMORY;
00149 
00150   NS_ADDREF(surf);
00151 
00152   nsresult rv = surf->Init(width, height);
00153 
00154   if (NS_FAILED(rv)) {
00155     NS_RELEASE(surf);
00156     return rv;
00157   }
00158   
00159   *result = surf;
00160   return rv;
00161 }
00162 
00163 //----------------------------------------------------------------------
00164 // nsISupports methods:
00165 
00166 NS_IMPL_ADDREF(nsSVGCairoSurface)
00167 NS_IMPL_RELEASE(nsSVGCairoSurface)
00168 
00169 NS_INTERFACE_MAP_BEGIN(nsSVGCairoSurface)
00170   NS_INTERFACE_MAP_ENTRY(nsISVGRendererSurface)
00171   NS_INTERFACE_MAP_ENTRY(nsISVGCairoSurface)
00172   NS_INTERFACE_MAP_ENTRY(nsISupports)
00173 NS_INTERFACE_MAP_END
00174 
00175 NS_IMETHODIMP
00176 nsSVGCairoSurface::GetWidth(PRUint32 *aWidth)
00177 {
00178   *aWidth = mWidth;
00179   return NS_OK;
00180 }
00181 
00182 NS_IMETHODIMP
00183 nsSVGCairoSurface::GetHeight(PRUint32 *aHeight)
00184 {
00185   *aHeight = mHeight;
00186   return NS_OK;
00187 }
00188 
00189 NS_IMETHODIMP
00190 nsSVGCairoSurface::GetData(PRUint8 **aData, PRUint32 *length, PRInt32 *stride)
00191 {
00192   *aData = mData;
00193   *length = 4*mWidth*mHeight;
00194   *stride = 4*mWidth;
00195   return NS_OK;
00196 }
00197 
00198 NS_IMETHODIMP
00199 nsSVGCairoSurface::Lock()
00200 {
00201   return NS_OK;
00202 }
00203 
00204 NS_IMETHODIMP
00205 nsSVGCairoSurface::Unlock()
00206 {
00207   return NS_OK;
00208 }
00209