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
printing / page_number.cc [blame]
// 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