blob: f0097f734232dd9728728fde2531f543ef48dace [file] [log] [blame]
thestig64c8e262015-10-28 19:04:261// Copyright 2015 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
Lei Zhangcc9acc12018-03-05 20:49:135#include "pdf/pdf_transform.h"
thestig64c8e262015-10-28 19:04:266
7#include <algorithm>
Lei Zhangcc9acc12018-03-05 20:49:138#include <utility>
thestig64c8e262015-10-28 19:04:269
Hans Wennborg5078d102020-04-29 18:26:4610#include "base/notreached.h"
Lei Zhang63443b202019-12-10 23:59:3511#include "ui/gfx/geometry/point_f.h"
thestig64c8e262015-10-28 19:04:2612#include "ui/gfx/geometry/rect.h"
Lei Zhang63443b202019-12-10 23:59:3513#include "ui/gfx/geometry/size_f.h"
thestig64c8e262015-10-28 19:04:2614
Lei Zhangcc9acc12018-03-05 20:49:1315namespace chrome_pdf {
thestig64c8e262015-10-28 19:04:2616
thestigb923f182016-04-05 00:23:3317namespace {
18
thestig09649fa2016-04-13 03:26:4919// When a PdfRectangle has top < bottom, or right < left, the values should be
thestigb923f182016-04-05 00:23:3320// swapped.
thestig09649fa2016-04-13 03:26:4921void SwapPdfRectangleValuesIfNeeded(PdfRectangle* rect) {
22 if (rect->top < rect->bottom)
23 std::swap(rect->top, rect->bottom);
24 if (rect->right < rect->left)
25 std::swap(rect->right, rect->left);
thestigb923f182016-04-05 00:23:3326}
27
28} // namespace
29
Lei Zhang63443b202019-12-10 23:59:3530float CalculateScaleFactor(const gfx::Rect& content_rect,
31 const gfx::SizeF& src_size,
32 bool rotated) {
33 if (src_size.IsEmpty())
34 return 1.0f;
thestig64c8e262015-10-28 19:04:2635
Lei Zhang63443b202019-12-10 23:59:3536 float actual_source_page_width =
37 rotated ? src_size.height() : src_size.width();
38 float actual_source_page_height =
39 rotated ? src_size.width() : src_size.height();
40 float ratio_x = content_rect.width() / actual_source_page_width;
41 float ratio_y = content_rect.height() / actual_source_page_height;
thestig64c8e262015-10-28 19:04:2642 return std::min(ratio_x, ratio_y);
43}
44
thestig09649fa2016-04-13 03:26:4945void SetDefaultClipBox(bool rotated, PdfRectangle* clip_box) {
Henrique Nakashima1c5da8e2018-10-05 21:56:4346 constexpr int kDpi = 72;
47 constexpr float kPaperWidth = 8.5 * kDpi;
48 constexpr float kPaperHeight = 11 * kDpi;
thestig64c8e262015-10-28 19:04:2649 clip_box->left = 0;
50 clip_box->bottom = 0;
51 clip_box->right = rotated ? kPaperHeight : kPaperWidth;
52 clip_box->top = rotated ? kPaperWidth : kPaperHeight;
53}
54
55void CalculateMediaBoxAndCropBox(bool rotated,
56 bool has_media_box,
57 bool has_crop_box,
thestig09649fa2016-04-13 03:26:4958 PdfRectangle* media_box,
59 PdfRectangle* crop_box) {
thestigb923f182016-04-05 00:23:3360 if (has_media_box)
thestig09649fa2016-04-13 03:26:4961 SwapPdfRectangleValuesIfNeeded(media_box);
thestigb923f182016-04-05 00:23:3362 if (has_crop_box)
thestig09649fa2016-04-13 03:26:4963 SwapPdfRectangleValuesIfNeeded(crop_box);
thestigb923f182016-04-05 00:23:3364
thestig64c8e262015-10-28 19:04:2665 if (!has_media_box && !has_crop_box) {
66 SetDefaultClipBox(rotated, crop_box);
67 SetDefaultClipBox(rotated, media_box);
68 } else if (has_crop_box && !has_media_box) {
69 *media_box = *crop_box;
70 } else if (has_media_box && !has_crop_box) {
71 *crop_box = *media_box;
72 }
73}
74
thestig09649fa2016-04-13 03:26:4975PdfRectangle CalculateClipBoxBoundary(const PdfRectangle& media_box,
76 const PdfRectangle& crop_box) {
77 PdfRectangle clip_box;
thestig64c8e262015-10-28 19:04:2678
Daniel Hosseiniane257d962021-04-23 21:18:3579 // Clip `media_box` to the size of `crop_box`, but ignore `crop_box` if it is
80 // bigger than `media_box`.
krb8f981ab2016-03-14 14:37:4481 clip_box.left = std::max(crop_box.left, media_box.left);
thestig09649fa2016-04-13 03:26:4982 clip_box.bottom = std::max(crop_box.bottom, media_box.bottom);
krb8f981ab2016-03-14 14:37:4483 clip_box.right = std::min(crop_box.right, media_box.right);
84 clip_box.top = std::min(crop_box.top, media_box.top);
thestig64c8e262015-10-28 19:04:2685 return clip_box;
86}
87
Lei Zhang63443b202019-12-10 23:59:3588void ScalePdfRectangle(float scale_factor, PdfRectangle* rect) {
thestig09649fa2016-04-13 03:26:4989 rect->left *= scale_factor;
90 rect->bottom *= scale_factor;
91 rect->right *= scale_factor;
92 rect->top *= scale_factor;
thestig64c8e262015-10-28 19:04:2693}
94
Lei Zhang63443b202019-12-10 23:59:3595gfx::PointF CalculateScaledClipBoxOffset(const gfx::Rect& content_rect,
96 const PdfRectangle& source_clip_box) {
thestig64c8e262015-10-28 19:04:2697 const float clip_box_width = source_clip_box.right - source_clip_box.left;
98 const float clip_box_height = source_clip_box.top - source_clip_box.bottom;
99
100 // Center the intended clip region to real clip region.
Lei Zhang63443b202019-12-10 23:59:35101 return gfx::PointF((content_rect.width() - clip_box_width) / 2 +
102 content_rect.x() - source_clip_box.left,
103 (content_rect.height() - clip_box_height) / 2 +
104 content_rect.y() - source_clip_box.bottom);
thestig64c8e262015-10-28 19:04:26105}
106
Lei Zhang63443b202019-12-10 23:59:35107gfx::PointF CalculateNonScaledClipBoxOffset(
108 int rotation,
109 int page_width,
110 int page_height,
111 const PdfRectangle& source_clip_box) {
thestig64c8e262015-10-28 19:04:26112 // Align the intended clip region to left-top corner of real clip region.
113 switch (rotation) {
114 case 0:
Lei Zhang63443b202019-12-10 23:59:35115 return gfx::PointF(-1 * source_clip_box.left,
116 page_height - source_clip_box.top);
thestig64c8e262015-10-28 19:04:26117 case 1:
Lei Zhang63443b202019-12-10 23:59:35118 return gfx::PointF(0, -1 * source_clip_box.bottom);
thestig64c8e262015-10-28 19:04:26119 case 2:
Lei Zhang63443b202019-12-10 23:59:35120 return gfx::PointF(page_width - source_clip_box.right, 0);
thestig64c8e262015-10-28 19:04:26121 case 3:
Lei Zhang63443b202019-12-10 23:59:35122 return gfx::PointF(page_height - source_clip_box.right,
123 page_width - source_clip_box.top);
thestig64c8e262015-10-28 19:04:26124 default:
125 NOTREACHED();
Lei Zhang63443b202019-12-10 23:59:35126 return gfx::PointF();
thestig64c8e262015-10-28 19:04:26127 }
128}
129
Lei Zhangcc9acc12018-03-05 20:49:13130} // namespace chrome_pdf