1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69

printing / nup_parameters.cc [blame]

// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/nup_parameters.h"

#include <algorithm>
#include <cmath>

#include "base/check.h"
#include "base/notreached.h"

namespace printing {

NupParameters::NupParameters(int pages_per_sheet, bool is_source_landscape) {
  SetParameters(pages_per_sheet, is_source_landscape);
}

// static
bool NupParameters::IsSupported(int pages_per_sheet) {
  // Supports N-up: 1 2 4 6 9 16

  return pages_per_sheet == 1 || pages_per_sheet == 2 || pages_per_sheet == 4 ||
         pages_per_sheet == 6 || pages_per_sheet == 9 || pages_per_sheet == 16;
}

void NupParameters::SetParameters(int pages_per_sheet,
                                  bool is_source_landscape) {
  DCHECK(IsSupported(pages_per_sheet));

  switch (pages_per_sheet) {
    case 1:
      num_pages_on_x_axis_ = 1;
      num_pages_on_y_axis_ = 1;
      break;
    case 2:
      if (!is_source_landscape) {
        num_pages_on_x_axis_ = 2;
        num_pages_on_y_axis_ = 1;
        landscape_ = true;
      } else {
        num_pages_on_x_axis_ = 1;
        num_pages_on_y_axis_ = 2;
      }
      break;
    case 6:
      if (!is_source_landscape) {
        num_pages_on_x_axis_ = 3;
        num_pages_on_y_axis_ = 2;
        landscape_ = true;
      } else {
        num_pages_on_x_axis_ = 2;
        num_pages_on_y_axis_ = 3;
      }
      break;
    case 4:
    case 9:
    case 16:
      num_pages_on_x_axis_ = std::sqrt(pages_per_sheet);
      num_pages_on_y_axis_ = std::sqrt(pages_per_sheet);
      if (is_source_landscape)
        landscape_ = true;
      break;
    default:
      NOTREACHED();
  }
}

}  // namespace printing