You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <printing.h>
  20. #import <AppKit/AppKit.h>
  21. #import <PDFKit/PDFKit.h>
  22. namespace KIPLATFORM
  23. {
  24. namespace PRINTING
  25. {
  26. BOOL hasMultipleOrientations( PDFDocument* document )
  27. {
  28. if( [document pageCount] == 0 )
  29. return NO;
  30. PDFPage* firstPage = [document pageAtIndex:0];
  31. NSRect firstBounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox];
  32. BOOL firstIsLandscape = firstBounds.size.width > firstBounds.size.height;
  33. for( NSUInteger i = 1; i < [document pageCount]; i++ )
  34. {
  35. PDFPage* page = [document pageAtIndex:i];
  36. NSRect bounds = [page boundsForBox:kPDFDisplayBoxMediaBox];
  37. BOOL isLandscape = bounds.size.width > bounds.size.height;
  38. if( isLandscape != firstIsLandscape )
  39. return YES;
  40. }
  41. return NO;
  42. }
  43. PRINT_RESULT PrintPDF( const std::string& aFile, bool fit_to_page)
  44. {
  45. @autoreleasepool
  46. {
  47. NSString* path = [NSString stringWithUTF8String:aFile.c_str()];
  48. if( ![[NSFileManager defaultManager] isReadableFileAtPath:path] )
  49. return PRINT_RESULT::FILE_NOT_FOUND;
  50. NSURL* url = [NSURL fileURLWithPath:path];
  51. PDFDocument* document = [[PDFDocument alloc] initWithURL:url];
  52. if( !document || [document pageCount] == 0 )
  53. {
  54. [document release];
  55. return PRINT_RESULT::FAILED_TO_LOAD;
  56. }
  57. PDFView* pdfView = [[PDFView alloc] init];
  58. [pdfView setDocument:document];
  59. NSPrintInfo* printInfo = [[NSPrintInfo sharedPrintInfo] copy];
  60. BOOL hasMixed = hasMultipleOrientations( document );
  61. if( hasMixed )
  62. {
  63. [printInfo setOrientation:NSPrintingOrientationPortrait];
  64. [printInfo setHorizontallyCentered:YES];
  65. [printInfo setVerticallyCentered:YES];
  66. }
  67. if( fit_to_page )
  68. {
  69. NSMutableDictionary* settings = [[printInfo printSettings] mutableCopy];
  70. [settings setObject:@YES forKey:@"com.apple.print.PrintSettings.PMScaleToFit"];
  71. [printInfo setPrintSettings:settings];
  72. [settings release];
  73. }
  74. NSPrintOperation* op = [NSPrintOperation printOperationWithView:pdfView printInfo:printInfo];
  75. [printInfo release];
  76. if( !op )
  77. {
  78. [pdfView release];
  79. [document release];
  80. return PRINT_RESULT::FAILED_TO_PRINT;
  81. }
  82. [op setShowsPrintPanel:YES];
  83. [op setShowsProgressPanel:YES];
  84. BOOL success = [op runOperation];
  85. PRINT_RESULT result;
  86. if( success )
  87. {
  88. result = PRINT_RESULT::OK;
  89. } else
  90. {
  91. NSPrintInfo* info = [op printInfo];
  92. NSDictionary* settings = [info printSettings];
  93. if( [[settings objectForKey:NSPrintJobDisposition] isEqualToString:NSPrintCancelJob] )
  94. {
  95. result = PRINT_RESULT::CANCELLED;
  96. } else
  97. {
  98. result = PRINT_RESULT::FAILED_TO_PRINT;
  99. }
  100. }
  101. [pdfView release];
  102. [document release];
  103. return result;
  104. }
  105. }
  106. PRINT_RESULT PrintPDF(const std::string& aFile)
  107. {
  108. return PrintPDF(aFile, true);
  109. }
  110. } // namespace PRINTING
  111. } // namespace KIPLATFORM