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 const OUString
& /*i_rAppName*/,
355 ImplJobSetup
* i_pSetupData
,
356 vcl::PrinterController
& i_rController
362 bool bSuccess
= false;
363 bool bWasAborted
= false;
364 AquaSalInstance
* pInst
= GetSalData()->mpInstance
;
365 PrintAccessoryViewState aAccViewState
;
366 sal_Int32 nAllPages
= 0;
369 i_rController
.setLastPage( false );
373 SetData( JobSetFlags::ALL
, i_pSetupData
);
375 // do we want a progress panel ?
376 bool bShowProgressPanel
= true;
377 beans::PropertyValue
* pMonitor
= i_rController
.getValue( OUString( "MonitorVisible" ) );
379 pMonitor
->Value
>>= bShowProgressPanel
;
380 if( ! i_rController
.isShowDialogs() )
381 bShowProgressPanel
= false;
383 // possibly create one job for collated output
384 bool bSinglePrintJobs
= i_rController
.getPrinter()->IsSinglePrintJobs();
386 // FIXME: jobStarted() should be done after the print dialog has ended (if there is one)
387 // how do I know when that might be ?
388 i_rController
.jobStarted();
390 int nCopies
= i_rController
.getPrinter()->GetCopyCount();
392 if( bSinglePrintJobs
)
398 for( int nCurJob
= 0; nCurJob
< nJobs
; nCurJob
++ )
400 aAccViewState
.bNeedRestart
= true;
403 if( aAccViewState
.bNeedRestart
)
405 mnCurPageRangeStart
= 0;
406 mnCurPageRangeCount
= 0;
407 nAllPages
= i_rController
.getFilteredPageCount();
410 aAccViewState
.bNeedRestart
= false;
412 Size
aCurSize( 21000, 29700 );
415 mnCurPageRangeCount
= 1;
416 aCurSize
= getPageSize( i_rController
, mnCurPageRangeStart
);
417 Size
aNextSize( aCurSize
);
419 // print pages up to a different size
420 while( mnCurPageRangeCount
+ mnCurPageRangeStart
< nAllPages
)
422 aNextSize
= getPageSize( i_rController
, mnCurPageRangeStart
+ mnCurPageRangeCount
);
423 if( aCurSize
== aNextSize
// same page size
425 (aCurSize
.Width() == aNextSize
.Height() && aCurSize
.Height() == aNextSize
.Width()) // same size, but different orientation
428 mnCurPageRangeCount
++;
435 mnCurPageRangeCount
= 0;
437 // now for the current run
438 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
439 // setup the paper size and orientation
440 // do this on our associated Printer object, since that is
441 // out interface to the applications which occasionally rely on the paper
442 // information (e.g. brochure printing scales to the found paper size)
443 // also SetPaperSizeUser has the advantage that we can share a
444 // platform independent paper matching algorithm
445 VclPtr
<Printer
> pPrinter( i_rController
.getPrinter() );
446 pPrinter
->SetMapMode( MapMode( MapUnit::Map100thMM
) );
447 pPrinter
->SetPaperSizeUser( aCurSize
);
450 NSView
* pPrintView
= [[AquaPrintView alloc
] initWithController
: &i_rController withInfoPrinter
: this];
452 NSMutableDictionary
* pPrintDict
= [mpPrintInfo dictionary
];
457 [mpPrintInfo setJobDisposition
: NSPrintSaveJob
];
458 NSString
* pPath
= CreateNSString( *i_pFileName
);
459 [pPrintDict setObject
:[NSURL fileURLWithPath
:pPath
] forKey
:NSPrintJobSavingURL
];
463 [pPrintDict setObject
: [[NSNumber numberWithInt
: nCopies
] autorelease
] forKey
: NSPrintCopies
];
465 [pPrintDict setObject
: [[NSNumber numberWithBool
: pPrinter
->IsCollateCopy()] autorelease
] forKey
: NSPrintMustCollate
];
466 [pPrintDict setObject
: [[NSNumber numberWithBool
: YES
] autorelease
] forKey
: NSPrintDetailedErrorReporting
];
467 [pPrintDict setObject
: [[NSNumber numberWithInt
: 1] autorelease
] forKey
: NSPrintFirstPage
];
468 // #i103253# weird: for some reason, autoreleasing the value below like the others above
469 // leads do a double free malloc error. Why this value should behave differently from all the others
471 [pPrintDict setObject
: [NSNumber numberWithInt
: mnCurPageRangeCount
] forKey
: NSPrintLastPage
];
473 // create print operation
474 NSPrintOperation
* pPrintOperation
= [NSPrintOperation printOperationWithView
: pPrintView printInfo
: mpPrintInfo
];
476 if( pPrintOperation
)
478 NSObject
* pReleaseAfterUse
= nil
;
479 bool bShowPanel
= !i_rController
.isDirectPrint()
480 && (officecfg::Office::Common::Misc::UseSystemPrintDialog::
482 && i_rController
.isShowDialogs();
483 [pPrintOperation setShowsPrintPanel
: bShowPanel
? YES
: NO
];
484 [pPrintOperation setShowsProgressPanel
: bShowProgressPanel
? YES
: NO
];
486 // set job title (since MacOSX 10.5)
487 if( [pPrintOperation respondsToSelector
: @
selector(setJobTitle
:)] )
488 [pPrintOperation performSelector
: @
selector(setJobTitle
:) withObject
: [CreateNSString( i_rJobName
) autorelease
]];
490 if( bShowPanel
&& mnCurPageRangeStart
== 0 && nCurJob
== 0) // only the first range of pages (in the first job) gets the accessory view
491 pReleaseAfterUse
= [AquaPrintAccessoryView setupPrinterPanel
: pPrintOperation withController
: &i_rController withState
: &aAccViewState
];
495 pInst
->startedPrintJob();
496 bool wasSuccessful
= [pPrintOperation runOperation
];
497 pInst
->endedPrintJob();
498 bSuccess
= wasSuccessful
;
499 bWasAborted
= [[[pPrintOperation printInfo
] jobDisposition
] compare
: NSPrintCancelJob
] == NSOrderedSame
;
501 if( pReleaseAfterUse
)
502 [pReleaseAfterUse release
];
505 mnCurPageRangeStart
+= mnCurPageRangeCount
;
506 mnCurPageRangeCount
= 1;
507 } while( aAccViewState
.bNeedRestart
|| mnCurPageRangeStart
+ mnCurPageRangeCount
< nAllPages
);
510 // inform application that it can release its data
511 // this is awkward, but the XRenderable interface has no method for this,
512 // so we need to call XRenderable::render one last time with IsLastPage = true
513 i_rController
.setLastPage( true );
514 GDIMetaFile aPageFile
;
516 SetupPrinterGraphics( mrContext
);
517 i_rController
.getFilteredPageFile( 0, aPageFile
);
519 i_rController
.setJobState( bWasAborted
520 ? view::PrintableState_JOB_ABORTED
521 : view::PrintableState_JOB_SPOOLED
);
523 mnCurPageRangeStart
= mnCurPageRangeCount
= 0;
528 bool AquaSalInfoPrinter::EndJob()
530 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
535 bool AquaSalInfoPrinter::AbortJob()
539 // FIXME: implementation
543 SalGraphics
* AquaSalInfoPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
545 if( i_bNewJobData
&& i_pSetupData
)
546 SetPrinterData( i_pSetupData
);
548 CGContextRef rContext
= [[NSGraphicsContext currentContext
] CGContext
];
550 SetupPrinterGraphics( rContext
);
555 bool AquaSalInfoPrinter::EndPage()
557 mpGraphics
->InvalidateContext();
561 AquaSalPrinter::AquaSalPrinter( AquaSalInfoPrinter
* i_pInfoPrinter
) :
562 mpInfoPrinter( i_pInfoPrinter
)
566 AquaSalPrinter::~AquaSalPrinter()
570 bool AquaSalPrinter::StartJob( const OUString
* i_pFileName
,
571 const OUString
& i_rJobName
,
572 const OUString
& i_rAppName
,
573 ImplJobSetup
* i_pSetupData
,
574 vcl::PrinterController
& i_rController
)
576 return mpInfoPrinter
->StartJob( i_pFileName
, i_rJobName
, i_rAppName
, i_pSetupData
, i_rController
);
579 bool AquaSalPrinter::StartJob( const OUString
* /*i_pFileName*/,
580 const OUString
& /*i_rJobName*/,
581 const OUString
& /*i_rAppName*/,
582 sal_uInt32
/*i_nCopies*/,
587 OSL_FAIL( "should never be called" );
591 bool AquaSalPrinter::EndJob()
593 return mpInfoPrinter
->EndJob();
596 SalGraphics
* AquaSalPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
598 return mpInfoPrinter
->StartPage( i_pSetupData
, i_bNewJobData
);
601 void AquaSalPrinter::EndPage()
603 mpInfoPrinter
->EndPage();
606 void AquaSalInfoPrinter::InitPaperFormats( const ImplJobSetup
* )
608 m_aPaperFormats
.clear();
609 m_bPapersInit
= true;
613 SAL_WNODEPRECATED_DECLARATIONS_PUSH
614 //TODO: 10.9 statusForTable:, stringListForKey:inTable:
615 if( [mpPrinter statusForTable
: @
"PPD"] == NSPrinterTableOK
)
617 NSArray
* pPaperNames
= [mpPrinter stringListForKey
: @
"PageSize" inTable
: @
"PPD"];
620 unsigned int nPapers
= [pPaperNames count
];
621 for( unsigned int i
= 0; i
< nPapers
; i
++ )
623 NSString
* pPaper
= [pPaperNames objectAtIndex
: i
];
624 // first try to match the name
625 OString
aPaperName( [pPaper UTF8String
] );
626 Paper ePaper
= PaperInfo::fromPSName( aPaperName
);
627 if( ePaper
!= PAPER_USER
)
629 m_aPaperFormats
.push_back( PaperInfo( ePaper
) );
633 NSSize aPaperSize
= [mpPrinter pageSizeForPaper
: pPaper
];
634 if( aPaperSize
.width
> 0 && aPaperSize
.height
> 0 )
636 PaperInfo
aInfo( PtTo10Mu( aPaperSize
.width
),
637 PtTo10Mu( aPaperSize
.height
) );
638 if( aInfo
.getPaper() == PAPER_USER
)
640 m_aPaperFormats
.push_back( aInfo
);
646 SAL_WNODEPRECATED_DECLARATIONS_POP
650 const PaperInfo
* AquaSalInfoPrinter::matchPaper( tools::Long i_nWidth
, tools::Long i_nHeight
, Orientation
& o_rOrientation
) const
652 if( ! m_bPapersInit
)
653 const_cast<AquaSalInfoPrinter
*>(this)->InitPaperFormats( nullptr );
655 const PaperInfo
* pMatch
= nullptr;
656 o_rOrientation
= Orientation::Portrait
;
657 for( int n
= 0; n
< 2 ; n
++ )
659 for( size_t i
= 0; i
< m_aPaperFormats
.size(); i
++ )
661 if( std::abs( m_aPaperFormats
[i
].getWidth() - i_nWidth
) < 50 &&
662 std::abs( m_aPaperFormats
[i
].getHeight() - i_nHeight
) < 50 )
664 pMatch
= &m_aPaperFormats
[i
];
668 o_rOrientation
= Orientation::Landscape
;
669 std::swap( i_nWidth
, i_nHeight
);
674 int AquaSalInfoPrinter::GetLandscapeAngle( const ImplJobSetup
* )
679 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */