Movatterモバイル変換


[0]ホーム

URL:


Google Git
Sign in
chromium /chromium /src /refs/heads/main /. /pdf /document_layout.cc
blob: 315c8d14471d384d0493790c0a81f058918995b5 [file] [log] [blame]
Avi Drissmandb497b32022-09-15 19:47:28[diff] [blame]1// Copyright 2019 The Chromium Authors
K Moonbd80ce72019-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 Apthorp17c873c2019-12-02 20:27:39[diff] [blame]7#include<algorithm>
8
Hans Wennborg5078d102020-04-29 18:26:46[diff] [blame]9#include"base/check_op.h"
Lei Zhangcc4a3142024-08-26 23:57:20[diff] [blame]10#include"base/notreached.h"
Gouarb Kunduc4338a62020-07-30 21:30:48[diff] [blame]11#include"base/values.h"
Lei Zhangd3ef3f32024-06-11 16:14:56[diff] [blame]12#include"pdf/draw_utils/coordinates.h"
13#include"ui/gfx/geometry/insets.h"
Ankit Kumar 🌪️aecc9f92020-08-18 19:11:22[diff] [blame]14#include"ui/gfx/geometry/point.h"
Ankit Kumar 🌪️b8ba092f2020-08-21 20:07:45[diff] [blame]15#include"ui/gfx/geometry/rect.h"
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]16#include"ui/gfx/geometry/size.h"
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]17
18namespace chrome_pdf{
19
Jeremy Chinsene819dea2019-08-07 21:58:59[diff] [blame]20namespace{
21
K. Moone2dda8f22021-10-05 00:37:29[diff] [blame]22constexprchar kDirection[]="direction";
K Moon23d56442019-10-03 05:06:23[diff] [blame]23constexprchar kDefaultPageOrientation[]="defaultPageOrientation";
Hui Yingst28f9f5c2020-01-16 19:52:56[diff] [blame]24constexprchar kTwoUpViewEnabled[]="twoUpViewEnabled";
K Moon23d56442019-10-03 05:06:23[diff] [blame]25
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]26intGetWidestPageWidth(const std::vector<gfx::Size>& page_sizes){
Jeremy Chinsene819dea2019-08-07 21:58:59[diff] [blame]27int widest_page_width=0;
28for(constauto& page_size: page_sizes){
29 widest_page_width= std::max(widest_page_width, page_size.width());
30}
31
32return widest_page_width;
33}
34
35}// namespace
36
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]37DocumentLayout::Options::Options()=default;
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]38
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]39DocumentLayout::Options::Options(constOptions& other)=default;
40DocumentLayout::Options&DocumentLayout::Options::operator=(
41constOptions& other)=default;
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]42
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]43DocumentLayout::Options::~Options()=default;
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]44
Lei Zhang277b156c2022-04-11 22:58:51[diff] [blame]45base::Value::DictDocumentLayout::Options::ToValue()const{
46 base::Value::Dict dictionary;
47 dictionary.Set(kDirection, direction_);
48 dictionary.Set(kDefaultPageOrientation,
49static_cast<int>(default_page_orientation_));
50 dictionary.Set(kTwoUpViewEnabled, page_spread_==PageSpread::kTwoUpOdd);
K Moon23d56442019-10-03 05:06:23[diff] [blame]51return dictionary;
52}
53
Lei Zhang277b156c2022-04-11 22:58:51[diff] [blame]54voidDocumentLayout::Options::FromValue(const base::Value::Dict& value){
55int32_t direction= value.FindInt(kDirection).value();
K. Moone2dda8f22021-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 Moon23d56442019-10-03 05:06:23[diff] [blame]60int32_t default_page_orientation=
Lei Zhang277b156c2022-04-11 22:58:51[diff] [blame]61 value.FindInt(kDefaultPageOrientation).value();
K Moon23d56442019-10-03 05:06:23[diff] [blame]62 DCHECK_GE(default_page_orientation,
63static_cast<int32_t>(PageOrientation::kOriginal));
64 DCHECK_LE(default_page_orientation,
65static_cast<int32_t>(PageOrientation::kLast));
66 default_page_orientation_=
67static_cast<PageOrientation>(default_page_orientation);
Hui Yingst28f9f5c2020-01-16 19:52:56[diff] [blame]68
Lei Zhang277b156c2022-04-11 22:58:51[diff] [blame]69 page_spread_= value.FindBool(kTwoUpViewEnabled).value()
Daniel Hosseinianbe882062021-04-22 01:22:39[diff] [blame]70?PageSpread::kTwoUpOdd
71:PageSpread::kOneUp;
K Moon23d56442019-10-03 05:06:23[diff] [blame]72}
73
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]74voidDocumentLayout::Options::RotatePagesClockwise(){
K Moon9a62bf42019-08-07 20:05:36[diff] [blame]75 default_page_orientation_=RotateClockwise(default_page_orientation_);
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]76}
77
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]78voidDocumentLayout::Options::RotatePagesCounterclockwise(){
K Moon9a62bf42019-08-07 20:05:36[diff] [blame]79 default_page_orientation_=RotateCounterclockwise(default_page_orientation_);
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]80}
81
K Mooneb9e0002019-08-06 19:25:32[diff] [blame]82DocumentLayout::DocumentLayout()=default;
83
84DocumentLayout::~DocumentLayout()=default;
85
K Moon6d326b92019-09-19 22:42:07[diff] [blame]86voidDocumentLayout::SetOptions(constOptions& options){
Hui Yingst28f9f5c2020-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 Moon6d326b92019-09-19 22:42:07[diff] [blame]90//
Hui Yingst28f9f5c2020-01-16 19:52:56[diff] [blame]91// We also probably don't want layout changes to actually kick in until
K Moon6d326b92019-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 Yingst28f9f5c2020-01-16 19:52:56[diff] [blame]94if(options_!= options){
K Moon6d326b92019-09-19 22:42:07[diff] [blame]95 dirty_=true;
96}
97 options_= options;
98}
99
Daniel Hosseinianbcbedda2021-04-23 19:09:51[diff] [blame]100voidDocumentLayout::ComputeLayout(const std::vector<gfx::Size>& page_sizes){
101switch(options_.page_spread()){
102casePageSpread::kOneUp:
103returnComputeOneUpLayout(page_sizes);
104casePageSpread::kTwoUpOdd:
105returnComputeTwoUpOddLayout(page_sizes);
106}
Lei Zhangcc4a3142024-08-26 23:57:20[diff] [blame]107 NOTREACHED();
Daniel Hosseinianbcbedda2021-04-23 19:09:51[diff] [blame]108}
109
110voidDocumentLayout::ComputeOneUpLayout(
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]111const std::vector<gfx::Size>& page_sizes){
112 gfx::Size document_size(GetWidestPageWidth(page_sizes),0);
Jeremy Chinsen08beb482019-08-07 01:58:54[diff] [blame]113
K Moon6d326b92019-09-19 22:42:07[diff] [blame]114if(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 Chinsen08beb482019-08-07 01:58:54[diff] [blame]120for(size_t i=0; i< page_sizes.size();++i){
121if(i!=0){
122// Add space for bottom separator.
K Moon6d326b92019-09-19 22:42:07[diff] [blame]123 document_size.Enlarge(0, kBottomSeparator);
Jeremy Chinsen08beb482019-08-07 01:58:54[diff] [blame]124}
125
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]126const gfx::Size& page_size= page_sizes[i];
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]127 gfx::Rect page_rect=
128 draw_utils::GetRectForSingleView(page_size, document_size);
129CopyRectIfModified(page_rect, page_layouts_[i].outer_rect);
Lei Zhang65a96c692024-06-11 17:15:37[diff] [blame]130 page_rect.Inset(kSingleViewInsets);
131CopyRectIfModified(page_rect, page_layouts_[i].inner_rect);
K Moone4bd7522019-08-23 00:12:56[diff] [blame]132
K Moon6d326b92019-09-19 22:42:07[diff] [blame]133 draw_utils::ExpandDocumentSize(page_size,&document_size);
134}
135
136if(size_!= document_size){
137 size_= document_size;
138 dirty_=true;
Jeremy Chinsen08beb482019-08-07 01:58:54[diff] [blame]139}
Jeremy Chinsen08beb482019-08-07 01:58:54[diff] [blame]140}
141
Daniel Hosseinianbcbedda2021-04-23 19:09:51[diff] [blame]142voidDocumentLayout::ComputeTwoUpOddLayout(
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]143const std::vector<gfx::Size>& page_sizes){
144 gfx::Size document_size(GetWidestPageWidth(page_sizes),0);
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]145
K Moon6d326b92019-09-19 22:42:07[diff] [blame]146if(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 Chinsen4a65aad2019-08-07 00:14:33[diff] [blame]152for(size_t i=0; i< page_sizes.size();++i){
Lei Zhangd3ef3f32024-06-11 16:14:56[diff] [blame]153 gfx::Insets page_insets= draw_utils::GetPageInsetsForTwoUpView(
154 i, page_sizes.size(), kSingleViewInsets, kHorizontalSeparator);
Ankit Kumar 🌪️e35101152020-07-30 09:57:59[diff] [blame]155const gfx::Size& page_size= page_sizes[i];
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]156
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]157 gfx::Rect page_rect;
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]158if(i%2==0){
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]159 page_rect= draw_utils::GetLeftRectForTwoUpView(
160 page_size,{document_size.width(), document_size.height()});
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]161}else{
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]162 page_rect= draw_utils::GetRightRectForTwoUpView(
163 page_size,{document_size.width(), document_size.height()});
K Moon6d326b92019-09-19 22:42:07[diff] [blame]164 document_size.Enlarge(
1650, std::max(page_size.height(), page_sizes[i-1].height()));
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]166}
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]167CopyRectIfModified(page_rect, page_layouts_[i].outer_rect);
Lei Zhang65a96c692024-06-11 17:15:37[diff] [blame]168 page_rect.Inset(page_insets);
169CopyRectIfModified(page_rect, page_layouts_[i].inner_rect);
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]170}
171
Jeremy Chinsen4a65aad2019-08-07 00:14:33[diff] [blame]172if(page_sizes.size()%2==1){
K Moon6d326b92019-09-19 22:42:07[diff] [blame]173 document_size.Enlarge(0, page_sizes.back().height());
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]174}
175
K Moon6d326b92019-09-19 22:42:07[diff] [blame]176 document_size.set_width(2* document_size.width());
177
178if(size_!= document_size){
179 size_= document_size;
180 dirty_=true;
181}
182}
183
Ankit Kumar 🌪️00798692020-08-26 22:57:14[diff] [blame]184voidDocumentLayout::CopyRectIfModified(const gfx::Rect& source_rect,
185 gfx::Rect& destination_rect){
186if(destination_rect!= source_rect){
187 destination_rect= source_rect;
K Moon6d326b92019-09-19 22:42:07[diff] [blame]188 dirty_=true;
189}
Jeremy Chinsend6fd27ce2019-08-06 00:40:17[diff] [blame]190}
191
K Moonbd80ce72019-07-26 19:27:50[diff] [blame]192}// namespace chrome_pdf

[8]ページ先頭

©2009-2025 Movatter.jp