Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "printing/print_settings_initializer_mac.h" |
| 6 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 7 | #include "base/strings/sys_string_conversions.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 8 | #include "printing/print_settings.h" |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 9 | #include "printing/units.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 10 | |
| 11 | namespace printing { |
| 12 | |
| 13 | // static |
| 14 | void PrintSettingsInitializerMac::InitPrintSettings( |
| 15 | PMPrinter printer, |
| 16 | PMPageFormat page_format, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 17 | PrintSettings* print_settings) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 18 | print_settings->set_device_name( |
| 19 | base::SysCFStringRefToUTF16(PMPrinterGetID(printer))); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 20 | |
| 21 | PMOrientation orientation = kPMPortrait; |
| 22 | PMGetOrientation(page_format, &orientation); |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 23 | print_settings->SetOrientation(orientation == kPMLandscape); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 24 | |
| 25 | UInt32 resolution_count = 0; |
| 26 | PMResolution best_resolution = { 72.0, 72.0 }; |
| 27 | OSStatus status = PMPrinterGetPrinterResolutionCount(printer, |
| 28 | &resolution_count); |
| 29 | if (status == noErr) { |
| 30 | // Resolution indexes are 1-based. |
| 31 | for (uint32 i = 1; i <= resolution_count; ++i) { |
| 32 | PMResolution resolution; |
| 33 | PMPrinterGetIndexedPrinterResolution(printer, i, &resolution); |
| 34 | if (resolution.hRes > best_resolution.hRes) |
| 35 | best_resolution = resolution; |
| 36 | } |
| 37 | } |
| 38 | int dpi = best_resolution.hRes; |
| 39 | print_settings->set_dpi(dpi); |
| 40 | |
| 41 | DCHECK_EQ(dpi, best_resolution.vRes); |
| 42 | |
| 43 | // Get printable area and paper rects (in points) |
| 44 | PMRect page_rect, paper_rect; |
| 45 | PMGetAdjustedPageRect(page_format, &page_rect); |
| 46 | PMGetAdjustedPaperRect(page_format, &paper_rect); |
| 47 | |
| 48 | // Device units are in points. Units per inch is 72. |
| 49 | gfx::Size physical_size_device_units( |
| 50 | (paper_rect.right - paper_rect.left), |
| 51 | (paper_rect.bottom - paper_rect.top)); |
| 52 | gfx::Rect printable_area_device_units( |
| 53 | (page_rect.left - paper_rect.left), |
| 54 | (page_rect.top - paper_rect.top), |
| 55 | (page_rect.right - page_rect.left), |
| 56 | (page_rect.bottom - page_rect.top)); |
| 57 | |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 58 | DCHECK_EQ(print_settings->device_units_per_inch(), kPointsPerInch); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 59 | print_settings->SetPrinterPrintableArea(physical_size_device_units, |
| 60 | printable_area_device_units, |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 61 | false); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace printing |