Back to index

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

#include <nsATSUIUtils.h>

Collaboration diagram for nsATSUIToolkit:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsATSUIToolkit ()
 ~nsATSUIToolkit ()
void PrepareToDraw (CGrafPtr aPort, nsIDeviceContext *aContext)
nsresult GetTextDimensions (const PRUnichar *aCharPt, PRUint32 aLen, nsTextDimensions &oDim, short aSize, short fontNum, PRBool aBold, PRBool aItalic, nscolor aColor)
nsresult DrawString (const PRUnichar *aCharPt, PRUint32 aLen, PRInt32 x, PRInt32 y, short &oWidth, short aSize, short fontNum, PRBool aBold, PRBool aItalic, nscolor aColor)

Private Member Functions

void StartDraw (const PRUnichar *aCharPt, PRUint32 aLen, short aSize, short fontNum, PRBool aBold, PRBool aItalic, nscolor aColor, ATSUTextLayout &oLayout)
ATSUTextLayout GetTextLayout (short aFontNum, short aSize, PRBool aBold, PRBool aItalic, nscolor aColor)

Private Attributes

CGrafPtr mPort
nsIDeviceContextmContext

Detailed Description

Definition at line 69 of file nsATSUIUtils.h.


Constructor & Destructor Documentation

Definition at line 242 of file nsATSUIUtils.cpp.

Here is the call graph for this function:

Definition at line 73 of file nsATSUIUtils.h.

{};

Member Function Documentation

nsresult nsATSUIToolkit::DrawString ( const PRUnichar aCharPt,
PRUint32  aLen,
PRInt32  x,
PRInt32  y,
short &  oWidth,
short  aSize,
short  fontNum,
PRBool  aBold,
PRBool  aItalic,
nscolor  aColor 
)

Definition at line 518 of file nsATSUIUtils.cpp.

