Re: Generating PDF images (+followup question)
Re: Generating PDF images (+followup question)
- Subject: Re: Generating PDF images (+followup question)
- From: "Stephen J. Butler" <email@hidden>
- Date: Fri, 28 May 2010 02:11:32 -0500
On Fri, May 28, 2010 at 1:36 AM, Graham Cox <email@hidden> wrote:
> I understand that contexts can (still) be flipped.
>
> Let's break this down a bit, since I'm fighting two problems simultaneously here. First, PDF File generation.
>
> Using the code I quoted, I generate PDF data in a flipped context. The flipped context matches the coordinate system that drawn objects expect. As such, objects and text are all positioned in the correct relative locations.
>
> If I write the PDF data immediately to a file, no NSImage or rep involved, result is an upside down PDF file when it's opened in Preview.
>
> If I generate the PDF data in a non-flipped context, the PDF File is still upside down, with the added problem that text glyphs are inverted individually.
>
> So at no point have I done anything with this image like draw it anywhere. How can it even be possible for PDF data in a file to be inverted? The problem appears to be with the PDF generation, so what's wrong there?
My understanding of this isn't the best, but here's how I see it:
The idea of "flipped/not-flipped" is a Cocoa idea. But CGContexts
aren't Cocoa and they don't have the notion of being flipped or not.
That's exactly why the +[NSGraphicsContext
graphicsContextWithGraphicsPort:flipped:] call has to ask if the CG
context it is getting is flipped or not! If the CG context knew,
NSGraphicsContext could just ask it.
So, your view is flipped and all your drawing operations assume a
flipped context. How do we get there for PDFs? Simple... we apply a
Current Transformation Matrix (CTM) that flips the CG context!
Something like this should work:
// Move the origin to the top of the context
CGContextTranslateCTM( pdfContext, 0.0, size.height );
// Now "flip" the y-axis so that it increases downwards
CGContextScaleCTM( pdfContext, 1.0, -1.0 );
And now you would pass flipped:YES to +[NSGraphicsContext
graphicsContextWithGraphicsPort:flipped:] because we just flipped it!
Basically, you were almost there. You got a perfect PDF file, just
upside down. Another way to look at all of this is "what CTM do I need
to apply to get everything right side up?" The answer is, flip the
y-axis over y=0 (the x-axis) and add the height.
_______________________________________________
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