Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes
nsFontMetricsBeOS Class Reference

#include <nsFontMetricsBeOS.h>

Inheritance diagram for nsFontMetricsBeOS:
Inheritance graph
[legend]
Collaboration diagram for nsFontMetricsBeOS:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsFontMetricsBeOS ()
virtual ~nsFontMetricsBeOS ()
NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
NS_DECL_ISUPPORTS NS_IMETHOD 
Init (const nsFont &aFont, nsIAtom *aLangGroup, nsIDeviceContext *aContext)
 Initialize the font metrics.
NS_IMETHOD Destroy ()
 Destroy this font metrics.
NS_IMETHOD GetXHeight (nscoord &aResult)
 Return the font's xheight property, scaled into app-units.
NS_IMETHOD GetSuperscriptOffset (nscoord &aResult)
 Return the font's superscript offset (the distance from the baseline to where a superscript's baseline should be placed).
NS_IMETHOD GetSubscriptOffset (nscoord &aResult)
 Return the font's subscript offset (the distance from the baseline to where a subscript's baseline should be placed).
NS_IMETHOD GetStrikeout (nscoord &aOffset, nscoord &aSize)
 Return the font's strikeout offset (the distance from the baseline to where a strikeout should be placed) and size Positive values are above the baseline, negative below.
NS_IMETHOD GetUnderline (nscoord &aOffset, nscoord &aSize)
 Return the font's underline offset (the distance from the baseline to where a underline should be placed) and size.
NS_IMETHOD GetHeight (nscoord &aHeight)
 Returns the height (in app units) of the font.
NS_IMETHOD GetNormalLineHeight (nscoord &aHeight)
 Returns the normal line height (em height + leading).
NS_IMETHOD GetLeading (nscoord &aLeading)
 Returns the amount of internal leading (in app units) for the font.
NS_IMETHOD GetEmHeight (nscoord &aHeight)
 Returns the height (in app units) of the Western font's em square.
NS_IMETHOD GetEmAscent (nscoord &aAscent)
 Returns, in app units, the ascent part of the Western font's em square.
NS_IMETHOD GetEmDescent (nscoord &aDescent)
 Returns, in app units, the descent part of the Western font's em square.
NS_IMETHOD GetMaxHeight (nscoord &aHeight)
 Returns the height (in app units) of the Western font's bounding box.
NS_IMETHOD GetMaxAscent (nscoord &aAscent)
 Returns, in app units, the maximum distance characters in this font extend above the base line.
NS_IMETHOD GetMaxDescent (nscoord &aDescent)
 Returns, in app units, the maximum distance characters in this font extend below the base line.
NS_IMETHOD GetMaxAdvance (nscoord &aAdvance)
 Returns, in app units, the maximum character advance for the font.
NS_IMETHOD GetAveCharWidth (nscoord &aAveCharWidth)
 Returns the average character width.
NS_IMETHOD GetLangGroup (nsIAtom **aLangGroup)
 Returns the language group associated with these metrics.
NS_IMETHOD GetFontHandle (nsFontHandle &aHandle)
 Returns the font handle associated with these metrics.
NS_IMETHOD GetSpaceWidth (nscoord &aSpaceWidth)
 Returns the often needed width of the space character.
PRBool IsBold ()
float GetStringWidth (char *string, uint32 len)
const nsFontFont ()
 Returns the font associated with these metrics.

Static Public Member Functions

static nsresult FamilyExists (const nsString &aFontName)
static int FontMatchesGenericType (font_family family, uint32 flags, const char *aGeneric, const char *aLangGroup)
static int MatchesLangGroup (font_family family, const char *aLangGroup)

Public Attributes

nsCOMPtr< nsIAtommLangGroup

Protected Member Functions

void RealizeFont (nsIDeviceContext *aContext)

Protected Attributes

nsIDeviceContextmDeviceContext
BFont mFontHandle
nscoord mLeading
nscoord mEmHeight
nscoord mEmAscent
nscoord mEmDescent
nscoord mMaxHeight
nscoord mMaxAscent
nscoord mMaxDescent
nscoord mMaxAdvance
nscoord mAveCharWidth
nscoord mXHeight
nscoord mSuperscriptOffset
nscoord mSubscriptOffset
nscoord mStrikeoutSize
nscoord mStrikeoutOffset
nscoord mUnderlineSize
nscoord mUnderlineOffset
nscoord mSpaceWidth
PRUint16 mPixelSize
PRUint8 mStretchIndex
PRUint8 mStyleIndex
PRBool mIsBold
nsDataHashtable
< nsUint32HashKey, float > 
mFontWidthCache
nsFont mFont

Detailed Description

Definition at line 58 of file nsFontMetricsBeOS.h.


Constructor & Destructor Documentation

Definition at line 63 of file nsFontMetricsBeOS.cpp.

{
}

Definition at line 67 of file nsFontMetricsBeOS.cpp.

{
  if (mDeviceContext) 
  {
    // Notify our device context that owns us so that it can update its font cache
    mDeviceContext->FontMetricsDeleted(this);
    mDeviceContext = nsnull;
  }
}

Here is the call graph for this function:


Member Function Documentation

Destroy this font metrics.

This breaks the association between the font metrics and the device context.

Implements nsIFontMetrics.

Definition at line 229 of file nsFontMetricsBeOS.cpp.

{
  mDeviceContext = nsnull;
  return NS_OK;
}

Definition at line 414 of file nsFontMetricsBeOS.cpp.

{ 
  NS_ConvertUTF16toUTF8 family(aName);
  printf("exists? %s", (font_family)family.get()); 
  return  (count_font_styles((font_family)family.get()) > 0) ? NS_OK : NS_ERROR_FAILURE;
} 

Here is the call graph for this function:

Here is the caller graph for this function:

const nsFont& nsIFontMetrics::Font ( ) [inline, inherited]

Returns the font associated with these metrics.

The return value is only defined after Init() has been called.

Definition at line 214 of file nsIFontMetrics.h.

{ return mFont; }

Here is the caller graph for this function:

static int nsFontMetricsBeOS::FontMatchesGenericType ( font_family  family,
uint32  flags,
const char *  aGeneric,
const char *  aLangGroup 
) [static]

Returns the average character width.

Implements nsIFontMetrics.

Definition at line 384 of file nsFontMetricsBeOS.cpp.

{
  aAveCharWidth = mAveCharWidth;
  return NS_OK;
}

Returns, in app units, the ascent part of the Western font's em square.

Implements nsIFontMetrics.

Definition at line 348 of file nsFontMetricsBeOS.cpp.

{ 
  aAscent = mEmAscent; 
  return NS_OK; 
} 

Returns, in app units, the descent part of the Western font's em square.

Implements nsIFontMetrics.

Definition at line 354 of file nsFontMetricsBeOS.cpp.

{ 
  aDescent = mEmDescent; 
  return NS_OK; 
} 

Returns the height (in app units) of the Western font's em square.

This is em ascent plus em descent.

Implements nsIFontMetrics.

Definition at line 342 of file nsFontMetricsBeOS.cpp.

{ 
  aHeight = mEmHeight; 
  return NS_OK; 
} 

Returns the font handle associated with these metrics.

Implements nsIFontMetrics.

Definition at line 407 of file nsFontMetricsBeOS.cpp.

Returns the height (in app units) of the font.

This is ascent plus descent plus any internal leading

This method will be removed once the callers have been moved over to the new GetEmHeight (and possibly GetMaxHeight).

Implements nsIFontMetrics.

Definition at line 324 of file nsFontMetricsBeOS.cpp.

{ 
  aHeight = mMaxHeight; 
  return NS_OK; 
} 

Returns the language group associated with these metrics.

Implements nsIFontMetrics.

Definition at line 396 of file nsFontMetricsBeOS.cpp.

{
  if (!aLangGroup)
    return NS_ERROR_NULL_POINTER;

  *aLangGroup = mLangGroup;
  NS_IF_ADDREF(*aLangGroup);

  return NS_OK;
}

Returns the amount of internal leading (in app units) for the font.

This is computed as the "height - (ascent + descent)"

Implements nsIFontMetrics.

Definition at line 336 of file nsFontMetricsBeOS.cpp.

{
  aLeading = mLeading;
  return NS_OK;
}

Returns, in app units, the maximum character advance for the font.

Implements nsIFontMetrics.

Definition at line 378 of file nsFontMetricsBeOS.cpp.

{
  aAdvance = mMaxAdvance;
  return NS_OK;
}

Returns, in app units, the maximum distance characters in this font extend above the base line.

Implements nsIFontMetrics.

Definition at line 366 of file nsFontMetricsBeOS.cpp.

{
  aAscent = mMaxAscent;
  return NS_OK;
}

Returns, in app units, the maximum distance characters in this font extend below the base line.

Implements nsIFontMetrics.

Definition at line 372 of file nsFontMetricsBeOS.cpp.

{
  aDescent = mMaxDescent;
  return NS_OK;
}

