// $Revision: 1.6 $ // Copyright (c) 1994-1995 Taligent, Inc. All rights reserved. #ifndef TaligentSamples_PRINTINGSNIPPETS #include "PrintingSnippets.h" #endif #ifndef Taligent_PRINT #include #endif #ifndef Taligent_PICTURE #include #endif TaligentTypeExtensionMacro(TPrintingSnippets) TPrintingSnippets::TPrintingSnippets() : TSnippets() { SNIPPETINFO(PrintADrawableToAChannel); SNIPPETINFO(PrintAGraphicUsingAFolio); SNIPPETINFO(PrintRangeOfPages); SNIPPETINFO(PrintRotatedTwoUp); SNIPPETINFO(PrintPagesWithPageNumber); } TPrintingSnippets::~TPrintingSnippets() { } // This illustrates the basic components of printing. Typically, printing is handled by // higher level mechanisms. // 1) You can print any object that inherits from MDrawable. This snippet uses TEllipse. // 2) You need a printer. Typically, some outside mechanism hands it to you, but // for now you access the default printer by creating an instance of TPrinter using a // custom constructor. // 3) You need a print job. The print job has two parts, a printer part and a client // part. You create the printer part by calling CreateJobDescription on the printer, // and TPrintJobDescription will default the client part for you. // 4) A print channel is needed to scope the job. Typically you don't create these // yourself and rely on subclasses of MPrintable to create one for you, but one is used // here to illustrate what happens. A job is passed to the print channel via BeginJob, // PrintPage is called for each page to be printed, and finally EndJob is called. // Each call to PrintPage requires an MDrawable and a page description. The printer // can be used to provide a description appropriate to the default page it prints on. void TPrintingSnippets::PrintADrawableToAChannel() { TEllipse drawable(TGPoint(100, 200), 100, 200); TPrinter printer(TPrinter::kSystemPrinter); TPrintJobDescription job(printer.CreateJobDescription()); TDeleterFor pageDescription = printer.CreateDefaultPageDescription(); TPrintChannel channel; channel.BeginJob(job); channel.PrintPage(drawable, *pageDescription); channel.EndJob(); } // Printing multiple pages is typically handled by using subclasses of MPrintable. MPrintable // defines a Print function that internally creates the TPrintChannel and uses it to print. // Common subclasses, such as MPageFolio, iterate over their pages when printing. // This example uses TPaginatedPageFolio to tile a large source page onto separate destination // pages. Note that the source page derives from MGraphic instead of simply MDrawable // since the printing framework needs to know the size of the source page. This example // queries the printer page size to create a source page that will tile onto exactly four // destination pages. This source page and the page description are passed to the folio. // When the folio is asked to print, it tiles the source page onto equal-sized destination // pages that match the page description. // Note: The source page is a picture containing three graphics each drawn with a thick inset // pen. The sizes and locations of the graphics are chosen so that the resulting picture has // its origin at 0, 0 and its extent equal to twice the extent of the printableRect. // Page origins must be 0, 0. void TPrintingSnippets::PrintAGraphicUsingAFolio() { TPrinter printer(TPrinter::kSystemPrinter); TDeleterFor pageDescription = printer.CreateDefaultPageDescription(); TGRect printableRect = pageDescription->GetPrintableRect(); TGRect sourcePageRect = TGRect(TGPoint::kOrigin, printableRect.GetSize() * 2); TPicture sourcePage; TFrameBundle bundle(TRGBColor(0, 0, 0), 5, TPen::kInsetFrame); TLinkedBundlePort port(sourcePage.GetGrafPort(), &bundle); port.Draw(sourcePageRect); port.Draw(TGEllipse(sourcePageRect)); port.Draw(TGRect(20, 20, 100, 100)); TPaginatedPageFolio folio(*pageDescription, &sourcePage); TPrintJobDescription job(printer.CreateJobDescription()); folio.Print(job); } // To print a range of pages, have the folio create a page iterator with a range, and print that. // This example also changes the scan order to print the leftmost column from top to bottom, // then the column to its right, and so on. The result is that only the leftmost column of pages // is printed. void TPrintingSnippets::PrintRangeOfPages() { TPrinter printer(TPrinter::kSystemPrinter); TDeleterFor pageDescription = printer.CreateDefaultPageDescription(); TGRect printableRect = pageDescription->GetPrintableRect(); TGRect sourcePageRect = TGRect(TGPoint::kOrigin, printableRect.GetSize() * 2); TPicture sourcePage; TFrameBundle bundle(TRGBColor(0, 0, 0), 5, TPen::kInsetFrame); TLinkedBundlePort port(sourcePage.GetGrafPort(), &bundle); port.Draw(sourcePageRect); port.Draw(TGEllipse(sourcePageRect)); port.Draw(TGRect(20, 20, 100, 100)); TPaginatedPageFolio folio(*pageDescription, &sourcePage); TStandardPageRange pageRange(0, folio.GetGridSize().fY - 1); TDeleterFor pages = (TPaginatedPageIterator*)folio.CreatePageIterator(&pageRange); pages->AdoptScanOrder(new TScanOrderDownRight); TPrintJobDescription job(printer.CreateJobDescription()); pages->Print(job); } // TwoUp printing is a form of composition. void TPrintingSnippets::PrintRotatedTwoUp() { TPrinter printer(TPrinter::kSystemPrinter); TDeleterFor pageDescription = printer.CreateDefaultPageDescription(); TGRect printableRect = pageDescription->GetPrintableRect(); TGRect sourcePageRect = TGRect(TGPoint::kOrigin, printableRect.GetSize() * 2); TPicture sourcePage; TFrameBundle bundle(TRGBColor(0, 0, 0), 5, TPen::kInsetFrame); TLinkedBundlePort port(sourcePage.GetGrafPort(), &bundle); port.Draw(sourcePageRect); port.Draw(TGEllipse(sourcePageRect)); port.Draw(TGRect(20, 20, 100, 100)); TPaginatedPageFolio folio(*pageDescription, &sourcePage); TDeleterFor sourcePages = folio.CreatePageIterator(); TStandardPageDescription masterPage(*pageDescription.GetObject()); TNUpCompositor twoUp(TULongPair(1, 2), masterPage); TDeleterFor pages = twoUp.CreatePageIterator(sourcePages); TPrintJobDescription job(printer.CreateJobDescription()); pages->Print(job); } #ifndef TaligentSamples_SAMPLEPAGENUMBERCOMPOSITOR #include "SamplePageNumberCompositor.h" #endif // Embellishing each page with a page number is also a form of composition. // CommonPoint does not provide such a compositor, so this uses the sample compositor // defined in the files SamplePageNumberCompositor.[hC] in this directory. // Note that in order to support fonts with more than 256 characters, a copy of the // font information is currently inserted into the postscript output once per page. // This makes the postscript output from this sample much larger than you might // expect. void TPrintingSnippets::PrintPagesWithPageNumber() { TPrinter printer(TPrinter::kSystemPrinter); TDeleterFor pageDescription = printer.CreateDefaultPageDescription(); TGRect printableRect = pageDescription->GetPrintableRect(); TGRect sourcePageRect = TGRect(TGPoint::kOrigin, printableRect.GetSize() * 2); TPicture sourcePage; TFrameBundle bundle(TRGBColor(0, 0, 0), 5, TPen::kInsetFrame); TLinkedBundlePort port(sourcePage.GetGrafPort(), &bundle); port.Draw(sourcePageRect); port.Draw(TGEllipse(sourcePageRect)); port.Draw(TGRect(20, 20, 100, 100)); TPaginatedPageFolio folio(*pageDescription, &sourcePage); TDeleterFor folioPages = folio.CreatePageIterator(); TSamplePageNumberCompositor numberedPages(folioPages, TSamplePageNumberCompositor::kTopCenter); TPrintJobDescription job(printer.CreateJobDescription()); numberedPages.Print(job); }