page_number.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2011 The Chromium Authors
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef PRINTING_PAGE_NUMBER_H_
  5. #define PRINTING_PAGE_NUMBER_H_
  6. #include <ostream>
  7. #include <vector>
  8. #include "base/check_op.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "printing/page_range.h"
  11. namespace printing {
  12. // Represents a page series using the array of page ranges. Pages are assumed
  13. // to be 0-indexed.
  14. class COMPONENT_EXPORT(PRINTING) PageNumber {
  15. public:
  16. // Initializes the page to the first page in the ranges or 0.
  17. PageNumber(const PageRanges& ranges, uint32_t document_page_count);
  18. PageNumber();
  19. PageNumber(const PageNumber& other);
  20. PageNumber& operator=(const PageNumber& other);
  21. // Initializes the page to the first page in the ranges or 0.
  22. // Initializes to npos if the ranges is empty and document_page_count is 0.
  23. void Init(const PageRanges& ranges, uint32_t document_page_count);
  24. // Converts to a page numbers.
  25. uint32_t ToUint() const {
  26. DCHECK(*this == npos() || page_number_ < document_page_count_);
  27. return page_number_;
  28. }
  29. // Calculates the next page in the series. Sets this PageNumber to
  30. // PageNumber::npos() if we reach document_page_count_.
  31. uint32_t operator++();
  32. // Returns an instance that represents the end of a series.
  33. static const PageNumber npos() { return PageNumber(); }
  34. // Equality operator. Only the current page number is verified so that
  35. // "page != PageNumber::npos()" works.
  36. bool operator==(const PageNumber& other) const;
  37. bool operator!=(const PageNumber& other) const;
  38. // Returns all pages represented by the given PageRanges up to and including
  39. // page document_page_count - 1.
  40. static std::vector<uint32_t> GetPages(PageRanges ranges,
  41. uint32_t document_page_count);
  42. private:
  43. // The page range to follow.
  44. raw_ptr<const PageRanges> ranges_;
  45. // The next page to be printed. `kInvalidPageIndex` when not printing.
  46. uint32_t page_number_;
  47. // The next page to be printed. `kInvalidPageIndex` when not used. Valid only
  48. // if document()->settings().range.empty() is false.
  49. uint32_t page_range_index_;
  50. // Total number of pages in the underlying document, including outside of the
  51. // specified ranges.
  52. uint32_t document_page_count_;
  53. };
  54. // Debug output support.
  55. template <class E, class T>
  56. inline typename std::basic_ostream<E, T>& operator<<(
  57. typename std::basic_ostream<E, T>& ss,
  58. const PageNumber& page) {
  59. return ss << page.ToUint();
  60. }
  61. } // namespace printing
  62. #endif // PRINTING_PAGE_NUMBER_H_