home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Silly String / Hack.c next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  4.7 KB  |  234 lines

  1. //Silly String
  2. //MacHack 16
  3. //©2001 Barry Semo. All Rights Reserved.
  4.  
  5. #include <MacTypes.h>
  6. #include <Fonts.h>
  7. #include <A4Stuff.h>
  8. #include <Resources.h>
  9. #include <Traps.h>
  10.  
  11. //color values taken from inside mac/think reference from old quickdraw
  12. RGBColor kCyan = {0x0241, 0xab54, 0xeaff};
  13. RGBColor kMagenta = {0xf2d7, 0x0856, 0x84ec};
  14. RGBColor kYellow = {0xfc00, 0xf37d, 0x052f};
  15. RGBColor kRed = {0xdd6b, 0x0862, 0x06a2};
  16. RGBColor kGreen = {0, 0x8000, 0x11b0};
  17. RGBColor kBlue = {0, 0, 0xd400};
  18. RGBColor kBlack = {0, 0, 0};
  19.  
  20. RGBColor kTangerine = {0xffff, 0x8000, 0};
  21. RGBColor kPurple = {0xaaaa, 0x0000, 0xffff};//indigo
  22.  
  23. typedef CALLBACK_API(void , DrawStringProcPtr)(ConstStr255Param);
  24. typedef CALLBACK_API(short , StringWidthProcPtr)(ConstStr255Param);
  25.  
  26. extern pascal void myDrawStringPatch(Str255 theString);
  27. extern pascal short myStringWidthPatch(Str255 theString);
  28.  
  29. void DoTag(const char inTag);
  30.  
  31. DrawStringProcPtr gOldDrawString;
  32. StringWidthProcPtr gOldStringWidth;
  33.  
  34. pascal void main(void)
  35. {
  36.     EnterCodeResource();
  37.     
  38.     ::DetachResource(::Get1Resource('INIT', 128));
  39.  
  40.     gOldDrawString = reinterpret_cast<DrawStringProcPtr>(::NGetTrapAddress(_DrawString, ToolTrap));
  41.     ::NSetTrapAddress(reinterpret_cast<UniversalProcPtr>(myDrawStringPatch), _DrawString, ToolTrap);
  42.  
  43.     gOldStringWidth = reinterpret_cast<StringWidthProcPtr>(::NGetTrapAddress(_StringWidth, ToolTrap));
  44.     ::NSetTrapAddress(reinterpret_cast<UniversalProcPtr>(myStringWidthPatch), _StringWidth, ToolTrap);
  45.  
  46.     ExitCodeResource();
  47. }
  48.  
  49. pascal short myStringWidthPatch(Str255 theString)
  50. {
  51.     EnterCodeResource();
  52.  
  53.     short width = 0;
  54.     
  55.     //calc orig width
  56.     short i;
  57.     for (i = 1; i <= theString[0]; i++)
  58.         {
  59.         if (theString[i] == '\\')
  60.             {
  61.             if ((theString[i+1]) != 0)//check for null end of string
  62.                 DoTag(static_cast<char>(theString[++i]));
  63.             }
  64.         else
  65.             width += ::CharWidth(theString[i]);
  66.         }
  67.         
  68.     ExitCodeResource();
  69.     
  70.     return width;
  71. }
  72.  
  73. pascal void myDrawStringPatch(Str255 theString)
  74. {
  75.     // Get globals
  76.     EnterCodeResource();
  77.     
  78.     short i;
  79.     RGBColor origColor;
  80.     short origFace, origFont, origSize;
  81.     GrafPtr grafPort;
  82.     
  83.     //get original state
  84.     ::GetPort(&grafPort);
  85.     if (grafPort != 0L)
  86.         {
  87.         origFace = grafPort->txFace;
  88.         origFont = grafPort->txFont;
  89.         origSize = grafPort->txSize;
  90.         }
  91.     ::GetForeColor(&origColor);
  92.     
  93.     //mess with it
  94.     short charIndex = 0;
  95.     for (i = 1; i <= theString[0]; i++)
  96.         {
  97.         if (theString[i] == '\\')
  98.             DoTag(static_cast<char>(theString[++i]));
  99.         else
  100.             ::DrawChar(theString[i]);
  101.         }
  102.     
  103.     //restore
  104.     ::RGBForeColor(&origColor);
  105.     ::TextFace (origFace);
  106.     ::TextFont(origFont);
  107.     ::TextSize(origSize);
  108.     
  109.     // Release globals
  110.     ExitCodeResource();
  111. }
  112.  
  113. #pragma mark -
  114.  
  115. void DoTag(const char inTag)
  116. {
  117.     GrafPtr grafPort = 0L;
  118.     short newFace = 0;
  119.     short fontNum = 0;
  120.  
  121.     ::GetPort(&grafPort);
  122.     if (grafPort != 0L)
  123.         newFace = grafPort->txFace;
  124.  
  125.     switch (inTag) {
  126.         case 'a'://appfont
  127.             ::TextFont(applFont);
  128.             break;
  129.         case 'b'://bold
  130.             newFace |= bold;
  131.             break;
  132.         case 'c'://cyan
  133.             ::RGBForeColor(&kCyan);
  134.             break;
  135.         case 'e'://cooper black
  136.             ::GetFNum("\pCooper Black", &fontNum);
  137.             ::TextFont(fontNum);
  138.             break;
  139.         case 'f'://parisian
  140.             ::GetFNum("\pParisian", &fontNum);
  141.             ::TextFont(fontNum);
  142.             break;
  143.         case 'g'://green
  144.             ::RGBForeColor(&kGreen);
  145.             break;
  146.         case 'i'://italic
  147.             newFace |= italic;
  148.             break;
  149.         case 'k'://black
  150.             ::RGBForeColor(&kBlack);
  151.             break;
  152.         case 'm'://magenta
  153.             ::RGBForeColor(&kMagenta);
  154.             break;
  155.         case 'o'://outline
  156.             newFace |= outline;
  157.             break;
  158.         case 'p'://purple
  159.             ::RGBForeColor(&kPurple);
  160.             break;
  161.         case 'r'://red
  162.             ::RGBForeColor(&kRed);
  163.             break;
  164.         case 's'://sand
  165.             ::GetFNum("\pSand", &fontNum);
  166.             ::TextFont(fontNum);
  167.             break;
  168.         case 't'://tangerine (not orange cuz o is used)
  169.             ::RGBForeColor(&kTangerine);
  170.             break;
  171.         case 'u'://underline
  172.             newFace |= underline;
  173.             break;
  174.         case 'v'://blue - b is bold so use v
  175.             ::RGBForeColor(&kBlue);
  176.             break;
  177.         case 'w'://stencil
  178.             ::GetFNum("\pStencil", &fontNum);
  179.             ::TextFont(fontNum);
  180.             break;
  181.         case 'x'://swing
  182.             ::GetFNum("\pSwing", &fontNum);
  183.             ::TextFont(fontNum);
  184.             break;
  185.         case 'y'://yellow
  186.             ::RGBForeColor(&kYellow);
  187.             break;
  188.         case 'z'://zapf dingbats
  189.             ::GetFNum("\pZapf Dingbats", &fontNum);
  190.             ::TextFont(fontNum);
  191.             break;
  192.         case '^'://superscript
  193.             ::TextSize(7);
  194.             ::Move(0,-4);
  195.             break;
  196.         case '-'://normal
  197.             newFace = normal;
  198.             break;
  199.         case '.'://subscript
  200.             ::TextSize(7);
  201.             break;
  202.         case '1':
  203.             ::TextSize(5);
  204.             break;
  205.         case '2':
  206.             ::TextSize(6);
  207.             break;
  208.         case '3':
  209.             ::TextSize(8);
  210.             break;
  211.         case '4':
  212.             ::TextSize(9);
  213.             break;
  214.         case '5':
  215.             ::TextSize(12);
  216.             break;
  217.         case '6':
  218.             ::TextSize(14);
  219.             break;
  220.         case '7':
  221.             ::TextSize(16);
  222.             break;
  223.         case '8':
  224.             ::TextSize(18);
  225.             break;
  226.         case '9':
  227.             ::TextSize(20);
  228.             break;
  229.         default:
  230.             break;
  231.         }
  232.     ::TextFace(newFace);
  233. }
  234.