Re: Generating PDF images (+followup question)
Re: Generating PDF images (+followup question)
- Subject: Re: Generating PDF images (+followup question)
- From: Ken Ferry <email@hidden>
- Date: Thu, 27 May 2010 22:20:21 -0700
On Thu, May 27, 2010 at 8:40 PM, Graham Cox <email@hidden> wrote:
> 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).
>
Hi Graham,
(First, for those following along, flipped images are deprecated in 10.6
along with -[NSImage setFlipped:].)
I agree, flipped images are confusing, and you can more or less think of
them as deprecated prior to 10.6 as well. This weblog post does a nice job
of explaining what everything means and what to do about it: <
http://www.noodlesoft.com/blog/2009/02/02/understanding-flipped-coordinate-systems/>.
This material is also covered in depth in the WWDC 2007 talk, Cocoa Drawing
Techniques.
The only reason I can think of off the top of my head to call -setFlipped:
on an NSImage is if you plan to lockFocus on the image and you want the
context to be a flipped context during drawing. This use case is addressed
in 10.6 by the addition of -[NSImage lockFocusFlipped:] which gives you a
flipped context without doing anything to the internal state of the image.
-Ken
Cocoa Frameworks
> 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
>
_______________________________________________
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