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 16:11:19 +1000
Thanks for the link Ken, though confusion still persists.
Seems to be saying don't use -setFlipped: unless you really know what you're doing. That concurs with your own advice about not using setFlipped unless you're locking focus on the image to get a flipped context for drawing. I'm not, since I generate my PDF in a context I create for the purpose, THEN add the image rep to an image.
So on the basis that I don't know what I'm doing, I've removed all calls to [NSImage setFlipped:] anywhere. In the PDF generating code quoted, I do pass YES for flipped because the object itself expects a flipped coordinate system, by which I simply mean that when I draw something BELOW something else, it has a GREATER Y coordinate value.
Having no flipping anywhere now, unfortunately it's still all over the place.
First, if I ask the NSPDFImageRep created using the data generated below for its -PDFRepresentation, and write it to disk as a PDF file, it's now inverted. Objects are placed in the correct positions relative to one-another, but the entire image is upside-down, including any text.
So, I try setting the image to flipped when I add the single PDF image rep to it:
NSPDFImageRep* rep = [NSPDFImageRep imageRepWithData:[self pdf]];
[image addRepresentation:rep];
[image setFlipped:YES];
Now this image appears right-side up in NSImageView, but still writes an inverted PDF file and comes into Preview inverted. I can't really see how this is possible, since raw PDF data doesn't have any concept of 'flipped', does it? That surely means that the original PDF generation is wrong, but if I pass NO for flipped, not only is everything still upside-down but text is screwed as well, with each glyph individually inverted (which means that if the whole image is turned right-way up, any text is inverted).
I've read all the documentation on flipped coordinates and now the blog post as well. I'm afraid I'm just as confused as ever. What I need is a clear way through this mess. Turning off all flippedness seemed to be that but isn't. Now I have no idea what I need to flip and when.
--Graham
On 28/05/2010, at 3:20 PM, Ken Ferry wrote:
> On Thu, May 27, 2010 at 8:40 PM, Graham Cox <email@hidden> wrote:
> 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;
>
>
> 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
_______________________________________________
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