1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <officecfg/Office/Common.hxx>
22 #include <vcl/print.hxx>
23 #include <sal/macros.h>
25 #include <osx/salinst.h>
26 #include <osx/salprn.h>
27 #include <osx/printview.h>
28 #include <quartz/salgdi.h>
29 #include <osx/saldata.hxx>
30 #include <quartz/utils.h>
33 #include <salptype.hxx>
35 #include <com/sun/star/beans/PropertyValue.hpp>
36 #include <com/sun/star/awt/Size.hpp>
37 #include <com/sun/star/uno/Sequence.hxx>
43 using namespace com::sun::star
;
44 using namespace com::sun::star::beans
;
46 AquaSalInfoPrinter::AquaSalInfoPrinter( const SalPrinterQueueInfo
& i_rQueue
) :
47 mpGraphics( nullptr ),
52 mePageOrientation( Orientation::Portrait
),
53 mnStartPageOffsetX( 0 ),
54 mnStartPageOffsetY( 0 ),
55 mnCurPageRangeStart( 0 ),
56 mnCurPageRangeCount( 0 )
58 NSString
* pStr
= CreateNSString( i_rQueue
.maPrinterName
);
59 mpPrinter
= [NSPrinter printerWithName
: pStr
];
62 NSPrintInfo
* pShared
= [NSPrintInfo sharedPrintInfo
];
65 mpPrintInfo
= [pShared copy
];
66 [mpPrintInfo setPrinter
: mpPrinter
];
67 mePageOrientation
= ([mpPrintInfo orientation
] == NSPaperOrientationLandscape
) ? Orientation::Landscape
: Orientation::Portrait
;
68 [mpPrintInfo setOrientation
: NSPaperOrientationPortrait
];
71 mpGraphics
= new AquaSalGraphics();
73 const int nWidth
= 100, nHeight
= 100;
74 mpContextMemory
.reset(static_cast<sal_uInt8
*>(rtl_allocateMemory(nWidth
* 4 * nHeight
)),
79 mrContext
= CGBitmapContextCreate(mpContextMemory
.get(),
80 nWidth
, nHeight
, 8, nWidth
* 4,
81 GetSalData()->mxRGBSpace
, kCGImageAlphaNoneSkipFirst
);
83 SetupPrinterGraphics( mrContext
);
87 AquaSalInfoPrinter::~AquaSalInfoPrinter()
91 [mpPrintInfo release
];
93 CFRelease( mrContext
);
96 void AquaSalInfoPrinter::SetupPrinterGraphics( CGContextRef i_rContext
) const
102 // FIXME: get printer resolution
103 long nDPIX
= 720, nDPIY
= 720;
104 NSSize aPaperSize
= [mpPrintInfo paperSize
];
106 NSRect aImageRect
= [mpPrintInfo imageablePageBounds
];
107 if( mePageOrientation
== Orientation::Portrait
)
109 // move mirrored CTM back into paper
110 double dX
= 0, dY
= aPaperSize
.height
;
111 // move CTM to reflect imageable area
112 dX
+= aImageRect
.origin
.x
;
113 dY
-= aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
;
114 CGContextTranslateCTM( i_rContext
, dX
+ mnStartPageOffsetX
, dY
- mnStartPageOffsetY
);
115 // scale to be top/down and reflect our "virtual" DPI
116 CGContextScaleCTM( i_rContext
, 72.0/double(nDPIX
), -(72.0/double(nDPIY
)) );
120 // move CTM to reflect imageable area
121 double dX
= aImageRect
.origin
.x
, dY
= aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
;
122 CGContextTranslateCTM( i_rContext
, -dX
, -dY
);
124 CGContextRotateCTM( i_rContext
, M_PI
/2 );
125 // move turned CTM back into paper
126 dX
= aPaperSize
.height
;
127 dY
= -aPaperSize
.width
;
128 CGContextTranslateCTM( i_rContext
, dX
+ mnStartPageOffsetY
, dY
- mnStartPageOffsetX
);
129 // scale to be top/down and reflect our "virtual" DPI
130 CGContextScaleCTM( i_rContext
, -(72.0/double(nDPIY
)), (72.0/double(nDPIX
)) );
132 mpGraphics
->SetPrinterGraphics( i_rContext
, nDPIX
, nDPIY
);
135 OSL_FAIL( "no print info in SetupPrinterGraphics" );
139 SalGraphics
* AquaSalInfoPrinter::AcquireGraphics()
141 SalGraphics
* pGraphics
= mbGraphics
? nullptr : mpGraphics
;
146 void AquaSalInfoPrinter::ReleaseGraphics( SalGraphics
* )
151 bool AquaSalInfoPrinter::Setup( weld::Window
*, ImplJobSetup
* )
156 bool AquaSalInfoPrinter::SetPrinterData( ImplJobSetup
* io_pSetupData
)
158 // FIXME: implement driver data
159 if( io_pSetupData
&& io_pSetupData
->GetDriverData() )
160 return SetData( JobSetFlags::ALL
, io_pSetupData
);
162 bool bSuccess
= true;
165 io_pSetupData
->SetSystem( JOBSETUP_SYSTEM_MAC
);
170 NSSize aPaperSize
= [mpPrintInfo paperSize
];
171 double width
= aPaperSize
.width
, height
= aPaperSize
.height
;
173 PaperInfo
aInfo( PtTo10Mu( width
), PtTo10Mu( height
) );
175 io_pSetupData
->SetPaperFormat( aInfo
.getPaper() );
176 if( io_pSetupData
->GetPaperFormat() == PAPER_USER
)
178 io_pSetupData
->SetPaperWidth( PtTo10Mu( width
) );
179 io_pSetupData
->SetPaperHeight( PtTo10Mu( height
) );
183 io_pSetupData
->SetPaperWidth( 0 );
184 io_pSetupData
->SetPaperHeight( 0 );
188 io_pSetupData
->SetOrientation( mePageOrientation
);
190 io_pSetupData
->SetPaperBin( 0 );
191 io_pSetupData
->SetDriverData( static_cast<sal_uInt8
*>(rtl_allocateMemory( 4 )) );
192 io_pSetupData
->SetDriverDataLen( 4 );
200 void AquaSalInfoPrinter::setPaperSize( long i_nWidth
, long i_nHeight
, Orientation i_eSetOrientation
)
203 Orientation ePaperOrientation
= Orientation::Portrait
;
204 const PaperInfo
* pPaper
= matchPaper( i_nWidth
, i_nHeight
, ePaperOrientation
);
208 NSString
* pPaperName
= [CreateNSString( OStringToOUString(PaperInfo::toPSName(pPaper
->getPaper()), RTL_TEXTENCODING_ASCII_US
) ) autorelease
];
209 [mpPrintInfo setPaperName
: pPaperName
];
211 else if( i_nWidth
> 0 && i_nHeight
> 0 )
213 NSSize aPaperSize
= { static_cast<CGFloat
>(TenMuToPt(i_nWidth
)), static_cast<CGFloat
>(TenMuToPt(i_nHeight
)) };
214 [mpPrintInfo setPaperSize
: aPaperSize
];
216 // this seems counterintuitive
217 mePageOrientation
= i_eSetOrientation
;
220 bool AquaSalInfoPrinter::SetData( JobSetFlags i_nFlags
, ImplJobSetup
* io_pSetupData
)
222 if( ! io_pSetupData
|| io_pSetupData
->GetSystem() != JOBSETUP_SYSTEM_MAC
)
227 if( i_nFlags
& JobSetFlags::ORIENTATION
)
228 mePageOrientation
= io_pSetupData
->GetOrientation();
230 if( i_nFlags
& JobSetFlags::PAPERSIZE
)
233 long width
= 21000, height
= 29700;
234 if( io_pSetupData
->GetPaperFormat() == PAPER_USER
)
236 // #i101108# sanity check
237 if( io_pSetupData
->GetPaperWidth() && io_pSetupData
->GetPaperHeight() )
239 width
= io_pSetupData
->GetPaperWidth();
240 height
= io_pSetupData
->GetPaperHeight();
245 PaperInfo
aInfo( io_pSetupData
->GetPaperFormat() );
246 width
= aInfo
.getWidth();
247 height
= aInfo
.getHeight();
250 setPaperSize( width
, height
, mePageOrientation
);
254 return mpPrintInfo
!= nil
;
257 sal_uInt16
AquaSalInfoPrinter::GetPaperBinCount( const ImplJobSetup
* )
262 OUString
AquaSalInfoPrinter::GetPaperBinName( const ImplJobSetup
*, sal_uInt16
)
267 sal_uInt32
AquaSalInfoPrinter::GetCapabilities( const ImplJobSetup
*, PrinterCapType i_nType
)
271 case PrinterCapType::SupportDialog
:
273 case PrinterCapType::Copies
:
275 case PrinterCapType::CollateCopies
:
277 case PrinterCapType::SetOrientation
:
279 case PrinterCapType::SetPaperSize
:
281 case PrinterCapType::SetPaper
:
283 case PrinterCapType::ExternalDialog
:
284 return officecfg::Office::Common::Misc::UseSystemPrintDialog::get()
286 case PrinterCapType::PDF
:
288 case PrinterCapType::UsePullModel
:
295 void AquaSalInfoPrinter::GetPageInfo( const ImplJobSetup
*,
296 long& o_rOutWidth
, long& o_rOutHeight
,
302 sal_Int32 nDPIX
= 72, nDPIY
= 72;
303 mpGraphics
->GetResolution( nDPIX
, nDPIY
);
304 const double fXScaling
= static_cast<double>(nDPIX
)/72.0,
305 fYScaling
= static_cast<double>(nDPIY
)/72.0;
307 NSSize aPaperSize
= [mpPrintInfo paperSize
];
308 rPaperSize
.setWidth( static_cast<long>( double(aPaperSize
.width
) * fXScaling
) );
309 rPaperSize
.setHeight( static_cast<long>( double(aPaperSize
.height
) * fYScaling
) );
311 NSRect aImageRect
= [mpPrintInfo imageablePageBounds
];
312 rPageOffset
.setX( static_cast<long>( aImageRect
.origin
.x
* fXScaling
) );
313 rPageOffset
.setY( static_cast<long>( (aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
) * fYScaling
) );
314 o_rOutWidth
= static_cast<long>( aImageRect
.size
.width
* fXScaling
);
315 o_rOutHeight
= static_cast<long>( aImageRect
.size
.height
* fYScaling
);
317 if( mePageOrientation
== Orientation::Landscape
)
319 std::swap( o_rOutWidth
, o_rOutHeight
);
320 // swap width and height
321 long n
= rPaperSize
.Width();
322 rPaperSize
.setWidth(rPaperSize
.Height());
323 rPaperSize
.setHeight(n
);
324 // swap offset x and y
326 rPageOffset
.setX(rPageOffset
.Y());
332 static Size
getPageSize( vcl::PrinterController
const & i_rController
, sal_Int32 i_nPage
)
335 uno::Sequence
< PropertyValue
> aPageParms( i_rController
.getPageParameters( i_nPage
) );
336 for( sal_Int32 nProperty
= 0, nPropertyCount
= aPageParms
.getLength(); nProperty
< nPropertyCount
; ++nProperty
)
338 if ( aPageParms
[ nProperty
].Name
== "PageSize" )
341 aPageParms
[ nProperty
].Value
>>= aSize
;
342 aPageSize
.setWidth( aSize
.Width
);
343 aPageSize
.setHeight( aSize
.Height
);
350 bool AquaSalInfoPrinter::StartJob( const OUString
* i_pFileName
,
351 const OUString
& i_rJobName
,
352 const OUString
& /*i_rAppName*/,
353 ImplJobSetup
* i_pSetupData
,
354 vcl::PrinterController
& i_rController
360 bool bSuccess
= false;
361 bool bWasAborted
= false;
362 AquaSalInstance
* pInst
= GetSalData()->mpInstance
;
363 PrintAccessoryViewState aAccViewState
;
364 sal_Int32 nAllPages
= 0;
367 i_rController
.setLastPage( false );
371 SetData( JobSetFlags::ALL
, i_pSetupData
);
373 // do we want a progress panel ?
374 bool bShowProgressPanel
= true;
375 beans::PropertyValue
* pMonitor
= i_rController
.getValue( OUString( "MonitorVisible" ) );
377 pMonitor
->Value
>>= bShowProgressPanel
;
378 if( ! i_rController
.isShowDialogs() )
379 bShowProgressPanel
= false;
381 // possibly create one job for collated output
382 bool bSinglePrintJobs
= false;
383 beans::PropertyValue
* pSingleValue
= i_rController
.getValue( OUString( "PrintCollateAsSingleJobs" ) );
386 pSingleValue
->Value
>>= bSinglePrintJobs
;
389 // FIXME: jobStarted() should be done after the print dialog has ended (if there is one)
390 // how do I know when that might be ?
391 i_rController
.jobStarted();
393 int nCopies
= i_rController
.getPrinter()->GetCopyCount();
395 if( bSinglePrintJobs
)
401 for( int nCurJob
= 0; nCurJob
< nJobs
; nCurJob
++ )
403 aAccViewState
.bNeedRestart
= true;
406 if( aAccViewState
.bNeedRestart
)
408 mnCurPageRangeStart
= 0;
409 mnCurPageRangeCount
= 0;
410 nAllPages
= i_rController
.getFilteredPageCount();
413 aAccViewState
.bNeedRestart
= false;
415 Size
aCurSize( 21000, 29700 );
418 mnCurPageRangeCount
= 1;
419 aCurSize
= getPageSize( i_rController
, mnCurPageRangeStart
);
420 Size
aNextSize( aCurSize
);
422 // print pages up to a different size
423 while( mnCurPageRangeCount
+ mnCurPageRangeStart
< nAllPages
)
425 aNextSize
= getPageSize( i_rController
, mnCurPageRangeStart
+ mnCurPageRangeCount
);
426 if( aCurSize
== aNextSize
// same page size
428 (aCurSize
.Width() == aNextSize
.Height() && aCurSize
.Height() == aNextSize
.Width()) // same size, but different orientation
431 mnCurPageRangeCount
++;
438 mnCurPageRangeCount
= 0;
440 // now for the current run
441 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
442 // setup the paper size and orientation
443 // do this on our associated Printer object, since that is
444 // out interface to the applications which occasionally rely on the paper
445 // information (e.g. brochure printing scales to the found paper size)
446 // also SetPaperSizeUser has the advantage that we can share a
447 // platform independent paper matching algorithm
448 VclPtr
<Printer
> pPrinter( i_rController
.getPrinter() );
449 pPrinter
->SetMapMode( MapMode( MapUnit::Map100thMM
) );
450 pPrinter
->SetPaperSizeUser( aCurSize
, true );
453 NSView
* pPrintView
= [[AquaPrintView alloc
] initWithController
: &i_rController withInfoPrinter
: this];
455 NSMutableDictionary
* pPrintDict
= [mpPrintInfo dictionary
];
460 [mpPrintInfo setJobDisposition
: NSPrintSaveJob
];
461 NSString
* pPath
= CreateNSString( *i_pFileName
);
462 [pPrintDict setObject
:[NSURL fileURLWithPath
:pPath
] forKey
:NSPrintJobSavingURL
];
466 [pPrintDict setObject
: [[NSNumber numberWithInt
: nCopies
] autorelease
] forKey
: NSPrintCopies
];
468 [pPrintDict setObject
: [[NSNumber numberWithBool
: pPrinter
->IsCollateCopy()] autorelease
] forKey
: NSPrintMustCollate
];
469 [pPrintDict setObject
: [[NSNumber numberWithBool
: YES
] autorelease
] forKey
: NSPrintDetailedErrorReporting
];
470 [pPrintDict setObject
: [[NSNumber numberWithInt
: 1] autorelease
] forKey
: NSPrintFirstPage
];
471 // #i103253# weird: for some reason, autoreleasing the value below like the others above
472 // leads do a double free malloc error. Why this value should behave differently from all the others
474 [pPrintDict setObject
: [NSNumber numberWithInt
: mnCurPageRangeCount
] forKey
: NSPrintLastPage
];
476 // create print operation
477 NSPrintOperation
* pPrintOperation
= [NSPrintOperation printOperationWithView
: pPrintView printInfo
: mpPrintInfo
];
479 if( pPrintOperation
)
481 NSObject
* pReleaseAfterUse
= nil
;
482 bool bShowPanel
= !i_rController
.isDirectPrint()
483 && (officecfg::Office::Common::Misc::UseSystemPrintDialog::
485 && i_rController
.isShowDialogs();
486 [pPrintOperation setShowsPrintPanel
: bShowPanel
? YES
: NO
];
487 [pPrintOperation setShowsProgressPanel
: bShowProgressPanel
? YES
: NO
];
489 // set job title (since MacOSX 10.5)
490 if( [pPrintOperation respondsToSelector
: @
selector(setJobTitle
:)] )
491 [pPrintOperation performSelector
: @
selector(setJobTitle
:) withObject
: [CreateNSString( i_rJobName
) autorelease
]];
493 if( bShowPanel
&& mnCurPageRangeStart
== 0 && nCurJob
== 0) // only the first range of pages (in the first job) gets the accessory view
494 pReleaseAfterUse
= [AquaPrintAccessoryView setupPrinterPanel
: pPrintOperation withController
: &i_rController withState
: &aAccViewState
];
498 pInst
->startedPrintJob();
499 BOOL wasSuccessful
= [pPrintOperation runOperation
];
500 pInst
->endedPrintJob();
501 bSuccess
= wasSuccessful
;
502 bWasAborted
= [[[pPrintOperation printInfo
] jobDisposition
] compare
: NSPrintCancelJob
] == NSOrderedSame
;
504 if( pReleaseAfterUse
)
505 [pReleaseAfterUse release
];
508 mnCurPageRangeStart
+= mnCurPageRangeCount
;
509 mnCurPageRangeCount
= 1;
510 } while( aAccViewState
.bNeedRestart
|| mnCurPageRangeStart
+ mnCurPageRangeCount
< nAllPages
);
513 // inform application that it can release its data
514 // this is awkward, but the XRenderable interface has no method for this,
515 // so we need to call XRenderadble::render one last time with IsLastPage = true
516 i_rController
.setLastPage( true );
517 GDIMetaFile aPageFile
;
519 SetupPrinterGraphics( mrContext
);
520 i_rController
.getFilteredPageFile( 0, aPageFile
);
522 i_rController
.setJobState( bWasAborted
523 ? view::PrintableState_JOB_ABORTED
524 : view::PrintableState_JOB_SPOOLED
);
526 mnCurPageRangeStart
= mnCurPageRangeCount
= 0;
531 bool AquaSalInfoPrinter::EndJob()
533 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
538 bool AquaSalInfoPrinter::AbortJob()
542 // FIXME: implementation
546 SalGraphics
* AquaSalInfoPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
548 if( i_bNewJobData
&& i_pSetupData
)
549 SetPrinterData( i_pSetupData
);
551 CGContextRef rContext
= static_cast<CGContextRef
>([[NSGraphicsContext currentContext
] graphicsPort
]);
553 SetupPrinterGraphics( rContext
);
558 bool AquaSalInfoPrinter::EndPage()
560 mpGraphics
->InvalidateContext();
564 AquaSalPrinter::AquaSalPrinter( AquaSalInfoPrinter
* i_pInfoPrinter
) :
565 mpInfoPrinter( i_pInfoPrinter
)
569 AquaSalPrinter::~AquaSalPrinter()
573 bool AquaSalPrinter::StartJob( const OUString
* i_pFileName
,
574 const OUString
& i_rJobName
,
575 const OUString
& i_rAppName
,
576 ImplJobSetup
* i_pSetupData
,
577 vcl::PrinterController
& i_rController
)
579 return mpInfoPrinter
->StartJob( i_pFileName
, i_rJobName
, i_rAppName
, i_pSetupData
, i_rController
);
582 bool AquaSalPrinter::StartJob( const OUString
* /*i_pFileName*/,
583 const OUString
& /*i_rJobName*/,
584 const OUString
& /*i_rAppName*/,
585 sal_uInt32
/*i_nCopies*/,
590 OSL_FAIL( "should never be called" );
594 bool AquaSalPrinter::EndJob()
596 return mpInfoPrinter
->EndJob();
599 SalGraphics
* AquaSalPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
601 return mpInfoPrinter
->StartPage( i_pSetupData
, i_bNewJobData
);
604 void AquaSalPrinter::EndPage()
606 mpInfoPrinter
->EndPage();
609 void AquaSalInfoPrinter::InitPaperFormats( const ImplJobSetup
* )
611 m_aPaperFormats
.clear();
612 m_bPapersInit
= true;
616 SAL_WNODEPRECATED_DECLARATIONS_PUSH
617 //TODO: 10.9 statusForTable:, stringListForKey:inTable:
618 if( [mpPrinter statusForTable
: @
"PPD"] == NSPrinterTableOK
)
620 NSArray
* pPaperNames
= [mpPrinter stringListForKey
: @
"PageSize" inTable
: @
"PPD"];
623 unsigned int nPapers
= [pPaperNames count
];
624 for( unsigned int i
= 0; i
< nPapers
; i
++ )
626 NSString
* pPaper
= [pPaperNames objectAtIndex
: i
];
627 // first try to match the name
628 OString
aPaperName( [pPaper UTF8String
] );
629 Paper ePaper
= PaperInfo::fromPSName( aPaperName
);
630 if( ePaper
!= PAPER_USER
)
632 m_aPaperFormats
.push_back( PaperInfo( ePaper
) );
636 NSSize aPaperSize
= [mpPrinter pageSizeForPaper
: pPaper
];
637 if( aPaperSize
.width
> 0 && aPaperSize
.height
> 0 )
639 PaperInfo
aInfo( PtTo10Mu( aPaperSize
.width
),
640 PtTo10Mu( aPaperSize
.height
) );
641 if( aInfo
.getPaper() == PAPER_USER
)
643 m_aPaperFormats
.push_back( aInfo
);
649 SAL_WNODEPRECATED_DECLARATIONS_POP
653 const PaperInfo
* AquaSalInfoPrinter::matchPaper( long i_nWidth
, long i_nHeight
, Orientation
& o_rOrientation
) const
655 if( ! m_bPapersInit
)
656 const_cast<AquaSalInfoPrinter
*>(this)->InitPaperFormats( nullptr );
658 const PaperInfo
* pMatch
= nullptr;
659 o_rOrientation
= Orientation::Portrait
;
660 for( int n
= 0; n
< 2 ; n
++ )
662 for( size_t i
= 0; i
< m_aPaperFormats
.size(); i
++ )
664 if( std::abs( m_aPaperFormats
[i
].getWidth() - i_nWidth
) < 50 &&
665 std::abs( m_aPaperFormats
[i
].getHeight() - i_nHeight
) < 50 )
667 pMatch
= &m_aPaperFormats
[i
];
671 o_rOrientation
= Orientation::Landscape
;
672 std::swap( i_nWidth
, i_nHeight
);
677 int AquaSalInfoPrinter::GetLandscapeAngle( const ImplJobSetup
* )
682 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */