Avi Drissman | db497b3 | 2022-09-15 19:47:28 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 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/document_layout.h" |
| 6 | |
Jeremy Apthorp | 17c873c | 2019-12-02 20:27:39 | [diff] [blame] | 7 | #include<algorithm> |
| 8 | |
Hans Wennborg | 5078d10 | 2020-04-29 18:26:46 | [diff] [blame] | 9 | #include"base/check_op.h" |
Lei Zhang | cc4a314 | 2024-08-26 23:57:20 | [diff] [blame] | 10 | #include"base/notreached.h" |
Gouarb Kundu | c4338a6 | 2020-07-30 21:30:48 | [diff] [blame] | 11 | #include"base/values.h" |
Lei Zhang | d3ef3f3 | 2024-06-11 16:14:56 | [diff] [blame] | 12 | #include"pdf/draw_utils/coordinates.h" |
| 13 | #include"ui/gfx/geometry/insets.h" |
Ankit Kumar 🌪️ | aecc9f9 | 2020-08-18 19:11:22 | [diff] [blame] | 14 | #include"ui/gfx/geometry/point.h" |
Ankit Kumar 🌪️ | b8ba092f | 2020-08-21 20:07:45 | [diff] [blame] | 15 | #include"ui/gfx/geometry/rect.h" |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 16 | #include"ui/gfx/geometry/size.h" |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 17 | |
| 18 | namespace chrome_pdf{ |
| 19 | |
Jeremy Chinsen | e819dea | 2019-08-07 21:58:59 | [diff] [blame] | 20 | namespace{ |
| 21 | |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame] | 22 | constexprchar kDirection[]="direction"; |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 23 | constexprchar kDefaultPageOrientation[]="defaultPageOrientation"; |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 24 | constexprchar kTwoUpViewEnabled[]="twoUpViewEnabled"; |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 25 | |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 26 | intGetWidestPageWidth(const std::vector<gfx::Size>& page_sizes){ |
Jeremy Chinsen | e819dea | 2019-08-07 21:58:59 | [diff] [blame] | 27 | int widest_page_width=0; |
| 28 | for(constauto& page_size: page_sizes){ |
| 29 | widest_page_width= std::max(widest_page_width, page_size.width()); |
| 30 | } |
| 31 | |
| 32 | return widest_page_width; |
| 33 | } |
| 34 | |
| 35 | }// namespace |
| 36 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 37 | DocumentLayout::Options::Options()=default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 38 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 39 | DocumentLayout::Options::Options(constOptions& other)=default; |
| 40 | DocumentLayout::Options&DocumentLayout::Options::operator=( |
| 41 | constOptions& other)=default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 42 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 43 | DocumentLayout::Options::~Options()=default; |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 44 | |
Lei Zhang | 277b156c | 2022-04-11 22:58:51 | [diff] [blame] | 45 | base::Value::DictDocumentLayout::Options::ToValue()const{ |
| 46 | base::Value::Dict dictionary; |
| 47 | dictionary.Set(kDirection, direction_); |
| 48 | dictionary.Set(kDefaultPageOrientation, |
| 49 | static_cast<int>(default_page_orientation_)); |
| 50 | dictionary.Set(kTwoUpViewEnabled, page_spread_==PageSpread::kTwoUpOdd); |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 51 | return dictionary; |
| 52 | } |
| 53 | |
Lei Zhang | 277b156c | 2022-04-11 22:58:51 | [diff] [blame] | 54 | voidDocumentLayout::Options::FromValue(const base::Value::Dict& value){ |
| 55 | int32_t direction= value.FindInt(kDirection).value(); |
K. Moon | e2dda8f2 | 2021-10-05 00:37:29 | [diff] [blame] | 56 | DCHECK_GE(direction, base::i18n::UNKNOWN_DIRECTION); |
| 57 | DCHECK_LE(direction, base::i18n::TEXT_DIRECTION_MAX); |
| 58 | direction_=static_cast<base::i18n::TextDirection>(direction); |
| 59 | |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 60 | int32_t default_page_orientation= |
Lei Zhang | 277b156c | 2022-04-11 22:58:51 | [diff] [blame] | 61 | value.FindInt(kDefaultPageOrientation).value(); |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 62 | DCHECK_GE(default_page_orientation, |
| 63 | static_cast<int32_t>(PageOrientation::kOriginal)); |
| 64 | DCHECK_LE(default_page_orientation, |
| 65 | static_cast<int32_t>(PageOrientation::kLast)); |
| 66 | default_page_orientation_= |
| 67 | static_cast<PageOrientation>(default_page_orientation); |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 68 | |
Lei Zhang | 277b156c | 2022-04-11 22:58:51 | [diff] [blame] | 69 | page_spread_= value.FindBool(kTwoUpViewEnabled).value() |
Daniel Hosseinian | be88206 | 2021-04-22 01:22:39 | [diff] [blame] | 70 | ?PageSpread::kTwoUpOdd |
| 71 | :PageSpread::kOneUp; |
K Moon | 23d5644 | 2019-10-03 05:06:23 | [diff] [blame] | 72 | } |
| 73 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 74 | voidDocumentLayout::Options::RotatePagesClockwise(){ |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 75 | default_page_orientation_=RotateClockwise(default_page_orientation_); |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 76 | } |
| 77 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 78 | voidDocumentLayout::Options::RotatePagesCounterclockwise(){ |
K Moon | 9a62bf4 | 2019-08-07 20:05:36 | [diff] [blame] | 79 | default_page_orientation_=RotateCounterclockwise(default_page_orientation_); |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 80 | } |
| 81 | |
K Moon | eb9e000 | 2019-08-06 19:25:32 | [diff] [blame] | 82 | DocumentLayout::DocumentLayout()=default; |
| 83 | |
| 84 | DocumentLayout::~DocumentLayout()=default; |
| 85 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 86 | voidDocumentLayout::SetOptions(constOptions& options){ |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 87 | // To be conservative, we want to consider the layout dirty for any layout |
| 88 | // option changes, even if the page rects don't necessarily change when |
| 89 | // layout options change. |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 90 | // |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 91 | // We also probably don't want layout changes to actually kick in until |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 92 | // the next call to ComputeLayout(). (In practice, we'll call ComputeLayout() |
| 93 | // shortly after calling SetOptions().) |
Hui Yingst | 28f9f5c | 2020-01-16 19:52:56 | [diff] [blame] | 94 | if(options_!= options){ |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 95 | dirty_=true; |
| 96 | } |
| 97 | options_= options; |
| 98 | } |
| 99 | |
Daniel Hosseinian | bcbedda | 2021-04-23 19:09:51 | [diff] [blame] | 100 | voidDocumentLayout::ComputeLayout(const std::vector<gfx::Size>& page_sizes){ |
| 101 | switch(options_.page_spread()){ |
| 102 | casePageSpread::kOneUp: |
| 103 | returnComputeOneUpLayout(page_sizes); |
| 104 | casePageSpread::kTwoUpOdd: |
| 105 | returnComputeTwoUpOddLayout(page_sizes); |
| 106 | } |
Lei Zhang | cc4a314 | 2024-08-26 23:57:20 | [diff] [blame] | 107 | NOTREACHED(); |
Daniel Hosseinian | bcbedda | 2021-04-23 19:09:51 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | voidDocumentLayout::ComputeOneUpLayout( |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 111 | const std::vector<gfx::Size>& page_sizes){ |
| 112 | gfx::Size document_size(GetWidestPageWidth(page_sizes),0); |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 113 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 114 | if(page_layouts_.size()!= page_sizes.size()){ |
| 115 | // TODO(kmoon): May want to do less work when shrinking a layout. |
| 116 | page_layouts_.resize(page_sizes.size()); |
| 117 | dirty_=true; |
| 118 | } |
| 119 | |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 120 | for(size_t i=0; i< page_sizes.size();++i){ |
| 121 | if(i!=0){ |
| 122 | // Add space for bottom separator. |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 123 | document_size.Enlarge(0, kBottomSeparator); |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 124 | } |
| 125 | |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 126 | const gfx::Size& page_size= page_sizes[i]; |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 127 | gfx::Rect page_rect= |
| 128 | draw_utils::GetRectForSingleView(page_size, document_size); |
| 129 | CopyRectIfModified(page_rect, page_layouts_[i].outer_rect); |
Lei Zhang | 65a96c69 | 2024-06-11 17:15:37 | [diff] [blame] | 130 | page_rect.Inset(kSingleViewInsets); |
| 131 | CopyRectIfModified(page_rect, page_layouts_[i].inner_rect); |
K Moon | e4bd752 | 2019-08-23 00:12:56 | [diff] [blame] | 132 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 133 | draw_utils::ExpandDocumentSize(page_size,&document_size); |
| 134 | } |
| 135 | |
| 136 | if(size_!= document_size){ |
| 137 | size_= document_size; |
| 138 | dirty_=true; |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 139 | } |
Jeremy Chinsen | 08beb48 | 2019-08-07 01:58:54 | [diff] [blame] | 140 | } |
| 141 | |
Daniel Hosseinian | bcbedda | 2021-04-23 19:09:51 | [diff] [blame] | 142 | voidDocumentLayout::ComputeTwoUpOddLayout( |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 143 | const std::vector<gfx::Size>& page_sizes){ |
| 144 | gfx::Size document_size(GetWidestPageWidth(page_sizes),0); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 145 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 146 | if(page_layouts_.size()!= page_sizes.size()){ |
| 147 | // TODO(kmoon): May want to do less work when shrinking a layout. |
| 148 | page_layouts_.resize(page_sizes.size()); |
| 149 | dirty_=true; |
| 150 | } |
| 151 | |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 152 | for(size_t i=0; i< page_sizes.size();++i){ |
Lei Zhang | d3ef3f3 | 2024-06-11 16:14:56 | [diff] [blame] | 153 | gfx::Insets page_insets= draw_utils::GetPageInsetsForTwoUpView( |
| 154 | i, page_sizes.size(), kSingleViewInsets, kHorizontalSeparator); |
Ankit Kumar 🌪️ | e3510115 | 2020-07-30 09:57:59 | [diff] [blame] | 155 | const gfx::Size& page_size= page_sizes[i]; |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 156 | |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 157 | gfx::Rect page_rect; |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 158 | if(i%2==0){ |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 159 | page_rect= draw_utils::GetLeftRectForTwoUpView( |
| 160 | page_size,{document_size.width(), document_size.height()}); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 161 | }else{ |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 162 | page_rect= draw_utils::GetRightRectForTwoUpView( |
| 163 | page_size,{document_size.width(), document_size.height()}); |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 164 | document_size.Enlarge( |
| 165 | 0, std::max(page_size.height(), page_sizes[i-1].height())); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 166 | } |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 167 | CopyRectIfModified(page_rect, page_layouts_[i].outer_rect); |
Lei Zhang | 65a96c69 | 2024-06-11 17:15:37 | [diff] [blame] | 168 | page_rect.Inset(page_insets); |
| 169 | CopyRectIfModified(page_rect, page_layouts_[i].inner_rect); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 170 | } |
| 171 | |
Jeremy Chinsen | 4a65aad | 2019-08-07 00:14:33 | [diff] [blame] | 172 | if(page_sizes.size()%2==1){ |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 173 | document_size.Enlarge(0, page_sizes.back().height()); |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 174 | } |
| 175 | |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 176 | document_size.set_width(2* document_size.width()); |
| 177 | |
| 178 | if(size_!= document_size){ |
| 179 | size_= document_size; |
| 180 | dirty_=true; |
| 181 | } |
| 182 | } |
| 183 | |
Ankit Kumar 🌪️ | 0079869 | 2020-08-26 22:57:14 | [diff] [blame] | 184 | voidDocumentLayout::CopyRectIfModified(const gfx::Rect& source_rect, |
| 185 | gfx::Rect& destination_rect){ |
| 186 | if(destination_rect!= source_rect){ |
| 187 | destination_rect= source_rect; |
K Moon | 6d326b9 | 2019-09-19 22:42:07 | [diff] [blame] | 188 | dirty_=true; |
| 189 | } |
Jeremy Chinsen | d6fd27ce | 2019-08-06 00:40:17 | [diff] [blame] | 190 | } |
| 191 | |
K Moon | bd80ce7 | 2019-07-26 19:27:50 | [diff] [blame] | 192 | }// namespace chrome_pdf |