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> | 10 #import <iomanip> |
| 11 #import <numeric> | 11 #import <numeric> |
| 12 | 12 |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/mac/scoped_cftyperef.h" | 14 #include "base/mac/scoped_cftyperef.h" |
| 15 #include "base/mac/scoped_nsautorelease_pool.h" | 15 #include "base/mac/scoped_nsautorelease_pool.h" |
| 16 #include "base/mac/scoped_nsexception_enabler.h" | 16 #include "base/mac/scoped_nsexception_enabler.h" |
| 17 #include "base/strings/sys_string_conversions.h" | 17 #include "base/strings/sys_string_conversions.h" |
| 18 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "printing/print_settings_initializer_mac.h" | 20 #include "printing/print_settings_initializer_mac.h" |
| 21 #include "printing/units.h" | 21 #include "printing/units.h" |
| 22 | 22 |
| 23 namespace printing { | 23 namespace printing { |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 // Return true if PPD name of paper is equal. | 27 // Return true if PPD name of paper is equal. |
| 28 bool IsPaperNameEqual(const PMPaper& paper1, const PMPaper& paper2) { | 28 bool IsPaperNameEqual(CFStringRef name1, const PMPaper& paper2) { |
| 29 CFStringRef name1 = NULL; | |
| 30 CFStringRef name2 = NULL; | 29 CFStringRef name2 = NULL; |
| 31 return (PMPaperGetPPDPaperName(paper1, &name1) == noErr) && | 30 return (PMPaperGetPPDPaperName(paper2, &name2) == noErr) && |
| 32 (PMPaperGetPPDPaperName(paper2, &name2) == noErr) && | 31 (CFStringCompare(name1, name2, kCFCompareCaseInsensitive) == |
| 33 (CFStringCompare(name1, name2, | 32 kCFCompareEqualTo); |
| 34 kCFCompareCaseInsensitive) == kCFCompareEqualTo); | 33 } |
| 34 | |
| 35 PMPaper MatchPaper(CFArrayRef paper_list, | |
| 36 CFStringRef name, | |
| 37 double width, | |
| 38 double height) { | |
| 39 double best_match = std::numeric_limits<double>::max(); | |
| 40 PMPaper best_matching_paper = kPMNoData; | |
| 41 int num_papers = CFArrayGetCount(paper_list); | |
| 42 for (int i = 0; i < num_papers; ++i) { | |
| 43 PMPaper paper = (PMPaper)[(NSArray*)paper_list objectAtIndex : i]; | |
| 44 double paper_width = 0.0; | |
| 45 double paper_height = 0.0; | |
| 46 PMPaperGetWidth(paper, &paper_width); | |
| 47 PMPaperGetHeight(paper, &paper_height); | |
| 48 double current_match = | |
| 49 std::max(fabs(width - paper_width), fabs(height - paper_height)); | |
| 50 // Ignore paper sizes that are very different. | |
| 51 if (current_match > 2) | |
| 52 continue; | |
| 53 current_match += IsPaperNameEqual(name, paper) ? 0 : 1; | |
| 54 if (current_match < best_match) { | |
| 55 best_matching_paper = paper; | |
| 56 best_match = current_match; | |
| 57 } | |
| 58 } | |
| 59 return best_matching_paper; | |
| 35 } | 60 } |
| 36 | 61 |
| 37 } // namespace | 62 } // namespace |
| 38 | 63 |
| 39 // static | 64 // static |
| 40 PrintingContext* PrintingContext::Create(const std::string& app_locale) { | 65 PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 41 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); | 66 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); |
| 42 } | 67 } |
| 43 | 68 |
| 44 PrintingContextMac::PrintingContextMac(const std::string& app_locale) | 69 PrintingContextMac::PrintingContextMac(const std::string& app_locale) |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 return status == noErr; | 247 return status == noErr; |
| 223 } | 248 } |
| 224 | 249 |
| 225 bool PrintingContextMac::UpdatePageFormatWithPaperInfo() { | 250 bool PrintingContextMac::UpdatePageFormatWithPaperInfo() { |
| 226 PMPrintSession print_session = | 251 PMPrintSession print_session = |
| 227 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); | 252 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); |
| 228 | 253 |
| 229 PMPageFormat default_page_format = | 254 PMPageFormat default_page_format = |
| 230 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); | 255 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); |
| 231 | 256 |
| 232 PMPaper default_paper; | |
| 233 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr) | |
| 234 return false; | |
| 235 | |
| 236 double default_page_width = 0.0; | |
| 237 double default_page_height = 0.0; | |
| 238 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr) | |
| 239 return false; | |
| 240 | |
| 241 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr) | |
| 242 return false; | |
| 243 | |
| 244 PMPrinter current_printer = NULL; | 257 PMPrinter current_printer = NULL; |
| 245 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) | 258 if (PMSessionGetCurrentPrinter(print_session, ¤t_printer) != noErr) |
| 246 return false; | 259 return false; |
| 247 | 260 |
| 248 if (current_printer == nil) | 261 if (current_printer == nil) |
|
Aleksey Shlyapnikov
2014/06/18 17:45:01
nil -> NULL?
Vitaly Buka (NO REVIEWS)
2014/06/19 00:39:08
should be nil
| |
| 249 return false; | 262 return false; |
| 250 | 263 |
| 251 CFArrayRef paper_list = NULL; | 264 CFArrayRef paper_list = NULL; |
| 252 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) | 265 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) |
| 253 return false; | 266 return false; |
|
Aleksey Shlyapnikov
2014/06/18 17:45:02
Move down to the first use.
Vitaly Buka (NO REVIEWS)
2014/06/19 00:39:09
Done.
| |
| 254 | 267 |
| 255 double best_match = std::numeric_limits<double>::max(); | 268 const PrintSettings::RequestedMedia& media = settings_.requested_media(); |
| 256 PMPaper best_matching_paper = kPMNoData; | 269 |
| 257 int num_papers = CFArrayGetCount(paper_list); | 270 double page_width = 0.0; |
| 258 for (int i = 0; i < num_papers; ++i) { | 271 double page_height = 0.0; |
| 259 PMPaper paper = (PMPaper)[(NSArray*)paper_list objectAtIndex: i]; | 272 CFStringRef paper_name = NULL; |
| 260 double paper_width = 0.0; | 273 PMPaperMargins margins = {0}; |
| 261 double paper_height = 0.0; | 274 |
| 262 PMPaperGetWidth(paper, &paper_width); | 275 if (media.size_microns.IsEmpty()) { |
| 263 PMPaperGetHeight(paper, &paper_height); | 276 PMPaper default_paper; |
| 264 double current_match = std::max(fabs(default_page_width - paper_width), | 277 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr || |
| 265 fabs(default_page_height - paper_height)); | 278 PMPaperGetPPDPaperName(default_paper, &paper_name) != noErr || |
| 266 // Ignore paper sizes that are very different. | 279 PMPaperGetWidth(default_paper, &page_width) != noErr || |
| 267 if (current_match > 2) | 280 PMPaperGetHeight(default_paper, &page_height) != noErr || |
| 268 continue; | 281 PMPaperGetMargins(default_paper, &margins) != noErr) { |
| 269 current_match += IsPaperNameEqual(paper, default_paper) ? 0 : 1; | 282 return false; |
| 270 if (current_match < best_match) { | |
| 271 best_matching_paper = paper; | |
| 272 best_match = current_match; | |
| 273 } | 283 } |
| 284 } else { | |
| 285 const double kMutiplier = kPointsPerInch / (10.0f * kHundrethsMMPerInch); | |
| 286 page_width = media.size_microns.width() * kMutiplier; | |
| 287 page_height = media.size_microns.height() * kMutiplier; | |
| 288 paper_name = base::SysUTF8ToCFStringRef(media.vendor_id); | |
| 274 } | 289 } |
| 275 | 290 |
| 291 PMPaper best_matching_paper = | |
| 292 MatchPaper(paper_list, paper_name, page_width, page_height); | |
| 293 | |
| 276 if (best_matching_paper == kPMNoData) { | 294 if (best_matching_paper == kPMNoData) { |
| 277 PMPaper paper = kPMNoData; | 295 PMPaper paper = kPMNoData; |
| 278 // Create a custom paper for the specified default page size. | |
| 279 PMPaperMargins default_margins; | |
| 280 if (PMPaperGetMargins(default_paper, &default_margins) != noErr) | |
| 281 return false; | |
| 282 | |
| 283 const PMPaperMargins margins = | |
| 284 {default_margins.top, default_margins.left, default_margins.bottom, | |
| 285 default_margins.right}; | |
| 286 CFStringRef paper_id = CFSTR("Custom paper ID"); | 296 CFStringRef paper_id = CFSTR("Custom paper ID"); |
| 287 CFStringRef paper_name = CFSTR("Custom paper"); | 297 CFStringRef paper_name = CFSTR("Custom paper"); |
| 288 if (PMPaperCreateCustom(current_printer, paper_id, paper_name, | 298 if (PMPaperCreateCustom(current_printer, |
| 289 default_page_width, default_page_height, &margins, &paper) != | 299 paper_id, |
| 290 noErr) { | 300 paper_name, |
| 301 page_width, | |
| 302 page_height, | |
| 303 &margins, | |
| 304 &paper) != noErr) { | |
|
Aleksey Shlyapnikov
2014/06/18 17:45:01
What for paper is created here? It's released on t
Vitaly Buka (NO REVIEWS)
2014/06/19 00:39:08
Done.
| |
| 291 return false; | 305 return false; |
| 292 } | 306 } |
| 293 [print_info_.get() updateFromPMPageFormat]; | |
| 294 PMRelease(paper); | 307 PMRelease(paper); |
| 295 } else { | 308 } else { |
| 296 PMPageFormat chosen_page_format = NULL; | 309 PMPageFormat chosen_page_format = NULL; |
| 297 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr) | 310 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr) |
| 298 return false; | 311 return false; |
| 299 | 312 |
| 300 // Create page format from that paper. | 313 // Create page format from that paper. |
| 301 if (PMCreatePageFormatWithPMPaper(&chosen_page_format, | 314 if (PMCreatePageFormatWithPMPaper(&chosen_page_format, |
| 302 best_matching_paper) != noErr) { | 315 best_matching_paper) != noErr) { |
| 303 PMRelease(chosen_page_format); | 316 PMRelease(chosen_page_format); |
| 304 return false; | 317 return false; |
| 305 } | 318 } |
| 306 // Copy over the original format with the new page format. | 319 // Copy over the original format with the new page format. |
| 307 if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) { | 320 if (PMCopyPageFormat(chosen_page_format, default_page_format) != noErr) { |
| 308 PMRelease(chosen_page_format); | 321 PMRelease(chosen_page_format); |
| 309 return false; | 322 return false; |
| 310 } | 323 } |
| 311 [print_info_.get() updateFromPMPageFormat]; | |
| 312 PMRelease(chosen_page_format); | 324 PMRelease(chosen_page_format); |
| 313 } | 325 } |
| 326 [print_info_.get() updateFromPMPageFormat]; | |
| 314 return true; | 327 return true; |
| 315 } | 328 } |
| 316 | 329 |
| 317 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { | 330 bool PrintingContextMac::SetCopiesInPrintSettings(int copies) { |
| 318 if (copies < 1) | 331 if (copies < 1) |
| 319 return false; | 332 return false; |
| 320 | 333 |
| 321 PMPrintSettings pmPrintSettings = | 334 PMPrintSettings pmPrintSettings = |
| 322 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); | 335 static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]); |
| 323 return PMSetCopies(pmPrintSettings, copies, false) == noErr; | 336 return PMSetCopies(pmPrintSettings, copies, false) == noErr; |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 void PrintingContextMac::ReleaseContext() { | 512 void PrintingContextMac::ReleaseContext() { |
| 500 print_info_.reset(); | 513 print_info_.reset(); |
| 501 context_ = NULL; | 514 context_ = NULL; |
| 502 } | 515 } |
| 503 | 516 |
| 504 gfx::NativeDrawingContext PrintingContextMac::context() const { | 517 gfx::NativeDrawingContext PrintingContextMac::context() const { |
| 505 return context_; | 518 return context_; |
| 506 } | 519 } |
| 507 | 520 |
| 508 } // namespace printing | 521 } // namespace printing |
| OLD | NEW |