Back to index

lightning-sunbird  0.9+nobinonly
nsRegressionTestFontMetrics.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  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsRegressionTestFontMetrics.h"
00039 
00040 #define MAPPING_FACTOR_FOR_SPACE 0.02f
00041 #define MAPPING_FACTOR_FOR_LOWER_CASE  0.50f
00042 #define MAPPING_FACTOR_FOR_OTHERS  0.70f
00043 
00044 
00045 nsresult
00046 NS_NewRegressionTestFontMetrics(nsIFontMetrics** aMetrics)
00047 {
00048   if (aMetrics == nsnull) {
00049     return NS_ERROR_NULL_POINTER;
00050   }
00051 
00052   nsRegressionTestFontMetrics  *fm = new nsRegressionTestFontMetrics();
00053 
00054   if (nsnull == fm) {
00055     return NS_ERROR_OUT_OF_MEMORY;
00056   }
00057 
00058   return CallQueryInterface(fm, aMetrics);
00059 }
00060 
00061 nsRegressionTestFontMetrics:: nsRegressionTestFontMetrics()
00062 {
00063   mDeviceContext = nsnull;
00064   
00065   mHeight = 0; 
00066   mAscent = 0; 
00067   mDescent = 0; 
00068   mLeading = 0; 
00069   mMaxAscent = 0; 
00070   mMaxDescent = 0; 
00071   mMaxAdvance = 0; 
00072   mXHeight = 0; 
00073   mSuperscriptOffset = 0; 
00074   mSubscriptOffset = 0; 
00075   mStrikeoutSize = 0; 
00076   mStrikeoutOffset = 0; 
00077   mUnderlineSize = 0; 
00078   mUnderlineOffset = 0; 
00079 }
00080   
00081 NS_IMPL_ISUPPORTS1(nsRegressionTestFontMetrics, nsIFontMetrics)
00082 
00083 nsRegressionTestFontMetrics::~nsRegressionTestFontMetrics()
00084 {
00085   mDeviceContext = nsnull;
00086 }
00087 
00088 
00089 NS_IMETHODIMP
00090 nsRegressionTestFontMetrics::Init(const nsFont& aFont, nsIDeviceContext *aContext)
00091 {
00092   mFont = aFont;
00093   mDeviceContext = aContext;
00094   RealizeFont();
00095   return NS_OK;
00096 }
00097 
00098 NS_IMETHODIMP
00099 nsRegressionTestFontMetrics::Destroy()
00100 {
00101   mDeviceContext = nsnull;
00102   return NS_OK;
00103 }
00104 
00105 void
00106 nsRegressionTestFontMetrics::RealizeFont()
00107 {
00108   float dev2app;
00109   dev2app = mDeviceContext->DevUnitsToAppUnits();
00110   nscoord onepixel = NSToCoordRound(1 * dev2app);
00111   PRUint32 fontsize = mFont.size;
00112  
00113   // Most of the numbers are just made up....
00114   // feel free to play around.
00115 
00116   mHeight = fontsize;
00117   mXHeight = NSToCoordRound((float)mHeight * 0.50f);
00118   mSuperscriptOffset = mXHeight;
00119   mSubscriptOffset = mXHeight;
00120   mStrikeoutSize = onepixel;
00121   mStrikeoutOffset = NSToCoordRound(mXHeight / 3.5f);
00122 
00123   
00124   mAscent = NSToCoordRound((float)fontsize * 0.60f);
00125   mDescent = NSToCoordRound((float)fontsize * 0.20f);
00126 
00127   mLeading = NSToCoordRound((float)fontsize * 0.10f);
00128   mMaxAscent = mAscent;
00129   mMaxDescent = mDescent;
00130   mMaxAdvance = NSToCoordRound((float)fontsize * 0.80f);
00131   mUnderlineSize = onepixel;
00132   mUnderlineOffset =  NSToCoordRound(fontsize / 100.0f);
00133   
00134 }
00135 
00136 
00146 NS_METHOD
00147 nsRegressionTestFontMetrics::GetWidth(const char aChar, nscoord& aWidth)
00148 {
00149   float size = (float)mFont.size;
00150   aWidth = 0;
00151 
00152   if(aChar == ' ')
00153     size *= MAPPING_FACTOR_FOR_SPACE;
00154   else if(aChar >= 'a' && aChar <= 'z')
00155     size *= MAPPING_FACTOR_FOR_LOWER_CASE;
00156   else
00157     size *= MAPPING_FACTOR_FOR_OTHERS;
00158     
00159   aWidth = NSToCoordRound(size);
00160 
00161   return NS_OK;
00162 }
00163 
00173 NS_METHOD
00174 nsRegressionTestFontMetrics::GetWidth(const PRUnichar aChar,nscoord& aWidth)
00175 {
00176   float size = (float)mFont.size;
00177   aWidth = 0;
00178 
00179   if(aChar == ' ')
00180     size *= MAPPING_FACTOR_FOR_SPACE;
00181   else if(aChar >= 'a' && aChar <= 'z')
00182     size *= MAPPING_FACTOR_FOR_LOWER_CASE;
00183   else 
00184     size *= MAPPING_FACTOR_FOR_OTHERS;
00185       
00186   aWidth = NSToCoordRound(size);
00187 
00188   return NS_OK;
00189 }
00190 
00203 NS_METHOD
00204 nsRegressionTestFontMetrics::GetWidth(const PRUnichar* aString, PRUint32 aLength, nscoord& aWidth)
00205 {
00206   aWidth = 0;
00207   
00208   if(aString == nsnull)
00209     return NS_ERROR_NULL_POINTER;
00210   
00211   float size;
00212   float totalsize = 0;
00213   
00214   for(PRUint32 index = 0; index < aLength; index++){
00215     size = (float)mFont.size;
00216     if(aString[index] == ' ')
00217       size *= MAPPING_FACTOR_FOR_SPACE;
00218     else if(aString[index] >= 'a' && aString[index] <= 'z')
00219       size *= MAPPING_FACTOR_FOR_LOWER_CASE;
00220     else 
00221       size *= MAPPING_FACTOR_FOR_OTHERS;
00222     totalsize += size;    
00223   }
00224   aWidth = NSToCoordRound(totalsize);
00225   return NS_OK;
00226 }
00227 
00240 NS_METHOD
00241 nsRegressionTestFontMetrics::GetWidth(const char* aString, PRUint32 aLength, nscoord& aWidth)
00242 {
00243   aWidth = 0;
00244 
00245   if(aString == nsnull)
00246     return NS_ERROR_NULL_POINTER;
00247   
00248   float size;
00249   float totalsize = 0;
00250  
00251   for(PRUint32 index=0; index < aLength; index++){
00252     size = (float)mFont.size;
00253     if(aString[index] == ' ')
00254       size *= MAPPING_FACTOR_FOR_SPACE;
00255     else if(aString[index] >= 'a' && aString[index] <= 'z')
00256       size *= MAPPING_FACTOR_FOR_LOWER_CASE;
00257     else
00258       size *= MAPPING_FACTOR_FOR_OTHERS;
00259     totalsize += size;    
00260   }
00261   aWidth += NSToCoordRound(totalsize);
00262   return NS_OK;
00263 }
00264 
00265 NS_IMETHODIMP
00266 nsRegressionTestFontMetrics::GetXHeight(nscoord& aResult)
00267 {
00268   aResult = mXHeight;
00269   return NS_OK;
00270 }
00271 
00272 NS_IMETHODIMP
00273 nsRegressionTestFontMetrics::GetSuperscriptOffset(nscoord& aResult)
00274 {
00275   aResult = mSuperscriptOffset;
00276   return NS_OK;
00277 }
00278 
00279 NS_IMETHODIMP
00280 nsRegressionTestFontMetrics::GetSubscriptOffset(nscoord& aResult)
00281 {
00282   aResult = mSubscriptOffset;
00283   return NS_OK;
00284 }
00285 
00286 NS_IMETHODIMP
00287 nsRegressionTestFontMetrics::GetStrikeout(nscoord& aOffset, nscoord& aSize)
00288 {
00289   aOffset = mStrikeoutOffset;
00290   aSize = mStrikeoutSize;
00291   return NS_OK;
00292 }
00293 
00294 NS_IMETHODIMP
00295 nsRegressionTestFontMetrics::GetUnderline(nscoord& aOffset, nscoord& aSize)
00296 {
00297   aOffset = mUnderlineOffset;
00298   aSize = mUnderlineSize;
00299   return NS_OK;
00300 }
00301 
00302 NS_IMETHODIMP
00303 nsRegressionTestFontMetrics::GetHeight(nscoord &aHeight)
00304 {
00305   aHeight = mHeight;
00306   return NS_OK;
00307 }
00308 
00309 NS_IMETHODIMP
00310 nsRegressionTestFontMetrics::GetLeading(nscoord &aLeading)
00311 {
00312   aLeading = mLeading;
00313   return NS_OK;
00314 }
00315 
00316 NS_IMETHODIMP
00317 nsRegressionTestFontMetrics::GetMaxAscent(nscoord &aAscent)
00318 {
00319   aAscent = mMaxAscent;
00320   return NS_OK;
00321 }
00322 
00323 NS_IMETHODIMP
00324 nsRegressionTestFontMetrics::GetMaxDescent(nscoord &aDescent)
00325 {
00326   aDescent = mMaxDescent;
00327   return NS_OK;
00328 }
00329 
00330 NS_IMETHODIMP
00331 nsRegressionTestFontMetrics::GetMaxAdvance(nscoord &aAdvance)
00332 {
00333   aAdvance = mMaxAdvance;
00334   return NS_OK;
00335 }
00336 
00337 nsRegressionTestFontMetrics::GetFontHandle(nsFontHandle &aHandle)
00338 {
00339   //We don't have a font handler
00340   aHandle = NULL;
00341   return NS_OK;
00342 }
00343