1 // Copyright 2015 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.
5 #include "pdf/pdf_transform.h"
10 #include "base/notreached.h"
11 #include "ui/gfx/geometry/point_f.h"
12 #include "ui/gfx/geometry/rect.h"
13 #include "ui/gfx/geometry/size_f.h"
15 namespace chrome_pdf {
19 // When a PdfRectangle has top < bottom, or right < left, the values should be
21 void 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);
30 float CalculateScaleFactor(const gfx::Rect& content_rect,
31 const gfx::SizeF& src_size,
33 if (src_size.IsEmpty())
36 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;
42 return std::min(ratio_x, ratio_y);
45 void SetDefaultClipBox(bool rotated, PdfRectangle* clip_box) {
46 constexpr int kDpi = 72;
47 constexpr float kPaperWidth = 8.5 * kDpi;
48 constexpr float kPaperHeight = 11 * kDpi;
51 clip_box->right = rotated ? kPaperHeight : kPaperWidth;
52 clip_box->top = rotated ? kPaperWidth : kPaperHeight;
55 void CalculateMediaBoxAndCropBox(bool rotated,
58 PdfRectangle* media_box,
59 PdfRectangle* crop_box) {
61 SwapPdfRectangleValuesIfNeeded(media_box);
63 SwapPdfRectangleValuesIfNeeded(crop_box);
65 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;
75 PdfRectangle CalculateClipBoxBoundary(const PdfRectangle& media_box,
76 const PdfRectangle& crop_box) {
77 PdfRectangle clip_box;
79 // Clip `media_box` to the size of `crop_box`, but ignore `crop_box` if it is
80 // bigger than `media_box`.
81 clip_box.left = std::max(crop_box.left, media_box.left);
82 clip_box.bottom = std::max(crop_box.bottom, media_box.bottom);
83 clip_box.right = std::min(crop_box.right, media_box.right);
84 clip_box.top = std::min(crop_box.top, media_box.top);
88 void ScalePdfRectangle(float scale_factor, PdfRectangle* rect) {
89 rect->left *= scale_factor;
90 rect->bottom *= scale_factor;
91 rect->right *= scale_factor;
92 rect->top *= scale_factor;
95 gfx::PointF CalculateScaledClipBoxOffset(const gfx::Rect& content_rect,
96 const PdfRectangle& source_clip_box) {
97 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;
100 // Center the intended clip region to real clip region.
101 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);
107 gfx::PointF CalculateNonScaledClipBoxOffset(
111 const PdfRectangle& source_clip_box) {
112 // Align the intended clip region to left-top corner of real clip region.
115 return gfx::PointF(-1 * source_clip_box.left,
116 page_height - source_clip_box.top);
118 return gfx::PointF(0, -1 * source_clip_box.bottom);
120 return gfx::PointF(page_width - source_clip_box.right, 0);
122 return gfx::PointF(page_height - source_clip_box.right,
123 page_width - source_clip_box.top);
126 return gfx::PointF();
130 } // namespace chrome_pdf