home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / OSAX Samples / Basic OSAX / FontMaxWidth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-07  |  2.5 KB  |  105 lines  |  [TEXT/KAHL]

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    FontMaxWidth.c
  4. //    Written by Donald Olson
  5. //    Copyright ®1993 Apple Computer Inc.
  6. //    All rights reserved.
  7. //
  8. //    This is a simple Scripting Addition that returns the font max width of the
  9. //    specified font and size. If the size parameter is not sp[ecified, default
  10. //    size of 12 is used. Written for a WWDC presentation by Donald Olson
  11. //    and Donn Denman.
  12. //
  13. //    Syntax: font max width <font name [size <short>]
  14. //    Returns: short
  15. //
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17.  
  18. // Our includes
  19. #include <string.h>
  20. #include <Quickdraw.h>
  21. #include <Fonts.h>
  22. #include <Errors.h>
  23. #include <AppleEvents.h>
  24.  
  25. // Our header
  26. Boolean GetFontNumber( Str255 fontName, short *fontNum );
  27.  
  28. /*
  29. This is the structure returned from FontInfo.
  30. struct FontInfo {
  31.     short ascent;
  32.     short descent;
  33.     short widMax;
  34.     short leading;
  35. };
  36. */
  37. */
  38. //AEVTFINFmxwd
  39. #define keyFONTSIZE    'FSIZ'
  40.  
  41. pascal OSErr main(AppleEvent *theEvent, 
  42.                 AppleEvent *theReply,
  43.                 long theRefCon) 
  44. {    
  45.     OSErr        theErr = noErr;
  46.     GrafPort        fontPort;
  47.     GrafPtr        savedPort, fontPortPtr = &fontPort;
  48.     Str255        fontName;
  49.     long            actualSize;
  50.     short        fontSize, fontNum;
  51.     DescType    typeCode;
  52.     FontInfo        fInfo;
  53.  
  54.     theErr = AEGetParamPtr(theEvent, keyDirectObject, 
  55.                 typeChar, &typeCode, (Ptr)&fontName,
  56.                  sizeof(fontName), &actualSize);
  57.     
  58.     if(theErr != noErr)
  59.         return theErr;
  60.     
  61.     fontName[actualSize] = '\0';        // c string please
  62.     c2pstr((char*)fontName);        // and now pascal
  63.     
  64.     theErr = AEGetParamPtr(theEvent, keyFONTSIZE, 
  65.                 typeShortInteger, &typeCode, (Ptr)&fontSize, 
  66.                 sizeof(fontSize), &actualSize);
  67.     
  68.     if(theErr != noErr)
  69.         fontSize = 12;
  70.     
  71.     if(!GetFontNumber(fontName, &fontNum))
  72.         return fontDecError;
  73.                 
  74.     GetPort(&savedPort);
  75.     OpenPort(&fontPortPtr);
  76.     SetPort(&fontPortPtr);
  77.     
  78.     TextFont(fontNum);
  79.     TextSize(fontSize);
  80.     
  81.     GetFontInfo(&fInfo);
  82.     
  83.     SetPort(&savedPort);
  84.     
  85.     theErr = AEPutParamPtr(    theReply, keyDirectObject, typeShortInteger, 
  86.                             (Ptr)&fInfo.widMax, sizeof(fInfo.widMax));
  87.     
  88.     return theErr;
  89. }
  90.  
  91. Boolean GetFontNumber( Str255 fontName, short *fontNum )
  92. {
  93.     Str255     systemFontName;
  94.  
  95.     GetFNum( fontName, fontNum );
  96.     if ( *fontNum == 0) {
  97.         /* Either we didn't find the font, or we were looking for the system
  98.           * font. */
  99.         GetFontName( 0, systemFontName );
  100.         return EqualString( fontName, systemFontName, FALSE, FALSE );
  101.     }
  102.     else
  103.         return TRUE;
  104. }
  105.