Returns the height (in app units) of the Western font's bounding box.

This is max ascent plus max descent.

Implements nsIFontMetrics.

Definition at line 360 of file nsFontMetricsBeOS.cpp.

{ 
  aHeight = mMaxHeight; 
  return NS_OK; 
} 

Returns the normal line height (em height + leading).

Implements nsIFontMetrics.

Definition at line 330 of file nsFontMetricsBeOS.cpp.

{
  aHeight = mEmHeight + mLeading; 
  return NS_OK;
}
NS_IMETHODIMP nsFontMetricsBeOS::GetSpaceWidth ( nscoord aSpaceCharWidth) [virtual]

Returns the often needed width of the space character.

Implements nsIFontMetrics.

Definition at line 390 of file nsFontMetricsBeOS.cpp.

{ 
  aSpaceWidth = mSpaceWidth; 
  return NS_OK; 
} 
NS_IMETHODIMP nsFontMetricsBeOS::GetStrikeout ( nscoord aOffset,
nscoord aSize 
) [virtual]

Return the font's strikeout offset (the distance from the baseline to where a strikeout should be placed) and size Positive values are above the baseline, negative below.

Implements nsIFontMetrics.

Definition at line 306 of file nsFontMetricsBeOS.cpp.

{
  aOffset = mStrikeoutOffset; 
  aSize = mStrikeoutSize; 
//  aOffset = nscoord( ( mAscent / 2 ) - mDescent ); 
//  aSize = nscoord( 20 );  // FIXME Put 1 pixel which equal 20 twips.. 
  return NS_OK;
}
float nsFontMetricsBeOS::GetStringWidth ( char *  string,
uint32  len 
)

Definition at line 443 of file nsFontMetricsBeOS.cpp.

{
       float retwidth = 0;
       uint32 charlen = 1;
       // Traversing utf8string - get, cache and sum widths for all utf8 chars
       for (uint32 i =0; i < bytelen && *utf8str  != '\0'; i += charlen)
       {
              float width = 0;
              // Calculating utf8 char bytelength
              charlen = ((0xE5000000 >> ((*utf8str >> 3) & 0x1E)) & 3) + 1;
              // Converting multibyte sequence to index
              PRUint32 index = utf8_to_index(utf8str);
              if (!mFontWidthCache.Get(index, &width))
              {
                     width = mFontHandle.StringWidth(utf8str, charlen);
                     mFontWidthCache.Put(index, width);
              }
              retwidth +=  width;
              utf8str += charlen;
       }
       if (mIsBold && !(mFontHandle.Face() & B_BOLD_FACE))
              retwidth += 1.0;
       return retwidth;
}

Here is the call graph for this function:

Return the font's subscript offset (the distance from the baseline to where a subscript's baseline should be placed).

The value returned will be a positive value.

Implements nsIFontMetrics.

Definition at line 300 of file nsFontMetricsBeOS.cpp.

Return the font's superscript offset (the distance from the baseline to where a superscript's baseline should be placed).

The value returned will be a positive value.

Implements nsIFontMetrics.

Definition at line 294 of file nsFontMetricsBeOS.cpp.

NS_IMETHODIMP nsFontMetricsBeOS::GetUnderline ( nscoord aOffset,
nscoord aSize 
) [virtual]

Return the font's underline offset (the distance from the baseline to where a underline should be placed) and size.

Positive values are above the baseline, negative below.

Implements nsIFontMetrics.

Definition at line 315 of file nsFontMetricsBeOS.cpp.

{
  aOffset = mUnderlineOffset;
  aSize = mUnderlineSize;
  //aOffset = nscoord( 0 ); // FIXME
  //aSize = nscoord( 20 );  // FIXME
  return NS_OK;
}

Return the font's xheight property, scaled into app-units.

Implements nsIFontMetrics.

Definition at line 288 of file nsFontMetricsBeOS.cpp.

{
  aResult = mXHeight;
  return NS_OK;
}
NS_IMETHODIMP nsFontMetricsBeOS::Init ( const nsFont aFont,
nsIAtom aLangGroup,
nsIDeviceContext aContext 
) [virtual]

Initialize the font metrics.

Call this after creating the font metrics. Font metrics you get from the font cache do NOT need to be initialized

See also:
nsIDeviceContext::GetMetricsFor()

Implements nsIFontMetrics.

Definition at line 98 of file nsFontMetricsBeOS.cpp.

