Back to index

lightning-sunbird  0.9+nobinonly
nsFontList.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; 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 Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Brian Stell <bstell@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041  * Implementation of nsIFontList
00042  */
00043 
00044 #include "nsCOMPtr.h"
00045 #include "nsXPCOM.h"
00046 #include "nsFontList.h"
00047 #include "nsGfxCIID.h"
00048 #include "nsDependentString.h"
00049 #include "nsIFontEnumerator.h"
00050 #include "nsISimpleEnumerator.h"
00051 #include "nsISupportsPrimitives.h"
00052 #include "nsMemory.h"
00053 #include "nsReadableUtils.h"
00054 #include "nsXPIDLString.h"
00055 
00056 static NS_DEFINE_IID(kCFontEnumerator, NS_FONT_ENUMERATOR_CID);
00057 
00058 nsFontList::nsFontList()
00059 {
00060 }
00061 
00062 nsFontList::~nsFontList()
00063 {
00064 }
00065 
00066 NS_IMPL_ISUPPORTS1(nsFontList,nsIFontList)
00067 
00068 /* font list enumerator */
00069 class
00070 nsFontListEnumerator : public nsISimpleEnumerator
00071 {
00072   public:
00073     nsFontListEnumerator();
00074     virtual ~nsFontListEnumerator();
00075 
00076     // nsISupports interface
00077     NS_DECL_ISUPPORTS
00078 
00079     // nsISimpleEnumerator interface
00080     NS_DECL_NSISIMPLEENUMERATOR
00081 
00082     NS_IMETHOD Init(const PRUnichar *aLangGroup, const PRUnichar *aFontType);
00083 
00084 
00085   protected:
00086     PRUnichar **mFonts;
00087     PRUint32 mCount;
00088     PRUint32 mIndex;
00089 };
00090 
00091 nsFontListEnumerator::nsFontListEnumerator() :
00092   mFonts(nsnull), mCount(0), mIndex(0)
00093 {
00094 }
00095 
00096 nsFontListEnumerator::~nsFontListEnumerator()
00097 {
00098   if (mFonts) {
00099     PRUint32 i;
00100     for (i=0; i<mCount; i++) {
00101       nsMemory::Free(mFonts[i]);
00102     }
00103     nsMemory::Free(mFonts);
00104   }
00105 }
00106 
00107 NS_IMPL_ISUPPORTS1(nsFontListEnumerator, nsISimpleEnumerator)
00108 
00109 NS_IMETHODIMP
00110 nsFontListEnumerator::Init(const PRUnichar *aLangGroup, 
00111                            const PRUnichar *aFontType)
00112 {
00113   nsresult rv;
00114   nsCOMPtr<nsIFontEnumerator> fontEnumerator;
00115 
00116   fontEnumerator = do_CreateInstance(kCFontEnumerator, &rv);
00117   if (NS_FAILED(rv))
00118     return rv;
00119  
00120   nsXPIDLCString langGroup;
00121   langGroup.Adopt(ToNewUTF8String(nsDependentString(aLangGroup)));
00122   nsXPIDLCString fontType;
00123   fontType.Adopt(ToNewUTF8String(nsDependentString(aFontType)));
00124   rv = fontEnumerator->EnumerateFonts(langGroup.get(), fontType.get(), 
00125                                         &mCount, &mFonts);
00126   return rv;
00127 }
00128 
00129 NS_IMETHODIMP
00130 nsFontListEnumerator::HasMoreElements(PRBool *result)
00131 {
00132   *result = (mIndex < mCount);
00133   return NS_OK;
00134 }
00135 
00136 NS_IMETHODIMP
00137 nsFontListEnumerator::GetNext(nsISupports **aFont)
00138 {
00139   NS_ENSURE_ARG_POINTER(aFont);
00140   *aFont = nsnull;
00141   if (mIndex >= mCount) {
00142     return NS_ERROR_UNEXPECTED;
00143   }
00144   PRUnichar *fontName = mFonts[mIndex++];
00145   nsCOMPtr<nsISupportsString> fontNameWrapper;
00146   nsresult rv;
00147   fontNameWrapper = do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00148   NS_ENSURE_SUCCESS(rv, rv);
00149   NS_ENSURE_TRUE(fontNameWrapper, NS_ERROR_OUT_OF_MEMORY);
00150   fontNameWrapper->SetData(nsDependentString(fontName));
00151   *aFont = NS_STATIC_CAST(nsISupports*, fontNameWrapper);
00152   NS_ADDREF(*aFont);
00153   return NS_OK;
00154 }
00155 
00156 NS_IMETHODIMP
00157 nsFontList::AvailableFonts(const PRUnichar *aLangGroup, 
00158                            const PRUnichar *aFontType, 
00159                            nsISimpleEnumerator **aFontEnumerator)
00160 {
00161   NS_ENSURE_ARG(aLangGroup);
00162   NS_ENSURE_ARG(aFontType);
00163   NS_ENSURE_ARG_POINTER(aFontEnumerator);
00164   nsCOMPtr<nsFontListEnumerator> fontListEnum = new nsFontListEnumerator();
00165   NS_ENSURE_TRUE(fontListEnum.get(), NS_ERROR_OUT_OF_MEMORY);
00166 
00167   nsresult rv = fontListEnum->Init(aLangGroup, aFontType);
00168   NS_ENSURE_SUCCESS(rv, rv);
00169 
00170   *aFontEnumerator = NS_STATIC_CAST(nsISimpleEnumerator*, fontListEnum);
00171   NS_ADDREF(*aFontEnumerator);
00172   return NS_OK;
00173 }
00174