wxMetafileRefData::wxMetafileRefData( int width, int height) { Init(); m_width = width; m_height = height; CGRect r = CGRectMake( 0 , 0 , width , height ); CFMutableDataRef data = CFDataCreateMutable(kCFAllocatorDefault, 0); m_data.reset(data); CGDataConsumerRef dataConsumer = wxMacCGDataConsumerCreateWithCFData(data); m_context = CGPDFContextCreate( dataConsumer, (width != 0 && height != 0) ? &r : NULL , NULL ); CGDataConsumerRelease( dataConsumer ); if ( m_context ) { CGPDFContextBeginPage(m_context, NULL); CGColorSpaceRef genericColorSpace = wxMacGetGenericRGBColorSpace(); CGContextSetFillColorSpace( m_context, genericColorSpace ); CGContextSetStrokeColorSpace( m_context, genericColorSpace ); CGContextTranslateCTM( m_context , 0 , height ) ; CGContextScaleCTM( m_context , 1 , -1 ) ; } }
void Picture_copyToClipboard (Picture me) { /* * Find the clipboard and clear it. */ PasteboardRef clipboard = nullptr; PasteboardCreate (kPasteboardClipboard, & clipboard); PasteboardClear (clipboard); /* * Add a PDF flavour to the clipboard. */ static CGDataConsumerCallbacks callbacks = { appendBytes, nullptr }; CFDataRef data = CFDataCreateMutable (kCFAllocatorDefault, 0); CGDataConsumerRef consumer = CGDataConsumerCreate ((void *) data, & callbacks); int resolution = 600; CGRect rect = CGRectMake (0, 0, (my selx2 - my selx1) * resolution, (my sely1 - my sely2) * resolution); CGContextRef context = CGPDFContextCreate (consumer, & rect, nullptr); //my selx1 * RES, (12 - my sely2) * RES, my selx2 * RES, (12 - my sely1) * RES) {// scope autoGraphics graphics = Graphics_create_pdf (context, resolution, my selx1, my selx2, my sely1, my sely2); Graphics_play (my graphics.get(), graphics.get()); } PasteboardPutItemFlavor (clipboard, (PasteboardItemID) 1, kUTTypePDF, data, kPasteboardFlavorNoFlags); CFRelease (data); /* * Forget the clipboard. */ CFRelease (clipboard); }
/* The idea of a scratch context comes from WebKit */ static CGContextRef CreateScratchContext() { CGDataConsumerCallbacks callbacks = {PutBytesNull, nullptr}; CGDataConsumerRef consumer = CGDataConsumerCreate(nullptr, &callbacks); CGContextRef cg = CGPDFContextCreate(consumer, nullptr, nullptr); CGDataConsumerRelease(consumer); return cg; }
static CGContextRef createScratchContext() { CGDataConsumerCallbacks callbacks = { putBytesNowhere, 0 }; RetainPtr<CGDataConsumerRef> consumer(AdoptCF, CGDataConsumerCreate(0, &callbacks)); CGContextRef context = CGPDFContextCreate(consumer.get(), 0, 0); CGFloat black[4] = { 0, 0, 0, 1 }; CGContextSetFillColor(context, black); CGContextSetStrokeColor(context, black); return context; }
CGContextRef scratchContext() { static CGContextRef scratch = 0; if (!scratch) { CFMutableDataRef empty = CFDataCreateMutable(NULL, 0); CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(empty); scratch = CGPDFContextCreate(consumer, NULL, NULL); CGDataConsumerRelease(consumer); CFRelease(empty); CGFloat black[4] = {0, 0, 0, 1}; CGContextSetFillColor(scratch, black); CGContextSetStrokeColor(scratch, black); } return scratch; }
static void quartzgen_begin_page(GVJ_t *job) { CGRect bounds = CGRectMake(0.0, 0.0, job->width, job->height); if (!job->context) { switch (job->device.id) { case FORMAT_PDF: { /* create the auxiliary info for PDF content, author and title */ CFStringRef auxiliaryKeys[] = { kCGPDFContextCreator, kCGPDFContextTitle }; CFStringRef auxiliaryValues[] = { CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%s %s"), job->common->info[0], job->common->info[1]), job->obj->type == ROOTGRAPH_OBJTYPE ? CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)job->obj->u.g->name, strlen(job->obj->u.g->name), kCFStringEncodingUTF8, false, kCFAllocatorNull) : CFSTR("") }; CFDictionaryRef auxiliaryInfo = CFDictionaryCreate( kCFAllocatorDefault, (const void **)&auxiliaryKeys, (const void **)&auxiliaryValues, sizeof(auxiliaryValues)/sizeof(auxiliaryValues[0]), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); /* create a PDF for drawing into */ CGDataConsumerRef data_consumer = CGDataConsumerCreate(job, &device_data_consumer_callbacks); job->context = CGPDFContextCreate(data_consumer, &bounds, auxiliaryInfo); /* clean up */ CGDataConsumerRelease(data_consumer); CFRelease(auxiliaryInfo); int i; for (i = 0; i < sizeof(auxiliaryValues)/sizeof(auxiliaryValues[0]); ++i) CFRelease(auxiliaryValues[i]); } break; default: /* bitmap formats */ { size_t bytes_per_row = (job->width * BYTES_PER_PIXEL + BYTE_ALIGN) & ~BYTE_ALIGN; void* buffer = NULL; #if __ENVIRONMENT_IPHONE_OS_VERSION_MIN_REQUIRED__ >= 20000 /* iPhoneOS has no swap files for memory, so if we're short of memory we need to make our own temp scratch file to back it */ size_t buffer_size = job->height * bytes_per_row; mach_msg_type_number_t vm_info_size = HOST_VM_INFO_COUNT; vm_statistics_data_t vm_info; if (host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vm_info, &vm_info_size) != KERN_SUCCESS || buffer_size * 2 > vm_info.free_count * vm_page_size) { FILE* temp_file = tmpfile(); if (temp_file) { int temp_file_descriptor = fileno(temp_file); if (temp_file_descriptor >= 0 && ftruncate(temp_file_descriptor, buffer_size) == 0) { buffer = mmap( NULL, buffer_size, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, temp_file_descriptor, 0); if (buffer == (void*)-1) buffer = NULL; } fclose(temp_file); } } if (!buffer) buffer = mmap( NULL, buffer_size, PROT_READ | PROT_WRITE, MAP_ANON| MAP_SHARED, -1, 0); #endif /* create a true color bitmap for drawing into */ CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB(); job->context = CGBitmapContextCreate( buffer, /* data: MacOSX lets system allocate, iPhoneOS use manual memory mapping */ job->width, /* width in pixels */ job->height, /* height in pixels */ BITS_PER_COMPONENT, /* bits per component */ bytes_per_row, /* bytes per row: align to 16 byte boundary */ color_space, /* color space: device RGB */ kCGImageAlphaPremultipliedFirst /* bitmap info: premul ARGB has best support in OS X */ ); job->imagedata = CGBitmapContextGetData((CGContextRef)job->context); /* clean up */ CGColorSpaceRelease(color_space); } break; } } /* start the page (if this is a paged context) and graphics state */ CGContextRef context = (CGContextRef)job->context; CGContextBeginPage(context, &bounds); CGContextSaveGState(context); CGContextSetMiterLimit(context, 1.0); CGContextSetLineJoin(context, kCGLineJoinRound); /* set up the context transformation */ CGContextScaleCTM(context, job->scale.x, job->scale.y); CGContextRotateCTM(context, -job->rotation * M_PI / 180.0); CGContextTranslateCTM(context, job->translation.x, job->translation.y); }