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
7#include "base/strings/sys_string_conversions.h"
8#include "printing/print_settings.h"
9#include "printing/units.h"
10
11namespace printing {
12
13// static
14void PrintSettingsInitializerMac::InitPrintSettings(
15    PMPrinter printer,
16    PMPageFormat page_format,
17    PrintSettings* print_settings) {
18  print_settings->set_device_name(
19      base::SysCFStringRefToUTF16(PMPrinterGetID(printer)));
20
21  PMOrientation orientation = kPMPortrait;
22  PMGetOrientation(page_format, &orientation);
23  print_settings->SetOrientation(orientation == kPMLandscape);
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
58  DCHECK_EQ(print_settings->device_units_per_inch(), kPointsPerInch);
59  print_settings->SetPrinterPrintableArea(physical_size_device_units,
60                                          printable_area_device_units,
61                                          false);
62}
63
64}  // namespace printing
65