NS_IMETHODIMP nsRenderingContextPh :: GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth ) 
{
	PhRect_t extent;

	/* Check for the very common case of trying to get the width of a single space */
	if( aString[0] == ' ' && aLength == 1 )
		return mFontMetrics->GetSpaceWidth(aWidth);

	PfExtent( &extent, NULL, mPhotonFontName, 0L, 0L, aString, aLength, PF_SIMPLE_METRICS, NULL );
	aWidth = NSToCoordRound((int) ((extent.lr.x - extent.ul.x + 1) * mP2T));
	return NS_OK;
}
NS_IMETHODIMP nsFontMetricsPh::Init ( const nsFont& aFont, nsIAtom* aLangGroup, nsIDeviceContext* aContext )
{
	NS_ASSERTION(!(nsnull == aContext), "attempt to init fontmetrics with null device context");
	
	nsAutoString  firstFace;
	char          *str = nsnull;
	nsresult      result;
	PhRect_t      extent;

	if( !gFontMetricsCache ) {
		nsresult res = InitGlobals( );
		if( NS_FAILED(res) ) return res;
		}
	
	mFont = aFont;
	mLangGroup = aLangGroup;
	
	mDeviceContext = aContext;
	
	result = aContext->FirstExistingFont(aFont, firstFace);

	str = ToNewCString(firstFace);

#ifdef DEBUG_Adrian
printf( "\n\n\t\t\tIn nsFontMetricsPh::Init str=%s\n", str );
#endif

	if( !str || !str[0] )
	{
		if( str ) free (str);
		str = strdup("serif");
	}
	
	const char *cstring;
	aLangGroup->GetUTF8String( &cstring );
	
	char *prop = PR_smprintf( "font.name.%s.%s", str, cstring );
		
	char *font_default = NULL;
	if( prop )
		{
		gPref->CopyCharPref( prop, &font_default );
		PR_smprintf_free( prop );
		}
	else gPref->CopyCharPref( "font.name.serif.x-western", &font_default );

	if( font_default )
		{
		free (str);
		/* font_default was allocated. in CopyCharPref. */
		str = font_default;
		}

	float app2dev;
	app2dev = mDeviceContext->AppUnitsToDevUnits();

	PRInt32 sizePoints;
	if( mFont.systemFont == PR_TRUE )
		sizePoints = NSToIntRound( app2dev * mFont.size * 0.68 );
	else sizePoints = NSToIntRound( app2dev * mFont.size * 0.74 );
	
	char NSFullFontName[MAX_FONT_TAG];

	unsigned int uiFlags = 0L;

	if(aFont.weight > NS_FONT_WEIGHT_NORMAL)
	   uiFlags |= PF_STYLE_BOLD;

	if(aFont.style & (NS_FONT_STYLE_ITALIC|NS_FONT_STYLE_OBLIQUE) )
	   uiFlags |= PF_STYLE_ITALIC;

	if(aFont.style & NS_FONT_STYLE_ANTIALIAS)
		uiFlags |= PF_STYLE_ANTIALIAS;

	if( PfGenerateFontName( (char *)str, uiFlags, sizePoints, (char *)NSFullFontName ) == NULL )
	  {
#ifdef DEBUG_Adrian
printf( "!!!!!!!!!!!! PfGenerateFontName failed\n" );
#endif
		  PfGenerateFontName( "TextFont", uiFlags, sizePoints, (char *)NSFullFontName );
	  }

	/* Once the Photon Font String is built get the attributes */
	FontQueryInfo *node;

	nsCStringKey key((char *)(NSFullFontName));
	node = (FontQueryInfo *) gFontMetricsCache->Get(&key);

#ifdef DEBUG_Adrian
printf( "\t\t\tThe generated font name is NSFullFontName=%s\n", NSFullFontName );
if( node ) printf( "\t\t\t( cached ) The real font is desc=%s\n", node->desc );
#endif

	if( !node )
	  {
		node = (FontQueryInfo *)calloc(sizeof(FontQueryInfo), 1);
		PfQueryFont(NSFullFontName, node);

#ifdef DEBUG_Adrian
printf( "\t\t\t(not cached ) The real font is desc=%s\n", node->desc );
printf( "\tCall PfLoadMetrics for NSFullFontName=%s\n", NSFullFontName );
#endif

		gFontMetricsCache->Put(&key, node);

		PfLoadMetrics( NSFullFontName );
	  }

	float dev2app;
	double height;
	nscoord onePixel;

	dev2app = mDeviceContext->DevUnitsToAppUnits();
	onePixel = NSToCoordRound(1 * dev2app);
	height = node->descender - node->ascender;
	PfExtent( &extent, NULL, NSFullFontName, 0L, 0L, " ", 1, PF_SIMPLE_METRICS, NULL );
	mSpaceWidth = NSToCoordRound((extent.lr.x - extent.ul.x + 1) * dev2app);

	mLeading = NSToCoordRound(0);
	mEmHeight = NSToCoordRound(height * dev2app);
	mEmAscent = NSToCoordRound(node->ascender * dev2app * -1.0);
	mEmDescent = NSToCoordRound(node->descender * dev2app);
	mHeight = mMaxHeight = NSToCoordRound(height * dev2app);
	mAscent = mMaxAscent = NSToCoordRound(node->ascender * dev2app * -1.0);
	mDescent = mMaxDescent = NSToCoordRound(node->descender * dev2app);
	mMaxAdvance = NSToCoordRound(node->width * dev2app);
	mAveCharWidth = mSpaceWidth;

	mXHeight = NSToCoordRound((float)node->ascender * dev2app * 0.56f * -1.0); // 56% of ascent, best guess for non-true type
	mSuperscriptOffset = mXHeight;     // XXX temporary code!
	mSubscriptOffset = mXHeight;     // XXX temporary code!

	mStrikeoutSize = onePixel; // XXX this is a guess
	mStrikeoutOffset = NSToCoordRound(mXHeight / 2.0f); // 50% of xHeight
	mUnderlineSize = onePixel; // XXX this is a guess
	mUnderlineOffset = -NSToCoordRound((float)node->descender * dev2app * 0.30f); // 30% of descent

	if (mFontHandle)
	   free (mFontHandle);
	mFontHandle = strdup(NSFullFontName);

	free (str);
	return NS_OK;
}