1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "pdf/page_orientation.h"
6 
7 #include <type_traits>
8 
9 namespace chrome_pdf {
10 
11 namespace {
12 
13 // Adds two PageOrientation values together. This works because the underlying
14 // integer values have been chosen to allow modular arithmetic.
AddOrientations(PageOrientation first,PageOrientation second)15 PageOrientation AddOrientations(PageOrientation first, PageOrientation second) {
16   using IntType = std::underlying_type<PageOrientation>::type;
17 
18   constexpr auto kOrientationCount =
19       static_cast<IntType>(PageOrientation::kLast) + 1;
20 
21   auto first_int = static_cast<IntType>(first);
22   auto second_int = static_cast<IntType>(second);
23   return static_cast<PageOrientation>((first_int + second_int) %
24                                       kOrientationCount);
25 }
26 
27 }  // namespace
28 
RotateClockwise(PageOrientation orientation)29 PageOrientation RotateClockwise(PageOrientation orientation) {
30   return AddOrientations(orientation, PageOrientation::kClockwise90);
31 }
32 
RotateCounterclockwise(PageOrientation orientation)33 PageOrientation RotateCounterclockwise(PageOrientation orientation) {
34   // Adding |kLast| is equivalent to rotating one step counterclockwise.
35   return AddOrientations(orientation, PageOrientation::kLast);
36 }
37 
38 }  // namespace chrome_pdf
39