Back to index

lightning-sunbird  0.9+nobinonly
nsSVGRendererLibart.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  * Alex Fritze.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex@croczilla.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or 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 "nsCOMPtr.h"
00040 #include "nsISVGRenderer.h"
00041 #include "nsSVGLibartPathGeometry.h"
00042 #include "nsSVGLibartCanvas.h"
00043 #include "nsSVGLibartRegion.h"
00044 #include "nsSVGLibartGlyphGeometryDefault.h"
00045 #include "nsSVGLibartGlyphMetricsDefault.h"
00046 #ifdef MOZ_ENABLE_FREETYPE2
00047 #include "nsString.h"
00048 #include "nsIServiceManager.h"
00049 #include "nsSVGLibartFreetype.h"
00050 #include "nsSVGLibartGlyphGeometryFT.h"
00051 #include "nsSVGLibartGlyphMetricsFT.h"
00052 #include "nsIPromptService.h"
00053 #include "nsEmbedCID.h"
00054 #endif
00055 
00060 
00061 
00064 class nsSVGRendererLibart : public nsISVGRenderer
00065 {
00066 protected:
00067   friend nsresult NS_NewSVGRendererLibart(nsISVGRenderer** aResult);
00068 
00069   nsSVGRendererLibart();
00070   virtual ~nsSVGRendererLibart();
00071 
00072 public:
00073   // nsISupports interface
00074   NS_DECL_ISUPPORTS
00075 
00076   // nsISVGRenderer interface
00077   NS_DECL_NSISVGRENDERER
00078 
00079   static PRBool sGlobalsInited;
00080 #ifdef MOZ_ENABLE_FREETYPE2
00081   static PRBool sUseFreetype;
00082 #endif
00083 };
00084 
00087 //----------------------------------------------------------------------
00088 // implementation
00089 
00090 PRBool nsSVGRendererLibart::sGlobalsInited = PR_FALSE;
00091 
00092 #ifdef MOZ_ENABLE_FREETYPE2
00093 PRBool nsSVGRendererLibart::sUseFreetype = PR_FALSE;
00094 #endif
00095 
00096 nsSVGRendererLibart::nsSVGRendererLibart()
00097 {
00098 }
00099 
00100 nsSVGRendererLibart::~nsSVGRendererLibart()
00101 {
00102 }
00103 
00104 nsresult
00105 NS_NewSVGRendererLibart(nsISVGRenderer** aResult)
00106 {
00107   NS_PRECONDITION(aResult != nsnull, "null ptr");
00108   if (! aResult)
00109     return NS_ERROR_NULL_POINTER;
00110 
00111   nsSVGRendererLibart* result = new nsSVGRendererLibart();
00112   if (! result)
00113     return NS_ERROR_OUT_OF_MEMORY;
00114 
00115   NS_ADDREF(result);
00116   *aResult = result;
00117 
00118   if (!nsSVGRendererLibart::sGlobalsInited) {
00119 #ifdef MOZ_ENABLE_FREETYPE2
00120     if (!nsSVGLibartFreetype::Init()) {
00121       nsCOMPtr<nsIPromptService> prompter(do_GetService(NS_PROMPTSERVICE_CONTRACTID));
00122       NS_NAMED_LITERAL_STRING(title, "Freetype error");
00123       NS_NAMED_LITERAL_STRING(msg, "The Libart/Freetype SVG rendering engine could not initialize the freetype library. Please go to http://www.mozilla.org/projects/fonts/unix/enabling_truetype.html and follow steps 2-7.");
00124       prompter->Alert(nsnull, title.get(), msg.get());
00125     }
00126     else if (!nsSVGLibartFreetype::HasSuitableFonts()) {
00127       nsCOMPtr<nsIPromptService> prompter(do_GetService(NS_PROMPTSERVICE_CONTRACTID));
00128       NS_NAMED_LITERAL_STRING(title, "Font Configuration Error");
00129       NS_NAMED_LITERAL_STRING(msg, "The Libart/Freetype SVG rendering engine can't find any truetype fonts on your system. Please go to http://www.mozilla.org/projects/fonts/unix/enabling_truetype.html and follow steps 2-7.");
00130       prompter->Alert(nsnull, title.get(), msg.get());
00131       nsSVGLibartFreetype::Shutdown();
00132     }
00133     else {
00134       nsSVGRendererLibart::sUseFreetype = PR_TRUE;
00135       NS_InitSVGLibartGlyphMetricsFTGlobals();
00136     }
00137 #endif    
00138     nsSVGRendererLibart::sGlobalsInited = PR_TRUE;
00139   }
00140   
00141   return NS_OK;
00142 }
00143 
00144 //----------------------------------------------------------------------
00145 // nsISupports methods
00146 
00147 NS_IMPL_ISUPPORTS1(nsSVGRendererLibart, nsISVGRenderer)
00148 
00149 //----------------------------------------------------------------------
00150 // nsISVGRenderer methods
00151 
00152 
00153 NS_IMETHODIMP
00154 nsSVGRendererLibart::CreatePathGeometry(nsISVGPathGeometrySource *src,
00155                                         nsISVGRendererPathGeometry **_retval)
00156 {
00157   return NS_NewSVGLibartPathGeometry(_retval, src);
00158 }
00159 
00161 NS_IMETHODIMP
00162 nsSVGRendererLibart::CreateGlyphMetrics(nsISVGGlyphMetricsSource *src,
00163                                         nsISVGRendererGlyphMetrics **_retval)
00164 {
00165 #ifdef MOZ_ENABLE_FREETYPE2
00166   if (sUseFreetype)
00167     return NS_NewSVGLibartGlyphMetricsFT(_retval, src);
00168 #endif
00169   return NS_NewSVGLibartGlyphMetricsDefault(_retval, src);
00170 }
00171 
00173 NS_IMETHODIMP
00174 nsSVGRendererLibart::CreateGlyphGeometry(nsISVGGlyphGeometrySource *src,
00175                                          nsISVGRendererGlyphGeometry **_retval)
00176 {
00177 #ifdef MOZ_ENABLE_FREETYPE2
00178   if (sUseFreetype)
00179     return NS_NewSVGLibartGlyphGeometryFT(_retval, src);
00180 #endif
00181   return NS_NewSVGLibartGlyphGeometryDefault(_retval, src);
00182 }
00183 
00186 NS_IMETHODIMP
00187 nsSVGRendererLibart::CreateCanvas(nsIRenderingContext *ctx,
00188                                   nsPresContext *presContext,
00189                                   const nsRect & dirtyRect,
00190                                    nsISVGRendererCanvas **_retval)
00191 {
00192   return NS_NewSVGLibartCanvas(_retval, ctx, presContext, dirtyRect);
00193 }
00194 
00196 NS_IMETHODIMP
00197 nsSVGRendererLibart::CreateRectRegion(float x, float y, float width, float height,
00198                                       nsISVGRendererRegion **_retval)
00199 {
00200   return NS_NewSVGLibartRectRegion(_retval, x, y, width, height);
00201 }
00202 
00204 NS_IMETHODIMP
00205 nsSVGRendererLibart::CreateSurface(PRUint32 width, PRUint32 height,
00206                                    nsISVGRendererSurface **_retval)
00207 {
00208   // XXX
00209   return NS_ERROR_NOT_IMPLEMENTED;
00210 }
00211 
00212 
00214 // module initialisation
00215 
00216 void NS_InitSVGRendererLibartGlobals()
00217 {
00218   // Initialization of freetype (if compiled-in) is done lazily in
00219   // NS_NewSVGRendererLibart(). The main reason is that we might want
00220   // to show error messages using the prompt service, and that's
00221   // probably not a good idea in *this* function, at a time when
00222   // layout is still not fully initialized.
00223 }
00224 
00225 void NS_FreeSVGRendererLibartGlobals()
00226 {
00227   if (!nsSVGRendererLibart::sGlobalsInited) return;
00228   
00229 #ifdef MOZ_ENABLE_FREETYPE2
00230   if (nsSVGRendererLibart::sUseFreetype) {
00231     NS_FreeSVGLibartGlyphMetricsFTGlobals();  
00232     nsSVGLibartFreetype::Shutdown();
00233   }
00234 #endif
00235 }