Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: printing/printing_context_mac.mm

Issue 10918117: Match paper by both PPD name and size. (Closed) Base URL: http://git.chromium.org/chromium/src.git@page
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 std::string PaperToString(const PMPaper paper) {
kmadhusu 2012/09/08 01:06:16 Document this function.
kmadhusu 2012/09/08 01:06:16 const PMPaper -> const PMPaper&
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Don't need if remove logging. On 2012/09/08 01:06
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Done.
26 CFStringRef name = NULL;
27 if (PMPaperGetPPDPaperName(paper, &name) != noErr)
28 return std::string();
29 std::string result(CFStringGetLength(name) + 1, ' ');
kmadhusu 2012/09/08 01:06:16 Use base::mac::ScopedCFTypeRef<CFStringRef>
30 if (result.empty() || !CFStringGetCString(name, &result[0], result.size(),
31 kCFStringEncodingASCII)) {
32 return std::string();
33 }
34 result.resize(result.size() - 1);
35 return result;
36 }
37
38 bool IsPaperNameEqual(const PMPaper paper1, const PMPaper paper2) {
kmadhusu 2012/09/08 01:06:16 nit: Document this function.
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Done.
39 CFStringRef name1 = NULL;
kmadhusu 2012/09/08 01:06:16 Use base::mac::ScopedCFTypeRef<CFStringRef>
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 I am not experienced with MAC dev, but it seems we
kmadhusu 2012/09/08 02:26:18 csilv@: Do we need to use base::mac::ScopedCFTypeR
csilv 2012/09/10 03:27:47 kmadhusu@, no reason to use ScopedCFTypeRef here.
40 CFStringRef name2 = NULL;
41 return PMPaperGetPPDPaperName(paper1, &name1) == noErr &&
kmadhusu 2012/09/08 01:06:16 nit: Please add parantheses. return (a == b) && (
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Done.
42 PMPaperGetPPDPaperName(paper2, &name2) == noErr &&
43 CFStringCompare(name1, name2,
44 kCFCompareCaseInsensitive) == kCFCompareEqualTo;
45 }
46
47 } // namespace
48
20 // static 49 // static
21 PrintingContext* PrintingContext::Create(const std::string& app_locale) { 50 PrintingContext* PrintingContext::Create(const std::string& app_locale) {
22 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale)); 51 return static_cast<PrintingContext*>(new PrintingContextMac(app_locale));
23 } 52 }
24 53
25 PrintingContextMac::PrintingContextMac(const std::string& app_locale) 54 PrintingContextMac::PrintingContextMac(const std::string& app_locale)
26 : PrintingContext(app_locale), 55 : PrintingContext(app_locale),
27 print_info_([[NSPrintInfo sharedPrintInfo] copy]), 56 print_info_([[NSPrintInfo sharedPrintInfo] copy]),
28 context_(NULL) { 57 context_(NULL) {
29 } 58 }
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 PMPrintSession print_session = 249 PMPrintSession print_session =
221 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); 250 static_cast<PMPrintSession>([print_info_.get() PMPrintSession]);
222 251
223 PMPageFormat default_page_format = 252 PMPageFormat default_page_format =
224 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); 253 static_cast<PMPageFormat>([print_info_.get() PMPageFormat]);
225 254
226 PMPaper default_paper; 255 PMPaper default_paper;
227 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr) 256 if (PMGetPageFormatPaper(default_page_format, &default_paper) != noErr)
228 return false; 257 return false;
229 258
230 double default_page_width, default_page_height; 259 double default_page_width = 0.0;
260 double default_page_height = 0.0;
231 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr) 261 if (PMPaperGetWidth(default_paper, &default_page_width) != noErr)
232 return false; 262 return false;
233 263
234 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr) 264 if (PMPaperGetHeight(default_paper, &default_page_height) != noErr)
235 return false; 265 return false;
266 VLOG(1) << "default_paper: " << PaperToString(default_paper) << " " <<
267 std::setprecision(10) << default_page_width << "x" <<
kmadhusu 2012/09/08 01:06:16 Do you need these VLOG here and at 290 and 324?
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Done.
268 std::setprecision(10) << default_page_height;
236 269
237 PMPrinter current_printer = NULL; 270 PMPrinter current_printer = NULL;
238 if (PMSessionGetCurrentPrinter(print_session, &current_printer) != noErr) 271 if (PMSessionGetCurrentPrinter(print_session, &current_printer) != noErr)
239 return false; 272 return false;
240 273
241 if (current_printer == nil) 274 if (current_printer == nil)
242 return false; 275 return false;
243 276
244 CFArrayRef paper_list = NULL; 277 CFArrayRef paper_list = NULL;
245 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr) 278 if (PMPrinterGetPaperList(current_printer, &paper_list) != noErr)
246 return false; 279 return false;
247 280
281 double best_match = std::numeric_limits<double>::max();
248 PMPaper best_matching_paper = kPMNoData; 282 PMPaper best_matching_paper = kPMNoData;
249 int num_papers = CFArrayGetCount(paper_list); 283 int num_papers = CFArrayGetCount(paper_list);
250 for (int i = 0; i < num_papers; ++i) { 284 for (int i = 0; i < num_papers; ++i) {
251 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i]; 285 PMPaper paper = (PMPaper) [(NSArray* ) paper_list objectAtIndex: i];
252 double paper_width, paper_height; 286 double paper_width = 0.0;
287 double paper_height = 0.0;
253 PMPaperGetWidth(paper, &paper_width); 288 PMPaperGetWidth(paper, &paper_width);
254 PMPaperGetHeight(paper, &paper_height); 289 PMPaperGetHeight(paper, &paper_height);
255 if (default_page_width == paper_width && 290 VLOG(1) << "paper_" << i << ": " << PaperToString(paper) << " " <<
256 default_page_height == paper_height) { 291 std::setprecision(10) << paper_width << "x" <<
292 std::setprecision(10) << paper_height;
293 double new_score = std::max(fabs(default_page_width - paper_width),
kmadhusu 2012/09/08 01:06:16 nit: Can you rename new_score to current_match or
kmadhusu 2012/09/08 01:06:16 Please re-add the comments that explain the paper
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 Done.
Vitaly Buka (NO REVIEWS) 2012/09/08 01:54:19 It's not relevant here. I removed the check for 0.
294 fabs(default_page_height - paper_height));
295 if (new_score > 2)
296 continue;
297 new_score += IsPaperNameEqual(paper, default_paper) ? 0 : 1;
298 if (new_score < best_match) {
257 best_matching_paper = paper; 299 best_matching_paper = paper;
258 break; 300 best_match = new_score;
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 } 301 }
265 } 302 }
266 303
267 if (best_matching_paper == kPMNoData) { 304 if (best_matching_paper == kPMNoData) {
268 PMPaper paper = kPMNoData; 305 PMPaper paper = kPMNoData;
269 // Create a custom paper for the specified default page size. 306 // Create a custom paper for the specified default page size.
270 PMPaperMargins default_margins; 307 PMPaperMargins default_margins;
271 if (PMPaperGetMargins(default_paper, &default_margins) != noErr) 308 if (PMPaperGetMargins(default_paper, &default_margins) != noErr)
272 return false; 309 return false;
273 310
274 const PMPaperMargins margins = 311 const PMPaperMargins margins =
275 {default_margins.top, default_margins.left, default_margins.bottom, 312 {default_margins.top, default_margins.left, default_margins.bottom,
276 default_margins.right}; 313 default_margins.right};
277 CFStringRef paper_id = CFSTR("Custom paper ID"); 314 CFStringRef paper_id = CFSTR("Custom paper ID");
278 CFStringRef paper_name = CFSTR("Custom paper"); 315 CFStringRef paper_name = CFSTR("Custom paper");
279 if (PMPaperCreateCustom(current_printer, paper_id, paper_name, 316 if (PMPaperCreateCustom(current_printer, paper_id, paper_name,
280 default_page_width, default_page_height, &margins, &paper) != 317 default_page_width, default_page_height, &margins, &paper) !=
281 noErr) { 318 noErr) {
282 return false; 319 return false;
283 } 320 }
284 [print_info_.get() updateFromPMPageFormat]; 321 [print_info_.get() updateFromPMPageFormat];
285 PMRelease(paper); 322 PMRelease(paper);
286 } else { 323 } else {
324 VLOG(1) << "best_paper_name: " << PaperToString(best_matching_paper);
287 PMPageFormat chosen_page_format = NULL; 325 PMPageFormat chosen_page_format = NULL;
288 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr) 326 if (PMCreatePageFormat((PMPageFormat*) &chosen_page_format) != noErr)
289 return false; 327 return false;
290 328
291 // Create page format from that paper. 329 // Create page format from that paper.
292 if (PMCreatePageFormatWithPMPaper(&chosen_page_format, 330 if (PMCreatePageFormatWithPMPaper(&chosen_page_format,
293 best_matching_paper) != noErr) { 331 best_matching_paper) != noErr) {
294 PMRelease(chosen_page_format); 332 PMRelease(chosen_page_format);
295 return false; 333 return false;
296 } 334 }
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
490 void PrintingContextMac::ReleaseContext() { 528 void PrintingContextMac::ReleaseContext() {
491 print_info_.reset(); 529 print_info_.reset();
492 context_ = NULL; 530 context_ = NULL;
493 } 531 }
494 532
495 gfx::NativeDrawingContext PrintingContextMac::context() const { 533 gfx::NativeDrawingContext PrintingContextMac::context() const {
496 return context_; 534 return context_;
497 } 535 }
498 536
499 } // namespace printing 537 } // namespace printing
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698