#include #include #include #include static OSStatus DrawHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void* inUserData); static void Heartbeat(EventLoopTimerRef inTimer, void *inRefCon); static CGImageRef GetCGImageFromPath(CFStringRef inPath); void JustDrawSomething(CGContextRef cg, CGRect *bounds); static void createpdf(void) { CGContextRef pdfcg; CFStringRef path; CFURLRef url; CGRect solbounds = {0, 0, 700, 700}; CGRect artbounds = {-5, -5, 710, 710}; path = CFStringCreateWithCString(NULL, "LeWitt.pdf", kCFStringEncodingUTF8); url = CFURLCreateWithFileSystemPath(NULL, path, kCFURLPOSIXPathStyle, 0); CFRelease(path); pdfcg = CGPDFContextCreateWithURL(url, &artbounds, NULL); CFRelease(url); CGContextBeginPage(pdfcg, &artbounds); JustDrawSomething(pdfcg, &solbounds); CGContextEndPage(pdfcg); CGContextRelease(pdfcg); } int main(void) { IBNibRef nibRef; WindowRef window; EventTypeSpec type; HIViewRef contentView; CreateNibReference(CFSTR("main"), &nibRef); SetMenuBarFromNib(nibRef, CFSTR("MenuBar")); CreateWindowFromNib(nibRef, CFSTR("MainWindow"), &window); HIViewFindByID(HIViewGetRoot(window), kHIViewWindowContentID, &contentView); type.eventClass = kEventClassControl; type.eventKind = kEventControlDraw; InstallEventHandler(HIViewGetEventTarget(contentView), DrawHandler, 1, &type, contentView, NULL); // InstallEventLoopTimer(GetCurrentEventLoop(), 1.0, 1.0, Heartbeat, contentView, NULL); createpdf();//added ShowWindow(window); RunApplicationEventLoop(); return 0; } //static CGImageRef image = NULL; /* Within four adjacent squares, each 350 pixels by 350 pixels, four draftsmen will be employed at $4.00/hour for four hours a day and for four days to draw straight lines 40 pixels long using four differently colored pencils; black, red, yellow and blue. Each draftsman will use the same color throughout the four day period, working on a different square each day.*/ void JustDrawSomething(CGContextRef cg, CGRect *bounds){ CGContextSetStrokeColorSpace(cg, CGColorSpaceCreateDeviceRGB()); //CGContextSetLineWidth(cg, .12); #define DAYSWORK 14400 int stroke, work, colorset, colormove; float xsp, ysp, xep, yep, xdif, ydif, flip, swap, correct, linewidth, color[4]; stroke = work = colorset = colormove = 0; //seed rand() w/ system time for different output each run time_t seconds; time(&seconds); srand((unsigned int) seconds); //loops through total number of lines for(stroke == 0; stroke <=(4 * 4 * 4 * 60 * 60); ++stroke){ correct = rand() % 310; //set end point with pythagorean theorem xep = rand() % 400; xep = xep/10; yep = sqrt(1600 - (xep * xep)); xsp = rand() % 350; ysp = rand() % 350; //translate line to start point xep = xep + xsp; yep = yep + ysp; xdif = xep - xsp; ydif = yep - ysp; //flip line 1/2 the time for opposite slope flip = rand() % 2; if(flip == 1){ swap = xsp; xsp = xep; xep = swap; } //translate errant line if(xsp > 350 || xep > 350){ xsp = xsp - (correct + xdif); xep = xep - (correct + xdif); } if(xsp < 0 || xep < 0){ xsp = xsp + (correct - xdif); xep = xep + (correct - xdif); } if(ysp > 350 || yep > 350){ ysp = ysp - (correct + ydif); yep = yep - (correct + ydif); } if(ysp < 0 || yep < 0){ ysp = ysp + (correct - ydif); yep = yep + (correct - ydif); } //translate line into square, set color if(work <= DAYSWORK){ ysp = ysp + 350; yep = yep + 350; colorset = 1 + colormove;} if(work > DAYSWORK && work <= 2*DAYSWORK){ ysp = ysp + 350; yep = yep + 350; xsp = xsp + 350; xep = xep + 350; colorset = 2 + colormove;} if(work > 2*DAYSWORK && work <= 3*DAYSWORK) colorset = 3 + colormove; if(work > 3*DAYSWORK && work <= 4*DAYSWORK){ xsp = xsp + 350; xep = xep + 350; colorset = 4 + colormove;} if(colorset >= 5) colorset = colorset - 4; if(colorset == 1){ color[0] = 0; color[1] = 0; color[2] = 0; color[3] = 1; CGContextSetStrokeColor(cg, color); } if(colorset == 2){ color[0] = 1; color[1] = 0; color[2] = 0; color[3] = 1; CGContextSetStrokeColor(cg, color); } if(colorset == 3){ color[0] = 0; color[1] = 0; color[2] = 1; color[3] = 1; CGContextSetStrokeColor(cg, color); } if(colorset == 4){ color[0] = 1; color[1] = 1; color[2] = 0; color[3] = 1; CGContextSetStrokeColor(cg, color); } linewidth = rand() % 3; linewidth = linewidth/10; CGContextSetLineWidth(cg, linewidth); CGContextMoveToPoint(cg, xsp, ysp); CGContextAddLineToPoint(cg, xep, yep); CGContextStrokePath(cg); ++work; if(work == 4*DAYSWORK){ work = 0; ++colormove; } if(colormove > 4) colormove = 0; } } #if 0 CGContextSelectFont(cg, "Helvetica", 18, kCGEncodingMacRoman); CGContextShowTextAtPoint(cg, 100, 100, "Hello Sol!", 12); #endif #if 0 if (image == NULL) { image = GetCGImageFromPath(CFSTR("lewitt.jpg")); } rect = CGRectMake(150, 250, 300, 200); CGContextDrawImage(cg, rect, image); #endif #if 0 static void Heartbeat(EventLoopTimerRef inTimer, void *inRefCon) { theView = (HIViewRef)inUserData; } #endif static OSStatus DrawHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) { HIViewRef theView; CGContextRef cg; CGRect viewBounds; theView = (HIViewRef)inUserData; GetEventParameter(inEvent, kEventParamCGContextRef, typeCGContextRef, NULL, sizeof(CGContextRef), NULL, &cg); CallNextEventHandler(inHandlerCallRef, inEvent); HIViewGetBounds(theView, &viewBounds); CGContextTranslateCTM(cg, 0, viewBounds.size.height); CGContextScaleCTM(cg, 1.0, -1.0); JustDrawSomething(cg, &viewBounds); return 0; } static CGImageRef GetCGImageFromPath(CFStringRef inPath) { CGImageRef imageRef; Handle dataRef; OSType dataRefType; GraphicsImportComponent gi; ComponentResult result; dataRef = NULL; gi = 0; // create the data reference result = QTNewDataReferenceFromFullPathCFString(inPath, kQTNativeDefaultPathStyle, 0, &dataRef, &dataRefType); if (dataRef == NULL || result != 0) exit(1); // get the graphics importer for the data ref GetGraphicsImporterForDataRefWithFlags(dataRef, dataRefType, &gi, 0); if (gi == NULL) exit(1); GraphicsImportCreateCGImage(gi, &imageRef, 0); if (imageRef == NULL) exit(1); CloseComponent(gi); DisposeHandle(dataRef); return imageRef; }