Re: Printing image represented by multiple tiles using Cocoa
Re: Printing image represented by multiple tiles using Cocoa
- Subject: Re: Printing image represented by multiple tiles using Cocoa
- From: Ken Thomases <email@hidden>
- Date: Sat, 05 Nov 2011 00:32:07 -0500
On Oct 23, 2011, at 6:26 AM, Rahul Kesharwani wrote:
> I have a application that prints a image of a page of document on paper. The image is usually tiled and is provided as a set of 4 tiles representing the entire image. Till now this is being done using Carbon printing APIs .
The APIs you list below are not Carbon. They are C APIs rather than Objective-C, but not all C APIs are Carbon. Both Carbon and Cocoa are windowing and event frameworks built on top of other frameworks, some of which have a C interface and others of which have Objective-C interface. Core Printing and Core Graphics, the two frameworks you're using, are below both Carbon and Cocoa.
My point is: there may be no need to replace your existing code. If you're trying to move away from Carbon, well, already done!
> The set of functions calls and their order for printing a document is :
>
> 1. PMSessionBeginCGDocumentNoDialog(); //Called at he beginning of a document
>
> 2. Following calls at the beginning of each page
>
> PMSessionBeginPageNoDialog();
>
> //Get the current graphics context for drawing the page
> PMSessionGetCGGraphicsContex(printSession , &mPrintContext);
>
> //Some Calculations based on paper rect and imageable area
>
> // move the origin of CG from that of paper device to imageable area based on above calculation
> CGContextTranslateCTM();
>
> 3. While there are more tiles
> //Copy image tile(img) to target area(tileRect) on the print device.
> CGContextDrawImage(mPrintContext, tileRect, img);
>
> 4. PMSessionEndPageNoDialog(); //At end of each page
>
> 5. Repeat steps 2-4 for other pages.
>
> 6. PMSessionEndDocumentNoDialog() //Called at the end of document
>
>
> I need to implement the above steps using Cocoa APIs. Any suggestions how to implement this entire workflow using Cocoa. I have my NSPrintInfo instance set up. I know I have to create an instance of NSPrintOperation and initialize it with a NSView representing the page I want to print. I am stuck at step 3, i.e drawing 4 tiles of the image to create a single image representing a page and then create a NSView out of it.
You don't create an image and then "create a NSView out of it". An NSView is an active thing. It draws itself. You create a subclass of NSView and in its -drawRect: method, you do the calculations, transforms, and drawing of the tiles. (No consolidated image necessary.) The one view represents the content of the entire document, not just a page. Within the -drawRect: method, you obtain the tiles to draw in the same way as you would in your existing code.
Since you want to print this document across multiple pages, you'll presumably want to implement the pagination methods, too. See Printing Programming Topics for Cocoa: Pagination <https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Printing/Concepts/pagination.html> and Providing a Custom Pagination Scheme <https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Printing/Tasks/PaginatingViews.html>.
If it helps you to think about it, Cocoa will do step 1 and the first part of step 2 for you. Prior to calling your view's -drawRect: method, it will have begun a page and set up the thread's active graphics context for that page. Your -drawRect: method does the latter half of step 2 (the computations and, optionally, tweaking the configuration of the graphics context) and step 3 (the actual drawing of the document content). Cocoa then does steps 4 and 5 (the looping back to step 2 for the subsequent pages), then step 6.
Conceptually, your -drawRect: method draws the entirety of the view, which is the entirety of the document, each time it is invoked, and the graphics context will have been translated and clipped so that only one page worth of content is actually printed each time. To be more efficient, though, you'd only draw those tiles corresponding to the passed-in rect, which would correspond to a single page due to your pagination scheme.
Regards,
Ken
_______________________________________________
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