-// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// 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 <stddef.h>
-
+#include <algorithm>
#include <limits>
-#include "base/check.h"
-#include "printing/print_settings.h"
+#include "base/check_op.h"
+#include "printing/print_job_constants.h"
namespace printing {
-PageNumber::PageNumber(const PrintSettings& settings,
- uint32_t document_page_count) {
- Init(settings, document_page_count);
+PageNumber::PageNumber(const PageRanges& ranges, uint32_t document_page_count) {
+ Init(ranges, document_page_count);
}
PageNumber::PageNumber()
PageNumber& PageNumber::operator=(const PageNumber& other) = default;
-void PageNumber::Init(const PrintSettings& settings,
- uint32_t document_page_count) {
+void PageNumber::Init(const PageRanges& ranges, uint32_t document_page_count) {
DCHECK(document_page_count);
- ranges_ = settings.ranges().empty() ? NULL : &settings.ranges();
+ ranges_ = ranges.empty() ? nullptr : &ranges;
document_page_count_ = document_page_count;
if (ranges_) {
- page_range_index_ = 0;
- page_number_ = (*ranges_)[0].from;
- } else {
- if (document_page_count) {
- page_number_ = 0;
+ 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;
}
}
-int PageNumber::operator++() {
- if (!ranges_) {
- // Switch to next page.
- if (++page_number_ >= document_page_count_) {
+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 {
- // Switch to next page.
- ++page_number_;
- // Page ranges are inclusive.
- if (page_number_ > (*ranges_)[page_range_index_].to) {
- DCHECK(ranges_->size() <=
- static_cast<size_t>(std::numeric_limits<int>::max()));
- if (++page_range_index_ == ranges_->size()) {
+ } else {
+ page_number_ = (*ranges_)[page_range_index_].from;
+ if (page_number_ >= document_page_count_) {
// Finished.
*this = npos();
- } else {
- page_number_ = (*ranges_)[page_range_index_].from;
}
}
}
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