fixup! [M120 Migration][NaCl][PPFWK] Upgradable pepper plugin requirement
[platform/framework/web/chromium-efl.git] / pdf / pdf_transform.cc
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.
4
5 #include "pdf/pdf_transform.h"
6
7 #include <algorithm>
8 #include <utility>
9
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"
14
15 namespace chrome_pdf {
16
17 namespace {
18
19 // When a PdfRectangle has top < bottom, or right < left, the values should be
20 // swapped.
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);
26 }
27
28 }  // namespace
29
30 float CalculateScaleFactor(const gfx::Rect& content_rect,
31                            const gfx::SizeF& src_size,
32                            bool rotated) {
33   if (src_size.IsEmpty())
34     return 1.0f;
35
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);
43 }
44
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;
49   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
55 void CalculateMediaBoxAndCropBox(bool rotated,
56                                  bool has_media_box,
57                                  bool has_crop_box,
58                                  PdfRectangle* media_box,
59                                  PdfRectangle* crop_box) {
60   if (has_media_box)
61     SwapPdfRectangleValuesIfNeeded(media_box);
62   if (has_crop_box)
63     SwapPdfRectangleValuesIfNeeded(crop_box);
64
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;
72   }
73 }
74
75 PdfRectangle CalculateClipBoxBoundary(const PdfRectangle& media_box,
76                                       const PdfRectangle& crop_box) {
77   PdfRectangle clip_box;
78
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);
85   return clip_box;
86 }
87
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;
93 }
94
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;
99
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);
105 }
106
107 gfx::PointF CalculateNonScaledClipBoxOffset(
108     int rotation,
109     int page_width,
110     int page_height,
111     const PdfRectangle& source_clip_box) {
112   // Align the intended clip region to left-top corner of real clip region.
113   switch (rotation) {
114     case 0:
115       return gfx::PointF(-1 * source_clip_box.left,
116                          page_height - source_clip_box.top);
117     case 1:
118       return gfx::PointF(0, -1 * source_clip_box.bottom);
119     case 2:
120       return gfx::PointF(page_width - source_clip_box.right, 0);
121     case 3:
122       return gfx::PointF(page_height - source_clip_box.right,
123                          page_width - source_clip_box.top);
124     default:
125       NOTREACHED();
126       return gfx::PointF();
127   }
128 }
129
130 }  // namespace chrome_pdf