Back to index

lightning-sunbird  0.9+nobinonly
nsSVGGDIPlusSurface.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 GDI+ 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 <windows.h>
00038 #include <unknwn.h>
00039 #include <Gdiplus.h>
00040 using namespace Gdiplus;
00041 
00042 #include "nsISupports.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsSVGGDIPlusSurface.h"
00045 #include "nsISVGGDIPlusSurface.h"
00046 
00051 
00052 
00055 class nsSVGGDIPlusSurface : public nsISVGGDIPlusSurface
00056 {
00057 public:
00058   nsSVGGDIPlusSurface();
00059   ~nsSVGGDIPlusSurface();
00060   nsresult Init(PRUint32 width, PRUint32 height);
00061     
00062   // nsISupports interface:
00063   NS_DECL_ISUPPORTS
00064     
00065   // nsISVGRendererSurface interface:
00066   NS_DECL_NSISVGRENDERERSURFACE
00067 
00068   // nsISVGGDIPlusSurface interface:
00069   NS_IMETHOD_(Bitmap*) GetSurface() { return mSurface; }
00070     
00071 private:
00072   Bitmap *mSurface;
00073   BitmapData mBitmapData;
00074   PRUint32 mWidth, mHeight;
00075 };
00076 
00077 
00080 //----------------------------------------------------------------------
00081 // implementation:
00082 
00083 nsSVGGDIPlusSurface::nsSVGGDIPlusSurface() : mSurface(nsnull)
00084 {
00085 }
00086 
00087 nsSVGGDIPlusSurface::~nsSVGGDIPlusSurface()
00088 {
00089   if (mSurface) {
00090     delete mSurface;
00091     mSurface = nsnull;
00092   }
00093 }
00094 
00095 nsresult
00096 nsSVGGDIPlusSurface::Init(PRUint32 width, PRUint32 height)
00097 {
00098   mWidth = width;
00099   mHeight = height;
00100 
00101   mSurface = new Bitmap(width, height, PixelFormat32bppPARGB);
00102 
00103   if (!mSurface)
00104     return NS_ERROR_FAILURE;
00105 
00106   return NS_OK;
00107 }
00108 
00109 nsresult
00110 NS_NewSVGGDIPlusSurface(nsISVGRendererSurface **result,
00111                         PRUint32 width, PRUint32 height)
00112 {
00113   nsSVGGDIPlusSurface* surf = new nsSVGGDIPlusSurface();
00114   if (!surf) return NS_ERROR_OUT_OF_MEMORY;
00115 
00116   NS_ADDREF(surf);
00117 
00118   nsresult rv = surf->Init(width, height);
00119 
00120   if (NS_FAILED(rv)) {
00121     NS_RELEASE(surf);
00122     return rv;
00123   }
00124   
00125   *result = surf;
00126   return rv;
00127 }
00128 
00129 //----------------------------------------------------------------------
00130 // nsISupports methods:
00131 
00132 NS_IMPL_ADDREF(nsSVGGDIPlusSurface)
00133 NS_IMPL_RELEASE(nsSVGGDIPlusSurface)
00134 
00135 NS_INTERFACE_MAP_BEGIN(nsSVGGDIPlusSurface)
00136   NS_INTERFACE_MAP_ENTRY(nsISVGRendererSurface)
00137   NS_INTERFACE_MAP_ENTRY(nsISVGGDIPlusSurface)
00138   NS_INTERFACE_MAP_ENTRY(nsISupports)
00139 NS_INTERFACE_MAP_END
00140 
00141 NS_IMETHODIMP
00142 nsSVGGDIPlusSurface::GetWidth(PRUint32 *aWidth)
00143 {
00144   *aWidth = mWidth;
00145   return NS_OK;
00146 }
00147 
00148 NS_IMETHODIMP
00149 nsSVGGDIPlusSurface::GetHeight(PRUint32 *aHeight)
00150 {
00151   *aHeight = mHeight;
00152   return NS_OK;
00153 }
00154 
00155 NS_IMETHODIMP
00156 nsSVGGDIPlusSurface::GetData(PRUint8 **aData, PRUint32 *length, PRInt32 *stride)
00157 {
00158   *aData = (PRUint8 *)mBitmapData.Scan0;
00159   *length = abs(mBitmapData.Stride) * mBitmapData.Height;
00160   *stride = mBitmapData.Stride;
00161   return NS_OK;
00162 }
00163 
00164 NS_IMETHODIMP
00165 nsSVGGDIPlusSurface::Lock()
00166 {
00167   Rect rect(0, 0, mWidth, mHeight);
00168   mSurface->LockBits(&rect,
00169                      ImageLockModeRead | ImageLockModeWrite,
00170                      PixelFormat32bppPARGB,
00171                      &mBitmapData);
00172 
00173   return NS_OK;
00174 }
00175 
00176 NS_IMETHODIMP
00177 nsSVGGDIPlusSurface::Unlock()
00178 {
00179   mSurface->UnlockBits(&mBitmapData);
00180 
00181   return NS_OK;
00182 }
00183