Re: Generating PDF images (+followup question)
Re: Generating PDF images (+followup question)
- Subject: Re: Generating PDF images (+followup question)
- From: Graham Cox <email@hidden>
- Date: Fri, 28 May 2010 13:40:55 +1000
Thanks Kyle, that seems to look like a fruitful approach. I'm doing the following, which appears to be enough, i.e. it works. But should I be doing anything else?
NSSize size = [self bounds].size;
NSRect destRect = NSZeroRect;
destRect.size = size;
NSMutableData* pdfData = [NSMutableData data];
CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData((CFMutableDataRef) pdfData );
CGRect mediaBox = CGRectMake( 0, 0, size.width, size.height );
CGContextRef pdfContext = CGPDFContextCreate( consumer, &mediaBox, NULL );
CGDataConsumerRelease( consumer );
NSGraphicsContext* newGC = [NSGraphicsContext graphicsContextWithGraphicsPort:pdfContext flipped:YES];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:newGC];
CGPDFContextBeginPage( pdfContext, NULL );
[self drawContentInRect:destRect fromRect:NSZeroRect withStyle:nil];
CGPDFContextEndPage( pdfContext );
[NSGraphicsContext restoreGraphicsState];
CGPDFContextClose( pdfContext );
CGContextRelease( pdfContext );
return pdfData;
This leads me on to a further question about correctly handling flippedness, which quite honestly has me really confused.
The objects I'm drawing generally assume a flipped context, i.e. my view is flipped and so is my entire coordinate system all the way down. I have not run into any problems with that anywhere, it all works fine. But when I come to generate the PDF above, I do have some flipping confusion.
I flip the context to match the expectation of the object itself. Thus text draws relatively the right way up and in the right place.
If I take that (flipped) PDF data, use it to create an NSPDFImageRep and add it to an NSImage, should the image be flipped or not? (Bear in mind this needs to work on 10.5, so the newer flipped stuff is unavailable to help sort this confusion out).
If I take the resulting image and display it in a cell, what should the cell do about flipping? For example, if I put the image into NSImageView, it works correctly and displays right way up. If I write the image to disk it is imported into Preview right way up. But I have a couple of custom cells that draw images that show the image wrong way up. What are these cells doing wrong that I'm not getting? Should they do anything - maybe just leaving flippedness of the image alone rather than try an match it to the control's context flippedness is right? I just don't know. I've tried all combos I can think of and none of them quite nails it.
--Graham
On 28/05/2010, at 11:24 AM, Kyle Sluder wrote:
> On May 27, 2010, at 6:20 PM, Graham Cox <email@hidden> wrote:
>
>> Can I not create a PDF context, associate that with a NSPDFImageRep instead? It appears I'm forced to use NSPrintOperation to do this which is slightly weird as this has nothing to do with printing, nor do I have a view readily available to attach to the NSPrintOperation. Surely there's a more straightforward way than going round the houses like this?
>
> I believe we create a PDF CGContext, wrap it in an NSGraphicsVontext, and use +setContext: to get our nice vectory goodness.
>
> But I'm going from memory here, and I didn't write that code.
>
> --Kyle Sluder
>>
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden