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(true);
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( std::make_unique
<sal_uInt8
[]>(4), 4 );
201 void AquaSalInfoPrinter::setPaperSize( tools::Long i_nWidth
, tools::Long i_nHeight
, Orientation i_eSetOrientation
)
204 Orientation ePaperOrientation
= Orientation::Portrait
;
205 const PaperInfo
* pPaper
= matchPaper( i_nWidth
, i_nHeight
, ePaperOrientation
);
209 NSString
* pPaperName
= [CreateNSString( OStringToOUString(PaperInfo::toPSName(pPaper
->getPaper()), RTL_TEXTENCODING_ASCII_US
) ) autorelease
];
210 [mpPrintInfo setPaperName
: pPaperName
];
212 else if( i_nWidth
> 0 && i_nHeight
> 0 )
214 NSSize aPaperSize
= { static_cast<CGFloat
>(TenMuToPt(i_nWidth
)), static_cast<CGFloat
>(TenMuToPt(i_nHeight
)) };
215 [mpPrintInfo setPaperSize
: aPaperSize
];
217 // this seems counterintuitive
218 mePageOrientation
= i_eSetOrientation
;
221 bool AquaSalInfoPrinter::SetData( JobSetFlags i_nFlags
, ImplJobSetup
* io_pSetupData
)
223 if( ! io_pSetupData
|| io_pSetupData
->GetSystem() != JOBSETUP_SYSTEM_MAC
)
228 if( i_nFlags
& JobSetFlags::ORIENTATION
)
229 mePageOrientation
= io_pSetupData
->GetOrientation();
231 if( i_nFlags
& JobSetFlags::PAPERSIZE
)
234 tools::Long width
= 21000, height
= 29700;
235 if( io_pSetupData
->GetPaperFormat() == PAPER_USER
)
237 // #i101108# sanity check
238 if( io_pSetupData
->GetPaperWidth() && io_pSetupData
->GetPaperHeight() )
240 width
= io_pSetupData
->GetPaperWidth();
241 height
= io_pSetupData
->GetPaperHeight();
246 PaperInfo
aInfo( io_pSetupData
->GetPaperFormat() );
247 width
= aInfo
.getWidth();
248 height
= aInfo
.getHeight();
251 setPaperSize( width
, height
, mePageOrientation
);
255 return mpPrintInfo
!= nil
;
258 sal_uInt16
AquaSalInfoPrinter::GetPaperBinCount( const ImplJobSetup
* )
263 OUString
AquaSalInfoPrinter::GetPaperBinName( const ImplJobSetup
*, sal_uInt16
)
268 sal_uInt16
AquaSalInfoPrinter::GetPaperBinBySourceIndex( const ImplJobSetup
*, sal_uInt16
)
273 sal_uInt16
AquaSalInfoPrinter::GetSourceIndexByPaperBin(const ImplJobSetup
*, sal_uInt16
)
278 sal_uInt32
AquaSalInfoPrinter::GetCapabilities( const ImplJobSetup
*, PrinterCapType i_nType
)
282 case PrinterCapType::SupportDialog
:
284 case PrinterCapType::Copies
:
286 case PrinterCapType::CollateCopies
:
288 case PrinterCapType::SetOrientation
:
290 case PrinterCapType::SetPaperSize
:
292 case PrinterCapType::SetPaper
:
294 case PrinterCapType::ExternalDialog
:
295 return officecfg::Office::Common::Misc::UseSystemPrintDialog::get()
297 case PrinterCapType::PDF
:
299 case PrinterCapType::UsePullModel
:
306 void AquaSalInfoPrinter::GetPageInfo( const ImplJobSetup
*,
307 tools::Long
& o_rOutWidth
, tools::Long
& o_rOutHeight
,
313 sal_Int32 nDPIX
= 72, nDPIY
= 72;
314 mpGraphics
->GetResolution( nDPIX
, nDPIY
);
315 const double fXScaling
= static_cast<double>(nDPIX
)/72.0,
316 fYScaling
= static_cast<double>(nDPIY
)/72.0;
318 NSSize aPaperSize
= [mpPrintInfo paperSize
];
319 rPaperSize
.setWidth( static_cast<tools::Long
>( double(aPaperSize
.width
) * fXScaling
) );
320 rPaperSize
.setHeight( static_cast<tools::Long
>( double(aPaperSize
.height
) * fYScaling
) );
322 NSRect aImageRect
= [mpPrintInfo imageablePageBounds
];
323 rPageOffset
.setX( static_cast<tools::Long
>( aImageRect
.origin
.x
* fXScaling
) );
324 rPageOffset
.setY( static_cast<tools::Long
>( (aPaperSize
.height
- aImageRect
.size
.height
- aImageRect
.origin
.y
) * fYScaling
) );
325 o_rOutWidth
= static_cast<tools::Long
>( aImageRect
.size
.width
* fXScaling
);
326 o_rOutHeight
= static_cast<tools::Long
>( aImageRect
.size
.height
* fYScaling
);
328 if( mePageOrientation
== Orientation::Landscape
)
330 std::swap( o_rOutWidth
, o_rOutHeight
);
331 // swap width and height
332 tools::Long n
= rPaperSize
.Width();
333 rPaperSize
.setWidth(rPaperSize
.Height());
334 rPaperSize
.setHeight(n
);
335 // swap offset x and y
337 rPageOffset
.setX(rPageOffset
.Y());
343 static Size
getPageSize( vcl::PrinterController
const & i_rController
, sal_Int32 i_nPage
)
346 uno::Sequence
< PropertyValue
> const aPageParms( i_rController
.getPageParameters( i_nPage
) );
347 for( const PropertyValue
& pv
: aPageParms
)
349 if ( pv
.Name
== "PageSize" )
353 aPageSize
.setWidth( aSize
.Width
);
354 aPageSize
.setHeight( aSize
.Height
);
361 bool AquaSalInfoPrinter::StartJob( const OUString
* i_pFileName
,
362 const OUString
& i_rJobName
,
363 ImplJobSetup
* i_pSetupData
,
364 vcl::PrinterController
& i_rController
370 bool bSuccess
= false;
371 bool bWasAborted
= false;
372 AquaSalInstance
* pInst
= GetSalData()->mpInstance
;
373 PrintAccessoryViewState aAccViewState
;
374 sal_Int32 nAllPages
= 0;
377 i_rController
.setLastPage( false );
381 SetData( JobSetFlags::ALL
, i_pSetupData
);
383 // do we want a progress panel ?
384 bool bShowProgressPanel
= true;
385 beans::PropertyValue
* pMonitor
= i_rController
.getValue( OUString( "MonitorVisible" ) );
387 pMonitor
->Value
>>= bShowProgressPanel
;
388 if( ! i_rController
.isShowDialogs() )
389 bShowProgressPanel
= false;
391 // possibly create one job for collated output
392 bool bSinglePrintJobs
= i_rController
.getPrinter()->IsSinglePrintJobs();
394 // FIXME: jobStarted() should be done after the print dialog has ended (if there is one)
395 // how do I know when that might be ?
396 i_rController
.jobStarted();
398 int nCopies
= i_rController
.getPrinter()->GetCopyCount();
400 if( bSinglePrintJobs
)
406 for( int nCurJob
= 0; nCurJob
< nJobs
; nCurJob
++ )
408 aAccViewState
.bNeedRestart
= true;
411 if( aAccViewState
.bNeedRestart
)
413 mnCurPageRangeStart
= 0;
414 mnCurPageRangeCount
= 0;
415 nAllPages
= i_rController
.getFilteredPageCount();
418 aAccViewState
.bNeedRestart
= false;
420 Size
aCurSize( 21000, 29700 );
423 mnCurPageRangeCount
= 1;
424 aCurSize
= getPageSize( i_rController
, mnCurPageRangeStart
);
425 Size
aNextSize( aCurSize
);
427 // print pages up to a different size
428 while( mnCurPageRangeCount
+ mnCurPageRangeStart
< nAllPages
)
430 aNextSize
= getPageSize( i_rController
, mnCurPageRangeStart
+ mnCurPageRangeCount
);
431 if( aCurSize
== aNextSize
// same page size
433 (aCurSize
.Width() == aNextSize
.Height() && aCurSize
.Height() == aNextSize
.Width()) // same size, but different orientation
436 mnCurPageRangeCount
++;
443 mnCurPageRangeCount
= 0;
445 // now for the current run
446 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
447 // setup the paper size and orientation
448 // do this on our associated Printer object, since that is
449 // out interface to the applications which occasionally rely on the paper
450 // information (e.g. brochure printing scales to the found paper size)
451 // also SetPaperSizeUser has the advantage that we can share a
452 // platform independent paper matching algorithm
453 VclPtr
<Printer
> pPrinter( i_rController
.getPrinter() );
454 pPrinter
->SetMapMode( MapMode( MapUnit::Map100thMM
) );
455 pPrinter
->SetPaperSizeUser( aCurSize
);
458 NSView
* pPrintView
= [[AquaPrintView alloc
] initWithController
: &i_rController withInfoPrinter
: this];
460 NSMutableDictionary
* pPrintDict
= [mpPrintInfo dictionary
];
465 [mpPrintInfo setJobDisposition
: NSPrintSaveJob
];
466 NSString
* pPath
= CreateNSString( *i_pFileName
);
467 [pPrintDict setObject
:[NSURL fileURLWithPath
:pPath
] forKey
:NSPrintJobSavingURL
];
471 [pPrintDict setObject
: [[NSNumber numberWithInt
: nCopies
] autorelease
] forKey
: NSPrintCopies
];
473 [pPrintDict setObject
: [[NSNumber numberWithBool
: pPrinter
->IsCollateCopy()] autorelease
] forKey
: NSPrintMustCollate
];
474 [pPrintDict setObject
: [[NSNumber numberWithBool
: YES
] autorelease
] forKey
: NSPrintDetailedErrorReporting
];
475 [pPrintDict setObject
: [[NSNumber numberWithInt
: 1] autorelease
] forKey
: NSPrintFirstPage
];
476 // #i103253# weird: for some reason, autoreleasing the value below like the others above
477 // leads do a double free malloc error. Why this value should behave differently from all the others
479 [pPrintDict setObject
: [NSNumber numberWithInt
: mnCurPageRangeCount
] forKey
: NSPrintLastPage
];
481 // create print operation
482 NSPrintOperation
* pPrintOperation
= [NSPrintOperation printOperationWithView
: pPrintView printInfo
: mpPrintInfo
];
484 if( pPrintOperation
)
486 NSObject
* pReleaseAfterUse
= nil
;
487 bool bShowPanel
= !i_rController
.isDirectPrint()
488 && (officecfg::Office::Common::Misc::UseSystemPrintDialog::
490 && i_rController
.isShowDialogs();
491 [pPrintOperation setShowsPrintPanel
: bShowPanel
? YES
: NO
];
492 [pPrintOperation setShowsProgressPanel
: bShowProgressPanel
? YES
: NO
];
494 // set job title (since MacOSX 10.5)
495 if( [pPrintOperation respondsToSelector
: @
selector(setJobTitle
:)] )
496 [pPrintOperation performSelector
: @
selector(setJobTitle
:) withObject
: [CreateNSString( i_rJobName
) autorelease
]];
498 if( bShowPanel
&& mnCurPageRangeStart
== 0 && nCurJob
== 0) // only the first range of pages (in the first job) gets the accessory view
499 pReleaseAfterUse
= [AquaPrintAccessoryView setupPrinterPanel
: pPrintOperation withController
: &i_rController withState
: &aAccViewState
];
503 pInst
->startedPrintJob();
504 bool wasSuccessful
= [pPrintOperation runOperation
];
505 pInst
->endedPrintJob();
506 bSuccess
= wasSuccessful
;
507 bWasAborted
= [[[pPrintOperation printInfo
] jobDisposition
] compare
: NSPrintCancelJob
] == NSOrderedSame
;
509 if( pReleaseAfterUse
)
510 [pReleaseAfterUse release
];
513 mnCurPageRangeStart
+= mnCurPageRangeCount
;
514 mnCurPageRangeCount
= 1;
515 } while( aAccViewState
.bNeedRestart
|| mnCurPageRangeStart
+ mnCurPageRangeCount
< nAllPages
);
518 // inform application that it can release its data
519 // this is awkward, but the XRenderable interface has no method for this,
520 // so we need to call XRenderable::render one last time with IsLastPage = true
521 i_rController
.setLastPage( true );
522 GDIMetaFile aPageFile
;
524 SetupPrinterGraphics( mrContext
);
525 i_rController
.getFilteredPageFile( 0, aPageFile
);
527 i_rController
.setJobState( bWasAborted
528 ? view::PrintableState_JOB_ABORTED
529 : view::PrintableState_JOB_SPOOLED
);
531 mnCurPageRangeStart
= mnCurPageRangeCount
= 0;
536 bool AquaSalInfoPrinter::EndJob()
538 mnStartPageOffsetX
= mnStartPageOffsetY
= 0;
543 bool AquaSalInfoPrinter::AbortJob()
547 // FIXME: implementation
551 SalGraphics
* AquaSalInfoPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
553 if( i_bNewJobData
&& i_pSetupData
)
554 SetPrinterData( i_pSetupData
);
556 CGContextRef rContext
= [[NSGraphicsContext currentContext
] CGContext
];
558 SetupPrinterGraphics( rContext
);
563 bool AquaSalInfoPrinter::EndPage()
565 mpGraphics
->InvalidateContext();
569 AquaSalPrinter::AquaSalPrinter( AquaSalInfoPrinter
* i_pInfoPrinter
) :
570 mpInfoPrinter( i_pInfoPrinter
)
574 AquaSalPrinter::~AquaSalPrinter()
578 bool AquaSalPrinter::StartJob( const OUString
* i_pFileName
,
579 const OUString
& i_rJobName
,
581 ImplJobSetup
* i_pSetupData
,
582 vcl::PrinterController
& i_rController
)
584 return mpInfoPrinter
->StartJob( i_pFileName
, i_rJobName
, i_pSetupData
, i_rController
);
587 bool AquaSalPrinter::StartJob( const OUString
* /*i_pFileName*/,
588 const OUString
& /*i_rJobName*/,
589 const OUString
& /*i_rAppName*/,
590 sal_uInt32
/*i_nCopies*/,
595 OSL_FAIL( "should never be called" );
599 bool AquaSalPrinter::EndJob()
601 return mpInfoPrinter
->EndJob();
604 SalGraphics
* AquaSalPrinter::StartPage( ImplJobSetup
* i_pSetupData
, bool i_bNewJobData
)
606 return mpInfoPrinter
->StartPage( i_pSetupData
, i_bNewJobData
);
609 void AquaSalPrinter::EndPage()
611 mpInfoPrinter
->EndPage();
614 void AquaSalInfoPrinter::InitPaperFormats( const ImplJobSetup
* )
616 m_aPaperFormats
.clear();
617 m_bPapersInit
= true;
621 SAL_WNODEPRECATED_DECLARATIONS_PUSH
622 //TODO: 10.9 statusForTable:, stringListForKey:inTable:
623 if( [mpPrinter statusForTable
: @
"PPD"] == NSPrinterTableOK
)
625 NSArray
* pPaperNames
= [mpPrinter stringListForKey
: @
"PageSize" inTable
: @
"PPD"];
628 unsigned int nPapers
= [pPaperNames count
];
629 for( unsigned int i
= 0; i
< nPapers
; i
++ )
631 NSString
* pPaper
= [pPaperNames objectAtIndex
: i
];
632 // first try to match the name
633 OString
aPaperName( [pPaper UTF8String
] );
634 Paper ePaper
= PaperInfo::fromPSName( aPaperName
);
635 if( ePaper
!= PAPER_USER
)
637 m_aPaperFormats
.push_back( PaperInfo( ePaper
) );
641 NSSize aPaperSize
= [mpPrinter pageSizeForPaper
: pPaper
];
642 if( aPaperSize
.width
> 0 && aPaperSize
.height
> 0 )
644 PaperInfo
aInfo( PtTo10Mu( aPaperSize
.width
),
645 PtTo10Mu( aPaperSize
.height
) );
646 if( aInfo
.getPaper() == PAPER_USER
)
648 m_aPaperFormats
.push_back( aInfo
);
654 SAL_WNODEPRECATED_DECLARATIONS_POP
658 const PaperInfo
* AquaSalInfoPrinter::matchPaper( tools::Long i_nWidth
, tools::Long i_nHeight
, Orientation
& o_rOrientation
) const
660 if( ! m_bPapersInit
)
661 const_cast<AquaSalInfoPrinter
*>(this)->InitPaperFormats( nullptr );
663 const PaperInfo
* pMatch
= nullptr;
664 o_rOrientation
= Orientation::Portrait
;
665 for( int n
= 0; n
< 2 ; n
++ )
667 for( size_t i
= 0; i
< m_aPaperFormats
.size(); i
++ )
669 if( std::abs( m_aPaperFormats
[i
].getWidth() - i_nWidth
) < 50 &&
670 std::abs( m_aPaperFormats
[i
].getHeight() - i_nHeight
) < 50 )
672 pMatch
= &m_aPaperFormats
[i
];
676 o_rOrientation
= Orientation::Landscape
;
677 std::swap( i_nWidth
, i_nHeight
);
682 int AquaSalInfoPrinter::GetLandscapeAngle( const ImplJobSetup
* )
687 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */