Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2012 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.h"
      6 
      7 #include "base/atomic_sequence_num.h"
      8 #include "base/logging.h"
      9 #include "printing/print_job_constants.h"
     10 #include "printing/units.h"
     11 
     12 namespace printing {
     13 
     14 #if defined(USE_CUPS)
     15 void GetColorModelForMode(
     16     int color_mode, std::string* color_setting_name, std::string* color_value) {
     17 #if defined(OS_MACOSX)
     18   const char kCUPSColorMode[] = "ColorMode";
     19   const char kCUPSColorModel[] = "ColorModel";
     20   const char kCUPSPrintoutMode[] = "PrintoutMode";
     21   const char kCUPSProcessColorModel[] = "ProcessColorModel";
     22 #else
     23   const char kCUPSColorMode[] = "cups-ColorMode";
     24   const char kCUPSColorModel[] = "cups-ColorModel";
     25   const char kCUPSPrintoutMode[] = "cups-PrintoutMode";
     26   const char kCUPSProcessColorModel[] = "cups-ProcessColorModel";
     27 #endif  // defined(OS_MACOSX)
     28 
     29   color_setting_name->assign(kCUPSColorModel);
     30   switch (color_mode) {
     31     case COLOR:
     32       color_value->assign(kColor);
     33       break;
     34     case CMYK:
     35       color_value->assign(kCMYK);
     36       break;
     37     case PRINTOUTMODE_NORMAL:
     38       color_value->assign(kNormal);
     39       color_setting_name->assign(kCUPSPrintoutMode);
     40       break;
     41     case PRINTOUTMODE_NORMAL_GRAY:
     42       color_value->assign(kNormalGray);
     43       color_setting_name->assign(kCUPSPrintoutMode);
     44       break;
     45     case RGB16:
     46       color_value->assign(kRGB16);
     47       break;
     48     case RGBA:
     49       color_value->assign(kRGBA);
     50       break;
     51     case RGB:
     52       color_value->assign(kRGB);
     53       break;
     54     case CMY:
     55       color_value->assign(kCMY);
     56       break;
     57     case CMY_K:
     58       color_value->assign(kCMY_K);
     59       break;
     60     case BLACK:
     61       color_value->assign(kBlack);
     62       break;
     63     case GRAY:
     64       color_value->assign(kGray);
     65       break;
     66     case COLORMODE_COLOR:
     67       color_setting_name->assign(kCUPSColorMode);
     68       color_value->assign(kColor);
     69       break;
     70     case COLORMODE_MONOCHROME:
     71       color_setting_name->assign(kCUPSColorMode);
     72       color_value->assign(kMonochrome);
     73       break;
     74     case HP_COLOR_COLOR:
     75       color_setting_name->assign(kColor);
     76       color_value->assign(kColor);
     77       break;
     78     case HP_COLOR_BLACK:
     79       color_setting_name->assign(kColor);
     80       color_value->assign(kBlack);
     81       break;
     82     case PROCESSCOLORMODEL_CMYK:
     83       color_setting_name->assign(kCUPSProcessColorModel);
     84       color_value->assign(kCMYK);
     85       break;
     86     case PROCESSCOLORMODEL_GREYSCALE:
     87       color_setting_name->assign(kCUPSProcessColorModel);
     88       color_value->assign(kGreyscale);
     89       break;
     90     case PROCESSCOLORMODEL_RGB:
     91       color_setting_name->assign(kCUPSProcessColorModel);
     92       color_value->assign(kRGB);
     93       break;
     94     default:
     95       color_value->assign(kGrayscale);
     96       break;
     97   }
     98 }
     99 #endif  // defined(USE_CUPS)
    100 
    101 bool IsColorModelSelected(int color_mode) {
    102   return (color_mode != GRAY &&
    103           color_mode != BLACK &&
    104           color_mode != PRINTOUTMODE_NORMAL_GRAY &&
    105           color_mode != COLORMODE_MONOCHROME &&
    106           color_mode != PROCESSCOLORMODEL_GREYSCALE &&
    107           color_mode != HP_COLOR_BLACK);
    108 }
    109 
    110 // Global SequenceNumber used for generating unique cookie values.
    111 static base::StaticAtomicSequenceNumber cookie_seq;
    112 
    113 PrintSettings::PrintSettings() {
    114   Clear();
    115 }
    116 
    117 PrintSettings::~PrintSettings() {
    118 }
    119 
    120 void PrintSettings::Clear() {
    121   ranges_.clear();
    122   margin_type_ = DEFAULT_MARGINS;
    123   min_shrink_ = 1.25;
    124   max_shrink_ = 2.;
    125   desired_dpi_ = 72;
    126   selection_only_ = false;
    127   title_ = base::string16();
    128   url_ = base::string16();
    129   display_header_footer_ = false;
    130   device_name_.clear();
    131   page_setup_device_units_.Clear();
    132   dpi_ = 0;
    133   landscape_ = false;
    134   supports_alpha_blend_ = true;
    135   should_print_backgrounds_ = false;
    136   collate_ = false;
    137   color_ = UNKNOWN_COLOR_MODEL;
    138   copies_ = 0;
    139   duplex_mode_ = UNKNOWN_DUPLEX_MODE;
    140 }
    141 
    142 void PrintSettings::SetPrinterPrintableArea(
    143     const gfx::Size& physical_size_device_units,
    144     const gfx::Rect& printable_area_device_units,
    145     bool landscape_needs_flip) {
    146   int units_per_inch = device_units_per_inch();
    147   int header_footer_text_height = 0;
    148   if (display_header_footer_) {
    149     // Hard-code text_height = 0.5cm = ~1/5 of inch.
    150     header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice,
    151                                             kPointsPerInch, units_per_inch);
    152   }
    153 
    154   PageMargins margins;
    155   switch (margin_type_) {
    156     case DEFAULT_MARGINS: {
    157       // Default margins 1.0cm = ~2/5 of an inch.
    158       int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch,
    159                                              units_per_inch);
    160       margins.header = header_footer_text_height;
    161       margins.footer = header_footer_text_height;
    162       margins.top = margin_printer_units;
    163       margins.bottom = margin_printer_units;
    164       margins.left = margin_printer_units;
    165       margins.right = margin_printer_units;
    166       break;
    167     }
    168     case NO_MARGINS:
    169     case PRINTABLE_AREA_MARGINS: {
    170       margins.header = 0;
    171       margins.footer = 0;
    172       margins.top = 0;
    173       margins.bottom = 0;
    174       margins.left = 0;
    175       margins.right = 0;
    176       break;
    177     }
    178     case CUSTOM_MARGINS: {
    179       margins.header = 0;
    180       margins.footer = 0;
    181       margins.top = ConvertUnitDouble(
    182           requested_custom_margins_in_points_.top,
    183           kPointsPerInch,
    184           units_per_inch);
    185       margins.bottom = ConvertUnitDouble(
    186           requested_custom_margins_in_points_.bottom,
    187           kPointsPerInch,
    188           units_per_inch);
    189       margins.left = ConvertUnitDouble(
    190           requested_custom_margins_in_points_.left,
    191           kPointsPerInch,
    192           units_per_inch);
    193       margins.right = ConvertUnitDouble(
    194           requested_custom_margins_in_points_.right,
    195           kPointsPerInch,
    196           units_per_inch);
    197       break;
    198     }
    199     default: {
    200       NOTREACHED();
    201     }
    202   }
    203 
    204   if (margin_type_ == DEFAULT_MARGINS || margin_type_ == PRINTABLE_AREA_MARGINS)
    205     page_setup_device_units_.SetRequestedMargins(margins);
    206   else
    207     page_setup_device_units_.ForceRequestedMargins(margins);
    208 
    209   page_setup_device_units_.Init(physical_size_device_units,
    210                                 printable_area_device_units,
    211                                 header_footer_text_height);
    212   if (landscape_ && landscape_needs_flip)
    213     page_setup_device_units_.FlipOrientation();
    214 }
    215 
    216 void PrintSettings::SetCustomMargins(
    217     const PageMargins& requested_margins_in_points) {
    218   requested_custom_margins_in_points_ = requested_margins_in_points;
    219   margin_type_ = CUSTOM_MARGINS;
    220 }
    221 
    222 int PrintSettings::NewCookie() {
    223   // A cookie of 0 is used to mark a document as unassigned, count from 1.
    224   return cookie_seq.GetNext() + 1;
    225 }
    226 
    227 void PrintSettings::SetOrientation(bool landscape) {
    228   if (landscape_ != landscape) {
    229     landscape_ = landscape;
    230     page_setup_device_units_.FlipOrientation();
    231   }
    232 }
    233 
    234 }  // namespace printing
    235