home *** CD-ROM | disk | FTP | other *** search
- //Silly String
- //MacHack 16
- //©2001 Barry Semo. All Rights Reserved.
-
- #include <MacTypes.h>
- #include <Fonts.h>
- #include <A4Stuff.h>
- #include <Resources.h>
- #include <Traps.h>
-
- //color values taken from inside mac/think reference from old quickdraw
- RGBColor kCyan = {0x0241, 0xab54, 0xeaff};
- RGBColor kMagenta = {0xf2d7, 0x0856, 0x84ec};
- RGBColor kYellow = {0xfc00, 0xf37d, 0x052f};
- RGBColor kRed = {0xdd6b, 0x0862, 0x06a2};
- RGBColor kGreen = {0, 0x8000, 0x11b0};
- RGBColor kBlue = {0, 0, 0xd400};
- RGBColor kBlack = {0, 0, 0};
-
- RGBColor kTangerine = {0xffff, 0x8000, 0};
- RGBColor kPurple = {0xaaaa, 0x0000, 0xffff};//indigo
-
- typedef CALLBACK_API(void , DrawStringProcPtr)(ConstStr255Param);
- typedef CALLBACK_API(short , StringWidthProcPtr)(ConstStr255Param);
-
- extern pascal void myDrawStringPatch(Str255 theString);
- extern pascal short myStringWidthPatch(Str255 theString);
-
- void DoTag(const char inTag);
-
- DrawStringProcPtr gOldDrawString;
- StringWidthProcPtr gOldStringWidth;
-
- pascal void main(void)
- {
- EnterCodeResource();
-
- ::DetachResource(::Get1Resource('INIT', 128));
-
- gOldDrawString = reinterpret_cast<DrawStringProcPtr>(::NGetTrapAddress(_DrawString, ToolTrap));
- ::NSetTrapAddress(reinterpret_cast<UniversalProcPtr>(myDrawStringPatch), _DrawString, ToolTrap);
-
- gOldStringWidth = reinterpret_cast<StringWidthProcPtr>(::NGetTrapAddress(_StringWidth, ToolTrap));
- ::NSetTrapAddress(reinterpret_cast<UniversalProcPtr>(myStringWidthPatch), _StringWidth, ToolTrap);
-
- ExitCodeResource();
- }
-
- pascal short myStringWidthPatch(Str255 theString)
- {
- EnterCodeResource();
-
- short width = 0;
-
- //calc orig width
- short i;
- for (i = 1; i <= theString[0]; i++)
- {
- if (theString[i] == '\\')
- {
- if ((theString[i+1]) != 0)//check for null end of string
- DoTag(static_cast<char>(theString[++i]));
- }
- else
- width += ::CharWidth(theString[i]);
- }
-
- ExitCodeResource();
-
- return width;
- }
-
- pascal void myDrawStringPatch(Str255 theString)
- {
- // Get globals
- EnterCodeResource();
-
- short i;
- RGBColor origColor;
- short origFace, origFont, origSize;
- GrafPtr grafPort;
-
- //get original state
- ::GetPort(&grafPort);
- if (grafPort != 0L)
- {
- origFace = grafPort->txFace;
- origFont = grafPort->txFont;
- origSize = grafPort->txSize;
- }
- ::GetForeColor(&origColor);
-
- //mess with it
- short charIndex = 0;
- for (i = 1; i <= theString[0]; i++)
- {
- if (theString[i] == '\\')
- DoTag(static_cast<char>(theString[++i]));
- else
- ::DrawChar(theString[i]);
- }
-
- //restore
- ::RGBForeColor(&origColor);
- ::TextFace (origFace);
- ::TextFont(origFont);
- ::TextSize(origSize);
-
- // Release globals
- ExitCodeResource();
- }
-
- #pragma mark -
-
- void DoTag(const char inTag)
- {
- GrafPtr grafPort = 0L;
- short newFace = 0;
- short fontNum = 0;
-
- ::GetPort(&grafPort);
- if (grafPort != 0L)
- newFace = grafPort->txFace;
-
- switch (inTag) {
- case 'a'://appfont
- ::TextFont(applFont);
- break;
- case 'b'://bold
- newFace |= bold;
- break;
- case 'c'://cyan
- ::RGBForeColor(&kCyan);
- break;
- case 'e'://cooper black
- ::GetFNum("\pCooper Black", &fontNum);
- ::TextFont(fontNum);
- break;
- case 'f'://parisian
- ::GetFNum("\pParisian", &fontNum);
- ::TextFont(fontNum);
- break;
- case 'g'://green
- ::RGBForeColor(&kGreen);
- break;
- case 'i'://italic
- newFace |= italic;
- break;
- case 'k'://black
- ::RGBForeColor(&kBlack);
- break;
- case 'm'://magenta
- ::RGBForeColor(&kMagenta);
- break;
- case 'o'://outline
- newFace |= outline;
- break;
- case 'p'://purple
- ::RGBForeColor(&kPurple);
- break;
- case 'r'://red
- ::RGBForeColor(&kRed);
- break;
- case 's'://sand
- ::GetFNum("\pSand", &fontNum);
- ::TextFont(fontNum);
- break;
- case 't'://tangerine (not orange cuz o is used)
- ::RGBForeColor(&kTangerine);
- break;
- case 'u'://underline
- newFace |= underline;
- break;
- case 'v'://blue - b is bold so use v
- ::RGBForeColor(&kBlue);
- break;
- case 'w'://stencil
- ::GetFNum("\pStencil", &fontNum);
- ::TextFont(fontNum);
- break;
- case 'x'://swing
- ::GetFNum("\pSwing", &fontNum);
- ::TextFont(fontNum);
- break;
- case 'y'://yellow
- ::RGBForeColor(&kYellow);
- break;
- case 'z'://zapf dingbats
- ::GetFNum("\pZapf Dingbats", &fontNum);
- ::TextFont(fontNum);
- break;
- case '^'://superscript
- ::TextSize(7);
- ::Move(0,-4);
- break;
- case '-'://normal
- newFace = normal;
- break;
- case '.'://subscript
- ::TextSize(7);
- break;
- case '1':
- ::TextSize(5);
- break;
- case '2':
- ::TextSize(6);
- break;
- case '3':
- ::TextSize(8);
- break;
- case '4':
- ::TextSize(9);
- break;
- case '5':
- ::TextSize(12);
- break;
- case '6':
- ::TextSize(14);
- break;
- case '7':
- ::TextSize(16);
- break;
- case '8':
- ::TextSize(18);
- break;
- case '9':
- ::TextSize(20);
- break;
- default:
- break;
- }
- ::TextFace(newFace);
- }
-