Back to index

lightning-sunbird  0.9+nobinonly
nsGfxFactoryOS2.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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Christopher Blizzard.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Christopher Blizzzard <blizzard@mozilla.org>
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 "nsIGenericFactory.h"
00040 #include "nsIModule.h"
00041 #include "nsCOMPtr.h"
00042 #include "nsGfxCIID.h"
00043 
00044 #include "nsBlender.h"
00045 #include "nsFontMetricsOS2.h"
00046 #include "nsRenderingContextOS2.h"
00047 #include "nsDeviceContextSpecOS2.h"
00048 #include "nsDeviceContextSpecFactoryO.h"
00049 #include "nsScreenManagerOS2.h"
00050 #include "nsScriptableRegion.h"
00051 #include "nsDeviceContextOS2.h"
00052 #include "nsImageOS2.h"
00053 #include "nsRegionOS2.h"
00054 #include "nsPrintOptionsOS2.h"
00055 #include "nsFontList.h"
00056 #include "nsPrintSession.h"
00057 #include "gfxImageFrame.h"
00058 #include "nsIServiceManager.h"
00059 #include "prenv.h"
00060 #include "nsOS2Uni.h"
00061 #include "nsPaletteOS2.h"
00062 
00063 // objects that just require generic constructors
00064 
00065 #if !defined(USE_FREETYPE)
00066 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontMetricsOS2)
00067 #endif
00068 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextOS2)
00069 NS_GENERIC_FACTORY_CONSTRUCTOR(nsRenderingContextOS2)
00070 NS_GENERIC_FACTORY_CONSTRUCTOR(nsImageOS2)
00071 NS_GENERIC_FACTORY_CONSTRUCTOR(nsBlender)
00072 NS_GENERIC_FACTORY_CONSTRUCTOR(nsRegionOS2)
00073 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecOS2)
00074 NS_GENERIC_FACTORY_CONSTRUCTOR(nsDeviceContextSpecFactoryOS2)
00075 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontEnumeratorOS2)
00076 NS_GENERIC_FACTORY_CONSTRUCTOR(nsFontList)
00077 NS_GENERIC_FACTORY_CONSTRUCTOR(nsScreenManagerOS2)
00078 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintOptionsOS2, Init)
00079 NS_GENERIC_FACTORY_CONSTRUCTOR(nsPrinterEnumeratorOS2)
00080 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsPrintSession, Init)
00081 NS_GENERIC_FACTORY_CONSTRUCTOR(gfxImageFrame)
00082 
00083 #ifdef USE_FREETYPE
00084 // Can't include os2win.h, since it screws things up.  So definitions go here.
00085 typedef ULONG   HKEY;
00086 #define HKEY_LOCAL_MACHINE      0xFFFFFFEFL
00087 #define HKEY_CURRENT_USER       0xFFFFFFEEL
00088 #define READ_CONTROL            0x00020000
00089 #define KEY_QUERY_VALUE         0x0001
00090 #define KEY_ENUMERATE_SUB_KEYS  0x0008
00091 #define KEY_NOTIFY              0x0010
00092 #define KEY_READ                READ_CONTROL | KEY_QUERY_VALUE | \
00093                                 KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY
00094 #define ERROR_SUCCESS           0L
00095 
00096 static PRBool
00097 UseFTFunctions()
00098 {
00099   static PRBool init = PR_FALSE;
00100   if (!init) {
00101     init = PR_TRUE;
00102 
00103     // For now, since this is somewhat experimental, we'll check for an
00104     // environment variable, rather than making this the default for anyone
00105     // that uses the FT2LIB.
00106     char* useFT = PR_GetEnv("MOZILLA_USE_EXTENDED_FT2LIB");
00107     if (useFT == nsnull || *useFT == '\0' || stricmp(useFT, "t") != 0) {
00108       return PR_FALSE;
00109     }
00110 
00111     // Test for availability of registry functions and query their addresses
00112     APIRET rc;
00113     HMODULE hmod = NULLHANDLE;
00114     char LoadError[CCHMAXPATH];
00115     rc = DosLoadModule(LoadError, CCHMAXPATH, "REGISTRY", &hmod);
00116     if (rc != NO_ERROR) {
00117       NS_WARNING("REGISTRY.DLL could not be loaded");
00118       return PR_FALSE;
00119     }
00120     LONG _System (*APIENTRY RegOpenKeyEx)(HKEY, const char*, ULONG, ULONG,
00121                                            HKEY* );
00122     LONG _System (*APIENTRY RegQueryValueEx)(HKEY, const char*, ULONG*, ULONG*,
00123                                               UCHAR*, ULONG*);
00124 
00125     rc = DosQueryProcAddr(hmod, 0L, "RegOpenKeyExA", (PFN*)&RegOpenKeyEx);
00126     rc += DosQueryProcAddr(hmod, 0L, "RegQueryValueExA", (PFN*)&RegQueryValueEx);
00127     if (rc != NO_ERROR) {
00128       NS_WARNING("Registry function(s) were not found in REGISTRY.DLL");
00129       DosFreeModule(hmod);
00130       return PR_FALSE;
00131     }
00132 
00133     // Is FT2LIB enabled?
00134     HKEY key;
00135     LONG result = RegOpenKeyEx(HKEY_CURRENT_USER,
00136                                "Software\\Innotek\\InnoTek Font Engine", 0,
00137                                KEY_READ, &key);
00138     if (result != ERROR_SUCCESS) {
00139       result = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
00140                             "Software\\Innotek\\InnoTek Font Engine", 0,
00141                             KEY_READ, &key);
00142       if (result != ERROR_SUCCESS) {
00143         DosFreeModule(hmod);
00144         return PR_FALSE;
00145       }
00146     }
00147 
00148     ULONG value;
00149     ULONG length = sizeof(value);
00150     result = RegQueryValueEx(key, "Enabled", NULL, NULL, (UCHAR*)&value,
00151                              &length);
00152     if (result != ERROR_SUCCESS || value == 0) {
00153       // check if "Innotek Font Engine" is disabled (value == 0)
00154       DosFreeModule(hmod);
00155       return PR_FALSE;
00156     }
00157 
00158     // Is FT2LIB enabled for this app?
00159     PPIB ppib;
00160     PTIB ptib;
00161     char buffer[CCHMAXPATH], name[_MAX_FNAME], ext[_MAX_EXT], keystr[256];
00162     DosGetInfoBlocks(&ptib, &ppib);
00163     DosQueryModuleName(ppib->pib_hmte, CCHMAXPATH, buffer);
00164     _splitpath(buffer, NULL, NULL, name, ext);
00165     strcpy(keystr, "Software\\Innotek\\InnoTek Font Engine\\Applications\\");
00166     strcat(keystr, name);
00167     strcat(keystr, ext);
00168     result = RegOpenKeyEx(HKEY_CURRENT_USER, keystr, 0, KEY_READ, &key);
00169     if (result != ERROR_SUCCESS) {
00170       result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, keystr, 0, KEY_READ, &key);
00171       if (result != ERROR_SUCCESS) {
00172         DosFreeModule(hmod);
00173         return PR_FALSE;
00174       }
00175     }
00176     result = RegQueryValueEx(key, "Enabled", NULL, NULL, (UCHAR*)&value,
00177                              &length);
00178     if (result != ERROR_SUCCESS || value == 0) {
00179       // check if FT2LIB is disabled for our application (value == 0)
00180       DosFreeModule(hmod);
00181       return PR_FALSE;
00182     }
00183 
00184     // REGISTRY.DLL use ends here
00185     DosFreeModule(hmod);
00186 
00187     // Load lib and functions
00188     rc = DosLoadModule(LoadError, 0, "FT2LIB", &hmod);
00189     if (rc == NO_ERROR) {
00190       rc = DosQueryProcAddr(hmod, 0, "Ft2EnableFontEngine",
00191                             (PFN*)&nsFontMetricsOS2FT::pfnFt2EnableFontEngine);
00192       if (rc == NO_ERROR) {
00193         DosQueryProcAddr(hmod, 0, "Ft2FontSupportsUnicodeChar1",
00194                          (PFN*)&nsFontOS2FT::pfnFt2FontSupportsUnicodeChar1);
00195 #ifdef USE_EXPANDED_FREETYPE_FUNCS
00196         DosQueryProcAddr(hmod, 0, "Ft2QueryTextBoxW",
00197                          (PFN*)&nsFontOS2FT::pfnFt2QueryTextBoxW);
00198         DosQueryProcAddr(hmod, 0, "Ft2CharStringPosAtW",
00199                          (PFN*)&nsFontOS2FT::pfnFt2CharStringPosAtW);
00200 #endif /* use_expanded_freetype_funcs */
00201         NS_WARNING("*** Now using Freetype functions ***");
00202         nsFontMetricsOS2::gUseFTFunctions = PR_TRUE;
00203       }
00204       DosFreeModule(hmod);
00205     }
00206   }
00207   return nsFontMetricsOS2::gUseFTFunctions;
00208 }
00209 
00210 static NS_IMETHODIMP
00211 nsFontMetricsOS2Constructor(nsISupports* aOuter, REFNSIID aIID, void** aResult)
00212 {
00213   *aResult = nsnull;
00214 
00215   if (aOuter)
00216     return NS_ERROR_NO_AGGREGATION;
00217 
00218   nsFontMetricsOS2* result;
00219   if (UseFTFunctions())
00220     result = new nsFontMetricsOS2FT();
00221   else
00222     result = new nsFontMetricsOS2();
00223 
00224   if (! result)
00225     return NS_ERROR_OUT_OF_MEMORY;
00226 
00227   nsresult rv;
00228   NS_ADDREF(result);
00229   rv = result->QueryInterface(aIID, aResult);
00230   NS_RELEASE(result);
00231   return rv;
00232 }
00233 #endif /* use_freetype */
00234 
00235 static NS_IMETHODIMP
00236 nsScriptableRegionConstructor(nsISupports *aOuter, REFNSIID aIID, void **aResult)
00237 {
00238   nsresult rv;
00239 
00240   nsIScriptableRegion *inst = NULL;
00241 
00242   if ( !aResult )
00243   {
00244     rv = NS_ERROR_NULL_POINTER;
00245     return rv;
00246   }
00247   *aResult = nsnull;
00248   if (aOuter)
00249   {
00250     rv = NS_ERROR_NO_AGGREGATION;
00251     return rv;
00252   }
00253   // create an nsRegionOS2 and get the scriptable region from it
00254   nsCOMPtr <nsIRegion> rgn;
00255   NS_NEWXPCOM(rgn, nsRegionOS2);
00256   nsCOMPtr<nsIScriptableRegion> scriptableRgn;
00257   if (rgn != nsnull)
00258   {
00259     scriptableRgn = new nsScriptableRegion(rgn);
00260     inst = scriptableRgn;
00261   }
00262   if (!inst)
00263   {
00264     rv = NS_ERROR_OUT_OF_MEMORY;
00265     return rv;
00266   }
00267   NS_ADDREF(inst);
00268   // release our variable above now that we have created our owning
00269   // reference - we don't want this to go out of scope early!
00270   scriptableRgn = nsnull;
00271   rv = inst->QueryInterface(aIID, aResult);
00272   NS_RELEASE(inst);
00273 
00274   return rv;
00275 }
00276 
00277 static const nsModuleComponentInfo components[] =
00278 {
00279   { "OS/2 Font Metrics",
00280     NS_FONT_METRICS_CID,
00281     //    "@mozilla.org/gfx/font_metrics/gtk;1",
00282     "@mozilla.org/gfx/fontmetrics;1",
00283     nsFontMetricsOS2Constructor },
00284   { "OS/2 Device Context",
00285     NS_DEVICE_CONTEXT_CID,
00286     //    "@mozilla.org/gfx/device_context/gtk;1",
00287     "@mozilla.org/gfx/devicecontext;1",
00288     nsDeviceContextOS2Constructor },
00289   { "OS/2 Rendering Context",
00290     NS_RENDERING_CONTEXT_CID,
00291     //    "@mozilla.org/gfx/rendering_context/gtk;1",
00292     "@mozilla.org/gfx/renderingcontext;1",
00293     nsRenderingContextOS2Constructor },
00294   { "OS/2 Image",
00295     NS_IMAGE_CID,
00296     //    "@mozilla.org/gfx/image/gtk;1",
00297     "@mozilla.org/gfx/image;1",
00298     nsImageOS2Constructor },
00299   { "OS/2 Region",
00300     NS_REGION_CID,
00301     "@mozilla.org/gfx/region/gtk;1",
00302     nsRegionOS2Constructor },
00303   { "Scriptable Region",
00304     NS_SCRIPTABLE_REGION_CID,
00305     //    "@mozilla.org/gfx/scriptable_region;1",
00306     "@mozilla.org/gfx/region;1",
00307     nsScriptableRegionConstructor },
00308   { "Blender",
00309     NS_BLENDER_CID,
00310     //    "@mozilla.org/gfx/blender;1",
00311     "@mozilla.org/gfx/blender;1",
00312     nsBlenderConstructor },
00313   { "OS/2 Device Context Spec",
00314     NS_DEVICE_CONTEXT_SPEC_CID,
00315     //    "@mozilla.org/gfx/device_context_spec/gtk;1",
00316     "@mozilla.org/gfx/devicecontextspec;1",
00317     nsDeviceContextSpecOS2Constructor },
00318   { "OS/2 Device Context Spec Factory",
00319     NS_DEVICE_CONTEXT_SPEC_FACTORY_CID,
00320     //    "@mozilla.org/gfx/device_context_spec_factory/gtk;1",
00321     "@mozilla.org/gfx/devicecontextspecfactory;1",
00322     nsDeviceContextSpecFactoryOS2Constructor },
00323   { "PrintSettings Service",
00324     NS_PRINTSETTINGSSERVICE_CID,
00325     //    "@mozilla.org/gfx/printsettings-service;1",
00326     "@mozilla.org/gfx/printsettings-service;1",
00327     nsPrintOptionsOS2Constructor },
00328   { "OS2 Font Enumerator",
00329     NS_FONT_ENUMERATOR_CID,
00330     //    "@mozilla.org/gfx/font_enumerator/gtk;1",
00331     "@mozilla.org/gfx/fontenumerator;1",
00332     nsFontEnumeratorOS2Constructor },
00333   { "Font List",  
00334     NS_FONTLIST_CID,
00335     //    "@mozilla.org/gfx/fontlist;1"
00336     NS_FONTLIST_CONTRACTID,
00337     nsFontListConstructor },
00338   { "OS/2 Screen Manager",
00339     NS_SCREENMANAGER_CID,
00340     //    "@mozilla.org/gfx/screenmanager/gtk;1",
00341     "@mozilla.org/gfx/screenmanager;1",
00342     nsScreenManagerOS2Constructor },
00343   { "OS/2 Printer Enumerator",
00344     NS_PRINTER_ENUMERATOR_CID,
00345     //    "@mozilla.org/gfx/printer_enumerator/gtk;1",
00346     "@mozilla.org/gfx/printerenumerator;1",
00347     nsPrinterEnumeratorOS2Constructor },
00348   { "windows image frame",
00349     GFX_IMAGEFRAME_CID,
00350     "@mozilla.org/gfx/image/frame;2",
00351     gfxImageFrameConstructor, },
00352   { "Print Session",
00353     NS_PRINTSESSION_CID,
00354     "@mozilla.org/gfx/printsession;1",
00355     nsPrintSessionConstructor }
00356 };
00357 
00358 PR_STATIC_CALLBACK(void)
00359 nsGfxOS2ModuleDtor(nsIModule *self)
00360 {
00361   OS2Uni::FreeUconvObjects();
00362   nsPaletteOS2::FreeGlobalPalette();
00363 //  nsRenderingContextOS2::Shutdown();
00364 }
00365 
00366 NS_IMPL_NSGETMODULE_WITH_DTOR(nsGfxOS2Module, components, nsGfxOS2ModuleDtor)
00367