{
  NS_ASSERTION(!(nsnull == aContext), "attempt to init fontmetrics with null device context");

  mLangGroup = aLangGroup;
  mDeviceContext = aContext;

  // get font family list
  NS_FONT_ENUM_PARAMS_BEOS param;
  param.metrics = this;
  aFont.EnumerateFamilies(FontEnumCallback, &param);
 
  PRInt16  face = 0;

  mFont = aFont;

  float app2twip = aContext->DevUnitsToTwips();

  // process specified fonts from first item of the array.
  // stop processing next when a real font found;
  PRBool fontfound = PR_FALSE;
  PRBool isfixed = PR_FALSE;
  for (int i=0 ; i<param.family.Count() && !fontfound ; i++) 
  {
    nsString *fam = param.family.StringAt(i);
    PRBool isgeneric = ( param.isgeneric[i] ) ? PR_TRUE: PR_FALSE;
    NS_ConvertUTF16toUTF8 family(*fam);
    // Fallback
    isfixed = family.Equals("monospace") || family.Equals("fixed");
    if (!isgeneric) 
    {
      // non-generic font
      if (count_font_styles((font_family)family.get()) <= 0) 
      {
        // the specified font does not exist on this computer.
        continue;
      }
      mFontHandle.SetFamilyAndStyle( (font_family)family.get(), NULL );
      fontfound = PR_TRUE;
      break;
    } 
    else 
    {
      // family is generic string like 
      // "serif" "sans-serif" "cursive" "fantasy" "monospace" "-moz-fixed"
      // so look up preferences and get real family name
      const char *lang;
      aLangGroup->GetUTF8String( &lang );
      char prop[256];
      snprintf( prop, sizeof(prop), "%s.%s", family.get(), lang );

      // look up prefs
      nsXPIDLCString real_family;
      nsresult res;
      //NS_WITH_SERVICE( nsIPref, prefs, kPrefCID, &res );
      nsCOMPtr<nsIPrefService> prefs = do_GetService( NS_PREFSERVICE_CONTRACTID, &res );
      if (NS_SUCCEEDED(res)) 
      {
        nsCOMPtr<nsIPrefBranch> branch;
        prefs->GetBranch("font.name.", getter_AddRefs(branch));
        branch->GetCharPref(prop, getter_Copies(real_family));

        if (!real_family.IsEmpty() && real_family.Length() <= B_FONT_FAMILY_LENGTH  && count_font_styles((font_family)real_family.get()) > 0) 
        {
          mFontHandle.SetFamilyAndStyle( (font_family)real_family.get(), NULL );
          fontfound = PR_TRUE;
          break;        
        }
      } 
      // not successful. use system font.
      if (isfixed)
        mFontHandle = BFont(be_fixed_font);
      else
        mFontHandle = BFont(be_plain_font);
      fontfound = PR_TRUE;
      break;
    }
  }

  // if got no font, then use system font.
  if (!fontfound)
  {
    if (isfixed)
      mFontHandle = BFont(be_fixed_font);
    else
      mFontHandle = BFont(be_plain_font);
  } 
 
  if (aFont.style == NS_FONT_STYLE_ITALIC)
    face |= B_ITALIC_FACE;

  if ( aFont.weight > NS_FONT_WEIGHT_NORMAL )
  {
    mIsBold = PR_TRUE;
       face |= B_BOLD_FACE;
  }
  else
    mIsBold = PR_FALSE;
        
  // I don't think B_UNDERSCORE_FACE and B_STRIKEOUT_FACE really works...
  // instead, nsTextFrame do them for us. ( my guess... Makoto Hamanaka )
  if ( aFont.decorations & NS_FONT_DECORATION_UNDERLINE )
       face |= B_UNDERSCORE_FACE;
       
  if ( aFont.decorations & NS_FONT_DECORATION_LINE_THROUGH )
       face |= B_STRIKEOUT_FACE;

  mFontHandle.SetFace( face );
  // emulate italic the selected family has no such style
  if (aFont.style == NS_FONT_STYLE_ITALIC
    && !(mFontHandle.Face() & B_ITALIC_FACE)) 
    mFontHandle.SetShear(105.0);

  mFontHandle.SetSize(mFont.size/app2twip);
  mFontHandle.SetSpacing(B_FIXED_SPACING);

#ifdef NOISY_FONTS
#ifdef DEBUG
  fprintf(stderr, "looking for font %s (%d)", wildstring, aFont.size / app2twip);
#endif
#endif
  //UTF8 charspace in BeOS is 0xFFFF, 256 is by "pighole rule" sqrt(0xFFFF), 
  // actually rare font contains more glyphs
  mFontWidthCache.Init(256);
  
  RealizeFont(aContext);

  return NS_OK;
}

Here is the call graph for this function:

Definition at line 95 of file nsFontMetricsBeOS.h.

{ return mIsBold; } 
static int nsFontMetricsBeOS::MatchesLangGroup ( font_family  family,
const char *  aLangGroup 
) [static]

Definition at line 236 of file nsFontMetricsBeOS.cpp.

{
  float f;
  f = aContext->DevUnitsToAppUnits();
  
  struct font_height height;
  mFontHandle.GetHeight( &height );
 
  struct font_height emHeight; 
  mFontHandle.GetHeight(&emHeight);
  //be_plain_font->GetHeight(&emHeight); 
 
  int lineSpacing = nscoord(height.ascent + height.descent); 
  if (lineSpacing > (emHeight.ascent + emHeight.descent))
    mLeading = nscoord((lineSpacing - (emHeight.ascent + emHeight.descent)) * f); 
  else
    mLeading = 0; 

  mEmHeight = PR_MAX(1, nscoord((emHeight.ascent + emHeight.descent) * f)); 
  mEmAscent = nscoord(height.ascent * (emHeight.ascent + emHeight.descent) * f / lineSpacing); 
  mEmDescent = mEmHeight - mEmAscent; 

  mMaxHeight = nscoord((height.ascent + 
                        height.descent) * f) ; 
  mMaxAscent = nscoord(height.ascent * f) ;
  mMaxDescent = nscoord(height.descent * f);
  
  mMaxAdvance = nscoord((mFontHandle.BoundingBox().Width()+1) * f); //fyy +1

  float rawWidth = mFontHandle.StringWidth("x"); 
  mAveCharWidth = NSToCoordRound(rawWidth * f); 

  // 56% of ascent, best guess for non-true type 
  mXHeight = NSToCoordRound((float) height.ascent* f * 0.56f); 

  rawWidth = mFontHandle.StringWidth(" "); 
  mSpaceWidth = NSToCoordRound(rawWidth * f); 
 
/* Temp */ 
  mUnderlineOffset = -NSToIntRound(MAX (1, floor (0.1 * (height.ascent + height.descent + height.leading) + 0.5)) * f); 
  
  mUnderlineSize = NSToIntRound(MAX(1, floor (0.05 * (height.ascent + height.descent + height.leading) + 0.5)) * f); 
 
  mSuperscriptOffset = mXHeight; 
 
  mSubscriptOffset = mXHeight; 
 
  /* need better way to calculate this */ 
  mStrikeoutOffset = NSToCoordRound(mXHeight / 2.0); 
  mStrikeoutSize = mUnderlineSize; 
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 115 of file nsFontMetricsBeOS.h.

Definition at line 104 of file nsFontMetricsBeOS.h.

Definition at line 109 of file nsFontMetricsBeOS.h.

Definition at line 110 of file nsFontMetricsBeOS.h.

Definition at line 108 of file nsFontMetricsBeOS.h.

nsFont nsIFontMetrics::mFont [protected, inherited]

Definition at line 238 of file nsIFontMetrics.h.

BFont nsFontMetricsBeOS::mFontHandle [protected]

Definition at line 105 of file nsFontMetricsBeOS.h.

Definition at line 129 of file nsFontMetricsBeOS.h.

Definition at line 128 of file nsFontMetricsBeOS.h.

Definition at line 97 of file nsFontMetricsBeOS.h.

Definition at line 107 of file nsFontMetricsBeOS.h.

Definition at line 114 of file nsFontMetricsBeOS.h.

Definition at line 112 of file nsFontMetricsBeOS.h.

Definition at line 113 of file nsFontMetricsBeOS.h.

Definition at line 111 of file nsFontMetricsBeOS.h.

Definition at line 125 of file nsFontMetricsBeOS.h.

Definition at line 123 of file nsFontMetricsBeOS.h.

Definition at line 126 of file nsFontMetricsBeOS.h.

Definition at line 120 of file nsFontMetricsBeOS.h.

Definition at line 119 of file nsFontMetricsBeOS.h.

Definition at line 127 of file nsFontMetricsBeOS.h.

Definition at line 118 of file nsFontMetricsBeOS.h.

Definition at line 117 of file nsFontMetricsBeOS.h.

Definition at line 122 of file nsFontMetricsBeOS.h.

Definition at line 121 of file nsFontMetricsBeOS.h.

Definition at line 116 of file nsFontMetricsBeOS.h.


The documentation for this class was generated from the following files: