Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Attributes | Private Attributes
nsCairoFontMetrics Class Reference

#include <nsCairoFontMetrics.h>

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

List of all members.

Public Member Functions

 nsCairoFontMetrics ()
virtual ~nsCairoFontMetrics ()
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 GetInternalLeading (nscoord &aLeading)
NS_IMETHOD GetExternalLeading (nscoord &aLeading)
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 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 GetAveCharWidth (nscoord &aAveCharWidth)
 Returns the average character width.
NS_IMETHOD GetSpaceWidth (nscoord &aSpaceCharWidth)
 Returns the often needed width of the space character.
NS_IMETHOD GetLeading (nscoord &aLeading)
 Returns the amount of internal leading (in app units) for the font.
NS_IMETHOD GetNormalLineHeight (nscoord &aLineHeight)
 Returns the normal line height (em height + leading).
nscoord MeasureString (const char *aString, PRUint32 aLength)
nscoord MeasureString (const PRUnichar *aString, PRUint32 aLength)
const nsFontFont ()
 Returns the font associated with these metrics.

Protected Attributes

nsFont mFont

Private Attributes

FT_Face mFace
nsCOMPtr< nsIDeviceContextmDeviceContext
nsCOMPtr< nsIAtommLangGroup
float mDev2App
long mMaxAscent
long mMaxDescent
short mMaxAdvance
long mUnderlineOffset
long mUnderlineHeight

Detailed Description

Definition at line 51 of file nsCairoFontMetrics.h.


Constructor & Destructor Documentation

Definition at line 61 of file nsCairoFontMetrics.cpp.

                                       :
    mMaxAscent(0),
    mMaxDescent(0),
    mMaxAdvance(0),
    mUnderlineOffset(0),
    mUnderlineHeight(0)
{
    NS_INIT_ISUPPORTS();
    if (!ftlib) {
        FT_Init_FreeType(&ftlib);
    }
}

Definition at line 74 of file nsCairoFontMetrics.cpp.

{
    FT_Done_Face(mFace);
}

Member Function Documentation

Destroy this font metrics.

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

Implements nsIFontMetrics.

Definition at line 125 of file nsCairoFontMetrics.cpp.

{
    return NS_OK;
}
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:

Returns the average character width.

Implements nsIFontMetrics.

Definition at line 294 of file nsCairoFontMetrics.cpp.

{
    aAveCharWidth = NSToCoordRound(7 * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 214 of file nsCairoFontMetrics.cpp.

{
    /* units above the base line */
#if FONT_SIZE == 10
    const nscoord emAscent = 10;
#else
    /* XXX this is for 12px verdana ... */
    const nscoord emAscent = 12;
#endif
    aAscent = NSToCoordRound(emAscent * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 228 of file nsCairoFontMetrics.cpp.

{
    /* units below the base line */
#if FONT_SIZE == 10
    const nscoord emDescent = 0;
#else
    /* XXX this is for 12px verdana ... */
    const nscoord emDescent = 2;
#endif
    aDescent = NSToCoordRound(emDescent * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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 199 of file nsCairoFontMetrics.cpp.

{
    /* ascent + descent */
#if FONT_SIZE == 10
    const nscoord emHeight = 10;
#else
    /* XXX this is for 12px verdana ... */
    const nscoord emHeight = 14;
#endif

    aHeight = NSToCoordRound(emHeight * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

Definition at line 191 of file nsCairoFontMetrics.cpp.

{
    // aLeading = 0 * mDev2App;
    aLeading = 0;
    return NS_OK;
}

Returns the font handle associated with these metrics.

Implements nsIFontMetrics.

Definition at line 288 of file nsCairoFontMetrics.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 176 of file nsCairoFontMetrics.cpp.

{
    aHeight = NSToCoordRound((mFace->size->metrics.height >> 6) * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

Definition at line 183 of file nsCairoFontMetrics.cpp.

{
    // aLeading = 0 * mDev2App;
    aLeading = 0;
    return NS_OK;
}

Returns the language group associated with these metrics.

Implements nsIFontMetrics.

Definition at line 280 of file nsCairoFontMetrics.cpp.

{
    *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 373 of file nsCairoFontMetrics.cpp.

{
    aLeading = 0;
    return NS_OK;
}

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

Implements nsIFontMetrics.

Definition at line 272 of file nsCairoFontMetrics.cpp.

{
    aAdvance = FT_CEIL(FT_MulFix(mMaxAdvance, mFace->size->metrics.x_scale));
    aAdvance = NSToCoordRound(aAdvance * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 254 of file nsCairoFontMetrics.cpp.

{
    /* units above the base line */
    aAscent = FT_CEIL(FT_MulFix(mMaxAscent, mFace->size->metrics.y_scale));
    aAscent = NSToCoordRound(aAscent * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 263 of file nsCairoFontMetrics.cpp.

{
    /* units below the base line */
    aDescent = -FT_CEIL(FT_MulFix(mMaxDescent, mFace->size->metrics.y_scale));
    aDescent = NSToCoordRound(aDescent * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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 242 of file nsCairoFontMetrics.cpp.

{
    /* ascent + descent */
    aHeight = FT_CEIL(FT_MulFix(mMaxAscent, mFace->size->metrics.y_scale))
              - FT_CEIL(FT_MulFix(mMaxDescent, mFace->size->metrics.y_scale))
              + 1;

    aHeight = NSToCoordRound(aHeight * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 380 of file nsCairoFontMetrics.cpp.

{
    aLineHeight = 10;
    return NS_OK;
}
NS_IMETHODIMP nsCairoFontMetrics::GetSpaceWidth ( nscoord aSpaceCharWidth) [virtual]

Returns the often needed width of the space character.

Implements nsIFontMetrics.

Definition at line 301 of file nsCairoFontMetrics.cpp.

{
    FT_Load_Char(mFace, ' ', FT_LOAD_DEFAULT | FT_LOAD_NO_AUTOHINT);

    aSpaceCharWidth = NSToCoordRound((mFace->glyph->advance.x >> 6) * mDev2App);

    return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP nsCairoFontMetrics::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 152 of file nsCairoFontMetrics.cpp.

{
    aOffset = 0;
    aSize = NSToCoordRound(1 * mDev2App);
    return NS_OK;
}

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 145 of file nsCairoFontMetrics.cpp.

{
    aResult = 0;
    return NS_OK;
}

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 138 of file nsCairoFontMetrics.cpp.

{
    aResult = 0;
    return NS_OK;
}
NS_IMETHODIMP nsCairoFontMetrics::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 160 of file nsCairoFontMetrics.cpp.

{
    const FT_Fixed scale = mFace->size->metrics.y_scale;

    aOffset = FT_FLOOR(FT_MulFix(mUnderlineOffset, scale));
    aOffset = NSToCoordRound(aOffset * mDev2App);

    aSize = FT_CEIL(FT_MulFix(mUnderlineHeight, scale));
    if (aSize > 1)
        aSize = 1;
    aSize = NSToCoordRound(aSize * mDev2App);

    return NS_OK;
}

Here is the call graph for this function:

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

Implements nsIFontMetrics.

Definition at line 131 of file nsCairoFontMetrics.cpp.

{
    aResult = NSToCoordRound(14 * mDev2App);
    return NS_OK;
}

Here is the call graph for this function:

NS_IMETHODIMP nsCairoFontMetrics::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 95 of file nsCairoFontMetrics.cpp.

{
    mFont = aFont;
    mLangGroup = aLangGroup;

    mDeviceContext = aContext;
    mDev2App = 1.0;

    char *fontPath = GetFontPath();
    FT_Error ferr = FT_New_Face(ftlib, fontPath, 0, &mFace);
    if (ferr != 0) {
        fprintf (stderr, "FT Error: %d\n", ferr);
        return NS_ERROR_FAILURE;
    }
    free(fontPath);

    FT_Set_Char_Size(mFace, 0, FONT_SIZE << 6, 72, 72);

    mMaxAscent  = mFace->bbox.yMax;
    mMaxDescent = mFace->bbox.yMin;
    mMaxAdvance = mFace->max_advance_width;

    mUnderlineOffset = mFace->underline_position;
    mUnderlineHeight = mFace->underline_thickness;

    return NS_OK;
}

Here is the call graph for this function:

nscoord nsCairoFontMetrics::MeasureString ( const char *  aString,
PRUint32  aLength 
)

Definition at line 311 of file nsCairoFontMetrics.cpp.

{
    nscoord width = 0;
    PRUint32 i;
    int error;

    FT_UInt glyph_index;
    FT_UInt previous = 0;

    for (i=0; i < aLength; ++i) {
        glyph_index = FT_Get_Char_Index(mFace, aString[i]);
/*
        if (previous && glyph_index) {
            FT_Vector delta;
            FT_Get_Kerning(mFace, previous, glyph_index,
                           FT_KERNING_DEFAULT, &delta);
            width += delta.x >> 6;
        }
*/
        error = FT_Load_Glyph(mFace, glyph_index, FT_LOAD_DEFAULT | FT_LOAD_NO_AUTOHINT);
        if (error)
            continue;

        width += mFace->glyph->advance.x;
        previous = glyph_index;
    }

    return NSToCoordRound((width >> 6) * mDev2App);
}

Here is the call graph for this function:

Definition at line 342 of file nsCairoFontMetrics.cpp.

{
    nscoord width = 0;
    PRUint32 i;
    int error;

    FT_UInt glyph_index;
    FT_UInt previous = 0;

    for (i=0; i < aLength; ++i) {
        glyph_index = FT_Get_Char_Index(mFace, aString[i]);
/*
        if (previous && glyph_index) {
            FT_Vector delta;
            FT_Get_Kerning(mFace, previous, glyph_index,
                           FT_KERNING_DEFAULT, &delta);
            width += delta.x >> 6;
        }
*/
        error = FT_Load_Glyph(mFace, glyph_index, FT_LOAD_DEFAULT | FT_LOAD_NO_AUTOHINT);
        if (error)
            continue;

        width += mFace->glyph->advance.x;
        previous = glyph_index;
    }

    return NSToCoordRound((width >> 6) * mDev2App);
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 92 of file nsCairoFontMetrics.h.

Definition at line 90 of file nsCairoFontMetrics.h.

Definition at line 89 of file nsCairoFontMetrics.h.

nsFont nsIFontMetrics::mFont [protected, inherited]

Definition at line 238 of file nsIFontMetrics.h.

Definition at line 91 of file nsCairoFontMetrics.h.

Definition at line 96 of file nsCairoFontMetrics.h.

Definition at line 94 of file nsCairoFontMetrics.h.

Definition at line 95 of file nsCairoFontMetrics.h.

Definition at line 99 of file nsCairoFontMetrics.h.

Definition at line 98 of file nsCairoFontMetrics.h.


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