{
  oWidth = 0;
  if (!nsATSUIUtils::IsAvailable())
       return NS_ERROR_NOT_INITIALIZED;
       
  StPortSetter    setter(mPort);

  ATSUTextLayout aTxtLayout;
  
  StartDraw(aCharPt, aLen, aSize, aFontNum, aBold, aItalic, aColor, aTxtLayout);
  if (nsnull == aTxtLayout)
    return NS_ERROR_FAILURE;

  OSStatus err = noErr;     
  ATSUTextMeasurement iAfter; 
  err = ::ATSUGetUnjustifiedBounds(aTxtLayout, 0, aLen, NULL, &iAfter, NULL,
                                   NULL);
  if (noErr != err) {
     NS_WARNING("MeasureText failed");
     return NS_ERROR_FAILURE;
  } 

  err = ::ATSUDrawText(aTxtLayout, 0, aLen, Long2Fix(x), Long2Fix(y));
  if (noErr != err) {
    NS_WARNING("ATSUDrawText failed");
    return NS_ERROR_FAILURE;
  } 

  oWidth = FixRound(iAfter);
  // aTxtLayout is cached and does not need to be disposed
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsATSUIToolkit::GetTextDimensions ( const PRUnichar aCharPt,
PRUint32  aLen,
nsTextDimensions oDim,
short  aSize,
short  fontNum,
PRBool  aBold,
PRBool  aItalic,
nscolor  aColor 
)

Definition at line 423 of file nsATSUIUtils.cpp.

{
  if (!nsATSUIUtils::IsAvailable())
    return NS_ERROR_NOT_INITIALIZED;

  StPortSetter    setter(mPort);

  ATSUTextLayout aTxtLayout;
  StartDraw(aCharPt, aLen, aSize, aFontNum, aBold, aItalic, aColor, aTxtLayout);
  if (nsnull == aTxtLayout) 
     return NS_ERROR_FAILURE;

  OSStatus err = noErr;
  ATSUTextMeasurement after;
  ATSUTextMeasurement ascent;
  ATSUTextMeasurement descent;
  err = ::ATSUGetUnjustifiedBounds(aTxtLayout, 0, aLen, NULL, &after, &ascent,
                                   &descent);
  if (noErr != err)
  {
    NS_WARNING("ATSUGetUnjustifiedBounds failed");
    return NS_ERROR_FAILURE;
  }

  oDim.width = FixRound(after);
  oDim.ascent = FixRound(ascent);
  oDim.descent = FixRound(descent);
  // aTxtLayout is cached and does not need to be disposed
  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

ATSUTextLayout nsATSUIToolkit::GetTextLayout ( short  aFontNum,
short  aSize,
PRBool  aBold,
PRBool  aItalic,
nscolor  aColor 
) [private]

Definition at line 257 of file nsATSUIUtils.cpp.

{ 
       ATSUTextLayout txLayout = nsnull;
       OSStatus err;
       if (nsATSUIUtils::gTxLayoutCache->Get(aFontNum, aSize, aBold, aItalic, aColor, &txLayout))
              return txLayout;
              
       UniChar dmy[1];
       err = ::ATSUCreateTextLayoutWithTextPtr (dmy, 0,0,0,0,NULL, NULL, &txLayout);
       if(noErr != err) {
              NS_WARNING("ATSUCreateTextLayoutWithTextPtr failed");
    // goto errorDone;
    return nsnull;
       }

       ATSUStyle                          theStyle;
       err = ::ATSUCreateStyle(&theStyle);
       if(noErr != err) {
              NS_WARNING("ATSUCreateStyle failed");
    // goto errorDoneDestroyTextLayout;
       err = ::ATSUDisposeTextLayout(txLayout);
    return nsnull;
       }

       ATSUAttributeTag            theTag[ATTR_CNT];
       ByteCount                          theValueSize[ATTR_CNT];
       ATSUAttributeValuePtr       theValue[ATTR_CNT];

       //--- Font ID & Face -----         
       ATSUFontID atsuFontID;
       
       // The use of ATSUFONDtoFontID is not recommended, see
       // http://developer.apple.com/documentation/Carbon/Reference/ATSUI_Reference/atsu_reference_Reference/chapter_1.2_section_19.html
       FMFontStyle fbStyle;
       if (::FMGetFontFromFontFamilyInstance(aFontNum, 0, &atsuFontID, &fbStyle) == kFMInvalidFontErr) {
              NS_WARNING("FMGetFontFromFontFamilyInstance failed");
    // goto errorDoneDestroyStyle;
       err = ::ATSUDisposeStyle(theStyle);
       err = ::ATSUDisposeTextLayout(txLayout);
    return nsnull;
       }
       
       theTag[0] = kATSUFontTag;
       theValueSize[0] = (ByteCount) sizeof(ATSUFontID);
       theValue[0] = (ATSUAttributeValuePtr) &atsuFontID;
       //--- Font ID & Face  -----        
       
       //--- Size -----            
       float  dev2app;
       short fontsize = aSize;

       dev2app = mContext->DevUnitsToAppUnits();
  //   Fixed size = FloatToFixed( roundf(float(fontsize) / dev2app));
  Fixed size = FloatToFixed( (float) rint(float(fontsize) / dev2app));
       if( FixRound ( size ) < 9  && !nsFontUtils::DisplayVerySmallFonts())
              size = X2Fix(9);

       theTag[1] = kATSUSizeTag;
       theValueSize[1] = (ByteCount) sizeof(Fixed);
       theValue[1] = (ATSUAttributeValuePtr) &size;
       //--- Size -----            
       
       //--- Color -----           
       RGBColor color;

       #define COLOR8TOCOLOR16(color8)     ((color8 << 8) | color8)          

       color.red = COLOR8TOCOLOR16(NS_GET_R(aColor));
       color.green = COLOR8TOCOLOR16(NS_GET_G(aColor));
       color.blue = COLOR8TOCOLOR16(NS_GET_B(aColor));                       
       theTag[2] = kATSUColorTag;
       theValueSize[2] = (ByteCount) sizeof(RGBColor);
       theValue[2] = (ATSUAttributeValuePtr) &color;
       //--- Color -----           

       //--- Bold -----
       Boolean isBold = aBold ? true : false;
       theTag[3] = kATSUQDBoldfaceTag;
       theValueSize[3] = (ByteCount) sizeof(Boolean);
       theValue[3] = (ATSUAttributeValuePtr) &isBold;
       //--- Bold -----

       //--- Italic -----
       Boolean isItalic = aItalic ? true : false;
       theTag[4] = kATSUQDItalicTag;
       theValueSize[4] = (ByteCount) sizeof(Boolean);
       theValue[4] = (ATSUAttributeValuePtr) &isItalic;
       //--- Italic -----

       err =  ::ATSUSetAttributes(theStyle, ATTR_CNT, theTag, theValueSize, theValue);
       if(noErr != err) {
              NS_WARNING("ATSUSetAttributes failed");
    // goto errorDoneDestroyStyle;
       err = ::ATSUDisposeStyle(theStyle);
       err = ::ATSUDisposeTextLayout(txLayout);
    return nsnull;
       }
              
       err = ::ATSUSetRunStyle(txLayout, theStyle, kATSUFromTextBeginning, kATSUToTextEnd);
       if(noErr != err) {
              NS_WARNING("ATSUSetRunStyle failed");
    // goto errorDoneDestroyStyle;
       err = ::ATSUDisposeStyle(theStyle);
       err = ::ATSUDisposeTextLayout(txLayout);
    return nsnull;
       }
       
    err = ::ATSUSetTransientFontMatching(txLayout, true);
       if(noErr != err) {
              NS_WARNING( "ATSUSetTransientFontMatching failed");
    // goto errorDoneDestroyStyle;
       err = ::ATSUDisposeStyle(theStyle);
       err = ::ATSUDisposeTextLayout(txLayout);
    return nsnull;
       }
       
       nsATSUIUtils::gTxLayoutCache->Set(aFontNum, aSize, aBold, aItalic, aColor,  txLayout);     

       return txLayout;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 382 of file nsATSUIUtils.cpp.

{
   mPort = aPort;
   mContext = aContext;
}

Here is the caller graph for this function:

void nsATSUIToolkit::StartDraw ( const PRUnichar aCharPt,
PRUint32  aLen,
short  aSize,
short  fontNum,
PRBool  aBold,
PRBool  aItalic,
nscolor  aColor,
ATSUTextLayout &  oLayout 
) [private]

Definition at line 392 of file nsATSUIUtils.cpp.

{
  OSStatus err = noErr;
  oLayout = GetTextLayout(aFontNum, aSize, aBold, aItalic, aColor);
  if (nsnull == oLayout) {
    NS_WARNING("GetTextLayout return nsnull");
       return;
  }

  // ATSUSetTextPointerLocation won't invalidate atsui's internal cache if aCharPt is
  // the same address it already has.  therefore, since we are definitely changing the
  // text here, we should explicitly invalidate any existing caches
  ::ATSUClearLayoutCache(oLayout, kATSUFromTextBeginning);
  
  err = ::ATSUSetTextPointerLocation( oLayout, (ConstUniCharArrayPtr)aCharPt, 0, aLen, aLen);
  if (noErr != err) {
    NS_WARNING("ATSUSetTextPointerLocation failed");
       oLayout = nsnull;
  }
  return;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 98 of file nsATSUIUtils.h.

Definition at line 97 of file nsATSUIUtils.h.


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