Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "printing/printing_context_mac.h" | 5 #include "printing/printing_context_mac.h" |
| 6 | 6 |
| 7 #import <ApplicationServices/ApplicationServices.h> | 7 #import <ApplicationServices/ApplicationServices.h> |
| 8 #import <AppKit/AppKit.h> | 8 #import <AppKit/AppKit.h> |
| 9 | 9 |
| 10 #import <iomanip> | |
| 11 #import <numeric> | |
| 12 | |
| 10 #include "base/logging.h" | 13 #include "base/logging.h" |
| 11 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 12 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 13 #include "base/mac/scoped_nsexception_enabler.h" | 16 #include "base/mac/scoped_nsexception_enabler.h" |
| 14 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
| 15 #include "base/values.h" | 18 #include "base/values.h" |
| 16 #include "printing/print_settings_initializer_mac.h" | 19 #include "printing/print_settings_initializer_mac.h" |
| 17 | 20 |
| 18 namespace printing { | 21 namespace printing { |
| 19 | 22 |
| 23 namespace { | |
| 24 | |
| 25 // Return true if PPD name of paper is equal. | |
| 26 bool IsPaperNameEqual(const PMPaper& paper1, const PMPaper& paper2) { | |
| 27 CFStringRef name1 = NULL; | |
| 28 CFStringRef name2 = NULL; | |
| 29 return (PMPaperGetPPDPaperName(paper1, &name1) == noErr) && | |
| 30 (PMPaperGetPPDPaperName(paper2, &name2) == noErr) && | |
| 31 (CFStringCompare(name1, name2, | |
| 32 kCFCompareCaseInsensitive) == kCFCompareEqualTo); | |
| 33 } | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 20 // static | 37 // static |
| 21 PrintingContext* PrintingContext::Create(const std::string& app_locale) { | 38 PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 22 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); | 39 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); |
| 23 } | 40 } |
| 24 | 41 |
| 25 PrintingContextMac::PrintingContextMac(const std::string& app_locale) | 42 PrintingContextMac::PrintingContextMac(const std::string& app_locale) |
| 26 : PrintingContext(app_locale), | 43 : PrintingContext(app_locale), |
| 27 print_info_([[NSPrintInfo sharedPrintInfo] copy]), | 44 print_info_([[NSPrintInfo sharedPrintInfo] copy]), |
| 28 context_(NULL) { | 45 context_(NULL) { |
| 29 } | 46 } |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 PMPrintSession print_session = | 237 PMPrintSession print_session = |
| 221 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); | 238 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 222 | 239 |
| 223 PMPageFormat default_page_format = | 240 PMPageFormat default_page_format = |
| 224 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); | 241 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); |
| 225 | 242 |
| 226 PMPaper default_paper; | 243 PMPaper default_paper; |
| 227 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr) | 244 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr) |
| 228 return false; | 245 return false; |
| 229 | 246 |
| 230 double default_page_width, default_page_height; | 247 double default_page_width = 0.0; |
| 248 double default_page_height = 0.0; | |
| 231 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr) | 249 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr) |
| 232 return false; | 250 return false; |
| 233 | 251 |
| 234 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr) | 252 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr) |
| 235 return false; | 253 return false; |
| 236 | 254 |
| 237 PMPrinter current_printer = NULL; | 255 PMPrinter current_printer = NULL; |
| 238 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) | 256 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) |
| 239 return false; | 257 return false; |
| 240 | 258 |
| 241 if (current_printer == nil) | 259 if (current_printer == nil) |
| 242 return false; | 260 return false; |
| 243 | 261 |
| 244 CFArrayRef paper_list = NULL; | 262 CFArrayRef paper_list = NULL; |
| 245 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) | 263 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) |
| 246 return false; | 264 return false; |
| 247 | 265 |
| 266 double best_match = std::numeric_limits<double>::max(); | |
| 248 PMPaper best_matching_paper = kPMNoData; | 267 PMPaper best_matching_paper = kPMNoData; |
| 249 int num_papers = CFArrayGetCount(paper_list); | 268 int num_papers = CFArrayGetCount(paper_list); |
| 250 for (int i = 0; i < num_papers; ++i) { | 269 for (int i = 0; i < num_papers; ++i) { |
| 251 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i]; | 270 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i]; |
|
csilv
2012/09/10 03:27:47
nit: remove random whitespace making it:
PMPaper
Vitaly Buka (NO REVIEWS)
2012/09/10 16:44:53
Done.
| |
| 252 double paper_width, paper_height; | 271 double paper_width = 0.0; |
| 272 double paper_height = 0.0; | |
| 253 PMPaperGetWidth(paper, &paper_width); | 273 PMPaperGetWidth(paper, &paper_width); |
| 254 PMPaperGetHeight(paper, &paper_height); | 274 PMPaperGetHeight(paper, &paper_height); |
| 255 if (default_page_width == paper_width && | 275 double current_match = std::max(fabs(default_page_width - paper_width), |
| 256 default_page_height == paper_height) { | 276 fabs(default_page_height - paper_height)); |
| 277 // Ignore paper that very different. | |
|
csilv
2012/09/10 03:27:47
nit: "Ignore paper sizes that are very different."
Vitaly Buka (NO REVIEWS)
2012/09/10 16:44:53
Done.
| |
| 278 if (current_match > 2) | |
| 279 continue; | |
| 280 current_match += IsPaperNameEqual(paper, default_paper) ? 0 : 1; | |
|
Vitaly Buka (NO REVIEWS)
2012/09/08 01:54:19
Without line 280 it should work exactly as pre-CL
| |
| 281 if (current_match < best_match) { | |
| 257 best_matching_paper = paper; | 282 best_matching_paper = paper; |
| 258 break; | 283 best_match = current_match; |
| 259 } | |
| 260 // Trying to find the best matching paper. | |
| 261 if (fabs(default_page_width - paper_width) < 2 && | |
| 262 fabs(default_page_height - paper_height) < 2) { | |
| 263 best_matching_paper = paper; | |
| 264 } | 284 } |
| 265 } | 285 } |
| 266 | 286 |
| 267 if (best_matching_paper == kPMNoData) { | 287 if (best_matching_paper == kPMNoData) { |
| 268 PMPaper paper = kPMNoData; | 288 PMPaper paper = kPMNoData; |
| 269 // Create a custom paper for the specified default page size. | 289 // Create a custom paper for the specified default page size. |
| 270 PMPaperMargins default_margins; | 290 PMPaperMargins default_margins; |
| 271 if (PMPaperGetMargins(default_paper, &default_margins) != noErr) | 291 if (PMPaperGetMargins(default_paper, &default_margins) != noErr) |
| 272 return false; | 292 return false; |
| 273 | 293 |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 490 void PrintingContextMac::ReleaseContext() { | 510 void PrintingContextMac::ReleaseContext() { |
| 491 print_info_.reset(); | 511 print_info_.reset(); |
| 492 context_ = NULL; | 512 context_ = NULL; |
| 493 } | 513 } |
| 494 | 514 |
| 495 gfx::NativeDrawingContext PrintingContextMac::context() const { | 515 gfx::NativeDrawingContext PrintingContextMac::context() const { |
| 496 return context_; | 516 return context_; |
| 497 } | 517 } |
| 498 | 518 |
| 499 } // namespace printing | 519 } // namespace printing |
| OLD | NEW |