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/gdimtf.hxx>
23 #include <vcl/print.hxx>
24 #include <sal/macros.h>
25 #include <osl/diagnose.h>
26 #include <tools/long.hxx>
28 #include <osx/salinst.h>
29 #include <osx/salprn.h>
30 #include <osx/printview.h>
31 #include <quartz/salgdi.h>
32 #include <osx/saldata.hxx>
33 #include <quartz/utils.h>
36 #include <salptype.hxx>
38 #include <com/sun/star/beans/PropertyValue.hpp>
39 #include <com/sun/star/awt/Size.hpp>
40 #include <com/sun/star/uno/Sequence.hxx>
46 using namespace com::sun::star
;
47 using namespace com::sun::star::beans
;
49 AquaSalInfoPrinter::AquaSalInfoPrinter( const SalPrinterQueueInfo
& i_rQueue
) :
50 mpGraphics( nullptr ),
55 mePageOrientation( Orientation::Portrait
),
56 mnStartPageOffsetX( 0 ),
57 mnStartPageOffsetY( 0 ),
58 mnCurPageRangeStart( 0 ),
59 mnCurPageRangeCount( 0 )
61 NSString
* pStr
= CreateNSString( i_rQueue
.maPrinterName
);
62 mpPrinter
= [NSPrinter printerWithName
: pStr
];
65 NSPrintInfo
* pShared
= [NSPrintInfo sharedPrintInfo
];
68 mpPrintInfo
= [pShared copy
];
69 [mpPrintInfo setPrinter
: mpPrinter
];
70 mePageOrientation
= ([mpPrintInfo orientation
] == NSPaperOrientationLandscape
) ? Orientation::Landscape
: Orientation::Portrait
;
71 [mpPrintInfo setOrientation
: NSPaperOrientationPortrait
];
74 mpGraphics
= new AquaSalGraphics();
76 const int nWidth
= 100, nHeight
= 100;
77 mpContextMemory
.reset(new (std::nothrow
) sal_uInt8
[nWidth
* 4 * nHeight
]);
81 mrContext
= CGBitmapContextCreate(mpContextMemory
.get(),
82 nWidth
, nHeight
, 8, nWidth
* 4,
83 GetSalData()->mxRGBSpace
, kCGImageAlphaNoneSkipFirst
);
85 SetupPrinterGraphics( mrContext
);
89 AquaSalInfoPrinter::~AquaSalInfoPrinter()
93 [mpPrintInfo release
];
95 CFRelease( mrContext
);
98 void AquaSalInfoPrinter::SetupPrinterGraphics( CGContextRef i_rContext
) const
104 // FIXME: get printer resolution
105 sal_Int32 nDPIX
= 720, nDPIY
= 720;
106 NSSize aPaperSize
= [mpPrintInfo paperSize
];
108 NSRect aImageRect
= [mpPrintInfo imageablePageBounds
];
109 if( mePageOrientation
== Orientation::Portrait
)
111 // move mirrored CTM back into paper
112 double dX
= 0, dY
= aPaperSize
.height
;
113 // move CTM to reflect imageable area
114 dX
+= aImageRect
.origin
.x
;
115 dY
-= aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
;
116 CGContextTranslateCTM( i_rContext
, dX
+ mnStartPageOffsetX
, dY
- mnStartPageOffsetY
);
117 // scale to be top/down and reflect our "virtual" DPI
118 CGContextScaleCTM( i_rContext
, 72.0/double(nDPIX
), -(72.0/double(nDPIY
)) );
122 // move CTM to reflect imageable area
123 double dX
= aImageRect
.origin
.x
, dY
= aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
;
124 CGContextTranslateCTM( i_rContext
, -dX
, -dY
);
126 CGContextRotateCTM( i_rContext
, M_PI
/2 );
127 // move turned CTM back into paper
128 dX
= aPaperSize
.height
;
129 dY
= -aPaperSize
.width
;
130 CGContextTranslateCTM( i_rContext
, dX
+ mnStartPageOffsetY
, dY
- mnStartPageOffsetX
);
131 // scale to be top/down and reflect our "virtual" DPI
132 CGContextScaleCTM( i_rContext
, -(72.0/double(nDPIY
)), (72.0/double(nDPIX
)) );
134 mpGraphics
->SetPrinterGraphics( i_rContext
, nDPIX
, nDPIY
);
137 OSL_FAIL( "no print info in SetupPrinterGraphics" );
141 SalGraphics
* AquaSalInfoPrinter::AcquireGraphics()
143 SalGraphics
* pGraphics
= mbGraphics
? nullptr : mpGraphics
;
148 void AquaSalInfoPrinter::ReleaseGraphics( SalGraphics
* )
153 bool AquaSalInfoPrinter::Setup( weld::Window
*, ImplJobSetup
* )
158 bool AquaSalInfoPrinter::SetPrinterData( ImplJobSetup
* io_pSetupData
)
160 // FIXME: implement driver data
161 if( io_pSetupData
&& io_pSetupData
->GetDriverData() )
162 return SetData( JobSetFlags::ALL
, io_pSetupData
);
164 bool bSuccess
= true;
167 io_pSetupData
->SetSystem( JOBSETUP_SYSTEM_MAC
);
172 NSSize aPaperSize
= [mpPrintInfo paperSize
];
173 double width
= aPaperSize
.width
, height
= aPaperSize
.height
;
175 PaperInfo
aInfo( PtTo10Mu( width
), PtTo10Mu( height
) );
177 io_pSetupData
->SetPaperFormat( aInfo
.getPaper() );
178 if( io_pSetupData
->GetPaperFormat() == PAPER_USER
)
180 io_pSetupData
->SetPaperWidth( PtTo10Mu( width
) );
181 io_pSetupData
->SetPaperHeight( PtTo10Mu( height
) );
185 io_pSetupData
->SetPaperWidth( 0 );
186 io_pSetupData
->SetPaperHeight( 0 );
190 io_pSetupData
->SetOrientation( mePageOrientation
);
192 io_pSetupData
->SetPaperBin( 0 );
193 io_pSetupData
->SetDriverData( static_cast<sal_uInt8
*>(std::malloc( 4 )) );
194 io_pSetupData
->SetDriverDataLen( 4 );
202 void AquaSalInfoPrinter::setPaperSize( tools::Long i_nWidth
, tools::Long i_nHeight
, Orientation i_eSetOrientation
)
205 Orientation ePaperOrientation
= Orientation::Portrait
;
206 const PaperInfo
* pPaper
= matchPaper( i_nWidth
, i_nHeight
, ePaperOrientation
);
210 NSString
* pPaperName
= [CreateNSString( OStringToOUString(PaperInfo::toPSName(pPaper
->getPaper()), RTL_TEXTENCODING_ASCII_US
) ) autorelease
];
211 [mpPrintInfo setPaperName
: pPaperName
];
213 else if( i_nWidth
> 0 && i_nHeight
> 0 )
215 NSSize aPaperSize
= { static_cast<CGFloat
>(TenMuToPt(i_nWidth
)), static_cast<CGFloat
>(TenMuToPt(i_nHeight
)) };
216 [mpPrintInfo setPaperSize
: aPaperSize
];
218 // this seems counterintuitive
219 mePageOrientation
= i_eSetOrientation
;
222 bool AquaSalInfoPrinter::SetData( JobSetFlags i_nFlags
, ImplJobSetup
* io_pSetupData
)
224 if( ! io_pSetupData
|| io_pSetupData
->GetSystem() != JOBSETUP_SYSTEM_MAC
)
229 if( i_nFlags
& JobSetFlags::ORIENTATION
)
230 mePageOrientation
= io_pSetupData
->GetOrientation();
232 if( i_nFlags
& JobSetFlags::PAPERSIZE
)
235 tools::Long width
= 21000, height
= 29700;
236 if( io_pSetupData
->GetPaperFormat() == PAPER_USER
)
238 // #i101108# sanity check
239 if( io_pSetupData
->GetPaperWidth() && io_pSetupData
->GetPaperHeight() )
241 width
= io_pSetupData
->GetPaperWidth();
242 height
= io_pSetupData
->GetPaperHeight();
247 PaperInfo
aInfo( io_pSetupData
->GetPaperFormat() );
248 width
= aInfo
.getWidth();
249 height
= aInfo
.getHeight();
252 setPaperSize( width
, height
, mePageOrientation
);
256 return mpPrintInfo
!= nil
;
259 sal_uInt16
AquaSalInfoPrinter::GetPaperBinCount( const ImplJobSetup
* )
264 OUString
AquaSalInfoPrinter::GetPaperBinName( const ImplJobSetup
*, sal_uInt16
)
269 sal_uInt32
AquaSalInfoPrinter::GetCapabilities( const ImplJobSetup
*, PrinterCapType i_nType
)
273 case PrinterCapType::SupportDialog
:
275 case PrinterCapType::Copies
:
277 case PrinterCapType::CollateCopies
:
279 case PrinterCapType::SetOrientation
:
281 case PrinterCapType::SetPaperSize
:
283 case PrinterCapType::SetPaper
:
285 case PrinterCapType::ExternalDialog
:
286 return officecfg::Office::Common::Misc::UseSystemPrintDialog::get()
288 case PrinterCapType::PDF
:
290 case PrinterCapType::UsePullModel
:
297 void AquaSalInfoPrinter::GetPageInfo( const ImplJobSetup
*,
298 tools::Long
& o_rOutWidth
, tools::Long
& o_rOutHeight
,
304 sal_Int32 nDPIX
= 72, nDPIY
= 72;
305 mpGraphics
->GetResolution( nDPIX
, nDPIY
);
306 const double fXScaling
= static_cast<double>(nDPIX
)/72.0,
307 fYScaling
= static_cast<double>(nDPIY
)/72.0;
309 NSSize aPaperSize
= [mpPrintInfo paperSize
];
310 rPaperSize
.setWidth( static_cast<tools::Long
>( double(aPaperSize
.width
) * fXScaling
) );
311 rPaperSize
.setHeight( static_cast<tools::Long
>( double(aPaperSize
.height
) * fYScaling
) );
313 NSRect aImageRect
= [mpPrintInfo imageablePageBounds
];
314 rPageOffset
.setX( static_cast<tools::Long
>( aImageRect
.origin
.x
* fXScaling
) );
315 rPageOffset
.setY( static_cast<tools::Long
>( (aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
) * fYScaling
) );
316 o_rOutWidth
= static_cast<tools::Long
>( aImageRect
.size
.width
* fXScaling
);
317 o_rOutHeight
= static_cast<tools::Long
>( aImageRect
.size
.height
* fYScaling
);
319 if( mePageOrientation
== Orientation::Landscape
)
321 std::swap( o_rOutWidth
, o_rOutHeight
);
322 // swap width and height
323 tools::Long n
= rPaperSize
.Width();
324 rPaperSize
.setWidth(rPaperSize
.Height());
325 rPaperSize
.setHeight(n
);
326 // swap offset x and y
328 rPageOffset
.setX(rPageOffset
.Y());
334 static Size
getPageSize( vcl::PrinterController
const & i_rController
, sal_Int32 i_nPage
)
337 uno::Sequence
< PropertyValue
> const aPageParms( i_rController
.getPageParameters( i_nPage
) );
338 for( const PropertyValue
& pv
: aPageParms
)
340 if ( pv
.Name
== "PageSize" )
344 aPageSize
.setWidth( aSize
.Width
);
345 aPageSize
.setHeight( aSize
.Height
);
352 bool AquaSalInfoPrinter::StartJob( const OUString
* i_pFileName
,
353 const OUString
& i_rJobName
,
354 ImplJobSetup
* i_pSetupData
,
355 vcl::PrinterController
& i_rController
361 bool bSuccess
= false;
362 bool bWasAborted
= false;
363 AquaSalInstance
* pInst
= GetSalData()->mpInstance
;
364 PrintAccessoryViewState aAccViewState
;
365 sal_Int32 nAllPages
= 0;
368 i_rController
.setLastPage( false );
372 SetData( JobSetFlags::ALL
, i_pSetupData
);
374 // do we want a progress panel ?
375 bool bShowProgressPanel
= true;
376 beans::PropertyValue
* pMonitor
= i_rController
.getValue( OUString( "MonitorVisible" ) );
378 pMonitor
->Value
>>= bShowProgressPanel
;
379 if( ! i_rController
.isShowDialogs() )
380 bShowProgressPanel
= false;
382 // possibly create one job for collated output
383 bool bSinglePrintJobs
= i_rController
.getPrinter()->IsSinglePrintJobs();
385 // FIXME: jobStarted() should be done after the print dialog has ended (if there is one)
386 // how do I know when that might be ?
387 i_rController
.jobStarted();
389 int nCopies
= i_rController
.getPrinter()->GetCopyCount();
391 if( bSinglePrintJobs
)
397 for( int nCurJob
= 0; nCurJob
< nJobs
; nCurJob
++ )
399 aAccViewState
.bNeedRestart
= true;
402 if( aAccViewState
.bNeedRestart
)
404 mnCurPageRangeStart
= 0;
405 mnCurPageRangeCount
= 0;
406 nAllPages
= i_rController
.getFilteredPageCount();
409 aAccViewState
.bNeedRestart
= false;
411 Size
aCurSize( 21000, 29700 );
414 mnCurPageRangeCount
= 1;
415 aCurSize
= getPageSize( i_rController
, mnCurPageRangeStart
);
416 Size
aNextSize( aCurSize
);
418 // print pages up to a different size
419 while( mnCurPageRangeCount
+ mnCurPageRangeStart
< nAllPages
)
421 aNextSize
= getPageSize( i_rController
, mnCurPageRangeStart
+ mnCurPageRangeCount
);
422 if( aCurSize
== aNextSize
// same page size
424 (aCurSize
.Width() == aNextSize
.Height() && aCurSize
.Height() == aNextSize
.Width()) // same size, but different orientation
427 mnCurPageRangeCount
++;
434 mnCurPageRangeCount
= 0;
436 // now for the current run
437 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
438 // setup the paper size and orientation
439 // do this on our associated Printer object, since that is
440 // out interface to the applications which occasionally rely on the paper
441 // information (e.g. brochure printing scales to the found paper size)
442 // also SetPaperSizeUser has the advantage that we can share a
443 // platform independent paper matching algorithm
444 VclPtr
<Printer
> pPrinter( i_rController
.getPrinter() );
445 pPrinter
->SetMapMode( MapMode( MapUnit::Map100thMM
) );
446 pPrinter
->SetPaperSizeUser( aCurSize
);
449 NSView
* pPrintView
= [[AquaPrintView alloc
] initWithController
: &i_rController withInfoPrinter
: this];
451 NSMutableDictionary
* pPrintDict
= [mpPrintInfo dictionary
];
456 [mpPrintInfo setJobDisposition
: NSPrintSaveJob
];
457 NSString
* pPath
= CreateNSString( *i_pFileName
);
458 [pPrintDict setObject
:[NSURL fileURLWithPath
:pPath
] forKey
:NSPrintJobSavingURL
];
462 [pPrintDict setObject
: [[NSNumber numberWithInt
: nCopies
] autorelease
] forKey
: NSPrintCopies
];
464 [pPrintDict setObject
: [[NSNumber numberWithBool
: pPrinter
->IsCollateCopy()] autorelease
] forKey
: NSPrintMustCollate
];
465 [pPrintDict setObject
: [[NSNumber numberWithBool
: YES
] autorelease
] forKey
: NSPrintDetailedErrorReporting
];
466 [pPrintDict setObject
: [[NSNumber numberWithInt
: 1] autorelease
] forKey
: NSPrintFirstPage
];
467 // #i103253# weird: for some reason, autoreleasing the value below like the others above
468 // leads do a double free malloc error. Why this value should behave differently from all the others
470 [pPrintDict setObject
: [NSNumber numberWithInt
: mnCurPageRangeCount
] forKey
: NSPrintLastPage
];
472 // create print operation
473 NSPrintOperation
* pPrintOperation
= [NSPrintOperation printOperationWithView
: pPrintView printInfo
: mpPrintInfo
];
475 if( pPrintOperation
)
477 NSObject
* pReleaseAfterUse
= nil
;
478 bool bShowPanel
= !i_rController
.isDirectPrint()
479 && (officecfg::Office::Common::Misc::UseSystemPrintDialog::
481 && i_rController
.isShowDialogs();
482 [pPrintOperation setShowsPrintPanel
: bShowPanel
? YES
: NO
];
483 [pPrintOperation setShowsProgressPanel
: bShowProgressPanel
? YES
: NO
];
485 // set job title (since MacOSX 10.5)
486 if( [pPrintOperation respondsToSelector
: @
selector(setJobTitle
:)] )
487 [pPrintOperation performSelector
: @
selector(setJobTitle
:) withObject
: [CreateNSString( i_rJobName
) autorelease
]];
489 if( bShowPanel
&& mnCurPageRangeStart
== 0 && nCurJob
== 0) // only the first range of pages (in the first job) gets the accessory view
490 pReleaseAfterUse
= [AquaPrintAccessoryView setupPrinterPanel
: pPrintOperation withController
: &i_rController withState
: &aAccViewState
];
494 pInst
->startedPrintJob();
495 bool wasSuccessful
= [pPrintOperation runOperation
];
496 pInst
->endedPrintJob();
497 bSuccess
= wasSuccessful
;
498 bWasAborted
= [[[pPrintOperation printInfo
] jobDisposition
] compare
: NSPrintCancelJob
] == NSOrderedSame
;
500 if( pReleaseAfterUse
)
501 [pReleaseAfterUse release
];
504 mnCurPageRangeStart
+= mnCurPageRangeCount
;
505 mnCurPageRangeCount
= 1;
506 } while( aAccViewState
.bNeedRestart
|| mnCurPageRangeStart
+ mnCurPageRangeCount
< nAllPages
);
509 // inform application that it can release its data
510 // this is awkward, but the XRenderable interface has no method for this,
511 // so we need to call XRenderable::render one last time with IsLastPage = true
512 i_rController
.setLastPage( true );
513 GDIMetaFile aPageFile
;
515 SetupPrinterGraphics( mrContext
);
516 i_rController
.getFilteredPageFile( 0, aPageFile
);
518 i_rController
.setJobState( bWasAborted
519 ? view::PrintableState_JOB_ABORTED
520 : view::PrintableState_JOB_SPOOLED
);
522 mnCurPageRangeStart
= mnCurPageRangeCount
= 0;
527 bool AquaSalInfoPrinter::EndJob()
529 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
534 bool AquaSalInfoPrinter::AbortJob()
538 // FIXME: implementation
542 SalGraphics
* AquaSalInfoPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
544 if( i_bNewJobData
&& i_pSetupData
)
545 SetPrinterData( i_pSetupData
);
547 CGContextRef rContext
= [[NSGraphicsContext currentContext
] CGContext
];
549 SetupPrinterGraphics( rContext
);
554 bool AquaSalInfoPrinter::EndPage()
556 mpGraphics
->InvalidateContext();
560 AquaSalPrinter::AquaSalPrinter( AquaSalInfoPrinter
* i_pInfoPrinter
) :
561 mpInfoPrinter( i_pInfoPrinter
)
565 AquaSalPrinter::~AquaSalPrinter()
569 bool AquaSalPrinter::StartJob( const OUString
* i_pFileName
,
570 const OUString
& i_rJobName
,
572 ImplJobSetup
* i_pSetupData
,
573 vcl::PrinterController
& i_rController
)
575 return mpInfoPrinter
->StartJob( i_pFileName
, i_rJobName
, i_pSetupData
, i_rController
);
578 bool AquaSalPrinter::StartJob( const OUString
* /*i_pFileName*/,
579 const OUString
& /*i_rJobName*/,
580 const OUString
& /*i_rAppName*/,
581 sal_uInt32
/*i_nCopies*/,
586 OSL_FAIL( "should never be called" );
590 bool AquaSalPrinter::EndJob()
592 return mpInfoPrinter
->EndJob();
595 SalGraphics
* AquaSalPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
597 return mpInfoPrinter
->StartPage( i_pSetupData
, i_bNewJobData
);
600 void AquaSalPrinter::EndPage()
602 mpInfoPrinter
->EndPage();
605 void AquaSalInfoPrinter::InitPaperFormats( const ImplJobSetup
* )
607 m_aPaperFormats
.clear();
608 m_bPapersInit
= true;
612 SAL_WNODEPRECATED_DECLARATIONS_PUSH
613 //TODO: 10.9 statusForTable:, stringListForKey:inTable:
614 if( [mpPrinter statusForTable
: @
"PPD"] == NSPrinterTableOK
)
616 NSArray
* pPaperNames
= [mpPrinter stringListForKey
: @
"PageSize" inTable
: @
"PPD"];
619 unsigned int nPapers
= [pPaperNames count
];
620 for( unsigned int i
= 0; i
< nPapers
; i
++ )
622 NSString
* pPaper
= [pPaperNames objectAtIndex
: i
];
623 // first try to match the name
624 OString
aPaperName( [pPaper UTF8String
] );
625 Paper ePaper
= PaperInfo::fromPSName( aPaperName
);
626 if( ePaper
!= PAPER_USER
)
628 m_aPaperFormats
.push_back( PaperInfo( ePaper
) );
632 NSSize aPaperSize
= [mpPrinter pageSizeForPaper
: pPaper
];
633 if( aPaperSize
.width
> 0 && aPaperSize
.height
> 0 )
635 PaperInfo
aInfo( PtTo10Mu( aPaperSize
.width
),
636 PtTo10Mu( aPaperSize
.height
) );
637 if( aInfo
.getPaper() == PAPER_USER
)
639 m_aPaperFormats
.push_back( aInfo
);
645 SAL_WNODEPRECATED_DECLARATIONS_POP
649 const PaperInfo
* AquaSalInfoPrinter::matchPaper( tools::Long i_nWidth
, tools::Long i_nHeight
, Orientation
& o_rOrientation
) const
651 if( ! m_bPapersInit
)
652 const_cast<AquaSalInfoPrinter
*>(this)->InitPaperFormats( nullptr );
654 const PaperInfo
* pMatch
= nullptr;
655 o_rOrientation
= Orientation::Portrait
;
656 for( int n
= 0; n
< 2 ; n
++ )
658 for( size_t i
= 0; i
< m_aPaperFormats
.size(); i
++ )
660 if( std::abs( m_aPaperFormats
[i
].getWidth() - i_nWidth
) < 50 &&
661 std::abs( m_aPaperFormats
[i
].getHeight() - i_nHeight
) < 50 )
663 pMatch
= &m_aPaperFormats
[i
];
667 o_rOrientation
= Orientation::Landscape
;
668 std::swap( i_nWidth
, i_nHeight
);
673 int AquaSalInfoPrinter::GetLandscapeAngle( const ImplJobSetup
* )
678 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */