blob: fd665989a59fb8ba7b5a6c2123ad5e4d29e9be22 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)c2e0dbd2013-05-09 18:35:53 +01007#include "base/strings/sys_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +00008#include "printing/print_settings.h"
9
10namespace printing {
11
12// static
13void PrintSettingsInitializerMac::InitPrintSettings(
14 PMPrinter printer,
15 PMPageFormat page_format,
16 const PageRanges& new_ranges,
17 bool print_selection_only,
18 PrintSettings* print_settings) {
19 DCHECK(print_settings);
20
21 print_settings->set_printer_name(
22 base::SysCFStringRefToUTF16(PMPrinterGetName(printer)));
23 print_settings->set_device_name(
24 base::SysCFStringRefToUTF16(PMPrinterGetID(printer)));
25 print_settings->ranges = new_ranges;
26
27 PMOrientation orientation = kPMPortrait;
28 PMGetOrientation(page_format, &orientation);
29 print_settings->set_landscape(orientation == kPMLandscape);
30 print_settings->selection_only = print_selection_only;
31
32 UInt32 resolution_count = 0;
33 PMResolution best_resolution = { 72.0, 72.0 };
34 OSStatus status = PMPrinterGetPrinterResolutionCount(printer,
35 &resolution_count);
36 if (status == noErr) {
37 // Resolution indexes are 1-based.
38 for (uint32 i = 1; i <= resolution_count; ++i) {
39 PMResolution resolution;
40 PMPrinterGetIndexedPrinterResolution(printer, i, &resolution);
41 if (resolution.hRes > best_resolution.hRes)
42 best_resolution = resolution;
43 }
44 }
45 int dpi = best_resolution.hRes;
46 print_settings->set_dpi(dpi);
47
48 DCHECK_EQ(dpi, best_resolution.vRes);
49
50 // Get printable area and paper rects (in points)
51 PMRect page_rect, paper_rect;
52 PMGetAdjustedPageRect(page_format, &page_rect);
53 PMGetAdjustedPaperRect(page_format, &paper_rect);
54
55 // Device units are in points. Units per inch is 72.
56 gfx::Size physical_size_device_units(
57 (paper_rect.right - paper_rect.left),
58 (paper_rect.bottom - paper_rect.top));
59 gfx::Rect printable_area_device_units(
60 (page_rect.left - paper_rect.left),
61 (page_rect.top - paper_rect.top),
62 (page_rect.right - page_rect.left),
63 (page_rect.bottom - page_rect.top));
64
65 print_settings->SetPrinterPrintableArea(physical_size_device_units,
66 printable_area_device_units,
67 72);
68}
69
70} // namespace printing