File: page_number.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (93 lines) | stat: -rw-r--r-- 2,819 bytes parent folder | download | duplicates (11)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2006-2008 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/page_number.h"

#include <algorithm>
#include <limits>

#include "base/check_op.h"
#include "printing/print_job_constants.h"

namespace printing {

PageNumber::PageNumber(const PageRanges& ranges, uint32_t document_page_count) {
  Init(ranges, document_page_count);
}

PageNumber::PageNumber()
    : ranges_(nullptr),
      page_number_(kInvalidPageIndex),
      page_range_index_(kInvalidPageIndex),
      document_page_count_(0) {}

PageNumber::PageNumber(const PageNumber& other) = default;

PageNumber& PageNumber::operator=(const PageNumber& other) = default;

void PageNumber::Init(const PageRanges& ranges, uint32_t document_page_count) {
  DCHECK(document_page_count);
  ranges_ = ranges.empty() ? nullptr : &ranges;
  document_page_count_ = document_page_count;
  if (ranges_) {
    uint32_t first_page = (*ranges_)[0].from;
    if (first_page < document_page_count) {
      page_range_index_ = 0;
      page_number_ = (*ranges_)[0].from;
    } else {
      page_range_index_ = kInvalidPageIndex;
      page_number_ = kInvalidPageIndex;
    }
  } else {
    page_range_index_ = kInvalidPageIndex;
    page_number_ = 0;
  }
}

uint32_t PageNumber::operator++() {
  ++page_number_;
  if (page_number_ >= document_page_count_) {
    // Finished.
    *this = npos();
  } else if (ranges_ && page_number_ > (*ranges_)[page_range_index_].to) {
    DCHECK_LE(ranges_->size(),
              static_cast<size_t>(std::numeric_limits<int>::max()));
    if (++page_range_index_ == ranges_->size()) {
      // Finished.
      *this = npos();
    } else {
      page_number_ = (*ranges_)[page_range_index_].from;
      if (page_number_ >= document_page_count_) {
        // Finished.
        *this = npos();
      }
    }
  }
  return ToUint();
}

bool PageNumber::operator==(const PageNumber& other) const {
  return page_number_ == other.page_number_ &&
         page_range_index_ == other.page_range_index_;
}
bool PageNumber::operator!=(const PageNumber& other) const {
  return page_number_ != other.page_number_ ||
         page_range_index_ != other.page_range_index_;
}

// static
std::vector<uint32_t> PageNumber::GetPages(PageRanges ranges,
                                           uint32_t page_count) {
  PageRange::Normalize(ranges);
  std::vector<uint32_t> printed_pages;
  static constexpr uint32_t kMaxNumberOfPages = 100000;
  printed_pages.reserve(std::min(page_count, kMaxNumberOfPages));
  for (PageNumber page_number(ranges, std::min(page_count, kMaxNumberOfPages));
       page_number != PageNumber::npos(); ++page_number) {
    printed_pages.push_back(page_number.ToUint());
  }
  return printed_pages;
}

}  // namespace printing